Skip to content

Commit

Permalink
Adding more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NicholasLYang committed Apr 11, 2023
1 parent 86f35a5 commit 9eb9e09
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 26 deletions.
1 change: 1 addition & 0 deletions crates/turborepo-lib/Cargo.toml
Expand Up @@ -7,6 +7,7 @@ license = "MPL-2.0"
[features]
# Allows configuring a specific tls backend for reqwest.
# See top level Cargo.toml for more details.
default = ["rustls-tls"]
native-tls = ["turborepo-api-client/native-tls", "turbo-updater/native-tls"]
rustls-tls = ["turborepo-api-client/rustls-tls", "turbo-updater/rustls-tls"]

Expand Down
26 changes: 2 additions & 24 deletions crates/turborepo-lib/src/commands/mod.rs
Expand Up @@ -10,9 +10,8 @@ use crate::{
default_user_config_path, get_repo_config_path, ClientConfig, ClientConfigLoader,
RepoConfig, RepoConfigLoader, UserConfig, UserConfigLoader,
},
execution_state::RemoteConfig,
ui::UI,
Args, ExecutionState,
Args,
};

pub(crate) mod bin;
Expand All @@ -28,7 +27,7 @@ pub struct CommandBase {
user_config: OnceCell<UserConfig>,
repo_config: OnceCell<RepoConfig>,
client_config: OnceCell<ClientConfig>,
args: Args,
pub args: Args,
version: &'static str,
}

Expand Down Expand Up @@ -156,27 +155,6 @@ impl CommandBase {
}
}

impl<'a> TryFrom<&'a CommandBase> for ExecutionState<'a> {
type Error = anyhow::Error;

fn try_from(base: &'a CommandBase) -> std::result::Result<Self, Self::Error> {
let repo_config = base.repo_config()?;
let user_config = base.user_config()?;

let remote_config = RemoteConfig {
token: user_config.token(),
team_id: repo_config.team_id(),
team_slug: repo_config.team_slug(),
api_url: repo_config.api_url(),
};

Ok(ExecutionState {
remote_config,
cli_args: &base.args,
})
}
}

#[cfg(test)]
mod test {
use test_case::test_case;
Expand Down
19 changes: 19 additions & 0 deletions crates/turborepo-lib/src/config/repo.rs
Expand Up @@ -180,6 +180,14 @@ mod test {

use super::*;

#[test]
fn test_repo_config_when_missing() -> Result<()> {
let config = RepoConfigLoader::new(PathBuf::from("missing")).load();
assert!(config.is_ok());

Ok(())
}

#[test]
fn test_repo_config_with_team_and_api_flags() -> Result<()> {
let mut config_file = NamedTempFile::new()?;
Expand All @@ -197,6 +205,17 @@ mod test {
Ok(())
}

#[test]
fn test_repo_config_includes_defaults() {
let config = RepoConfigLoader::new(PathBuf::from("missing"))
.load()
.unwrap();
assert_eq!(config.api_url(), DEFAULT_API_URL);
assert_eq!(config.login_url(), DEFAULT_LOGIN_URL);
assert_eq!(config.team_slug(), None);
assert_eq!(config.team_id(), None);
}

#[test]
fn test_team_override_clears_id() -> Result<()> {
let mut config_file = NamedTempFile::new()?;
Expand Down
15 changes: 14 additions & 1 deletion crates/turborepo-lib/src/config/user.rs
Expand Up @@ -92,7 +92,8 @@ impl UserConfigLoader {

let config = Config::builder()
.add_source(raw_disk_config.clone())
.add_source(Environment::with_prefix("turbo").source(environment))
.add_source(Environment::with_prefix("TURBO").source(environment.clone()))
.add_source(Environment::with_prefix("VERCEL_ARTIFACTS").source(environment))
.set_override_option("token", token)?
.build()?
.try_deserialize()?;
Expand Down Expand Up @@ -157,6 +158,18 @@ mod test {
.with_environment(Some(env))
.load()?;
assert_eq!(config.token(), Some("bar"));

let mut config_file = NamedTempFile::new()?;
writeln!(&mut config_file, "{{\"token\": \"baz\"}}")?;
let env = {
let mut map = HashMap::new();
map.insert("VERCEL_ARTIFACTS_TOKEN".into(), "bat".into());
map
};
let config = UserConfigLoader::new(config_file.path().to_path_buf())
.with_environment(Some(env))
.load()?;
assert_eq!(config.token(), Some("bat"));
Ok(())
}
}
26 changes: 25 additions & 1 deletion crates/turborepo-lib/src/execution_state.rs
@@ -1,6 +1,6 @@
use serde::Serialize;

use crate::cli::Args;
use crate::{cli::Args, commands::CommandBase};

#[derive(Debug, Serialize)]
pub struct ExecutionState<'a> {
Expand All @@ -15,3 +15,27 @@ pub struct RemoteConfig<'a> {
pub team_slug: Option<&'a str>,
pub api_url: &'a str,
}

impl<'a> TryFrom<&'a CommandBase> for ExecutionState<'a> {
type Error = anyhow::Error;

fn try_from(base: &'a CommandBase) -> Result<Self, Self::Error> {
let repo_config = base.repo_config()?;
let user_config = base.user_config()?;

let remote_config = RemoteConfig {
token: user_config.token(),
team_id: repo_config.team_id(),
team_slug: repo_config.team_slug(),
api_url: repo_config.api_url(),
};

Ok(ExecutionState {
remote_config,
cli_args: &base.args,
})
}
}

#[cfg(test)]
mod tests {}

0 comments on commit 9eb9e09

Please sign in to comment.