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

test: deal with lockfile checksums dynamically #13739

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 13 additions & 4 deletions crates/cargo-test-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,14 +311,23 @@ impl ProjectBuilder {
}

impl Project {
/// Copy the test project from a fixed state
pub fn from_template(template_path: impl AsRef<std::path::Path>) -> Self {
fn new(template_path: &std::path::Path, name: &str) -> Self {
let root = paths::root();
let project_root = root.join("case");
snapbox::path::copy_template(template_path.as_ref(), &project_root).unwrap();
let project_root = root.join(name);
snapbox::path::copy_template(template_path, &project_root).unwrap();
Self { root: project_root }
}

/// Copy the test project from a fixed state
pub fn from_template(template_path: impl AsRef<std::path::Path>) -> Self {
Self::new(template_path.as_ref(), "case")
}

/// Copy the test project from expected output
pub fn from_expected(template_path: impl AsRef<std::path::Path>) -> Self {
Self::new(template_path.as_ref(), "expected")
}

/// Root of the project, ex: `/path/to/cargo/target/cit/t0/foo`
pub fn root(&self) -> PathBuf {
self.root.clone()
Expand Down
8 changes: 5 additions & 3 deletions tests/testsuite/alt_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1456,7 +1456,7 @@ fn sparse_lockfile() {
.http_index()
.alternative()
.build();
Package::new("foo", "0.1.0").alternative(true).publish();
let cksum = Package::new("foo", "0.1.0").alternative(true).publish();

let p = project()
.file(
Expand All @@ -1478,7 +1478,8 @@ fn sparse_lockfile() {
p.cargo("generate-lockfile").run();
assert_match_exact(
&p.read_lockfile(),
r#"# This file is automatically @generated by Cargo.
&format!(
r#"# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3

Expand All @@ -1493,7 +1494,8 @@ dependencies = [
name = "foo"
version = "0.1.0"
source = "sparse+http://[..]/"
checksum = "f6a200a9339fef960979d94d5c99cbbfd899b6f5a396a55d9775089119050203""#,
checksum = "{cksum}""#,
),
);
}

Expand Down
16 changes: 0 additions & 16 deletions tests/testsuite/cargo_add/locked_unchanged/in/Cargo.lock

This file was deleted.

2 changes: 2 additions & 0 deletions tests/testsuite/cargo_add/locked_unchanged/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ fn case() {
let project_root = project.root();
let cwd = &project_root;

project.cargo("generate-lockfile").run();

snapbox::cmd::Command::cargo_ui()
.arg("add")
.arg_line("my-package --locked")
Expand Down
17 changes: 0 additions & 17 deletions tests/testsuite/cargo_add/lockfile_updated/in/Cargo.lock

This file was deleted.

6 changes: 5 additions & 1 deletion tests/testsuite/cargo_add/lockfile_updated/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ fn case() {
let project_root = project.root();
let cwd = &project_root;

project.cargo("generate-lockfile").run();

snapbox::cmd::Command::cargo_ui()
.arg("add")
.arg_line("my-package")
Expand All @@ -35,5 +37,7 @@ fn case() {
.stdout_matches(str![""])
.stderr_matches(file!["stderr.term.svg"]);

assert_ui().subset_matches(current_dir!().join("out"), &project_root);
let expected = Project::from_expected(current_dir!().join("out"));
expected.cargo("generate-lockfile").run();
assert_ui().subset_matches(&expected.root(), &project_root);
}
23 changes: 0 additions & 23 deletions tests/testsuite/cargo_add/lockfile_updated/out/Cargo.lock

This file was deleted.

Empty file.
58 changes: 0 additions & 58 deletions tests/testsuite/cargo_remove/update_lock_file/in/Cargo.lock

This file was deleted.

6 changes: 5 additions & 1 deletion tests/testsuite/cargo_remove/update_lock_file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ fn case() {
let project_root = project.root();
let cwd = &project_root;

project.cargo("generate-lockfile").run();

snapbox::cmd::Command::cargo_ui()
.arg("remove")
.args(["rustc-serialize"])
Expand All @@ -33,5 +35,7 @@ fn case() {
.stdout_matches(str![""])
.stderr_matches(file!["stderr.term.svg"]);

assert_ui().subset_matches(current_dir!().join("out"), &project_root);
let expected = Project::from_expected(current_dir!().join("out"));
Copy link
Member

Choose a reason for hiding this comment

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

The dynamic check seems fine to me, though I am a bit unsure since most UI tests in Cargo only asserts one command invocation.

@epage, do you have any input for this?

Copy link
Contributor

Choose a reason for hiding this comment

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

Dynamically generating the snapshots like this runs counter to point of the snapshots

  • We are bringing code under test into the process for verifying the code under tests
  • This is brittle as a generate-lockfile happens to work right now for these specific tests but that likely won't be true always.

A good example of where this can fall flat in both cases is my on-going work with MSRV-aware resolver.

Something I've been considering is adding support for dynamic substitutions so a function could run and replace all checksums with a [CHECKSUM].

Copy link
Member Author

Choose a reason for hiding this comment

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

Note that it's not just about expected output in the tests. In cargo_add::locked_unchanged, cargo itself complains that the input lockfile looks corrupt with its mismatched checksum.

expected.cargo("generate-lockfile").run();
assert_ui().subset_matches(&expected.root(), &project_root);
}
51 changes: 0 additions & 51 deletions tests/testsuite/cargo_remove/update_lock_file/out/Cargo.lock

This file was deleted.