diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..9bbcbb2 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,49 @@ +# Contributing to `cargo-workspaces` + +If you want to contribute to the project, take a look at [open issues](https://github.com/pksunkara/cargo-workspaces/issues) +or create a PR. + +Please make sure that new functionality has tests. + +## Running tests + +The recommended way to run tests for the crate is as follows: + +```sh +cargo test --manifest-path cargo-workspaces/Cargo.toml -j1 +``` + +The integration tests manipulate the file system, so running them from multiple threads +may cause race conditions and unexpected failures. + +### Adding tests and updating snapshots + +`cargo-workspaces` uses [`insta`](https://docs.rs/insta/) for snapshot testing. + +When updating tests, you may run tests via: + +``` +INSTA_UPDATE=always cargo test +``` + +If you don't want to override any existing snapshots, use: + +``` +INSTA_UPDATE=unseen cargo test +``` + +Always make sure that generated snapshots match what you expect the test to produce. +Do not blindly commit changes in snapshots that you do not anticipate. + +For more details, check the `insta` documentation. + +### Troubleshooting + +If you observe unexpected differences in snapshots, you may want to override your compiler to +the same version as used in [CI](.github/workflows/ci.yml), e.g.: + +``` +cargo override set 1.70 +``` + +The newer versions of `cargo` may produce different output, which would break snapshot tests. diff --git a/README.md b/README.md index 7b3fa61..639a99e 100644 --- a/README.md +++ b/README.md @@ -316,6 +316,8 @@ overrides the value specified in **Workspace**. ## Contributors +See the [Contributor guide](CONTRIBUTING.md). + Here is a list of [Contributors](http://github.com/pksunkara/cargo-workspaces/contributors) diff --git a/cargo-workspaces/Cargo.lock b/cargo-workspaces/Cargo.lock index b6c604e..6e6fa41 100644 --- a/cargo-workspaces/Cargo.lock +++ b/cargo-workspaces/Cargo.lock @@ -202,6 +202,7 @@ dependencies = [ "serde", "serde_json", "serial_test", + "strip-ansi-escapes", "tame-index", "tempfile", "thiserror", @@ -2414,6 +2415,15 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" +[[package]] +name = "strip-ansi-escapes" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55ff8ef943b384c414f54aefa961dd2bd853add74ec75e7ac74cf91dba62bcfa" +dependencies = [ + "vte", +] + [[package]] name = "strsim" version = "0.10.0" @@ -2790,12 +2800,38 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "version_check" version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +[[package]] +name = "vte" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f5022b5fbf9407086c180e9557be968742d839e68346af7792b8592489732197" +dependencies = [ + "utf8parse", + "vte_generate_state_changes", +] + +[[package]] +name = "vte_generate_state_changes" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e369bee1b05d510a7b4ed645f5faa90619e05437111783ea5848f28d97d3c2e" +dependencies = [ + "proc-macro2", + "quote", +] + [[package]] name = "wait-timeout" version = "0.2.0" diff --git a/cargo-workspaces/Cargo.toml b/cargo-workspaces/Cargo.toml index 77343d7..e794ba7 100644 --- a/cargo-workspaces/Cargo.toml +++ b/cargo-workspaces/Cargo.toml @@ -51,6 +51,7 @@ insta = { version = "1.32.0", features = ["redactions"] } indoc = "1.0.9" serial_test = "2.0.0" tempfile = "3.6.0" +strip-ansi-escapes = "0.2.0" [workspace.metadata.workspaces] no_individual_tags = true diff --git a/cargo-workspaces/src/publish.rs b/cargo-workspaces/src/publish.rs index 107f710..d0d5005 100644 --- a/cargo-workspaces/src/publish.rs +++ b/cargo-workspaces/src/publish.rs @@ -166,7 +166,9 @@ impl Publish { } } - info!("success", "ok"); + if !self.dry_run { + info!("success", "ok"); + } Ok(()) } diff --git a/cargo-workspaces/tests/plan.rs b/cargo-workspaces/tests/plan.rs new file mode 100644 index 0000000..f55d076 --- /dev/null +++ b/cargo-workspaces/tests/plan.rs @@ -0,0 +1,23 @@ +mod utils; +use insta::assert_snapshot; + +#[test] +fn test_plan_single() { + // `err` may contain a warning about missing `http.cainfo` config value. + let (out, _err) = utils::run("../fixtures/single", &["ws", "plan"]); + assert_snapshot!(out); +} + +#[test] +fn test_plan_normal() { + // `err` may contain a warning about missing `http.cainfo` config value. + let (out, _err) = utils::run("../fixtures/normal", &["ws", "plan"]); + assert_snapshot!(out); +} + +#[test] +fn test_plan_normal_long() { + // `err` may contain a warning about missing `http.cainfo` config value. + let (out, _err) = utils::run("../fixtures/normal", &["ws", "plan", "--long"]); + assert_snapshot!(out); +} diff --git a/cargo-workspaces/tests/publish.rs b/cargo-workspaces/tests/publish.rs new file mode 100644 index 0000000..18d1ee9 --- /dev/null +++ b/cargo-workspaces/tests/publish.rs @@ -0,0 +1,35 @@ +mod utils; +use insta::assert_snapshot; + +#[test] +fn test_dry_run_single() { + // `--allow-dirty` is supplied to not break tests during development. + let mut err = utils::run_err( + "../fixtures/single", + &["ws", "publish", "--dry-run", "--allow-dirty"], + ); + utils::normalize_output(&mut err); + assert_snapshot!(err); +} + +#[test] +fn test_dry_run_normal() { + // `--allow-dirty` is supplied to not break tests during development. + let mut err = utils::run_err( + "../fixtures/normal", + &["ws", "publish", "--dry-run", "--allow-dirty"], + ); + utils::normalize_output(&mut err); + assert_snapshot!(err); +} + +#[test] +fn test_dry_run_normal_missing_fields() { + // `--allow-dirty` is supplied to not break tests during development. + let mut err = utils::run_err( + "../fixtures/normal_missing_fields", + &["ws", "publish", "--dry-run", "--allow-dirty"], + ); + utils::normalize_output(&mut err); + assert_snapshot!(err); +} diff --git a/cargo-workspaces/tests/snapshots/exec__normal-2.snap b/cargo-workspaces/tests/snapshots/exec__normal-2.snap index 2402e3c..dd387ae 100644 --- a/cargo-workspaces/tests/snapshots/exec__normal-2.snap +++ b/cargo-workspaces/tests/snapshots/exec__normal-2.snap @@ -7,6 +7,8 @@ name = "dep1" version = "0.1.0" authors = ["Pavan Kumar Sunkara "] edition = "2018" +description = "dep1" +license = "MIT" [dependencies] [package] @@ -14,6 +16,8 @@ name = "dep2" version = "0.1.0" authors = ["Pavan Kumar Sunkara "] edition = "2018" +description = "dep2" +license = "MIT" [dependencies] pre_dep1 = { version = "0.1.0", path = "../dep1", package = "dep1" } @@ -22,6 +26,8 @@ name = "top" version = "0.1.0" authors = ["Pavan Kumar Sunkara "] edition = "2018" +description = "top" +license = "MIT" [dependencies] dep = { version = "0.1.0", path = "../dep1", package = "dep1" } diff --git a/cargo-workspaces/tests/snapshots/exec__normal_ignore-2.snap b/cargo-workspaces/tests/snapshots/exec__normal_ignore-2.snap index 4cce8bf..3963d20 100644 --- a/cargo-workspaces/tests/snapshots/exec__normal_ignore-2.snap +++ b/cargo-workspaces/tests/snapshots/exec__normal_ignore-2.snap @@ -1,14 +1,14 @@ --- source: tests/exec.rs -assertion_line: 29 expression: out - --- [package] name = "dep1" version = "0.1.0" authors = ["Pavan Kumar Sunkara "] edition = "2018" +description = "dep1" +license = "MIT" [dependencies] diff --git a/cargo-workspaces/tests/snapshots/plan__plan_normal.snap b/cargo-workspaces/tests/snapshots/plan__plan_normal.snap new file mode 100644 index 0000000..49613cc --- /dev/null +++ b/cargo-workspaces/tests/snapshots/plan__plan_normal.snap @@ -0,0 +1,8 @@ +--- +source: tests/plan.rs +expression: out +--- +dep1 +dep2 +top + diff --git a/cargo-workspaces/tests/snapshots/plan__plan_normal_long.snap b/cargo-workspaces/tests/snapshots/plan__plan_normal_long.snap new file mode 100644 index 0000000..87de9e9 --- /dev/null +++ b/cargo-workspaces/tests/snapshots/plan__plan_normal_long.snap @@ -0,0 +1,8 @@ +--- +source: tests/plan.rs +expression: out +--- +dep1 v0.1.0 dep1 +dep2 v0.1.0 dep2 +top v0.1.0 top + diff --git a/cargo-workspaces/tests/snapshots/plan__plan_single.snap b/cargo-workspaces/tests/snapshots/plan__plan_single.snap new file mode 100644 index 0000000..590e328 --- /dev/null +++ b/cargo-workspaces/tests/snapshots/plan__plan_single.snap @@ -0,0 +1,6 @@ +--- +source: tests/plan.rs +expression: out +--- +simple + diff --git a/cargo-workspaces/tests/snapshots/publish__dry_run_normal.snap b/cargo-workspaces/tests/snapshots/publish__dry_run_normal.snap new file mode 100644 index 0000000..0b47cc7 --- /dev/null +++ b/cargo-workspaces/tests/snapshots/publish__dry_run_normal.snap @@ -0,0 +1,10 @@ +--- +source: tests/publish.rs +expression: err +--- +warn Dry run doesn't check that all dependencies have been published. +warn Dry run doesn't perform versioning. +info checking dep1 +info checking dep2 +info checking top +info success ok diff --git a/cargo-workspaces/tests/snapshots/publish__dry_run_normal_missing_fields.snap b/cargo-workspaces/tests/snapshots/publish__dry_run_normal_missing_fields.snap new file mode 100644 index 0000000..41eae1e --- /dev/null +++ b/cargo-workspaces/tests/snapshots/publish__dry_run_normal_missing_fields.snap @@ -0,0 +1,11 @@ +--- +source: tests/publish.rs +expression: err +--- +warn Dry run doesn't check that all dependencies have been published. +warn Dry run doesn't perform versioning. +info checking dep1 +warn check failed 'description' field should be set +info checking dep2 +info checking top +warn failure some checks failed diff --git a/cargo-workspaces/tests/snapshots/publish__dry_run_single.snap b/cargo-workspaces/tests/snapshots/publish__dry_run_single.snap new file mode 100644 index 0000000..aeb5f16 --- /dev/null +++ b/cargo-workspaces/tests/snapshots/publish__dry_run_single.snap @@ -0,0 +1,8 @@ +--- +source: tests/publish.rs +expression: err +--- +warn Dry run doesn't check that all dependencies have been published. +warn Dry run doesn't perform versioning. +info checking simple +info success ok diff --git a/cargo-workspaces/tests/utils.rs b/cargo-workspaces/tests/utils.rs index 57b26fa..bfeb811 100644 --- a/cargo-workspaces/tests/utils.rs +++ b/cargo-workspaces/tests/utils.rs @@ -29,3 +29,27 @@ pub fn run_err(dir: &str, args: &[&str]) -> String { assert!(out.is_empty()); err } + +/// Makes the output of commands suitable for snapshot testing: +/// - Removes the error for missing `http.cainfo` config value. +/// - Removes `cargo build` output. +pub fn normalize_output(input: &mut String) { + *input = input + .lines() + .filter(|line| { + // `cargo build` output starts with 3 spaces. + // Depending on configuration, there may be also ANSI escape codes, + // so we're performing the simplest check possible. + if strip_ansi_escapes::strip_str(line).starts_with(" ") { + return false; + } + // `cargo` may warn about missing `http.cainfo` config value, and it + // depends on the user configuration. + if line.contains("http.cainfo") { + return false; + } + true + }) + .collect::>() + .join("\n"); +} diff --git a/fixtures/normal/dep1/Cargo.toml b/fixtures/normal/dep1/Cargo.toml index ac39d62..1db7b49 100644 --- a/fixtures/normal/dep1/Cargo.toml +++ b/fixtures/normal/dep1/Cargo.toml @@ -3,5 +3,7 @@ name = "dep1" version = "0.1.0" authors = ["Pavan Kumar Sunkara "] edition = "2018" +description = "dep1" +license = "MIT" [dependencies] diff --git a/fixtures/normal/dep2/Cargo.toml b/fixtures/normal/dep2/Cargo.toml index 9fe96a4..a5ccd92 100644 --- a/fixtures/normal/dep2/Cargo.toml +++ b/fixtures/normal/dep2/Cargo.toml @@ -3,6 +3,8 @@ name = "dep2" version = "0.1.0" authors = ["Pavan Kumar Sunkara "] edition = "2018" +description = "dep2" +license = "MIT" [dependencies] pre_dep1 = { version = "0.1.0", path = "../dep1", package = "dep1" } diff --git a/fixtures/normal/top/Cargo.toml b/fixtures/normal/top/Cargo.toml index bd761c4..57dbd6c 100644 --- a/fixtures/normal/top/Cargo.toml +++ b/fixtures/normal/top/Cargo.toml @@ -3,6 +3,8 @@ name = "top" version = "0.1.0" authors = ["Pavan Kumar Sunkara "] edition = "2018" +description = "top" +license = "MIT" [dependencies] dep = { version = "0.1.0", path = "../dep1", package = "dep1" } diff --git a/fixtures/normal_missing_fields/Cargo.toml b/fixtures/normal_missing_fields/Cargo.toml new file mode 100644 index 0000000..e446b73 --- /dev/null +++ b/fixtures/normal_missing_fields/Cargo.toml @@ -0,0 +1,6 @@ +[workspace] +members = [ + "top", + "dep1", + "dep2", +] diff --git a/fixtures/normal_missing_fields/dep1/Cargo.toml b/fixtures/normal_missing_fields/dep1/Cargo.toml new file mode 100644 index 0000000..8515bcb --- /dev/null +++ b/fixtures/normal_missing_fields/dep1/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "dep1" +version = "0.1.0" +authors = ["Pavan Kumar Sunkara "] +edition = "2018" +license = "MIT" + +[dependencies] diff --git a/fixtures/normal_missing_fields/dep1/src/lib.rs b/fixtures/normal_missing_fields/dep1/src/lib.rs new file mode 100644 index 0000000..e69de29 diff --git a/fixtures/normal_missing_fields/dep2/Cargo.toml b/fixtures/normal_missing_fields/dep2/Cargo.toml new file mode 100644 index 0000000..01019bd --- /dev/null +++ b/fixtures/normal_missing_fields/dep2/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "dep2" +version = "0.1.0" +authors = ["Pavan Kumar Sunkara "] +edition = "2018" +description = "dep2" + +[dependencies] +pre_dep1 = { version = "0.1.0", path = "../dep1", package = "dep1" } diff --git a/fixtures/normal_missing_fields/dep2/src/lib.rs b/fixtures/normal_missing_fields/dep2/src/lib.rs new file mode 100644 index 0000000..e69de29 diff --git a/fixtures/normal_missing_fields/top/Cargo.toml b/fixtures/normal_missing_fields/top/Cargo.toml new file mode 100644 index 0000000..d8a4905 --- /dev/null +++ b/fixtures/normal_missing_fields/top/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "top" +version = "0.1.0" +authors = ["Pavan Kumar Sunkara "] +edition = "2018" +keywords = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven"] + +[dependencies] +dep = { version = "0.1.0", path = "../dep1", package = "dep1" } +dep2 = { version = "0.1.0", path = "../dep2" } diff --git a/fixtures/normal_missing_fields/top/src/main.rs b/fixtures/normal_missing_fields/top/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/fixtures/normal_missing_fields/top/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/fixtures/single/simple/Cargo.toml b/fixtures/single/simple/Cargo.toml index 066e61e..0822099 100644 --- a/fixtures/single/simple/Cargo.toml +++ b/fixtures/single/simple/Cargo.toml @@ -3,6 +3,8 @@ name = "simple" version = "0.1.0" authors = ["Pavan Kumar Sunkara "] edition = "2018" +description = "simple" +license = "MIT" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html