Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
est31 committed Apr 6, 2023
1 parent c7c96dc commit 7909555
Showing 1 changed file with 151 additions and 0 deletions.
151 changes: 151 additions & 0 deletions tests/testsuite/weak_dep_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,3 +630,154 @@ feat2 = ["bar?/feat"]
)],
);
}

#[cargo_test]
fn disabled_weak_direct_dep() {
// Issue #10801
// A weak direct dependency should be included in Cargo.lock,
// even if disabled, and even if on lockfile version 4.
Package::new("bar", "1.0.0")
.feature("feat", &[])
.file("src/lib.rs", &require(&["feat"], &[]))
.publish();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
[dependencies]
bar = { version = "1.0", optional = true }
[features]
f1 = ["bar?/feat"]
"#,
)
.file("src/lib.rs", &require(&["f1"], &[]))
.build();

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

let lockfile = p.read_lockfile();
assert!(
lockfile.contains(r#"version = 3"#),
"lockfile version is not 3!\n{lockfile}",
);
// Previous behavior: bar is inside lockfile.
assert!(
lockfile.contains(r#"name = "bar""#),
"bar not found\n{lockfile}",
);

// Update to new lockfile version
let new_lockfile = lockfile.replace("version = 3", "version = 4");
p.change_file("Cargo.lock", &new_lockfile);

p.cargo("check --features f1")
.with_stderr(
"\
[CHECKING] foo v0.1.0 [..]
[FINISHED] [..]
",
)
.run();

let lockfile = p.read_lockfile();
assert!(
lockfile.contains(r#"version = 4"#),
"lockfile version is not 4!\n{lockfile}",
);
// New behavior: bar is still there because it is a direct (optional) dependency.
assert!(
lockfile.contains(r#"name = "bar""#),
"bar not found\n{lockfile}",
);

p.cargo("check --features f1,bar")
.with_stderr(
"\
[DOWNLOADING] crates ...
[DOWNLOADED] bar v1.0.0 [..]
[CHECKING] bar v1.0.0
[CHECKING] foo v0.1.0 [..]
[FINISHED] [..]
",
)
.run();

}

#[cargo_test]
fn disabled_weak_optional_deps() {
// Issue #10801
// A weak dependency of a dependency should not be included in Cargo.lock,
// at least on lockfile version 4.
Package::new("bar", "1.0.0")
.feature("feat", &[])
.file("src/lib.rs", &require(&["feat"], &[]))
.publish();
Package::new("dep", "1.0.0")
.add_dep(Dependency::new("bar", "1.0").optional(true))
.feature("feat", &["bar?/feat"])
//.feature("default", &["feat"])
.file("src/lib.rs", "")
.publish();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
[dependencies]
dep = { version = "1.0", features = ["feat"] }
"#,
)
.file("src/lib.rs", "")
.build();

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

let lockfile = p.read_lockfile();

assert!(
lockfile.contains(r#"version = 3"#),
"lockfile version is not 3!\n{lockfile}",
);
// Previous behavior: bar is inside lockfile.
assert!(
lockfile.contains(r#"name = "bar""#),
"bar not found\n{lockfile}",
);

// Update to new lockfile version
let new_lockfile = lockfile.replace("version = 3", "version = 4");
p.change_file("Cargo.lock", &new_lockfile);

// Note how we are not downloading bar here
p.cargo("check")
.with_stderr(
"\
[DOWNLOADING] crates ...
[DOWNLOADED] dep v1.0.0 [..]
[CHECKING] dep v1.0.0
[CHECKING] foo v0.1.0 [..]
[FINISHED] [..]
",
)
.run();

let lockfile = p.read_lockfile();
assert!(
lockfile.contains(r#"version = 4"#),
"lockfile version is not 4!\n{lockfile}",
);
// New behavior: bar is gone.
assert!(
!lockfile.contains(r#"name = "bar""#),
"bar inside lockfile!\n{lockfile}",
);
}

0 comments on commit 7909555

Please sign in to comment.