Skip to content

Commit

Permalink
chore(shim): add config detection tests and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-olszewski committed Jun 10, 2024
1 parent 6d15306 commit 54a3029
Show file tree
Hide file tree
Showing 5 changed files with 311 additions and 2 deletions.
6 changes: 6 additions & 0 deletions crates/turborepo-lib/fixtures/local_config/turbo.v1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
// some comments
"pipeline": {
"build": {"dependsOn": ["^build"]}
}
}
6 changes: 6 additions & 0 deletions crates/turborepo-lib/fixtures/local_config/turbo.v2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
// some comments
"tasks": {
"build": {"dependsOn": ["^build"]}
}
}
116 changes: 116 additions & 0 deletions crates/turborepo-lib/fixtures/local_config/turbov2.package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

181 changes: 179 additions & 2 deletions crates/turborepo-lib/src/shim/local_turbo_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ use jsonc_parser::{
ast::{ObjectPropName, Value},
parse_to_ast,
};
use tracing::debug;
use turborepo_repository::{inference::RepoState, package_manager::PackageManager};

const TURBO_DOWNLOAD_LOCAL_DISABLED: &str = "TURBO_DOWNLOAD_LOCAL_DISABLED";

/// Struct containing information about the desired local turbo version
/// according to lockfiles, package.jsons, and if all else fails turbo.json
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LocalTurboConfig {
turbo_version: String,
}
Expand All @@ -20,11 +22,21 @@ impl LocalTurboConfig {
if env::var(TURBO_DOWNLOAD_LOCAL_DISABLED)
.map_or(false, |disable| matches!(disable.as_str(), "1" | "true"))
{
debug!("downloading correct local version disabled");
return None;
}
let turbo_version = Self::turbo_version_from_lockfile(repo_state)
.or_else(|| Self::turbo_version_from_package_json(repo_state))
.or_else(|| Self::turbo_version_from_turbo_json_schema(repo_state))?;
.or_else(|| {
debug!(
"No turbo version found in a lockfile. Attempting to read version from root \
package.json"
);
Self::turbo_version_from_package_json(repo_state)
})
.or_else(|| {
debug!("No turbo version found in package.json. Checking if turbo.json is for v1");
Self::turbo_version_from_turbo_json_schema(repo_state)
})?;
Some(Self { turbo_version })
}

Expand Down Expand Up @@ -90,3 +102,168 @@ impl LocalTurboConfig {
None
}
}

