Skip to content

Commit

Permalink
chore: fix tests by mocking env
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-olszewski committed Jun 12, 2024
1 parent 0d3c6ce commit 459c462
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions crates/turborepo-lib/src/shim/local_turbo_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@ fn is_env_var_truthy(env_var: &str) -> Option<bool> {

impl LocalTurboConfig {
pub fn infer(repo_state: &RepoState) -> Option<Self> {
Self::infer_internal(repo_state, is_env_var_truthy(TURBO_DOWNLOAD_LOCAL_ENABLED))
}

// Used for testing when we want to manually set the controlling env var
fn infer_internal(repo_state: &RepoState, is_enabled: Option<bool>) -> Option<Self> {
// TODO: once we have properly communicated this functionality we should make
// this opt-out.
if !is_env_var_truthy(TURBO_DOWNLOAD_LOCAL_ENABLED).unwrap_or(false) {
if !is_enabled.unwrap_or(false) {
debug!("downloading correct local version not enabled");
return None;
}
Expand Down Expand Up @@ -86,7 +91,7 @@ mod test {
.unwrap();

assert_eq!(
LocalTurboConfig::infer(&repo),
LocalTurboConfig::infer_internal(&repo, Some(true)),
Some(LocalTurboConfig {
turbo_version: "2.0.3".into()
})
Expand All @@ -111,7 +116,7 @@ mod test {
.unwrap();

assert_eq!(
LocalTurboConfig::infer(&repo),
LocalTurboConfig::infer_internal(&repo, Some(true)),
Some(LocalTurboConfig {
turbo_version: "2.0.3".into()
})
Expand All @@ -136,7 +141,7 @@ mod test {
package_manager: Err(Error::MissingPackageManager),
};

assert_eq!(LocalTurboConfig::infer(&repo), None,);
assert_eq!(LocalTurboConfig::infer_internal(&repo, Some(true)), None,);
}

#[test]
Expand All @@ -157,7 +162,7 @@ mod test {
package_manager: Err(Error::MissingPackageManager),
};

assert_eq!(LocalTurboConfig::infer(&repo), None);
assert_eq!(LocalTurboConfig::infer_internal(&repo, Some(true)), None);
}

#[test]
Expand All @@ -174,7 +179,7 @@ mod test {
turbo_json
.create_with_contents(include_bytes!("../../fixtures/local_config/turbo.v1.json"))
.unwrap();
assert_eq!(LocalTurboConfig::infer(&repo), None);
assert_eq!(LocalTurboConfig::infer_internal(&repo, Some(true)), None);
}

#[test]
Expand All @@ -191,7 +196,7 @@ mod test {
turbo_json
.create_with_contents(include_bytes!("../../fixtures/local_config/turbo.v2.json"))
.unwrap();
assert_eq!(LocalTurboConfig::infer(&repo), None,);
assert_eq!(LocalTurboConfig::infer_internal(&repo, Some(true)), None,);
}

#[test]
Expand All @@ -204,6 +209,6 @@ mod test {
root_package_json: PackageJson::default(),
package_manager: Err(Error::MissingPackageManager),
};
assert_eq!(LocalTurboConfig::infer(&repo), None,);
assert_eq!(LocalTurboConfig::infer_internal(&repo, Some(true)), None,);
}
}

0 comments on commit 459c462

Please sign in to comment.