Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for --dry-run and cargo ws plan #157

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need the j1 if you add #[serial] to all the tests.

```

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 <args>
```

If you don't want to override any existing snapshots, use:

```
INSTA_UPDATE=unseen cargo test <args>
```

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.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ overrides the value specified in **Workspace**.

<!-- omit from toc -->
## Contributors
See the [Contributor guide](CONTRIBUTING.md).

Here is a list of [Contributors](http://github.com/pksunkara/cargo-workspaces/contributors)

<!-- omit from toc -->
Expand Down
36 changes: 36 additions & 0 deletions cargo-workspaces/Cargo.lock

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

1 change: 1 addition & 0 deletions cargo-workspaces/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion cargo-workspaces/src/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ impl Publish {
}
}

info!("success", "ok");
if !self.dry_run {
info!("success", "ok");
}
Ok(())
}

Expand Down
23 changes: 23 additions & 0 deletions cargo-workspaces/tests/plan.rs
Original file line number Diff line number Diff line change
@@ -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);
}
35 changes: 35 additions & 0 deletions cargo-workspaces/tests/publish.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
mod utils;
use insta::assert_snapshot;

#[test]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see some test failures caused by parallel execution. #[serial] should be added to all these tests.

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);
}
6 changes: 6 additions & 0 deletions cargo-workspaces/tests/snapshots/exec__normal-2.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ name = "dep1"
version = "0.1.0"
authors = ["Pavan Kumar Sunkara <[email protected]>"]
edition = "2018"
description = "dep1"
license = "MIT"

[dependencies]
[package]
name = "dep2"
version = "0.1.0"
authors = ["Pavan Kumar Sunkara <[email protected]>"]
edition = "2018"
description = "dep2"
license = "MIT"

[dependencies]
pre_dep1 = { version = "0.1.0", path = "../dep1", package = "dep1" }
Expand All @@ -22,6 +26,8 @@ name = "top"
version = "0.1.0"
authors = ["Pavan Kumar Sunkara <[email protected]>"]
edition = "2018"
description = "top"
license = "MIT"

[dependencies]
dep = { version = "0.1.0", path = "../dep1", package = "dep1" }
Expand Down
4 changes: 2 additions & 2 deletions cargo-workspaces/tests/snapshots/exec__normal_ignore-2.snap
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
---
source: tests/exec.rs
assertion_line: 29
expression: out

---
[package]
name = "dep1"
version = "0.1.0"
authors = ["Pavan Kumar Sunkara <[email protected]>"]
edition = "2018"
description = "dep1"
license = "MIT"

[dependencies]

8 changes: 8 additions & 0 deletions cargo-workspaces/tests/snapshots/plan__plan_normal.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: tests/plan.rs
expression: out
---
dep1
dep2
top

8 changes: 8 additions & 0 deletions cargo-workspaces/tests/snapshots/plan__plan_normal_long.snap
Original file line number Diff line number Diff line change
@@ -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

6 changes: 6 additions & 0 deletions cargo-workspaces/tests/snapshots/plan__plan_single.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: tests/plan.rs
expression: out
---
simple

10 changes: 10 additions & 0 deletions cargo-workspaces/tests/snapshots/publish__dry_run_normal.snap
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions cargo-workspaces/tests/snapshots/publish__dry_run_single.snap
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions cargo-workspaces/tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>()
.join("\n");
}
2 changes: 2 additions & 0 deletions fixtures/normal/dep1/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ name = "dep1"
version = "0.1.0"
authors = ["Pavan Kumar Sunkara <[email protected]>"]
edition = "2018"
description = "dep1"
license = "MIT"

[dependencies]
2 changes: 2 additions & 0 deletions fixtures/normal/dep2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ name = "dep2"
version = "0.1.0"
authors = ["Pavan Kumar Sunkara <[email protected]>"]
edition = "2018"
description = "dep2"
license = "MIT"

[dependencies]
pre_dep1 = { version = "0.1.0", path = "../dep1", package = "dep1" }
2 changes: 2 additions & 0 deletions fixtures/normal/top/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ name = "top"
version = "0.1.0"
authors = ["Pavan Kumar Sunkara <[email protected]>"]
edition = "2018"
description = "top"
license = "MIT"

[dependencies]
dep = { version = "0.1.0", path = "../dep1", package = "dep1" }
Expand Down
6 changes: 6 additions & 0 deletions fixtures/normal_missing_fields/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[workspace]
members = [
"top",
"dep1",
"dep2",
]
8 changes: 8 additions & 0 deletions fixtures/normal_missing_fields/dep1/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "dep1"
version = "0.1.0"
authors = ["Pavan Kumar Sunkara <[email protected]>"]
edition = "2018"
license = "MIT"

[dependencies]
Empty file.
9 changes: 9 additions & 0 deletions fixtures/normal_missing_fields/dep2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "dep2"
version = "0.1.0"
authors = ["Pavan Kumar Sunkara <[email protected]>"]
edition = "2018"
description = "dep2"

[dependencies]
pre_dep1 = { version = "0.1.0", path = "../dep1", package = "dep1" }
Empty file.
10 changes: 10 additions & 0 deletions fixtures/normal_missing_fields/top/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "top"
version = "0.1.0"
authors = ["Pavan Kumar Sunkara <[email protected]>"]
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" }
3 changes: 3 additions & 0 deletions fixtures/normal_missing_fields/top/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}
Loading
Loading