From 33862df8374430387e653cc3dbfc33e3f6ab1a8c Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 7 Aug 2024 11:47:40 -0500 Subject: [PATCH] test(vendor): Port 'package' tests to 'vendor' --- tests/testsuite/vendor.rs | 513 +++++++++++++++++++++++--------------- 1 file changed, 312 insertions(+), 201 deletions(-) diff --git a/tests/testsuite/vendor.rs b/tests/testsuite/vendor.rs index c238d816613..1a2b783fe8f 100644 --- a/tests/testsuite/vendor.rs +++ b/tests/testsuite/vendor.rs @@ -4,12 +4,11 @@ //! "fake" crates.io is used. Otherwise `vendor` would download the crates.io //! index from the network. -use std::fs::{self, File}; +use std::fs; use cargo_test_support::compare::assert_e2e; use cargo_test_support::git; use cargo_test_support::prelude::*; -use cargo_test_support::publish::validate_crate_contents; use cargo_test_support::registry::{self, Package, RegistryBuilder}; use cargo_test_support::str; use cargo_test_support::{basic_lib_manifest, basic_manifest, paths, project, Project}; @@ -165,6 +164,23 @@ fn add_crates_io_vendor_config(p: &Project) { ); } +fn add_git_vendor_config(p: &Project, git_project: &Project) { + p.change_file( + ".cargo/config.toml", + &format!( + r#" + [source."git+{url}"] + git = "{url}" + replace-with = 'vendor' + + [source.vendor] + directory = 'vendor' + "#, + url = git_project.url() + ), + ); +} + #[cargo_test] fn package_exclude() { let p = project() @@ -211,12 +227,13 @@ fn package_exclude() { #[cargo_test] fn discovery_inferred_build_rs_included() { - let p = project() - .file( - "Cargo.toml", - r#" + let git_project = git::new("dep", |project| { + project + .file( + "Cargo.toml", + r#" [package] - name = "foo" + name = "dep" version = "0.0.1" edition = "2015" license = "MIT" @@ -225,31 +242,38 @@ fn discovery_inferred_build_rs_included() { authors = [] include = ["src/lib.rs", "build.rs"] "#, + ) + .file("src/lib.rs", "") + .file("build.rs", "fn main() {}") + }); + + let p = project() + .file( + "Cargo.toml", + &format!( + r#" + [package] + name = "foo" + version = "0.5.0" + edition = "2015" + + [dependencies.dep] + git = '{}' + "#, + git_project.url() + ), ) - .file("src/lib.rs", "") - .file("build.rs", "fn main() {}") + .file("src/main.rs", "fn main() {}") .build(); - p.cargo("package") - .with_stdout_data("") - .with_stderr_data(str![[r#" -[PACKAGING] foo v0.0.1 ([ROOT]/foo) -[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed) -[VERIFYING] foo v0.0.1 ([ROOT]/foo) -[COMPILING] foo v0.0.1 ([ROOT]/foo/target/package/foo-0.0.1) -[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s - -"#]]) - .run(); + p.cargo("vendor --respect-source-config").run(); + add_git_vendor_config(&p, &git_project); - let f = File::open(&p.root().join("target/package/foo-0.0.1.crate")).unwrap(); - validate_crate_contents( - f, - "foo-0.0.1.crate", - &["Cargo.toml", "Cargo.toml.orig", "src/lib.rs", "build.rs"], - &[( - "Cargo.toml", - r#"# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO + let lock = p.read_file("vendor/dep/Cargo.toml"); + assert_e2e().eq( + lock, + str![[r##" +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility @@ -260,9 +284,14 @@ fn discovery_inferred_build_rs_included() { # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. +bin = [] +example = [] +test = [] +bench = [] + [package] edition = "2015" -name = "foo" +name = "dep" version = "0.0.1" authors = [] build = "build.rs" @@ -280,21 +309,24 @@ readme = false license = "MIT" [lib] -name = "foo" +name = "dep" path = "src/lib.rs" -"#, - )], + +"##]], ); + + p.cargo("check").run(); } #[cargo_test] fn discovery_inferred_build_rs_excluded() { - let p = project() - .file( - "Cargo.toml", - r#" + let git_project = git::new("dep", |project| { + project + .file( + "Cargo.toml", + r#" [package] - name = "foo" + name = "dep" version = "0.0.1" edition = "2015" license = "MIT" @@ -303,32 +335,38 @@ fn discovery_inferred_build_rs_excluded() { authors = [] include = ["src/lib.rs"] "#, + ) + .file("src/lib.rs", "") + .file("build.rs", "fn main() {}") + }); + + let p = project() + .file( + "Cargo.toml", + &format!( + r#" + [package] + name = "foo" + version = "0.5.0" + edition = "2015" + + [dependencies.dep] + git = '{}' + "#, + git_project.url() + ), ) - .file("src/lib.rs", "") - .file("build.rs", "fn main() {}") + .file("src/main.rs", "fn main() {}") .build(); - p.cargo("package") - .with_stdout_data("") - .with_stderr_data(str![[r#" -[PACKAGING] foo v0.0.1 ([ROOT]/foo) -[WARNING] ignoring `package.build` as `build.rs` is not included in the published package -[PACKAGED] 3 files, [FILE_SIZE]B ([FILE_SIZE]B compressed) -[VERIFYING] foo v0.0.1 ([ROOT]/foo) -[COMPILING] foo v0.0.1 ([ROOT]/foo/target/package/foo-0.0.1) -[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s - -"#]]) - .run(); + p.cargo("vendor --respect-source-config").run(); + add_git_vendor_config(&p, &git_project); - let f = File::open(&p.root().join("target/package/foo-0.0.1.crate")).unwrap(); - validate_crate_contents( - f, - "foo-0.0.1.crate", - &["Cargo.toml", "Cargo.toml.orig", "src/lib.rs"], - &[( - "Cargo.toml", - r#"# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO + let lock = p.read_file("vendor/dep/Cargo.toml"); + assert_e2e().eq( + lock, + str![[r##" +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility @@ -339,12 +377,17 @@ fn discovery_inferred_build_rs_excluded() { # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. +bin = [] +example = [] +test = [] +bench = [] + [package] edition = "2015" -name = "foo" +name = "dep" version = "0.0.1" authors = [] -build = false +build = "build.rs" include = ["src/lib.rs"] autobins = false autoexamples = false @@ -356,21 +399,33 @@ readme = false license = "MIT" [lib] -name = "foo" +name = "dep" path = "src/lib.rs" -"#, - )], + +"##]], ); + + p.cargo("check") + .with_status(101) + .with_stderr_data(str![[r#" +[COMPILING] dep v0.0.1 ([ROOTURL]/dep#[..]) +[ERROR] couldn't read [ROOT]/foo/vendor/dep/build.rs: [NOT_FOUND] + +[ERROR] could not compile `dep` (build script) due to 1 previous error + +"#]]) + .run(); } #[cargo_test] fn discovery_inferred_lib_included() { - let p = project() - .file( - "Cargo.toml", - r#" + let git_project = git::new("dep", |project| { + project + .file( + "Cargo.toml", + r#" [package] - name = "foo" + name = "dep" version = "0.0.1" edition = "2015" license = "MIT" @@ -379,37 +434,38 @@ fn discovery_inferred_lib_included() { authors = [] include = ["src/main.rs", "src/lib.rs"] "#, + ) + .file("src/main.rs", "fn main() {}") + .file("src/lib.rs", "") + }); + + let p = project() + .file( + "Cargo.toml", + &format!( + r#" + [package] + name = "foo" + version = "0.5.0" + edition = "2015" + + [dependencies.dep] + git = '{}' + "#, + git_project.url() + ), ) .file("src/main.rs", "fn main() {}") - .file("src/lib.rs", "") .build(); - p.cargo("package") - .with_stdout_data("") - .with_stderr_data(str![[r#" -[PACKAGING] foo v0.0.1 ([ROOT]/foo) -[PACKAGED] 5 files, [FILE_SIZE]B ([FILE_SIZE]B compressed) -[VERIFYING] foo v0.0.1 ([ROOT]/foo) -[COMPILING] foo v0.0.1 ([ROOT]/foo/target/package/foo-0.0.1) -[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s - -"#]]) - .run(); + p.cargo("vendor --respect-source-config").run(); + add_git_vendor_config(&p, &git_project); - let f = File::open(&p.root().join("target/package/foo-0.0.1.crate")).unwrap(); - validate_crate_contents( - f, - "foo-0.0.1.crate", - &[ - "Cargo.lock", - "Cargo.toml", - "Cargo.toml.orig", - "src/main.rs", - "src/lib.rs", - ], - &[( - "Cargo.toml", - r#"# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO + let lock = p.read_file("vendor/dep/Cargo.toml"); + assert_e2e().eq( + lock, + str![[r##" +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility @@ -420,9 +476,13 @@ fn discovery_inferred_lib_included() { # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. +example = [] +test = [] +bench = [] + [package] edition = "2015" -name = "foo" +name = "dep" version = "0.0.1" authors = [] build = false @@ -440,25 +500,28 @@ readme = false license = "MIT" [lib] -name = "foo" +name = "dep" path = "src/lib.rs" [[bin]] -name = "foo" +name = "dep" path = "src/main.rs" -"#, - )], + +"##]], ); + + p.cargo("check").run(); } #[cargo_test] fn discovery_inferred_lib_excluded() { - let p = project() - .file( - "Cargo.toml", - r#" + let git_project = git::new("dep", |project| { + project + .file( + "Cargo.toml", + r#" [package] - name = "foo" + name = "dep" version = "0.0.1" edition = "2015" license = "MIT" @@ -467,32 +530,38 @@ fn discovery_inferred_lib_excluded() { authors = [] include = ["src/main.rs"] "#, + ) + .file("src/main.rs", "fn main() {}") + .file("src/lib.rs", "") + }); + + let p = project() + .file( + "Cargo.toml", + &format!( + r#" + [package] + name = "foo" + version = "0.5.0" + edition = "2015" + + [dependencies.dep] + git = '{}' + "#, + git_project.url() + ), ) .file("src/main.rs", "fn main() {}") - .file("src/lib.rs", "") .build(); - p.cargo("package") - .with_stdout_data("") - .with_stderr_data(str![[r#" -[PACKAGING] foo v0.0.1 ([ROOT]/foo) -[WARNING] ignoring library `foo` as `src/lib.rs` is not included in the published package -[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed) -[VERIFYING] foo v0.0.1 ([ROOT]/foo) -[COMPILING] foo v0.0.1 ([ROOT]/foo/target/package/foo-0.0.1) -[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s - -"#]]) - .run(); + p.cargo("vendor --respect-source-config").run(); + add_git_vendor_config(&p, &git_project); - let f = File::open(&p.root().join("target/package/foo-0.0.1.crate")).unwrap(); - validate_crate_contents( - f, - "foo-0.0.1.crate", - &["Cargo.lock", "Cargo.toml", "Cargo.toml.orig", "src/main.rs"], - &[( - "Cargo.toml", - r#"# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO + let lock = p.read_file("vendor/dep/Cargo.toml"); + assert_e2e().eq( + lock, + str![[r##" +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility @@ -503,9 +572,13 @@ fn discovery_inferred_lib_excluded() { # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. +example = [] +test = [] +bench = [] + [package] edition = "2015" -name = "foo" +name = "dep" version = "0.0.1" authors = [] build = false @@ -519,22 +592,38 @@ documentation = "docs.rs/foo" readme = false license = "MIT" +[lib] +name = "dep" +path = "src/lib.rs" + [[bin]] -name = "foo" +name = "dep" path = "src/main.rs" -"#, - )], + +"##]], ); + + p.cargo("check") + .with_status(101) + .with_stderr_data(str![[r#" +[CHECKING] dep v0.0.1 ([ROOTURL]/dep#[..]) +[ERROR] couldn't read [ROOT]/foo/vendor/dep/src/lib.rs: [NOT_FOUND] + +[ERROR] could not compile `dep` (lib) due to 1 previous error + +"#]]) + .run(); } #[cargo_test] fn discovery_inferred_other_included() { - let p = project() + let git_project = git::new("dep", |project| { + project .file( "Cargo.toml", r#" [package] - name = "foo" + name = "dep" version = "0.0.1" edition = "2015" license = "MIT" @@ -549,37 +638,35 @@ fn discovery_inferred_other_included() { .file("examples/example_foo.rs", "fn main() {}") .file("tests/test_foo.rs", "fn main() {}") .file("benches/bench_foo.rs", "fn main() {}") - .build(); + }); - p.cargo("package") - .with_stdout_data("") - .with_stderr_data(str![[r#" -[PACKAGING] foo v0.0.1 ([ROOT]/foo) -[PACKAGED] 8 files, [FILE_SIZE]B ([FILE_SIZE]B compressed) -[VERIFYING] foo v0.0.1 ([ROOT]/foo) -[COMPILING] foo v0.0.1 ([ROOT]/foo/target/package/foo-0.0.1) -[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s + let p = project() + .file( + "Cargo.toml", + &format!( + r#" + [package] + name = "foo" + version = "0.5.0" + edition = "2015" -"#]]) - .run(); + [dependencies.dep] + git = '{}' + "#, + git_project.url() + ), + ) + .file("src/main.rs", "fn main() {}") + .build(); - let f = File::open(&p.root().join("target/package/foo-0.0.1.crate")).unwrap(); - validate_crate_contents( - f, - "foo-0.0.1.crate", - &[ - "Cargo.lock", - "Cargo.toml", - "Cargo.toml.orig", - "src/lib.rs", - "src/bin/foo/main.rs", - "examples/example_foo.rs", - "tests/test_foo.rs", - "benches/bench_foo.rs", - ], - &[( - "Cargo.toml", - r#"# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO + p.cargo("vendor --respect-source-config").run(); + add_git_vendor_config(&p, &git_project); + + let lock = p.read_file("vendor/dep/Cargo.toml"); + assert_e2e().eq( + lock, + str![[r##" +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility @@ -592,7 +679,7 @@ fn discovery_inferred_other_included() { [package] edition = "2015" -name = "foo" +name = "dep" version = "0.0.1" authors = [] build = false @@ -613,7 +700,7 @@ readme = false license = "MIT" [lib] -name = "foo" +name = "dep" path = "src/lib.rs" [[bin]] @@ -631,19 +718,22 @@ path = "tests/test_foo.rs" [[bench]] name = "bench_foo" path = "benches/bench_foo.rs" -"#, - )], + +"##]], ); + + p.cargo("check").run(); } #[cargo_test] fn discovery_inferred_other_excluded() { - let p = project() - .file( - "Cargo.toml", - r#" + let git_project = git::new("dep", |project| { + project + .file( + "Cargo.toml", + r#" [package] - name = "foo" + name = "dep" version = "0.0.1" edition = "2015" license = "MIT" @@ -652,38 +742,41 @@ fn discovery_inferred_other_excluded() { authors = [] include = ["src/lib.rs"] "#, + ) + .file("src/lib.rs", "") + .file("src/bin/foo/main.rs", "fn main() {}") + .file("examples/example_foo.rs", "fn main() {}") + .file("tests/test_foo.rs", "fn main() {}") + .file("benches/bench_foo.rs", "fn main() {}") + }); + + let p = project() + .file( + "Cargo.toml", + &format!( + r#" + [package] + name = "foo" + version = "0.5.0" + edition = "2015" + + [dependencies.dep] + git = '{}' + "#, + git_project.url() + ), ) - .file("src/lib.rs", "") - .file("src/bin/foo/main.rs", "fn main() {}") - .file("examples/example_foo.rs", "fn main() {}") - .file("tests/test_foo.rs", "fn main() {}") - .file("benches/bench_foo.rs", "fn main() {}") + .file("src/main.rs", "fn main() {}") .build(); - p.cargo("package") - .with_stdout_data("") - .with_stderr_data(str![[r#" -[PACKAGING] foo v0.0.1 ([ROOT]/foo) -[WARNING] ignoring binary `foo` as `src/bin/foo/main.rs` is not included in the published package -[WARNING] ignoring example `example_foo` as `examples/example_foo.rs` is not included in the published package -[WARNING] ignoring test `test_foo` as `tests/test_foo.rs` is not included in the published package -[WARNING] ignoring benchmark `bench_foo` as `benches/bench_foo.rs` is not included in the published package -[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed) -[VERIFYING] foo v0.0.1 ([ROOT]/foo) -[COMPILING] foo v0.0.1 ([ROOT]/foo/target/package/foo-0.0.1) -[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s - -"#]]) - .run(); + p.cargo("vendor --respect-source-config").run(); + add_git_vendor_config(&p, &git_project); - let f = File::open(&p.root().join("target/package/foo-0.0.1.crate")).unwrap(); - validate_crate_contents( - f, - "foo-0.0.1.crate", - &["Cargo.lock", "Cargo.toml", "Cargo.toml.orig", "src/lib.rs"], - &[( - "Cargo.toml", - r#"# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO + let lock = p.read_file("vendor/dep/Cargo.toml"); + assert_e2e().eq( + lock, + str![[r##" +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility @@ -696,7 +789,7 @@ fn discovery_inferred_other_excluded() { [package] edition = "2015" -name = "foo" +name = "dep" version = "0.0.1" authors = [] build = false @@ -711,11 +804,29 @@ readme = false license = "MIT" [lib] -name = "foo" +name = "dep" path = "src/lib.rs" -"#, - )], + +[[bin]] +name = "foo" +path = "src/bin/foo/main.rs" + +[[example]] +name = "example_foo" +path = "examples/example_foo.rs" + +[[test]] +name = "test_foo" +path = "tests/test_foo.rs" + +[[bench]] +name = "bench_foo" +path = "benches/bench_foo.rs" + +"##]], ); + + p.cargo("check").run(); } #[cargo_test]