From 9ba05e12aae213883678801f51c56674c856ebfc Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 10 Nov 2022 15:56:03 +0100 Subject: [PATCH 1/3] Add simple CLI test The "unused_crate_dependencies" lint has to be disabled now, as it apparently fails because there are dev-dependencies now. Strange. Signed-off-by: Matthias Beyer --- Cargo.lock | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 3 +- src/main.rs | 1 - tests/cli.rs | 23 ++++++++++++ 4 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 tests/cli.rs diff --git a/Cargo.lock b/Cargo.lock index eeff2f6a..23e1957e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -75,6 +75,20 @@ version = "3.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "51f0a58f3b1453981b910b603a4a7346be14fccd50f8edd7c954725a9210c24f" +[[package]] +name = "assert_cmd" +version = "2.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba45b8163c49ab5f972e59a8a5a03b6d2972619d486e19ec9fe744f7c2753d3c" +dependencies = [ + "bstr 1.0.1", + "doc-comment", + "predicates", + "predicates-core", + "predicates-tree", + "wait-timeout", +] + [[package]] name = "atty" version = "0.2.14" @@ -149,6 +163,18 @@ dependencies = [ "serde 1.0.147", ] +[[package]] +name = "bstr" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fca0852af221f458706eb0725c03e4ed6c46af9ac98e6a689d5e634215d594dd" +dependencies = [ + "memchr", + "once_cell", + "regex-automata", + "serde 1.0.147", +] + [[package]] name = "bumpalo" version = "3.11.1" @@ -162,6 +188,7 @@ dependencies = [ "anyhow", "aquamarine", "ascii_table", + "assert_cmd", "atty", "bytesize", "chrono", @@ -191,6 +218,7 @@ dependencies = [ "log", "parse-display", "pom", + "predicates", "ptree", "rand", "rayon", @@ -458,7 +486,7 @@ version = "1.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "22813a6dc45b335f9bade10bf7271dc477e81113e89eb251a0bc2a8a81c536e1" dependencies = [ - "bstr", + "bstr 0.2.17", "csv-core", "itoa 0.4.8", "ryu", @@ -575,6 +603,12 @@ dependencies = [ "migrations_macros", ] +[[package]] +name = "difflib" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8" + [[package]] name = "digest" version = "0.10.5" @@ -614,6 +648,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "doc-comment" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" + [[package]] name = "either" version = "1.8.0" @@ -691,6 +731,15 @@ dependencies = [ "miniz_oxide", ] +[[package]] +name = "float-cmp" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98de4bbd547a563b716d8dfa9aad1cb19bfab00f4fa09a6a4ed21dbcf44ce9c4" +dependencies = [ + "num-traits 0.2.15", +] + [[package]] name = "fnv" version = "1.0.7" @@ -1421,6 +1470,12 @@ dependencies = [ "version_check", ] +[[package]] +name = "normalize-line-endings" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be" + [[package]] name = "num-integer" version = "0.1.45" @@ -1776,6 +1831,36 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "predicates" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab68289ded120dcbf9d571afcf70163233229052aec9b08ab09532f698d0e1e6" +dependencies = [ + "difflib", + "float-cmp", + "itertools 0.10.5", + "normalize-line-endings", + "predicates-core", + "regex", +] + +[[package]] +name = "predicates-core" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6e7125585d872860e9955ca571650b27a4979c5823084168c5ed5bbfb016b56" + +[[package]] +name = "predicates-tree" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad3f7fa8d61e139cbc7c3edfebf3b6678883a53f5ffac65d1259329a93ee43a5" +dependencies = [ + "predicates-core", + "termtree", +] + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -2350,6 +2435,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "termtree" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95059e91184749cb66be6dc994f67f182b6d897cb3df74a5bf66b5e709295fd8" + [[package]] name = "thiserror" version = "1.0.37" @@ -2658,6 +2749,15 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +[[package]] +name = "wait-timeout" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f200f5b12eb75f8c1ed65abd4b2db8a6e1b138a20de009dacee265a2498f3f6" +dependencies = [ + "libc", +] + [[package]] name = "walkdir" version = "2.3.2" diff --git a/Cargo.toml b/Cargo.toml index 7e1d4017..ffc89459 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -109,4 +109,5 @@ encoding_rs = ">=0.8.0, <=0.8.29" [dev-dependencies] toml = "0.5" - +assert_cmd = "2" +predicates = "2.1" diff --git a/src/main.rs b/src/main.rs index ad2e41dc..d31c1220 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,7 +33,6 @@ unused, unused_allocation, unused_comparisons, - unused_crate_dependencies, unused_extern_crates, unused_import_braces, unused_imports, diff --git a/tests/cli.rs b/tests/cli.rs new file mode 100644 index 00000000..1972ad7a --- /dev/null +++ b/tests/cli.rs @@ -0,0 +1,23 @@ +use std::process::Command; // Run programs +use assert_cmd::prelude::*; // Add methods on commands +use predicates::prelude::*; // Used for writing assertions + +#[test] +fn test_cli_build() -> Result<(), Box> { + let mut cmd = Command::cargo_bin("butido")?; + + cmd.current_dir(tmpdir.as_ref()) + .arg("build") + .arg("example") + .arg("-I") + .arg("foo:bar") + .arg("example") + .arg("1.0.0"); + + // butido cannot do anything if there are no packages. So this should fail. + cmd.assert() + .failure() + .stderr(predicate::str::contains("No such file or directory")); + + Ok(()) +} From 4a7ae7e87b3977d4884755fc6a30e7ffb9eee5ac Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 10 Nov 2022 16:01:15 +0100 Subject: [PATCH 2/3] Fix: Number of args for --env in "build" subcommand Fixes: a53f3bfd0622f1ee955af053c52f1d1577695c90 ("Rewrite value fetching") Signed-off-by: Matthias Beyer --- src/cli.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cli.rs b/src/cli.rs index d9b36f4a..512101d3 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -465,7 +465,7 @@ pub fn cli() -> Command { .arg(Arg::new("env") .required(false) - .num_args(0) + .num_args(1) .short('E') .long("env") .value_parser(ValueParser::new(env_pass_validator)) From c4e926980d94c9d099403dda424f9ea979cb0336 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 10 Nov 2022 16:22:38 +0100 Subject: [PATCH 3/3] Add more generic test setup helper For a testing directory where butido can potentially be automatically tested in. Signed-off-by: Matthias Beyer --- Cargo.lock | 11 +++++++++++ Cargo.toml | 1 + tests/cli.rs | 4 ++++ tests/common.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+) create mode 100644 tests/common.rs diff --git a/Cargo.lock b/Cargo.lock index 23e1957e..a12007e0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -235,6 +235,7 @@ dependencies = [ "shiplift", "syntect", "tar", + "tempdir", "terminal_size", "tokio", "tokio-stream", @@ -2402,6 +2403,16 @@ dependencies = [ "xattr", ] +[[package]] +name = "tempdir" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8" +dependencies = [ + "rand", + "remove_dir_all", +] + [[package]] name = "tempfile" version = "3.3.0" diff --git a/Cargo.toml b/Cargo.toml index ffc89459..6eb90932 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -111,3 +111,4 @@ encoding_rs = ">=0.8.0, <=0.8.29" toml = "0.5" assert_cmd = "2" predicates = "2.1" +tempdir = "0.3" diff --git a/tests/cli.rs b/tests/cli.rs index 1972ad7a..832db6f5 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -2,8 +2,12 @@ use std::process::Command; // Run programs use assert_cmd::prelude::*; // Add methods on commands use predicates::prelude::*; // Used for writing assertions +mod common; + #[test] fn test_cli_build() -> Result<(), Box> { + let tmpdir = tempdir::TempDir::new("butido")?; + common::setup_cwd(&tmpdir)?; let mut cmd = Command::cargo_bin("butido")?; cmd.current_dir(tmpdir.as_ref()) diff --git a/tests/common.rs b/tests/common.rs new file mode 100644 index 00000000..3171e95e --- /dev/null +++ b/tests/common.rs @@ -0,0 +1,42 @@ +use std::path::Path; +use assert_cmd::prelude::*; // Add methods on commands +use predicates::prelude::*; + +const CONFIG: &str = include_str!("../config.toml"); + +pub fn setup_cwd>(path: P) -> Result<(), Box> { + let mut toml: toml::Value = toml::from_str(CONFIG)?; + + for (key, value) in [ + ("releases_root", "releases"), + ("staging", "staging"), + ("source_cache", "sources"), + ("log_dir", "logs"), + ] { + let path = path.as_ref().join(value); + std::fs::create_dir(&path)?; + let value = toml::Value::String(path.display().to_string()); + let toml_key = toml + .get_mut(key) + .ok_or_else(|| format!("{} missing in configuration", key))?; + *toml_key = value + } + + std::fs::write( + path.as_ref().join("config.toml"), + toml::to_string_pretty(&toml)?, + )?; + + std::fs::write( + path.as_ref().join("pkg.toml"), + "" + )?; + + std::process::Command::new("git") + .current_dir(path) + .arg("init") + .assert() + .success(); + + Ok(()) +}