diff --git a/lib/cli/src/commands/add.rs b/lib/cli/src/commands/add.rs index d6960a5f73d..f21a4a1423f 100644 --- a/lib/cli/src/commands/add.rs +++ b/lib/cli/src/commands/add.rs @@ -4,13 +4,13 @@ use anyhow::{Context, Error}; use clap::Parser; use wasmer_registry::{Bindings, ProgrammingLanguage}; -use crate::WasmerDir; +use crate::WasmerEnv; /// Add a Wasmer package's bindings to your application. #[derive(Debug, Parser)] pub struct Add { #[clap(flatten)] - wasmer_dir: WasmerDir, + env: WasmerEnv, /// Add the JavaScript bindings using "npm install". #[clap(long, groups = &["bindings", "js"])] npm: bool, @@ -33,7 +33,7 @@ impl Add { anyhow::ensure!(!self.packages.is_empty(), "No packages specified"); let registry = self - .wasmer_dir + .env .registry_endpoint() .context("Unable to determine which registry to use")?; diff --git a/lib/cli/src/commands/config.rs b/lib/cli/src/commands/config.rs index 2c4ae8ae402..85934f1dcdc 100644 --- a/lib/cli/src/commands/config.rs +++ b/lib/cli/src/commands/config.rs @@ -1,4 +1,4 @@ -use crate::{WasmerDir, VERSION}; +use crate::{WasmerEnv, VERSION}; use anyhow::{Context, Result}; use clap::Parser; use std::str::ParseBoolError; @@ -8,7 +8,7 @@ use wasmer_registry::WasmerConfig; /// The options for the `wasmer config` subcommand: `wasmer config get --OPTION` or `wasmer config set [FLAG]` pub struct Config { #[clap(flatten)] - wasmer_dir: WasmerDir, + env: WasmerEnv, #[clap(flatten)] flags: Flags, @@ -169,12 +169,12 @@ impl Config { fn inner_execute(&self) -> Result<()> { if let Some(s) = self.set.as_ref() { - return s.execute(&self.wasmer_dir); + return s.execute(&self.env); } let flags = &self.flags; - let prefix = self.wasmer_dir.dir(); + let prefix = self.env.dir(); let prefixdir = prefix.display().to_string(); let bindir = prefix.join("bin").display().to_string(); @@ -217,7 +217,7 @@ impl Config { } if flags.config_path { - let path = WasmerConfig::get_file_location(self.wasmer_dir.dir()); + let path = WasmerConfig::get_file_location(self.env.dir()); println!("{}", path.display()); } @@ -226,9 +226,9 @@ impl Config { } impl GetOrSet { - fn execute(&self, wasmer_dir: &WasmerDir) -> Result<()> { - let config_file = WasmerConfig::get_file_location(wasmer_dir.dir()); - let mut config = wasmer_dir.config()?; + fn execute(&self, env: &WasmerEnv) -> Result<()> { + let config_file = WasmerConfig::get_file_location(env.dir()); + let mut config = env.config()?; match self { GetOrSet::Get(g) => match g { diff --git a/lib/cli/src/commands/init.rs b/lib/cli/src/commands/init.rs index fff6c701d72..a69f53e93f4 100644 --- a/lib/cli/src/commands/init.rs +++ b/lib/cli/src/commands/init.rs @@ -6,7 +6,7 @@ use std::collections::HashMap; use std::path::Path; use std::path::PathBuf; -use crate::WasmerDir; +use crate::WasmerEnv; static NOTE: &str = "# See more keys and definitions at https://docs.wasmer.io/registry/manifest"; @@ -16,7 +16,7 @@ const NEWLINE: &str = if cfg!(windows) { "\r\n" } else { "\n" }; #[derive(Debug, Parser)] pub struct Init { #[clap(flatten)] - wasmer_dir: WasmerDir, + env: WasmerEnv, /// Initialize wasmer.toml for a library package #[clap(long, group = "crate-type")] @@ -140,7 +140,7 @@ impl Init { self.template.as_ref(), self.include.as_slice(), self.quiet, - self.wasmer_dir.dir(), + self.env.dir(), )?; if let Some(parent) = target_file.parent() { diff --git a/lib/cli/src/commands/login.rs b/lib/cli/src/commands/login.rs index 2f7c4bea197..9ffe9aca3f9 100644 --- a/lib/cli/src/commands/login.rs +++ b/lib/cli/src/commands/login.rs @@ -4,7 +4,7 @@ use clap::Parser; #[cfg(not(test))] use dialoguer::Input; -use crate::{Registry, WasmerDir}; +use crate::{Registry, WasmerEnv}; /// Subcommand for listing packages #[derive(Debug, Clone, Parser)] @@ -22,12 +22,12 @@ pub struct Login { } impl Login { - fn get_token_or_ask_user(&self, wasmer_dir: &WasmerDir) -> Result { + fn get_token_or_ask_user(&self, env: &WasmerEnv) -> Result { if let Some(token) = &self.token { return Ok(token.clone()); } - let registry_host = wasmer_dir.registry_endpoint()?; + let registry_host = env.registry_endpoint()?; let registry_tld = tldextract::TldExtractor::new(tldextract::TldOption::default()) .extract(registry_host.as_str()) .map_err(|e| { @@ -56,8 +56,8 @@ impl Login { } } - fn wasmer_dir(&self) -> WasmerDir { - WasmerDir::new( + fn wasmer_env(&self) -> WasmerEnv { + WasmerEnv::new( self.wasmer_dir.clone(), self.registry.clone(), self.token.clone(), @@ -66,15 +66,11 @@ impl Login { /// execute [List] pub fn execute(&self) -> Result<(), anyhow::Error> { - let wasmer_dir = self.wasmer_dir(); - let token = self.get_token_or_ask_user(&wasmer_dir)?; - - let registry = wasmer_dir.registry_endpoint()?; - match wasmer_registry::login::login_and_save_token( - wasmer_dir.dir(), - registry.as_str(), - &token, - )? { + let env = self.wasmer_env(); + let token = self.get_token_or_ask_user(&env)?; + + let registry = env.registry_endpoint()?; + match wasmer_registry::login::login_and_save_token(env.dir(), registry.as_str(), &token)? { Some(s) => println!("Login for Wasmer user {:?} saved", s), None => println!( "Error: no user found on registry {:?} with token {:?}. Token saved regardless.", @@ -99,10 +95,12 @@ mod tests { wasmer_dir: temp.path().to_path_buf(), token: None, }; - let wasmer_dir = login.wasmer_dir(); + let env = login.wasmer_env(); + + let token = login.get_token_or_ask_user(&env).unwrap(); assert_eq!( - login.get_token_or_ask_user(&wasmer_dir).unwrap(), + token, "Please paste the login token from https://wasmer.wtf/settings/access-tokens" ); } @@ -115,8 +113,10 @@ mod tests { wasmer_dir: temp.path().to_path_buf(), token: Some("abc".to_string()), }; - let wasmer_dir = login.wasmer_dir(); + let env = login.wasmer_env(); + + let token = login.get_token_or_ask_user(&env).unwrap(); - assert_eq!(login.get_token_or_ask_user(&wasmer_dir).unwrap(), "abc"); + assert_eq!(token, "abc"); } } diff --git a/lib/cli/src/commands/publish.rs b/lib/cli/src/commands/publish.rs index 5ceb1eec26f..6dacea868bc 100644 --- a/lib/cli/src/commands/publish.rs +++ b/lib/cli/src/commands/publish.rs @@ -3,13 +3,13 @@ use std::path::Path; use clap::Parser; use wasmer_wasix::runtime::resolver::WapmSource; -use crate::WasmerDir; +use crate::WasmerEnv; /// Publish a package to the package registry. #[derive(Debug, Parser)] pub struct Publish { #[clap(flatten)] - wasmer_dir: WasmerDir, + env: WasmerEnv, /// Run the publish logic without sending anything to the registry server #[clap(long, name = "dry-run")] pub dry_run: bool, @@ -36,22 +36,18 @@ impl Publish { /// Executes `wasmer publish` pub fn execute(&self) -> Result<(), anyhow::Error> { let publish = wasmer_registry::package::builder::Publish { - registry: self - .wasmer_dir - .registry_endpoint() - .map(|u| u.to_string()) - .ok(), + registry: self.env.registry_endpoint().map(|u| u.to_string()).ok(), dry_run: self.dry_run, quiet: self.quiet, package_name: self.package_name.clone(), version: self.version.clone(), - token: self.wasmer_dir.token(), + token: self.env.token(), no_validate: self.no_validate, package_path: self.package_path.clone(), }; publish.execute().map_err(on_error)?; - if let Err(e) = invalidate_graphql_query_cache(self.wasmer_dir.dir()) { + if let Err(e) = invalidate_graphql_query_cache(self.env.dir()) { tracing::warn!( error = &*e, "Unable to invalidate the cache used for package version queries", diff --git a/lib/cli/src/commands/run.rs b/lib/cli/src/commands/run.rs index 502ce4bdfbc..60aebf9f975 100644 --- a/lib/cli/src/commands/run.rs +++ b/lib/cli/src/commands/run.rs @@ -50,7 +50,7 @@ use wasmer_wasix::{ use webc::{metadata::Manifest, Container}; use crate::{ - commands::run::wasi::Wasi, error::PrettyError, logging::Output, store::StoreOptions, WasmerDir, + commands::run::wasi::Wasi, error::PrettyError, logging::Output, store::StoreOptions, WasmerEnv, }; const TICK: Duration = Duration::from_millis(250); @@ -59,7 +59,7 @@ const TICK: Duration = Duration::from_millis(250); #[derive(Debug, Parser)] pub struct Run { #[clap(flatten)] - wasmer_dir: WasmerDir, + env: WasmerEnv, #[clap(flatten)] store: StoreOptions, #[clap(flatten)] @@ -106,9 +106,9 @@ impl Run { } let (store, _) = self.store.get_store()?; - let runtime = - self.wasi - .prepare_runtime(store.engine().clone(), &self.wasmer_dir, handle)?; + let runtime = self + .wasi + .prepare_runtime(store.engine().clone(), &self.env, handle)?; // This is a slow operation, so let's temporarily wrap the runtime with // something that displays progress @@ -352,7 +352,7 @@ impl Run { }; let store = StoreOptions::default(); Ok(Run { - wasmer_dir: WasmerDir::default(), + env: WasmerEnv::default(), store, wasi: Wasi::for_binfmt_interpreter()?, wcgi: WcgiOptions::default(), diff --git a/lib/cli/src/commands/run/wasi.rs b/lib/cli/src/commands/run/wasi.rs index d5a03da1a1a..1e2ff2424ef 100644 --- a/lib/cli/src/commands/run/wasi.rs +++ b/lib/cli/src/commands/run/wasi.rs @@ -37,7 +37,7 @@ use wasmer_wasix::{ use crate::{ utils::{parse_envvar, parse_mapdir}, - WasmerDir, + WasmerEnv, }; const WAPM_SOURCE_CACHE_TIMEOUT: Duration = Duration::from_secs(10 * 60); @@ -248,7 +248,7 @@ impl Wasi { pub fn prepare_runtime( &self, engine: Engine, - wasmer_dir: &WasmerDir, + env: &WasmerEnv, handle: Handle, ) -> Result { let mut rt = PluggableRuntime::new(Arc::new(TokioTaskManager::new(handle))); @@ -270,12 +270,12 @@ impl Wasi { let client = Arc::new(client); let package_loader = self - .prepare_package_loader(wasmer_dir.dir(), client.clone()) + .prepare_package_loader(env.dir(), client.clone()) .context("Unable to prepare the package loader")?; - let registry = self.prepare_source(wasmer_dir, client)?; + let registry = self.prepare_source(env, client)?; - let cache_dir = FileSystemCache::default_cache_dir(wasmer_dir.dir()); + let cache_dir = FileSystemCache::default_cache_dir(env.dir()); let module_cache = wasmer_wasix::runtime::module_cache::in_memory() .with_fallback(FileSystemCache::new(cache_dir)); @@ -327,7 +327,7 @@ impl Wasi { fn prepare_source( &self, - wasmer_dir: &WasmerDir, + env: &WasmerEnv, client: Arc, ) -> Result { let mut source = MultiSource::new(); @@ -342,13 +342,13 @@ impl Wasi { } source.add_source(preloaded); - let graphql_endpoint = self.graphql_endpoint(wasmer_dir)?; - let cache_dir = WapmSource::default_cache_dir(wasmer_dir.dir()); + let graphql_endpoint = self.graphql_endpoint(env)?; + let cache_dir = WapmSource::default_cache_dir(env.dir()); let wapm_source = WapmSource::new(graphql_endpoint, Arc::clone(&client)) .with_local_cache(cache_dir, WAPM_SOURCE_CACHE_TIMEOUT); source.add_source(wapm_source); - let cache_dir = WebSource::default_cache_dir(wasmer_dir.dir()); + let cache_dir = WebSource::default_cache_dir(env.dir()); source.add_source(WebSource::new(cache_dir, client)); source.add_source(FileSystemSource::default()); @@ -356,12 +356,12 @@ impl Wasi { Ok(source) } - fn graphql_endpoint(&self, wasmer_dir: &WasmerDir) -> Result { - if let Ok(endpoint) = wasmer_dir.registry_endpoint() { + fn graphql_endpoint(&self, env: &WasmerEnv) -> Result { + if let Ok(endpoint) = env.registry_endpoint() { return Ok(endpoint); } - let config = wasmer_dir.config()?; + let config = env.config()?; let graphql_endpoint = config.registry.get_graphql_url(); let graphql_endpoint = graphql_endpoint .parse() diff --git a/lib/cli/src/commands/whoami.rs b/lib/cli/src/commands/whoami.rs index adab954d0fb..72aa8c0e549 100644 --- a/lib/cli/src/commands/whoami.rs +++ b/lib/cli/src/commands/whoami.rs @@ -1,20 +1,20 @@ use clap::Parser; -use crate::WasmerDir; +use crate::WasmerEnv; #[derive(Debug, Parser)] /// The options for the `wasmer whoami` subcommand pub struct Whoami { #[clap(flatten)] - wasmer_dir: WasmerDir, + env: WasmerEnv, } impl Whoami { /// Execute `wasmer whoami` pub fn execute(&self) -> Result<(), anyhow::Error> { - let registry = self.wasmer_dir.registry_endpoint()?; + let registry = self.env.registry_endpoint()?; let (registry, username) = - wasmer_registry::whoami(self.wasmer_dir.dir(), Some(registry.as_str()), None)?; + wasmer_registry::whoami(self.env.dir(), Some(registry.as_str()), None)?; println!("logged into registry {registry:?} as user {username:?}"); Ok(()) } diff --git a/lib/cli/src/lib.rs b/lib/cli/src/lib.rs index 5e0a5cae53c..0adb77afdc0 100644 --- a/lib/cli/src/lib.rs +++ b/lib/cli/src/lib.rs @@ -27,9 +27,9 @@ pub mod package_source; pub mod store; pub mod suggestions; pub mod utils; -mod wasmer_dir; +mod wasmer_env; /// Version number of this crate. pub const VERSION: &str = env!("CARGO_PKG_VERSION"); -pub use crate::wasmer_dir::{Registry, WasmerDir, WASMER_DIR}; +pub use crate::wasmer_env::{Registry, WasmerEnv, WASMER_DIR}; diff --git a/lib/cli/src/wasmer_dir.rs b/lib/cli/src/wasmer_env.rs similarity index 91% rename from lib/cli/src/wasmer_dir.rs rename to lib/cli/src/wasmer_env.rs index cdd3b9fbf9d..3a34fa78b59 100644 --- a/lib/cli/src/wasmer_dir.rs +++ b/lib/cli/src/wasmer_env.rs @@ -5,10 +5,12 @@ use once_cell::sync::Lazy; use url::Url; use wasmer_registry::WasmerConfig; -/// Command-line flags for determining `$WASMER_DIR` and interactions with the -/// registry. +/// Command-line flags for determining the local "Wasmer Environment". +/// +/// This is where you access `$WASMER_DIR`, the `$WASMER_DIR/wasmer.toml` config +/// file, and specify the current registry. #[derive(Debug, Clone, PartialEq, clap::Parser)] -pub struct WasmerDir { +pub struct WasmerEnv { /// Set Wasmer's home directory #[clap(long, env = "WASMER_DIR", default_value = WASMER_DIR.as_os_str())] wasmer_dir: PathBuf, @@ -22,13 +24,13 @@ pub struct WasmerDir { token: Option, } -impl WasmerDir { +impl WasmerEnv { pub(crate) fn new( wasmer_dir: PathBuf, registry: Option, token: Option, ) -> Self { - WasmerDir { + WasmerEnv { wasmer_dir, registry, token, @@ -72,7 +74,7 @@ impl WasmerDir { } } -impl Default for WasmerDir { +impl Default for WasmerEnv { fn default() -> Self { Self { wasmer_dir: WASMER_DIR.clone(), diff --git a/tests/integration/cli/tests/config.rs b/tests/integration/cli/tests/config.rs index 9bbfcb122ce..994382fef23 100644 --- a/tests/integration/cli/tests/config.rs +++ b/tests/integration/cli/tests/config.rs @@ -1,5 +1,5 @@ +use assert_cmd::Command; use std::path::{Path, PathBuf}; -use std::process::Command; use wasmer_integration_tests_cli::{get_repo_root_path, get_wasmer_path}; fn get_wasmer_dir() -> Result { @@ -26,61 +26,50 @@ fn get_wasmer_dir() -> Result { } #[test] -fn wasmer_config_multiget() -> anyhow::Result<()> { - let bin_path = get_wasmer_dir()?.join("bin"); - let include_path = get_wasmer_dir()?.join("include"); - +fn wasmer_config_multiget() { + let wasmer_dir = get_wasmer_dir().unwrap(); + let bin_path = wasmer_dir.join("bin"); + let include_path = wasmer_dir.join("include"); let bin = format!("{}", bin_path.display()); let include = format!("-I{}", include_path.display()); - let output = Command::new(get_wasmer_path()) + let assert = Command::new(get_wasmer_path()) .arg("config") .arg("--bindir") .arg("--cflags") - .output()?; - - let lines = String::from_utf8_lossy(&output.stdout) - .lines() - .map(|s| s.trim().to_string()) - .collect::>(); + .assert(); - let expected = vec![bin, include]; - - assert_eq!(lines, expected); - - Ok(()) + assert + .success() + .stdout(predicates::str::contains(&bin)) + .stdout(predicates::str::contains(&include)); } #[test] -fn wasmer_config_error() -> anyhow::Result<()> { - let output = Command::new(get_wasmer_path()) +fn wasmer_config_error() { + let assert = Command::new(get_wasmer_path()) .arg("config") .arg("--bindir") .arg("--cflags") .arg("--pkg-config") - .output()?; + .assert(); - let lines = String::from_utf8_lossy(&output.stderr) - .lines() - .map(|s| s.trim().to_string()) - .collect::>(); - #[cfg(not(windows))] - let expected_1 = "Usage: wasmer config --bindir --cflags"; - #[cfg(windows)] - let expected_1 = "Usage: wasmer.exe config --bindir --cflags"; - - let expected = vec![ - "error: the argument '--bindir' cannot be used with '--pkg-config'", - "", - expected_1, - "", - "For more information, try '--help'.", - ]; - - assert_eq!(lines, expected); - - Ok(()) + let expected_1 = if cfg!(windows) { + "Usage: wasmer.exe config --bindir --cflags" + } else { + "Usage: wasmer config --bindir --cflags" + }; + + assert + .stderr(predicates::str::contains( + "error: the argument '--bindir' cannot be used with '--pkg-config'", + )) + .stderr(predicates::str::contains(expected_1)) + .stderr(predicates::str::contains( + "For more information, try '--help'.", + )); } + #[test] fn config_works() -> anyhow::Result<()> { let bindir = Command::new(get_wasmer_path())