Skip to content

Commit

Permalink
Auto merge of #10620 - weihanglo:snapbox-cargo-init, r=epage
Browse files Browse the repository at this point in the history
Migrate tests of `cargo-init` to snapbox

### What does this PR try to resolve?

An attempt of migrating tests of `cargo-init` to [snapbox](https://crates.io/crates/snapbox/).

### How should we test and review this PR?

To review this PR, you may compare the old and the new version one by one. Files not listed in `<test-name>.out` are not asserted, so please help me make sure we don't miss any essential file to check.

Some redundant tests are covered by other tests or renamed.

### Additional information

I won't say the process of the migration was pleasant, but overall it results to a higher coverage of output file changes. Here are steps I performed to migrate a test case:

1. Run the old test and observe its output layout. Recommend using a separate worktree to preserve the temporary test output files.
2. Read the test code to arrange its input fixture at `<test-name>.in`.
3. Copy the old output layout or hand-pick the output layout you need. You don't need to fill contents of those files. `snapbox` will do it for you.
4. Run `SNAPSHOTS=overwrite cargo test --test testsuite <your-test-filter>` to assert and generate snapshots.
5. Compare the old layout and the new layout to see if anything missing.

Something observations when dealing with the migration:

* snapbox hasn't yet support unordered assertion.
* snapbox cannot assert inexistence of a file (And probably never?).
* No performance hit so far (measured with hyperfine).
  • Loading branch information
bors committed May 4, 2022
2 parents cfb8969 + 20c0c58 commit ad05cec
Show file tree
Hide file tree
Showing 193 changed files with 1,112 additions and 506 deletions.
1 change: 1 addition & 0 deletions tests/snapshots/init/auto_git.in
2 changes: 2 additions & 0 deletions tests/snapshots/init/auto_git.out/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
/Cargo.lock
8 changes: 8 additions & 0 deletions tests/snapshots/init/auto_git.out/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "case"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
8 changes: 8 additions & 0 deletions tests/snapshots/init/auto_git.out/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
let result = 2 + 2;
assert_eq!(result, 4);
}
}
1 change: 1 addition & 0 deletions tests/snapshots/init/auto_git.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Created library package
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
println!("Check that our file is not overwritten")
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "case"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
println!("Check that our file is not overwritten")
}

1 change: 1 addition & 0 deletions tests/snapshots/init/bin_already_exists_explicit.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Created binary (application) package
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
println!("Check that our file is not overwritten")
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "case"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

[[bin]]
name = "case"
path = "main.rs"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
println!("Check that our file is not overwritten")
}

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Created binary (application) package
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
println!("Check that our file is not overwritten")
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "case"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
println!("Check that our file is not overwritten")
}

1 change: 1 addition & 0 deletions tests/snapshots/init/bin_already_exists_implicit.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Created binary (application) package
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
println!("Check that our file is not overwritten")
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "case"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

[[bin]]
name = "case"
path = "case.rs"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
println!("Check that our file is not overwritten")
}

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Created binary (application) package
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
println!("Check that our file is not overwritten")
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "case"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

[[bin]]
name = "case"
path = "src/case.rs"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
println!("Check that our file is not overwritten")
}

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Created binary (application) package
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
println!("Check that our file is not overwritten")
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "case"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

[[bin]]
name = "case"
path = "main.rs"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
println!("Check that our file is not overwritten")
}

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Created binary (application) package
Empty file.
1 change: 1 addition & 0 deletions tests/snapshots/init/both_lib_and_bin.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
error: can't specify both lib and binary outputs
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn f() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
error: cannot have a package with multiple libraries, found both `case.rs` and `lib.rs`
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn f() { println!("lib.rs"); }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn f() { println!("src/lib.rs"); }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn f() { println!("lib.rs"); }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn f() { println!("src/lib.rs"); }
1 change: 1 addition & 0 deletions tests/snapshots/init/confused_by_multiple_lib_files.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
error: cannot have a package with multiple libraries, found both `src/lib.rs` and `lib.rs`
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn f() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "case"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

[[bin]]
name = "case"
path = "case.rs"

[lib]
name = "case"
path = "lib.rs"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn f() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Created binary (application) package
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn f() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "case"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

[[bin]]
name = "case"
path = "case.rs"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn f() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
warning: file `case.rs` seems to be a library file
Created binary (application) package
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "case"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

[lib]
name = "case"
path = "case.rs"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
warning: file `case.rs` seems to be a binary (application) file
Created library package
Empty file.
Empty file.
1 change: 1 addition & 0 deletions tests/snapshots/init/explicit_bin_with_git.in
1 change: 1 addition & 0 deletions tests/snapshots/init/explicit_bin_with_git.out/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
8 changes: 8 additions & 0 deletions tests/snapshots/init/explicit_bin_with_git.out/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "case"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
3 changes: 3 additions & 0 deletions tests/snapshots/init/explicit_bin_with_git.out/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}
1 change: 1 addition & 0 deletions tests/snapshots/init/explicit_bin_with_git.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Created binary (application) package
Empty file.
1 change: 1 addition & 0 deletions tests/snapshots/init/formats_source.in/rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tab_spaces = 2
8 changes: 8 additions & 0 deletions tests/snapshots/init/formats_source.out/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "case"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
1 change: 1 addition & 0 deletions tests/snapshots/init/formats_source.out/rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tab_spaces = 2
8 changes: 8 additions & 0 deletions tests/snapshots/init/formats_source.out/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
let result = 2 + 2;
assert_eq!(result, 4);
}
}
1 change: 1 addition & 0 deletions tests/snapshots/init/formats_source.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Created library package
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target
Cargo.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target
Cargo.lock
8 changes: 8 additions & 0 deletions tests/snapshots/init/fossil_autodetect.out/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "case"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
8 changes: 8 additions & 0 deletions tests/snapshots/init/fossil_autodetect.out/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
let result = 2 + 2;
assert_eq!(result, 4);
}
}
1 change: 1 addition & 0 deletions tests/snapshots/init/fossil_autodetect.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Created library package
Empty file.
2 changes: 2 additions & 0 deletions tests/snapshots/init/git_autodetect.out/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
/Cargo.lock
8 changes: 8 additions & 0 deletions tests/snapshots/init/git_autodetect.out/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "foo"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
8 changes: 8 additions & 0 deletions tests/snapshots/init/git_autodetect.out/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
let result = 2 + 2;
assert_eq!(result, 4);
}
}
1 change: 1 addition & 0 deletions tests/snapshots/init/git_autodetect.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Created library package
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/some.file
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
**/some.file

# Added by cargo

/target
/Cargo.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "case"
version = "0.1.0"
edition = "2015"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
let result = 2 + 2;
assert_eq!(result, 4);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Created library package
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tab_spaces = 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "case"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tab_spaces = 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
let result = 2 + 2;
assert_eq!(result, 4);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Created library package
Empty file.
Loading

0 comments on commit ad05cec

Please sign in to comment.