Skip to content

Commit

Permalink
Add test_name to WapmConfig::get_folder
Browse files Browse the repository at this point in the history
  • Loading branch information
fschutt committed Nov 11, 2022
1 parent 8559eae commit fcaf255
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 43 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions lib/registry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ edition = "2021"
license = "MIT"
description = "Crate to interact with the wasmer registry (wapm.io), download packages, etc."

[dev-dependencies]
rand = "0.8.5"

[dependencies]
dirs = "4.0.0"
graphql_client = "0.11.0"
Expand Down
70 changes: 40 additions & 30 deletions lib/registry/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use graphql_client::GraphQLQuery;
use serde::Deserialize;
use serde::Serialize;
use std::collections::BTreeMap;
#[cfg(not(test))]
use std::env;
use std::path::{Path, PathBuf};

Expand Down Expand Up @@ -255,7 +256,10 @@ impl PartialWapmConfig {
Ok(())
}

pub fn from_file() -> Result<Self, String> {
pub fn from_file(#[cfg(test)] test_name: &str) -> Result<Self, String> {
#[cfg(test)]
let path = Self::get_file_location(test_name)?;
#[cfg(not(test))]
let path = Self::get_file_location()?;

match std::fs::read_to_string(&path) {
Expand All @@ -270,38 +274,44 @@ impl PartialWapmConfig {
std::env::current_dir()
}

#[cfg(test)]
pub fn get_folder(test_name: &str) -> Result<PathBuf, String> {
let test_name = std::env::var("WASMER_REGISTRY_TEST_NAME").unwrap();
let test_dir = std::env::temp_dir().join("test_wasmer").join(test_name);
let _ = std::fs::create_dir_all(&test_dir);
Ok(test_dir.to_path_buf())
}

#[cfg(not(test))]
pub fn get_folder() -> Result<PathBuf, String> {
#[cfg(test)]
{
let test_dir = std::env::temp_dir().join("test_wasmer");
let _ = std::fs::create_dir_all(&test_dir);
Ok(test_dir.to_path_buf())
}
#[cfg(not(test))]
{
Ok(
if let Some(folder_str) = env::var("WASMER_DIR").ok().filter(|s| !s.is_empty()) {
let folder = PathBuf::from(folder_str);
std::fs::create_dir_all(folder.clone())
.map_err(|e| format!("cannot create config directory: {e}"))?;
folder
} else {
#[allow(unused_variables)]
let default_dir = Self::get_current_dir()
.ok()
.unwrap_or_else(|| PathBuf::from("/".to_string()));
let home_dir =
dirs::home_dir().ok_or_else(|| "cannot find home directory".to_string())?;
let mut folder = home_dir;
folder.push(".wasmer");
std::fs::create_dir_all(folder.clone())
.map_err(|e| format!("cannot create config directory: {e}"))?;
folder
},
)
}
Ok(
if let Some(folder_str) = env::var("WASMER_DIR").ok().filter(|s| !s.is_empty()) {
let folder = PathBuf::from(folder_str);
std::fs::create_dir_all(folder.clone())
.map_err(|e| format!("cannot create config directory: {e}"))?;
folder
} else {
#[allow(unused_variables)]
let default_dir = Self::get_current_dir()
.ok()
.unwrap_or_else(|| PathBuf::from("/".to_string()));
let home_dir =
dirs::home_dir().ok_or_else(|| "cannot find home directory".to_string())?;
let mut folder = home_dir;
folder.push(".wasmer");
std::fs::create_dir_all(folder.clone())
.map_err(|e| format!("cannot create config directory: {e}"))?;
folder
},
)
}

#[cfg(test)]
pub fn get_file_location(test_name: &str) -> Result<PathBuf, String> {
Ok(Self::get_folder(test_name)?.join(GLOBAL_CONFIG_FILE_NAME))
}

#[cfg(not(test))]
pub fn get_file_location() -> Result<PathBuf, String> {
Ok(Self::get_folder()?.join(GLOBAL_CONFIG_FILE_NAME))
}
Expand Down
59 changes: 48 additions & 11 deletions lib/registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ pub fn get_package_local_dir(
Ok(install_dir.join(namespace).join(name).join(version))
}

pub fn try_finding_local_command(cmd: &str) -> Option<LocalPackage> {
for p in get_all_local_packages(None) {
pub fn try_finding_local_command(#[cfg(test)] test_name: &str, cmd: &str) -> Option<LocalPackage> {
#[cfg(test)]
let local_packages = get_all_local_packages(None, test_name);
#[cfg(not(test))]
let local_packages = get_all_local_packages(None);
for p in local_packages {
if p.get_commands()
.unwrap_or_default()
.iter()
Expand Down Expand Up @@ -157,7 +161,10 @@ fn get_all_names_in_dir(dir: &PathBuf) -> Vec<(PathBuf, String)> {
}

/// Returns a list of all locally installed packages
pub fn get_all_local_packages(registry: Option<&str>) -> Vec<LocalPackage> {
pub fn get_all_local_packages(
registry: Option<&str>,
#[cfg(test)] test_name: &str,
) -> Vec<LocalPackage> {
let mut packages = Vec::new();
let registries = match registry {
Some(s) => vec![s.to_string()],
Expand All @@ -169,7 +176,12 @@ pub fn get_all_local_packages(registry: Option<&str>) -> Vec<LocalPackage> {
.filter_map(|s| url::Url::parse(&s).ok()?.host_str().map(|s| s.to_string()))
.collect::<Vec<_>>();

let mut registries_in_root_dir = get_checkouts_dir()
#[cfg(not(test))]
let checkouts_dir = get_checkouts_dir();
#[cfg(test)]
let checkouts_dir = get_checkouts_dir(test_name);

let mut registries_in_root_dir = checkouts_dir
.as_ref()
.map(get_all_names_in_dir)
.unwrap_or_default()
Expand Down Expand Up @@ -208,11 +220,17 @@ pub fn get_all_local_packages(registry: Option<&str>) -> Vec<LocalPackage> {
}

pub fn get_local_package(
#[cfg(test)] test_name: &str,
registry: Option<&str>,
name: &str,
version: Option<&str>,
) -> Option<LocalPackage> {
get_all_local_packages(registry)
#[cfg(not(test))]
let local_packages = get_all_local_packages(registry);
#[cfg(test)]
let local_packages = get_all_local_packages(registry, test_name);

local_packages
.iter()
.find(|p| {
if p.name != name {
Expand Down Expand Up @@ -538,16 +556,35 @@ pub fn query_package_from_registry(
})
}

pub fn get_wasmer_root_dir() -> Option<PathBuf> {
PartialWapmConfig::get_folder().ok()
pub fn get_wasmer_root_dir(#[cfg(test)] test_name: &str) -> Option<PathBuf> {
#[cfg(test)]
{
PartialWapmConfig::get_folder(test_name).ok()
}
#[cfg(not(test))]
{
PartialWapmConfig::get_folder().ok()
}
}
pub fn get_checkouts_dir() -> Option<PathBuf> {
Some(get_wasmer_root_dir()?.join("checkouts"))

pub fn get_checkouts_dir(#[cfg(test)] test_name: &str) -> Option<PathBuf> {
#[cfg(test)]
let root_dir = get_wasmer_root_dir(test_name)?;
#[cfg(not(test))]
let root_dir = get_wasmer_root_dir()?;
Some(root_dir.join("checkouts"))
}

/// Returs the path to the directory where all packages on this computer are being stored
pub fn get_global_install_dir(registry_host: &str) -> Option<PathBuf> {
Some(get_checkouts_dir()?.join(registry_host))
pub fn get_global_install_dir(
#[cfg(test)] test_name: &str,
registry_host: &str,
) -> Option<PathBuf> {
#[cfg(test)]
let root_dir = get_checkouts_dir(test_name)?;
#[cfg(not(test))]
let root_dir = get_checkouts_dir()?;
Some(root_dir.join(registry_host))
}

/// Whether the top-level directory should be stripped
Expand Down
13 changes: 12 additions & 1 deletion lib/registry/src/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,26 @@ use crate::PartialWapmConfig;
/// Login to a registry and save the token associated with it.
///
/// Also sets the registry as the currently active registry to provide a better UX.
pub fn login_and_save_token(registry: &str, token: &str) -> Result<(), anyhow::Error> {
pub fn login_and_save_token(
#[cfg(test)] test_name: &str,
registry: &str,
token: &str,
) -> Result<(), anyhow::Error> {
let registry = format_graphql(registry);
#[cfg(test)]
let mut config = PartialWapmConfig::from_file(test_name).map_err(|e| anyhow::anyhow!("{e}"))?;
#[cfg(not(test))]
let mut config = PartialWapmConfig::from_file().map_err(|e| anyhow::anyhow!("{e}"))?;
config.registry.set_current_registry(&registry);
config.registry.set_login_token_for_registry(
&config.registry.get_current_registry(),
token,
UpdateRegistry::Update,
);
#[cfg(test)]
let path =
PartialWapmConfig::get_file_location(test_name).map_err(|e| anyhow::anyhow!("{e}"))?;
#[cfg(not(test))]
let path = PartialWapmConfig::get_file_location().map_err(|e| anyhow::anyhow!("{e}"))?;
config.save(&path)?;
let username = crate::utils::get_username_registry_token(&registry, token);
Expand Down
5 changes: 4 additions & 1 deletion lib/registry/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use graphql_client::GraphQLQuery;
)]
struct WhoAmIQuery;

pub fn get_username() -> anyhow::Result<Option<String>> {
pub fn get_username(#[cfg(test)] test_name: &str) -> anyhow::Result<Option<String>> {
#[cfg(test)]
let config = PartialWapmConfig::from_file(test_name).map_err(|e| anyhow::anyhow!("{e}"))?;
#[cfg(not(test))]
let config = PartialWapmConfig::from_file().map_err(|e| anyhow::anyhow!("{e}"))?;
let registry = &config.registry.get_current_registry();
let q = WhoAmIQuery::build_query(who_am_i_query::Variables {});
Expand Down

0 comments on commit fcaf255

Please sign in to comment.