From c82effc9465b53dabab95a6f2fd88c7717e3040e Mon Sep 17 00:00:00 2001 From: Michael-F-Bryan Date: Wed, 28 Jun 2023 16:19:26 +0800 Subject: [PATCH] Fixed some unhygienic publish and config tests --- Cargo.lock | 1 + tests/integration/cli/Cargo.toml | 1 + tests/integration/cli/src/assets.rs | 23 +++ tests/integration/cli/tests/config.rs | 270 ++++++++++++------------- tests/integration/cli/tests/publish.rs | 136 +++++-------- 5 files changed, 203 insertions(+), 228 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2756c0a21a8..c11f2e56eac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5899,6 +5899,7 @@ dependencies = [ "target-lexicon 0.12.8", "tempfile", "tokio", + "wasmer-registry 5.2.0", ] [[package]] diff --git a/tests/integration/cli/Cargo.toml b/tests/integration/cli/Cargo.toml index 913fa148702..6f2c39e8700 100644 --- a/tests/integration/cli/Cargo.toml +++ b/tests/integration/cli/Cargo.toml @@ -32,6 +32,7 @@ tar = "0.4.38" flate2 = "1.0.24" dirs = "4.0.0" derivative = { version = "^2" } +wasmer-registry = { path = "../../../lib/registry", default-features = false } [features] default = ["webc_runner"] diff --git a/tests/integration/cli/src/assets.rs b/tests/integration/cli/src/assets.rs index c3882c0c8ed..f6692636d82 100644 --- a/tests/integration/cli/src/assets.rs +++ b/tests/integration/cli/src/assets.rs @@ -150,3 +150,26 @@ pub fn get_repo_root_path() -> Option { } result } + +pub fn get_wasmer_dir() -> Result { + if let Ok(s) = std::env::var("WASMER_DIR") { + Ok(Path::new(&s).to_path_buf()) + } else if let Some(root_dir) = get_repo_root_path().and_then(|root| { + if root.join("package").exists() { + Some(root.join("package")) + } else { + None + } + }) { + Ok(root_dir) + } else { + let home_dir = dirs::home_dir() + .ok_or(anyhow::anyhow!("no home dir"))? + .join(".wasmer"); + if home_dir.exists() { + Ok(home_dir) + } else { + Err(anyhow::anyhow!("no .wasmer home dir")) + } + } +} diff --git a/tests/integration/cli/tests/config.rs b/tests/integration/cli/tests/config.rs index 994382fef23..491ae4b2589 100644 --- a/tests/integration/cli/tests/config.rs +++ b/tests/integration/cli/tests/config.rs @@ -1,58 +1,55 @@ +use std::path::Path; + use assert_cmd::Command; -use std::path::{Path, PathBuf}; -use wasmer_integration_tests_cli::{get_repo_root_path, get_wasmer_path}; - -fn get_wasmer_dir() -> Result { - if let Ok(s) = std::env::var("WASMER_DIR") { - Ok(Path::new(&s).to_path_buf()) - } else if let Some(root_dir) = get_repo_root_path().and_then(|root| { - if root.join("package").exists() { - Some(root.join("package")) - } else { - None - } - }) { - Ok(root_dir) - } else { - let home_dir = dirs::home_dir() - .ok_or(anyhow::anyhow!("no home dir"))? - .join(".wasmer"); - if home_dir.exists() { - Ok(home_dir) - } else { - Err(anyhow::anyhow!("no .wasmer home dir")) - } - } +use predicates::str::contains; +use tempfile::TempDir; +use wasmer_integration_tests_cli::get_wasmer_path; +use wasmer_registry::WasmerConfig; + +fn setup_wasmer_dir() -> TempDir { + let temp = TempDir::new().unwrap(); + + let config_path = WasmerConfig::get_file_location(temp.path()); + WasmerConfig::default().save(&config_path).unwrap(); + + temp +} + +fn contains_path(path: impl AsRef) -> predicates::str::ContainsPredicate { + let expected = path.as_ref().display().to_string(); + contains(expected) +} + +fn wasmer_cmd(temp: &TempDir) -> Command { + let mut cmd = Command::new(get_wasmer_path()); + cmd.env("WASMER_DIR", temp.path()); + cmd } #[test] fn wasmer_config_multiget() { - let wasmer_dir = get_wasmer_dir().unwrap(); + let temp = setup_wasmer_dir(); + let wasmer_dir = temp.path(); + let bin_path = wasmer_dir.join("bin"); let include_path = wasmer_dir.join("include"); - let bin = format!("{}", bin_path.display()); + let bin = bin_path.display().to_string(); let include = format!("-I{}", include_path.display()); - let assert = Command::new(get_wasmer_path()) + wasmer_cmd(&temp) .arg("config") .arg("--bindir") .arg("--cflags") - .assert(); - - assert + .env("WASMER_DIR", wasmer_dir) + .assert() .success() - .stdout(predicates::str::contains(&bin)) - .stdout(predicates::str::contains(&include)); + .stdout(contains(bin)) + .stdout(contains(include)); } #[test] -fn wasmer_config_error() { - let assert = Command::new(get_wasmer_path()) - .arg("config") - .arg("--bindir") - .arg("--cflags") - .arg("--pkg-config") - .assert(); +fn wasmer_config_conflicting_flags() { + let temp = setup_wasmer_dir(); let expected_1 = if cfg!(windows) { "Usage: wasmer.exe config --bindir --cflags" @@ -60,129 +57,112 @@ fn wasmer_config_error() { "Usage: wasmer config --bindir --cflags" }; - assert - .stderr(predicates::str::contains( + wasmer_cmd(&temp) + .arg("config") + .arg("--bindir") + .arg("--cflags") + .arg("--pkg-config") + .assert() + .stderr(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'.", - )); + .stderr(contains(expected_1)) + .stderr(contains("For more information, try '--help'.")); } #[test] -fn config_works() -> anyhow::Result<()> { - let bindir = Command::new(get_wasmer_path()) +fn c_flags() { + let temp = setup_wasmer_dir(); + let wasmer_dir = temp.path(); + + wasmer_cmd(&temp) .arg("config") .arg("--bindir") - .output()?; - - let bin_path = get_wasmer_dir()?.join("bin"); - assert_eq!( - String::from_utf8(bindir.stdout).unwrap(), - format!("{}\n", bin_path.display()) - ); + .assert() + .success() + .stdout(contains_path(temp.path().join("bin"))); - let bindir = Command::new(get_wasmer_path()) + wasmer_cmd(&temp) .arg("config") .arg("--cflags") - .output()?; - - let include_path = get_wasmer_dir()?.join("include"); - assert_eq!( - String::from_utf8(bindir.stdout).unwrap(), - format!("-I{}\n", include_path.display()) - ); + .assert() + .success() + .stdout(contains(format!( + "-I{}\n", + wasmer_dir.join("include").display() + ))); - let bindir = Command::new(get_wasmer_path()) + wasmer_cmd(&temp) .arg("config") .arg("--includedir") - .output()?; - - let include_path = get_wasmer_dir()?.join("include"); - assert_eq!( - String::from_utf8(bindir.stdout).unwrap(), - format!("{}\n", include_path.display()) - ); + .assert() + .success() + .stdout(contains_path(wasmer_dir.join("include"))); - let bindir = Command::new(get_wasmer_path()) + wasmer_cmd(&temp) .arg("config") .arg("--libdir") - .output()?; - - let lib_path = get_wasmer_dir()?.join("lib"); - assert_eq!( - String::from_utf8(bindir.stdout).unwrap(), - format!("{}\n", lib_path.display()) - ); + .assert() + .success() + .stdout(contains_path(wasmer_dir.join("lib"))); - let bindir = Command::new(get_wasmer_path()) + wasmer_cmd(&temp) .arg("config") .arg("--libs") - .output()?; + .assert() + .stdout(contains(format!( + "-L{} -lwasmer\n", + wasmer_dir.join("lib").display() + ))); - let lib_path = get_wasmer_dir()?.join("lib"); - assert_eq!( - String::from_utf8(bindir.stdout).unwrap(), - format!("-L{} -lwasmer\n", lib_path.display()) - ); - - let bindir = Command::new(get_wasmer_path()) + wasmer_cmd(&temp) .arg("config") .arg("--prefix") - .output()?; - - let wasmer_dir = get_wasmer_dir()?; - assert_eq!( - String::from_utf8(bindir.stdout).unwrap(), - format!("{}\n", wasmer_dir.display()) - ); + .assert() + .success() + .stdout(contains_path(wasmer_dir)); - let bindir = Command::new(get_wasmer_path()) + let output = wasmer_cmd(&temp) .arg("config") .arg("--pkg-config") - .output()?; - - let bin_path = format!("{}", bin_path.display()); - let include_path = format!("{}", include_path.display()); - let lib_path = format!("{}", lib_path.display()); - let wasmer_dir = format!("{}", wasmer_dir.display()); - - let args = vec![ - format!("prefix={wasmer_dir}"), - format!("exec_prefix={bin_path}"), - format!("includedir={include_path}"), - format!("libdir={lib_path}"), + .output() + .unwrap(); + + let pkg_config = vec![ + format!("prefix={}", wasmer_dir.display()), + format!("exec_prefix={}", wasmer_dir.join("bin").display()), + format!("includedir={}", wasmer_dir.join("include").display()), + format!("libdir={}", wasmer_dir.join("lib").display()), format!(""), format!("Name: wasmer"), format!("Description: The Wasmer library for running WebAssembly"), format!("Version: {}", env!("CARGO_PKG_VERSION")), - format!("Cflags: -I{include_path}"), - format!("Libs: -L{lib_path} -lwasmer"), - ]; + format!("Cflags: -I{}", wasmer_dir.join("include").display()), + format!("Libs: -L{} -lwasmer", wasmer_dir.join("lib").display()), + ] + .join("\n"); - let lines = String::from_utf8(bindir.stdout) + assert!(output.status.success()); + let stderr = std::str::from_utf8(&output.stdout) .unwrap() - .lines() - .map(|s| s.trim().to_string()) - .collect::>(); - - assert_eq!(lines, args); + .replace("\r\n", "\n"); + assert_eq!(stderr.trim(), pkg_config.trim()); - let output = Command::new(get_wasmer_path()) + wasmer_cmd(&temp) .arg("config") .arg("--config-path") - .output()?; + .assert() + .success() + .stdout(contains_path(temp.path().join("wasmer.toml"))); +} - let config_path = get_wasmer_dir()?.join("wasmer.toml"); - assert_eq!( - String::from_utf8_lossy(&output.stdout), - format!("{}\n", config_path.display()) - ); +#[test] +fn get_and_set_config_fields() -> anyhow::Result<()> { + let temp = setup_wasmer_dir(); // ---- config get - let output = Command::new(get_wasmer_path()) + let output = wasmer_cmd(&temp) .arg("config") .arg("get") .arg("registry.token") @@ -190,7 +170,7 @@ fn config_works() -> anyhow::Result<()> { let original_token = String::from_utf8_lossy(&output.stdout); - let output = Command::new(get_wasmer_path()) + let output = wasmer_cmd(&temp) .arg("config") .arg("set") .arg("registry.token") @@ -199,7 +179,7 @@ fn config_works() -> anyhow::Result<()> { assert_eq!(String::from_utf8_lossy(&output.stdout), "".to_string()); - let output = Command::new(get_wasmer_path()) + let output = wasmer_cmd(&temp) .arg("config") .arg("get") .arg("registry.token") @@ -210,7 +190,7 @@ fn config_works() -> anyhow::Result<()> { "abc123\n".to_string() ); - let output = Command::new(get_wasmer_path()) + let output = wasmer_cmd(&temp) .arg("config") .arg("set") .arg("registry.token") @@ -219,7 +199,7 @@ fn config_works() -> anyhow::Result<()> { assert_eq!(String::from_utf8_lossy(&output.stdout), "".to_string()); - let output = Command::new(get_wasmer_path()) + let output = wasmer_cmd(&temp) .arg("config") .arg("get") .arg("registry.token") @@ -230,7 +210,7 @@ fn config_works() -> anyhow::Result<()> { format!("{}\n", original_token.to_string().trim()) ); - let output = Command::new(get_wasmer_path()) + let output = wasmer_cmd(&temp) .arg("config") .arg("get") .arg("registry.url") @@ -238,18 +218,18 @@ fn config_works() -> anyhow::Result<()> { let original_url = String::from_utf8_lossy(&output.stdout); - let output = Command::new(get_wasmer_path()) + let output = wasmer_cmd(&temp) .arg("config") .arg("set") .arg("registry.url") - .arg("wapm.dev") + .arg("wasmer.wtf") .output()?; let output_str = String::from_utf8_lossy(&output.stdout); assert_eq!(output_str, "".to_string()); - let output = Command::new(get_wasmer_path()) + let output = wasmer_cmd(&temp) .arg("config") .arg("get") .arg("registry.url") @@ -258,10 +238,10 @@ fn config_works() -> anyhow::Result<()> { let output_str = String::from_utf8_lossy(&output.stdout); assert_eq!( output_str, - "https://registry.wapm.dev/graphql\n".to_string() + "https://registry.wasmer.wtf/graphql\n".to_string() ); - let output = Command::new(get_wasmer_path()) + let output = wasmer_cmd(&temp) .arg("config") .arg("set") .arg("registry.url") @@ -271,7 +251,7 @@ fn config_works() -> anyhow::Result<()> { let output_str = String::from_utf8_lossy(&output.stdout); assert_eq!(output_str, "".to_string()); - let output = Command::new(get_wasmer_path()) + let output = wasmer_cmd(&temp) .arg("config") .arg("get") .arg("registry.url") @@ -280,7 +260,7 @@ fn config_works() -> anyhow::Result<()> { let output_str = String::from_utf8_lossy(&output.stdout); assert_eq!(output_str, original_url.to_string()); - let output = Command::new(get_wasmer_path()) + let output = wasmer_cmd(&temp) .arg("config") .arg("get") .arg("telemetry.enabled") @@ -288,7 +268,7 @@ fn config_works() -> anyhow::Result<()> { let original_output = String::from_utf8_lossy(&output.stdout); - let output = Command::new(get_wasmer_path()) + let output = wasmer_cmd(&temp) .arg("config") .arg("set") .arg("telemetry.enabled") @@ -297,7 +277,7 @@ fn config_works() -> anyhow::Result<()> { assert_eq!(String::from_utf8_lossy(&output.stdout), "".to_string()); - let output = Command::new(get_wasmer_path()) + let output = wasmer_cmd(&temp) .arg("config") .arg("get") .arg("telemetry.enabled") @@ -308,7 +288,7 @@ fn config_works() -> anyhow::Result<()> { "true\n".to_string() ); - let output = Command::new(get_wasmer_path()) + let output = wasmer_cmd(&temp) .arg("config") .arg("set") .arg("telemetry.enabled") @@ -317,7 +297,7 @@ fn config_works() -> anyhow::Result<()> { assert_eq!(String::from_utf8_lossy(&output.stdout), "".to_string()); - let output = Command::new(get_wasmer_path()) + let output = wasmer_cmd(&temp) .arg("config") .arg("get") .arg("telemetry.enabled") @@ -328,7 +308,7 @@ fn config_works() -> anyhow::Result<()> { original_output.to_string() ); - let output = Command::new(get_wasmer_path()) + let output = wasmer_cmd(&temp) .arg("config") .arg("get") .arg("update-notifications.enabled") @@ -336,7 +316,7 @@ fn config_works() -> anyhow::Result<()> { let original_output = String::from_utf8_lossy(&output.stdout); - let output = Command::new(get_wasmer_path()) + let output = wasmer_cmd(&temp) .arg("config") .arg("set") .arg("update-notifications.enabled") @@ -345,7 +325,7 @@ fn config_works() -> anyhow::Result<()> { assert_eq!(String::from_utf8_lossy(&output.stdout), "".to_string()); - let output = Command::new(get_wasmer_path()) + let output = wasmer_cmd(&temp) .arg("config") .arg("get") .arg("update-notifications.enabled") @@ -356,7 +336,7 @@ fn config_works() -> anyhow::Result<()> { "true\n".to_string() ); - let output = Command::new(get_wasmer_path()) + let output = wasmer_cmd(&temp) .arg("config") .arg("set") .arg("update-notifications.enabled") @@ -365,7 +345,7 @@ fn config_works() -> anyhow::Result<()> { assert_eq!(String::from_utf8_lossy(&output.stdout), "".to_string()); - let output = Command::new(get_wasmer_path()) + let output = wasmer_cmd(&temp) .arg("config") .arg("get") .arg("update-notifications.enabled") diff --git a/tests/integration/cli/tests/publish.rs b/tests/integration/cli/tests/publish.rs index 7fa05eca45d..1b583467c51 100644 --- a/tests/integration/cli/tests/publish.rs +++ b/tests/integration/cli/tests/publish.rs @@ -1,15 +1,15 @@ -use std::process::Stdio; +use assert_cmd::prelude::OutputAssertExt; use wasmer_integration_tests_cli::{fixtures, get_wasmer_path}; #[test] -fn wasmer_publish() -> anyhow::Result<()> { +fn wasmer_publish() { // Only run this test in the CI if std::env::var("GITHUB_TOKEN").is_err() { - return Ok(()); + return; } let wapm_dev_token = std::env::var("WAPM_DEV_TOKEN").ok(); - let tempdir = tempfile::tempdir()?; + let tempdir = tempfile::tempdir().unwrap(); let path = tempdir.path(); let username = "ciuser"; @@ -25,47 +25,39 @@ fn wasmer_publish() -> anyhow::Result<()> { .replace("RANDOMVERSION1", &random1) .replace("RANDOMVERSION2", &random2) .replace("RANDOMVERSION3", &random3), - )?; + ) + .unwrap(); let mut cmd = std::process::Command::new(get_wasmer_path()); - cmd.arg("publish"); - cmd.arg("--quiet"); - cmd.arg("--registry"); - cmd.arg("wapm.dev"); - cmd.arg(path); + cmd.arg("publish") + .arg("--quiet") + .arg("--registry=wasmer.wtf") + .arg(path); if let Some(token) = wapm_dev_token { // Special case: GitHub secrets aren't visible to outside collaborators if token.is_empty() { - return Ok(()); + return; } - cmd.arg("--token"); - cmd.arg(token); + cmd.arg("--token").arg(token); } - let output = cmd.stdin(Stdio::null()).output().unwrap(); - - let stdout = String::from_utf8_lossy(&output.stdout); - let stderr = String::from_utf8_lossy(&output.stderr); - - assert_eq!(stdout, format!("Successfully published package `{username}/largewasmfile@{random1}.{random2}.{random3}`\n"), "failed to publish: {cmd:?}: {stderr}"); - - println!("wasmer publish ok! test done."); - - Ok(()) + cmd.assert().success().stdout(format!( + "Successfully published package `{username}/largewasmfile@{random1}.{random2}.{random3}`\n" + )); } // Runs a full integration test to test that the flow wasmer init - cargo build - // wasmer publish is working #[test] -fn wasmer_init_publish() -> anyhow::Result<()> { +fn wasmer_init_publish() { // Only run this test in the CI if std::env::var("GITHUB_TOKEN").is_err() { - return Ok(()); + return; } let wapm_dev_token = std::env::var("WAPM_DEV_TOKEN").ok(); - let tempdir = tempfile::tempdir()?; + let tempdir = tempfile::tempdir().unwrap(); let path = tempdir.path(); let username = "ciuser"; @@ -73,48 +65,33 @@ fn wasmer_init_publish() -> anyhow::Result<()> { let random2 = format!("{}", rand::random::()); let random3 = format!("{}", rand::random::()); - let mut cmd = std::process::Command::new("cargo"); - cmd.arg("init"); - cmd.arg("--bin"); - cmd.arg(path.join("randomversion")); - - let _ = cmd - .stdin(Stdio::null()) - .stdout(Stdio::inherit()) - .stderr(Stdio::inherit()) - .output() - .unwrap(); - - let mut cmd = std::process::Command::new("cargo"); - cmd.arg("build"); - cmd.arg("--release"); - cmd.arg("--target"); - cmd.arg("wasm32-wasi"); - cmd.arg("--manifest-path"); - cmd.arg(path.join("randomversion").join("Cargo.toml")); - - let _ = cmd - .stdin(Stdio::null()) - .stdout(Stdio::inherit()) - .stderr(Stdio::inherit()) - .output() - .unwrap(); + // Create a new Rust project and build it + std::process::Command::new("cargo") + .arg("init") + .arg("--bin") + .arg(path.join("randomversion")) + .assert() + .success(); + std::process::Command::new("cargo") + .arg("build") + .arg("--release") + .arg("--target") + .arg("wasm32-wasi") + .arg("--manifest-path") + .arg(path.join("randomversion").join("Cargo.toml")) + .assert() + .success(); // generate the wasmer.toml - let mut cmd = std::process::Command::new(get_wasmer_path()); - cmd.arg("init"); - cmd.arg("--namespace"); - cmd.arg(username); - cmd.arg("--version"); - cmd.arg(format!("{random1}.{random2}.{random3}")); - cmd.arg(path.join("randomversion")); - - let _ = cmd - .stdin(Stdio::null()) - .stdout(Stdio::inherit()) - .stderr(Stdio::inherit()) - .output() - .unwrap(); + std::process::Command::new(get_wasmer_path()) + .arg("init") + .arg("--namespace") + .arg(username) + .arg("--version") + .arg(format!("{random1}.{random2}.{random3}")) + .arg(path.join("randomversion")) + .assert() + .success(); let s = std::fs::read_to_string(path.join("randomversion").join("wasmer.toml")).unwrap(); @@ -122,29 +99,22 @@ fn wasmer_init_publish() -> anyhow::Result<()> { // publish let mut cmd = std::process::Command::new(get_wasmer_path()); - cmd.arg("publish"); - cmd.arg("--quiet"); - cmd.arg("--registry"); - cmd.arg("wapm.dev"); - cmd.arg(path.join("randomversion")); + cmd.arg("publish") + .arg("--quiet") + .arg("--registry=wasmer.wtf") + .arg(path.join("randomversion")); if let Some(token) = wapm_dev_token { // Special case: GitHub secrets aren't visible to outside collaborators if token.is_empty() { - return Ok(()); + return; } - cmd.arg("--token"); - cmd.arg(token); + cmd.arg("--token").arg(token); } - let output = cmd.stdin(Stdio::null()).output().unwrap(); - - let stdout = String::from_utf8_lossy(&output.stdout); - let stderr = String::from_utf8_lossy(&output.stderr); - - assert_eq!(stdout, format!("Successfully published package `{username}/randomversion@{random1}.{random2}.{random3}`\n"), "failed to publish: {cmd:?}: {stderr}"); - - println!("wasmer init publish ok! test done."); + let assert = cmd.assert(); - Ok(()) + assert.success().stdout(format!( + "Successfully published package `{username}/randomversion@{random1}.{random2}.{random3}`\n" + )); }