Skip to content

Commit

Permalink
port(turborepo): Fixing API Client config tests (#6356)
Browse files Browse the repository at this point in the history
### Description

Changed our tests to use turbo info instead of `--_test-run`, which is
specific to how we execute the Go binary.

### Testing Instructions

<!--
  Give a quick description of steps to test your changes.
-->


Closes TURBO-1579

Co-authored-by: nicholaslyang <Nicholas Yang>
  • Loading branch information
NicholasLYang authored Nov 6, 2023
1 parent 285bc68 commit be8573d
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 90 deletions.
12 changes: 9 additions & 3 deletions crates/turborepo-lib/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,12 @@ pub enum Command {
command: Option<Box<GenerateCommand>>,
},
#[clap(hide = true)]
Info { workspace: Option<String> },
Info {
workspace: Option<String>,
// We output turbo info as json. Currently just for internal testing
#[clap(long)]
json: bool,
},
/// Link your local directory to a Vercel organization and enable remote
/// caching.
Link {
Expand Down Expand Up @@ -740,10 +745,11 @@ pub async fn run(
generate::run(tag, command, &args)?;
Ok(Payload::Rust(Ok(0)))
}
Command::Info { workspace } => {
Command::Info { workspace, json } => {
let json = *json;
let workspace = workspace.clone();
let mut base = CommandBase::new(cli_args, repo_root, version, ui);
info::run(&mut base, workspace.as_deref())?;
info::run(&mut base, workspace.as_deref(), json)?;

Ok(Payload::Rust(Ok(0)))
}
Expand Down
156 changes: 114 additions & 42 deletions crates/turborepo-lib/src/commands/info.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
//! A command for outputting information about a turborepo.
//! Currently just for internal use (not a public command)
//! Can output in either text or JSON
//! Different than run summary or dry run because it can include
//! sensitive data like your auth token
use anyhow::Result;
use serde::Serialize;
use turbopath::AnchoredSystemPath;
use turborepo_repository::{package_json::PackageJson, package_manager::PackageManager};
use turborepo_ui::GREY;

use crate::{
commands::CommandBase,
config::ConfigurationOptions,
package_graph::{PackageGraph, WorkspaceName, WorkspaceNode},
};

pub fn run(base: &mut CommandBase, workspace: Option<&str>) -> Result<()> {
#[derive(Serialize)]
struct RepositoryDetails<'a> {
config: &'a ConfigurationOptions,
workspaces: Vec<(&'a WorkspaceName, RepositoryWorkspaceDetails<'a>)>,
}

#[derive(Serialize)]
struct RepositoryWorkspaceDetails<'a> {
path: &'a AnchoredSystemPath,
}

#[derive(Serialize)]
struct WorkspaceDetails<'a> {
name: &'a str,
dependencies: Vec<&'a str>,
}

pub fn run(base: &mut CommandBase, workspace: Option<&str>, json: bool) -> Result<()> {
let root_package_json = PackageJson::load(&base.repo_root.join_component("package.json"))?;

let package_manager =
Expand All @@ -17,60 +42,107 @@ pub fn run(base: &mut CommandBase, workspace: Option<&str>) -> Result<()> {
.with_package_manger(Some(package_manager))
.build()?;

let config = base.config()?;

if let Some(workspace) = workspace {
print_workspace_details(&package_graph, workspace)
let workspace_details = WorkspaceDetails::new(&package_graph, workspace);
if json {
println!("{}", serde_json::to_string_pretty(&workspace_details)?);
} else {
workspace_details.print()?;
}
} else {
print_repo_details(&package_graph)
let repo_details = RepositoryDetails::new(&package_graph, config);
if json {
println!("{}", serde_json::to_string_pretty(&repo_details)?);
} else {
repo_details.print()?;
}
}

Ok(())
}

fn print_repo_details(package_graph: &PackageGraph) -> Result<()> {
// We subtract 1 for the root workspace
println!("{} packages found in workspace\n", package_graph.len() - 1);
impl<'a> RepositoryDetails<'a> {
fn new(package_graph: &'a PackageGraph, config: &'a ConfigurationOptions) -> Self {
let mut workspaces: Vec<_> = package_graph
.workspaces()
.map(|(workspace_name, workspace_info)| {
let workspace_details = RepositoryWorkspaceDetails {
path: workspace_info.package_path(),
};

let mut workspaces: Vec<_> = package_graph.workspaces().collect();
workspaces.sort_by(|a, b| a.0.cmp(b.0));
(workspace_name, workspace_details)
})
.collect();
workspaces.sort_by(|a, b| a.0.cmp(b.0));

for (workspace_name, entry) in workspaces {
if matches!(workspace_name, WorkspaceName::Root) {
continue;
Self { config, workspaces }
}
fn print(&self) -> Result<()> {
let is_logged_in = self.config.token.is_some();
let is_linked = self.config.team_id.is_some();
let team_slug = self.config.team_slug.as_deref();

match (is_logged_in, is_linked, team_slug) {
(true, true, Some(slug)) => println!("You are logged in and linked to {}", slug),
(true, true, None) => println!("You are logged in and linked"),
(true, false, _) => println!("You are logged in but not linked"),
(false, _, _) => println!("You are not logged in"),
}

// We subtract 1 for the root workspace
println!(
"- {} {}",
workspace_name,
GREY.apply_to(entry.package_json_path())
"{} packages found in workspace\n",
self.workspaces.len() - 1
);
}

Ok(())
for (workspace_name, entry) in &self.workspaces {
if matches!(workspace_name, WorkspaceName::Root) {
continue;
}
println!("- {} {}", workspace_name, GREY.apply_to(entry.path));
}

Ok(())
}
}

fn print_workspace_details(package_graph: &PackageGraph, workspace_name: &str) -> Result<()> {
let workspace_node = match workspace_name {
"//" => WorkspaceNode::Root,
name => WorkspaceNode::Workspace(WorkspaceName::Other(name.to_string())),
};

let transitive_dependencies = package_graph.transitive_closure(Some(&workspace_node));

let mut workspace_dep_names: Vec<&str> = transitive_dependencies
.into_iter()
.filter_map(|dependency| match dependency {
WorkspaceNode::Root | WorkspaceNode::Workspace(WorkspaceName::Root) => Some("root"),
WorkspaceNode::Workspace(WorkspaceName::Other(dep_name))
if dep_name == workspace_name =>
{
None
}
WorkspaceNode::Workspace(WorkspaceName::Other(dep_name)) => Some(dep_name.as_str()),
})
.collect();
workspace_dep_names.sort();

println!("{} depends on:", workspace_name);
for dep_name in workspace_dep_names {
println!("- {}", dep_name);
impl<'a> WorkspaceDetails<'a> {
fn new(package_graph: &'a PackageGraph, workspace_name: &'a str) -> Self {
let workspace_node = match workspace_name {
"//" => WorkspaceNode::Root,
name => WorkspaceNode::Workspace(WorkspaceName::Other(name.to_string())),
};

let transitive_dependencies = package_graph.transitive_closure(Some(&workspace_node));

let mut workspace_dep_names: Vec<&str> = transitive_dependencies
.into_iter()
.filter_map(|dependency| match dependency {
WorkspaceNode::Root | WorkspaceNode::Workspace(WorkspaceName::Root) => Some("root"),
WorkspaceNode::Workspace(WorkspaceName::Other(dep_name))
if dep_name == workspace_name =>
{
None
}
WorkspaceNode::Workspace(WorkspaceName::Other(dep_name)) => Some(dep_name.as_str()),
})
.collect();
workspace_dep_names.sort();

Self {
name: workspace_name,
dependencies: workspace_dep_names,
}
}

Ok(())
fn print(&self) -> Result<()> {
println!("{} depends on:", self.name);
for dep_name in &self.dependencies {
println!("- {}", dep_name);
}

Ok(())
}
}
42 changes: 0 additions & 42 deletions turborepo-tests/integration/tests/api-client-config.t

This file was deleted.

7 changes: 4 additions & 3 deletions turborepo-tests/integration/tests/command-info.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ Setup

Run info
$ ${TURBO} info
You are not logged in
3 packages found in workspace

- another packages/another/package.json
- my-app apps/my-app/package.json
- util packages/util/package.json
- another packages/another
- my-app apps/my-app
- util packages/util


Run info on package `another`
Expand Down
45 changes: 45 additions & 0 deletions turborepo-tests/integration/tests/config.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
Setup
$ . ${TESTDIR}/../../helpers/setup.sh
$ . ${TESTDIR}/_helpers/setup_monorepo.sh $(pwd)

Run test run
$ ${TURBO} info --json | jq .config
{
"apiUrl": null,
"loginUrl": null,
"teamSlug": null,
"teamId": null,
"token": null,
"signature": null,
"preflight": null,
"timeout": null,
"enabled": null
}

Run test run with api overloaded
$ ${TURBO} info --json --api http://localhost:8000 | jq .config.apiUrl
"http://localhost:8000"

Run test run with token overloaded
$ ${TURBO} info --json --token 1234567890 | jq .config.token
"1234567890"

Run test run with token overloaded from both TURBO_TOKEN and VERCEL_ARTIFACTS_TOKEN
$ TURBO_TOKEN=turbo VERCEL_ARTIFACTS_TOKEN=vercel ${TURBO} info --json | jq .config.token
"vercel"

Run test run with team overloaded
$ ${TURBO} info --json --team vercel | jq .config.teamSlug
"vercel"

Run test run with team overloaded from both env and flag (flag should take precedence)
$ TURBO_TEAM=vercel ${TURBO} info --json --team turbo | jq .config.teamSlug
"turbo"

Run test run with remote cache timeout env variable set
$ TURBO_REMOTE_CACHE_TIMEOUT=123 ${TURBO} info --json | jq .config.timeout
123

Run test run with remote cache timeout from both env and flag (flag should take precedence)
$ TURBO_REMOTE_CACHE_TIMEOUT=123 ${TURBO} info --json --remote-cache-timeout 456 | jq .config.timeout
456

0 comments on commit be8573d

Please sign in to comment.