#[cfg(test)]
mod test {
use tempfile::TempDir;
use turbopath::AbsoluteSystemPath;
use turborepo_repository::{
inference::RepoMode, package_json::PackageJson, package_manager::Error,
};

use super::*;

#[test]
fn test_package_manager_and_lockfile() {
let tmpdir = TempDir::with_prefix("local_config").unwrap();
let root = AbsoluteSystemPath::from_std_path(tmpdir.path()).unwrap();
let repo = RepoState {
root: root.to_owned(),
mode: RepoMode::MultiPackage,
root_package_json: PackageJson::default(),
package_manager: Ok(PackageManager::Npm),
};
let lockfile = root.join_component("package-lock.json");
lockfile
.create_with_contents(include_bytes!(
"../../fixtures/local_config/turbov2.package-lock.json"
))
.unwrap();

assert_eq!(
LocalTurboConfig::infer(&repo),
Some(LocalTurboConfig {
turbo_version: "2.0.3".into()
})
);
}

#[test]
fn test_just_lockfile() {
let tmpdir = TempDir::with_prefix("local_config").unwrap();
let root = AbsoluteSystemPath::from_std_path(tmpdir.path()).unwrap();
let repo = RepoState {
root: root.to_owned(),
mode: RepoMode::MultiPackage,
root_package_json: PackageJson::default(),
package_manager: Err(Error::MissingPackageManager),
};
let lockfile = root.join_component("package-lock.json");
lockfile
.create_with_contents(include_bytes!(
"../../fixtures/local_config/turbov2.package-lock.json"
))
.unwrap();

assert_eq!(
LocalTurboConfig::infer(&repo),
Some(LocalTurboConfig {
turbo_version: "2.0.3".into()
})
);
}

#[test]
fn test_package_json_dep() {
let tmpdir = TempDir::with_prefix("local_config").unwrap();
let root = AbsoluteSystemPath::from_std_path(tmpdir.path()).unwrap();
let repo = RepoState {
root: root.to_owned(),
mode: RepoMode::MultiPackage,
root_package_json: PackageJson {
dependencies: Some(
vec![("turbo".into(), "^2.0.0".into())]
.into_iter()
.collect(),
),
..Default::default()
},
package_manager: Err(Error::MissingPackageManager),
};

assert_eq!(
LocalTurboConfig::infer(&repo),
Some(LocalTurboConfig {
turbo_version: "^2.0.0".into()
})
);
}

#[test]
fn test_package_json_dev_dep() {
let tmpdir = TempDir::with_prefix("local_config").unwrap();
let root = AbsoluteSystemPath::from_std_path(tmpdir.path()).unwrap();
let repo = RepoState {
root: root.to_owned(),
mode: RepoMode::MultiPackage,
root_package_json: PackageJson {
dev_dependencies: Some(
vec![("turbo".into(), "^2.0.0".into())]
.into_iter()
.collect(),
),
..Default::default()
},
package_manager: Err(Error::MissingPackageManager),
};

assert_eq!(
LocalTurboConfig::infer(&repo),
Some(LocalTurboConfig {
turbo_version: "^2.0.0".into()
})
);
}

#[test]
fn test_v1_schema() {
let tmpdir = TempDir::with_prefix("local_config").unwrap();
let root = AbsoluteSystemPath::from_std_path(tmpdir.path()).unwrap();
let repo = RepoState {
root: root.to_owned(),
mode: RepoMode::MultiPackage,
root_package_json: PackageJson::default(),
package_manager: Err(Error::MissingPackageManager),
};
let turbo_json = root.join_component("turbo.json");
turbo_json
.create_with_contents(include_bytes!("../../fixtures/local_config/turbo.v1.json"))
.unwrap();
assert_eq!(
LocalTurboConfig::infer(&repo),
Some(LocalTurboConfig {
turbo_version: "^1".into()
})
);
}

#[test]
fn test_v2_schema() {
let tmpdir = TempDir::with_prefix("local_config").unwrap();
let root = AbsoluteSystemPath::from_std_path(tmpdir.path()).unwrap();
let repo = RepoState {
root: root.to_owned(),
mode: RepoMode::MultiPackage,
root_package_json: PackageJson::default(),
package_manager: Err(Error::MissingPackageManager),
};
let turbo_json = root.join_component("turbo.json");
turbo_json
.create_with_contents(include_bytes!("../../fixtures/local_config/turbo.v2.json"))
.unwrap();
assert_eq!(LocalTurboConfig::infer(&repo), None,);
}

#[test]
fn nothing() {
let tmpdir = TempDir::with_prefix("local_config").unwrap();
let root = AbsoluteSystemPath::from_std_path(tmpdir.path()).unwrap();
let repo = RepoState {
root: root.to_owned(),
mode: RepoMode::MultiPackage,
root_package_json: PackageJson::default(),
package_manager: Err(Error::MissingPackageManager),
};
assert_eq!(LocalTurboConfig::infer(&repo), None,);
}
}
4 changes: 4 additions & 0 deletions crates/turborepo-lib/src/shim/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ fn run_correct_turbo(
spawn_local_turbo(&repo_state, turbo_state, shim_args)
}
} else if let Some(local_config) = LocalTurboConfig::infer(&repo_state) {
debug!(
"Found configuration for turbo version {}",
local_config.turbo_version()
);
spawn_npx_turbo(&repo_state, local_config.turbo_version(), shim_args)
} else {
let version = get_version();
Expand Down

0 comments on commit 54a3029

Please sign in to comment.