Skip to content

Commit

Permalink
Added some regression tests to WasmerEnv
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-F-Bryan committed Jun 26, 2023
1 parent ced5684 commit ca50bb7
Showing 1 changed file with 123 additions and 0 deletions.
123 changes: 123 additions & 0 deletions lib/registry/src/wasmer_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,126 @@ impl From<&str> for Registry {
Registry(value.to_string())
}
}

#[cfg(test)]
mod tests {
use tempfile::TempDir;

use super::*;

const WASMER_TOML: &str = r#"
telemetry_enabled = false
update_notifications_enabled = false
[registry]
active_registry = "https://registry.wasmer.io/graphql"
[[registry.tokens]]
registry = "https://registry.wasmer.wtf/graphql"
token = "dev-token"
[[registry.tokens]]
registry = "https://registry.wasmer.io/graphql"
token = "prod-token"
"#;

#[test]
fn load_defaults_from_config() {
let temp = TempDir::new().unwrap();
std::fs::write(temp.path().join("wasmer.toml"), WASMER_TOML).unwrap();

let env = WasmerEnv {
wasmer_dir: temp.path().to_path_buf(),
registry: None,
cache_dir: None,
token: None,
};

assert_eq!(
env.registry_endpoint().unwrap().as_str(),
"https://registry.wasmer.io/graphql"
);
assert_eq!(env.token().unwrap(), "prod-token");
assert_eq!(env.cache_dir(), temp.path().join("cache"));
}

#[test]
fn override_token() {
let temp = TempDir::new().unwrap();
std::fs::write(temp.path().join("wasmer.toml"), WASMER_TOML).unwrap();

let env = WasmerEnv {
wasmer_dir: temp.path().to_path_buf(),
registry: None,
cache_dir: None,
token: Some("asdf".to_string()),
};

assert_eq!(
env.registry_endpoint().unwrap().as_str(),
"https://registry.wasmer.io/graphql"
);
assert_eq!(env.token().unwrap(), "asdf");
assert_eq!(env.cache_dir(), temp.path().join("cache"));
}

#[test]
fn override_registry() {
let temp = TempDir::new().unwrap();
std::fs::write(temp.path().join("wasmer.toml"), WASMER_TOML).unwrap();
let env = WasmerEnv {
wasmer_dir: temp.path().to_path_buf(),
registry: Some(Registry::from("wasmer.wtf")),
cache_dir: None,
token: None,
};

assert_eq!(
env.registry_endpoint().unwrap().as_str(),
"https://registry.wasmer.wtf/graphql"
);
assert_eq!(env.token().unwrap(), "dev-token");
assert_eq!(env.cache_dir(), temp.path().join("cache"));
}

#[test]
fn override_registry_and_token() {
let temp = TempDir::new().unwrap();
std::fs::write(temp.path().join("wasmer.toml"), WASMER_TOML).unwrap();

let env = WasmerEnv {
wasmer_dir: temp.path().to_path_buf(),
registry: Some(Registry::from("wasmer.wtf")),
cache_dir: None,
token: Some("asdf".to_string()),
};

assert_eq!(
env.registry_endpoint().unwrap().as_str(),
"https://registry.wasmer.wtf/graphql"
);
assert_eq!(env.token().unwrap(), "asdf");
assert_eq!(env.cache_dir(), temp.path().join("cache"));
}

#[test]
fn override_cache_dir() {
let temp = TempDir::new().unwrap();
std::fs::write(temp.path().join("wasmer.toml"), WASMER_TOML).unwrap();
let expected_cache_dir = temp.path().join("some-other-cache");

let env = WasmerEnv {
wasmer_dir: temp.path().to_path_buf(),
registry: None,
cache_dir: Some(expected_cache_dir.clone()),
token: None,
};

assert_eq!(
env.registry_endpoint().unwrap().as_str(),
"https://registry.wasmer.io/graphql"
);
assert_eq!(env.token().unwrap(), "prod-token");
assert_eq!(env.cache_dir(), expected_cache_dir);
}
}

0 comments on commit ca50bb7

Please sign in to comment.