From f349eca844e12a36bb937df41c661a63174d74c5 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Wed, 2 Oct 2024 10:40:15 +0200 Subject: [PATCH] Add support for `package.autolib` field --- src/lib.rs | 6 +++++- tests/autolib.rs | 15 +++++++++++++++ tests/snapshots/autotarget__full_example.snap | 1 + ...rget__full_example_with_declarations_2015.snap | 1 + ...rget__full_example_with_declarations_2021.snap | 1 + ...utotarget__full_example_without_discovery.snap | 1 + tests/snapshots/parse__autobuild.snap | 1 + .../parse__build_dependencies_casing.snap | 1 + .../snapshots/parse__default_features_casing.snap | 1 + .../snapshots/parse__dev_dependencies_casing.snap | 1 + tests/snapshots/parse__legacy-2.snap | 1 + tests/snapshots/parse__legacy.snap | 1 + tests/snapshots/parse__metadata.snap | 1 + tests/snapshots/parse__opt_level.snap | 1 + tests/snapshots/parse__opt_version.snap | 1 + tests/snapshots/parse__package_inheritance.snap | 1 + tests/snapshots/parse__proc_macro_casing-2.snap | 1 + tests/snapshots/parse__proc_macro_casing.snap | 1 + .../snapshots/parse__workspace_dependency-2.snap | 1 + 19 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 34ff3ff..63b1574 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -310,7 +310,7 @@ impl Deserialize<'a>> Manifest { // `lib.required-features` has no effect on `[lib]` // (see https://doc.rust-lang.org/cargo/reference/cargo-targets.html#the-required-features-field). lib.required_features.clear(); - } else if src.contains("lib.rs") { + } else if !matches!(package.autolib, Some(false)) && src.contains("lib.rs") { self.lib = Some(Product { name: Some(package.name.replace('-', "_")), path: Some("src/lib.rs".to_string()), @@ -1001,6 +1001,9 @@ pub struct Package { /// The default binary to run by cargo run. pub default_run: Option, + /// Disables library auto discovery. + #[serde(skip_serializing_if = "Option::is_none")] + pub autolib: Option, /// Disables binary auto discovery. /// /// Use [Manifest::autobins()] to get the effective value. @@ -1051,6 +1054,7 @@ impl Package { exclude: None, include: None, default_run: None, + autolib: None, autobins: None, autoexamples: None, autotests: None, diff --git a/tests/autolib.rs b/tests/autolib.rs index d15efd9..9bd5d14 100644 --- a/tests/autolib.rs +++ b/tests/autolib.rs @@ -143,3 +143,18 @@ fn test_legacy_lib_with_new_edition() { let error = Manifest::from_path(tempdir.path().join("Cargo.toml")).unwrap_err(); insta::assert_snapshot!(error, @"can't find library, rename file to `src/lib.rs` or specify lib.path"); } + +#[test] +fn test_disabled_autolib() { + let manifest = r#" + [package] + name = "auto-lib" + version = "0.1.0" + edition = "2021" + autolib = false + "#; + let tempdir = utils::prepare(manifest, vec!["src/lib.rs"]); + let m = Manifest::from_path(tempdir.path().join("Cargo.toml")).unwrap(); + + assert!(m.lib.is_none()); +} diff --git a/tests/snapshots/autotarget__full_example.snap b/tests/snapshots/autotarget__full_example.snap index 5f97213..81f5b17 100644 --- a/tests/snapshots/autotarget__full_example.snap +++ b/tests/snapshots/autotarget__full_example.snap @@ -30,6 +30,7 @@ Manifest { exclude: None, include: None, default_run: None, + autolib: None, autobins: None, autoexamples: None, autotests: None, diff --git a/tests/snapshots/autotarget__full_example_with_declarations_2015.snap b/tests/snapshots/autotarget__full_example_with_declarations_2015.snap index 810cd6c..042171c 100644 --- a/tests/snapshots/autotarget__full_example_with_declarations_2015.snap +++ b/tests/snapshots/autotarget__full_example_with_declarations_2015.snap @@ -30,6 +30,7 @@ Manifest { exclude: None, include: None, default_run: None, + autolib: None, autobins: None, autoexamples: None, autotests: None, diff --git a/tests/snapshots/autotarget__full_example_with_declarations_2021.snap b/tests/snapshots/autotarget__full_example_with_declarations_2021.snap index 4482174..a54224b 100644 --- a/tests/snapshots/autotarget__full_example_with_declarations_2021.snap +++ b/tests/snapshots/autotarget__full_example_with_declarations_2021.snap @@ -34,6 +34,7 @@ Manifest { exclude: None, include: None, default_run: None, + autolib: None, autobins: None, autoexamples: None, autotests: None, diff --git a/tests/snapshots/autotarget__full_example_without_discovery.snap b/tests/snapshots/autotarget__full_example_without_discovery.snap index 51abd20..6f0b2af 100644 --- a/tests/snapshots/autotarget__full_example_without_discovery.snap +++ b/tests/snapshots/autotarget__full_example_without_discovery.snap @@ -34,6 +34,7 @@ Manifest { exclude: None, include: None, default_run: None, + autolib: None, autobins: Some( false, ), diff --git a/tests/snapshots/parse__autobuild.snap b/tests/snapshots/parse__autobuild.snap index f673a5f..cb0b6a6 100644 --- a/tests/snapshots/parse__autobuild.snap +++ b/tests/snapshots/parse__autobuild.snap @@ -34,6 +34,7 @@ Manifest { exclude: None, include: None, default_run: None, + autolib: None, autobins: None, autoexamples: None, autotests: None, diff --git a/tests/snapshots/parse__build_dependencies_casing.snap b/tests/snapshots/parse__build_dependencies_casing.snap index 69f19b4..561020b 100644 --- a/tests/snapshots/parse__build_dependencies_casing.snap +++ b/tests/snapshots/parse__build_dependencies_casing.snap @@ -30,6 +30,7 @@ Manifest { exclude: None, include: None, default_run: None, + autolib: None, autobins: None, autoexamples: None, autotests: None, diff --git a/tests/snapshots/parse__default_features_casing.snap b/tests/snapshots/parse__default_features_casing.snap index 7d0def2..2126930 100644 --- a/tests/snapshots/parse__default_features_casing.snap +++ b/tests/snapshots/parse__default_features_casing.snap @@ -30,6 +30,7 @@ Manifest { exclude: None, include: None, default_run: None, + autolib: None, autobins: None, autoexamples: None, autotests: None, diff --git a/tests/snapshots/parse__dev_dependencies_casing.snap b/tests/snapshots/parse__dev_dependencies_casing.snap index 4640536..f05c772 100644 --- a/tests/snapshots/parse__dev_dependencies_casing.snap +++ b/tests/snapshots/parse__dev_dependencies_casing.snap @@ -30,6 +30,7 @@ Manifest { exclude: None, include: None, default_run: None, + autolib: None, autobins: None, autoexamples: None, autotests: None, diff --git a/tests/snapshots/parse__legacy-2.snap b/tests/snapshots/parse__legacy-2.snap index 6cb24eb..2320eb4 100644 --- a/tests/snapshots/parse__legacy-2.snap +++ b/tests/snapshots/parse__legacy-2.snap @@ -30,6 +30,7 @@ Manifest { exclude: None, include: None, default_run: None, + autolib: None, autobins: None, autoexamples: None, autotests: None, diff --git a/tests/snapshots/parse__legacy.snap b/tests/snapshots/parse__legacy.snap index 6cb24eb..2320eb4 100644 --- a/tests/snapshots/parse__legacy.snap +++ b/tests/snapshots/parse__legacy.snap @@ -30,6 +30,7 @@ Manifest { exclude: None, include: None, default_run: None, + autolib: None, autobins: None, autoexamples: None, autotests: None, diff --git a/tests/snapshots/parse__metadata.snap b/tests/snapshots/parse__metadata.snap index 7203f57..c41ff8e 100644 --- a/tests/snapshots/parse__metadata.snap +++ b/tests/snapshots/parse__metadata.snap @@ -34,6 +34,7 @@ Manifest { exclude: None, include: None, default_run: None, + autolib: None, autobins: None, autoexamples: None, autotests: None, diff --git a/tests/snapshots/parse__opt_level.snap b/tests/snapshots/parse__opt_level.snap index ad62ede..c0b71e1 100644 --- a/tests/snapshots/parse__opt_level.snap +++ b/tests/snapshots/parse__opt_level.snap @@ -79,6 +79,7 @@ Manifest { exclude: None, include: None, default_run: None, + autolib: None, autobins: None, autoexamples: None, autotests: None, diff --git a/tests/snapshots/parse__opt_version.snap b/tests/snapshots/parse__opt_version.snap index fcf35b8..7b152da 100644 --- a/tests/snapshots/parse__opt_version.snap +++ b/tests/snapshots/parse__opt_version.snap @@ -26,6 +26,7 @@ Manifest { exclude: None, include: None, default_run: None, + autolib: None, autobins: None, autoexamples: None, autotests: None, diff --git a/tests/snapshots/parse__package_inheritance.snap b/tests/snapshots/parse__package_inheritance.snap index ab172e8..c5f5994 100644 --- a/tests/snapshots/parse__package_inheritance.snap +++ b/tests/snapshots/parse__package_inheritance.snap @@ -42,6 +42,7 @@ Manifest { exclude: None, include: None, default_run: None, + autolib: None, autobins: None, autoexamples: None, autotests: None, diff --git a/tests/snapshots/parse__proc_macro_casing-2.snap b/tests/snapshots/parse__proc_macro_casing-2.snap index 0a7d3f2..8fa4d88 100644 --- a/tests/snapshots/parse__proc_macro_casing-2.snap +++ b/tests/snapshots/parse__proc_macro_casing-2.snap @@ -30,6 +30,7 @@ Manifest { exclude: None, include: None, default_run: None, + autolib: None, autobins: None, autoexamples: None, autotests: None, diff --git a/tests/snapshots/parse__proc_macro_casing.snap b/tests/snapshots/parse__proc_macro_casing.snap index 0a7d3f2..8fa4d88 100644 --- a/tests/snapshots/parse__proc_macro_casing.snap +++ b/tests/snapshots/parse__proc_macro_casing.snap @@ -30,6 +30,7 @@ Manifest { exclude: None, include: None, default_run: None, + autolib: None, autobins: None, autoexamples: None, autotests: None, diff --git a/tests/snapshots/parse__workspace_dependency-2.snap b/tests/snapshots/parse__workspace_dependency-2.snap index f00e958..865bad5 100644 --- a/tests/snapshots/parse__workspace_dependency-2.snap +++ b/tests/snapshots/parse__workspace_dependency-2.snap @@ -30,6 +30,7 @@ Manifest { exclude: None, include: None, default_run: None, + autolib: None, autobins: None, autoexamples: None, autotests: None,