From 2ff6080fe42d4b1c28519b15b4685ef40dc5568f Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Tue, 18 Jun 2024 13:00:41 +0200 Subject: [PATCH] Fill explicit `lib` declaration with default values (#38) * Move `autolib` test to a dedicated file * tests/autolib: Remove unnecessary manifest entries We only care about the library auto-discovery here, the rest is just noise. * Fill explicit `lib` declaration with default values Previously, it was only possible to either use the auto-discovered lib target or explicitly declare one. In reality, cargo merges the definitions though. It is for example possible to rely on the auto-discovered `src/lib.rs`, but rename the library via `lib.name`. This commit changes the `lib = Some(_)` code path to assign proper defaults to the `name`, `path`, `edition`, and `crate_type` fields unless they are explicitly specified, which matches the behavior from cargo. --- src/lib.rs | 34 +++++- tests/autolib.rs | 51 +++++++++ tests/autolib/Cargo.toml | 11 -- tests/autolib/bin/Cargo.toml | 3 + tests/autolib/bin/src/main.rs | 1 + tests/autolib/lib_rs/Cargo.toml | 3 + tests/autolib/{ => lib_rs}/src/lib.rs | 0 tests/autolib/name_override/Cargo.toml | 6 ++ tests/autolib/name_override/src/lib.rs | 0 tests/autolib/other_override/Cargo.toml | 8 ++ tests/autolib/other_override/src/lib.rs | 0 tests/autolib/path_override/Cargo.toml | 6 ++ tests/autolib/path_override/src/foo.rs | 0 tests/autolib/path_override/src/lib.rs | 0 tests/parse.rs | 6 -- tests/snapshots/autolib__lib_rs.snap | 26 +++++ tests/snapshots/autolib__name_override.snap | 26 +++++ tests/snapshots/autolib__other_override.snap | 28 +++++ tests/snapshots/autolib__path_override.snap | 26 +++++ tests/snapshots/parse__autolib.snap | 107 ------------------- tests/snapshots/parse__metadata.snap | 10 +- tests/snapshots/parse__opt_version.snap | 10 +- 22 files changed, 233 insertions(+), 129 deletions(-) create mode 100644 tests/autolib.rs delete mode 100644 tests/autolib/Cargo.toml create mode 100644 tests/autolib/bin/Cargo.toml create mode 100644 tests/autolib/bin/src/main.rs create mode 100644 tests/autolib/lib_rs/Cargo.toml rename tests/autolib/{ => lib_rs}/src/lib.rs (100%) create mode 100644 tests/autolib/name_override/Cargo.toml create mode 100644 tests/autolib/name_override/src/lib.rs create mode 100644 tests/autolib/other_override/Cargo.toml create mode 100644 tests/autolib/other_override/src/lib.rs create mode 100644 tests/autolib/path_override/Cargo.toml create mode 100644 tests/autolib/path_override/src/foo.rs create mode 100644 tests/autolib/path_override/src/lib.rs create mode 100644 tests/snapshots/autolib__lib_rs.snap create mode 100644 tests/snapshots/autolib__name_override.snap create mode 100644 tests/snapshots/autolib__other_override.snap create mode 100644 tests/snapshots/autolib__path_override.snap delete mode 100644 tests/snapshots/parse__autolib.snap diff --git a/src/lib.rs b/src/lib.rs index d83b63d..ff5d039 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -268,7 +268,39 @@ impl Deserialize<'a>> Manifest { }; if let Some(ref mut lib) = self.lib { - lib.required_features.clear(); // not applicable + // Use `lib.name` if it's set, otherwise use `package.name` with dashes replaced by underscores + // (see ). + if lib.name.is_none() { + lib.name = Some(package.name.replace('-', "_")); + } + + // Use `lib.path` if it's set, otherwise use `src/lib.rs` if it exists, otherwise return an error + // (see https://doc.rust-lang.org/cargo/reference/cargo-targets.html#the-path-field). + if lib.path.is_none() { + if src.contains("lib.rs") { + lib.path = Some("src/lib.rs".to_string()); + } else { + let msg = + "can't find library, rename file to `src/lib.rs` or specify lib.path"; + return Err(Error::Other(msg.to_string())); + } + } + + // Use `lib.edition` if it's set, otherwise use `package.edition` unless it's inherited + // (see https://doc.rust-lang.org/cargo/reference/cargo-targets.html#the-edition-field). + if lib.edition.is_none() { + lib.edition = edition; + } + + // Use `lib.crate_type` if it's set, otherwise use `["lib"]` + // (see https://doc.rust-lang.org/cargo/reference/cargo-targets.html#the-crate-type-field). + if lib.crate_type.is_none() { + lib.crate_type = Some(vec!["lib".to_string()]); + } + + // `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") { self.lib = Some(Product { name: Some(package.name.replace('-', "_")), diff --git a/tests/autolib.rs b/tests/autolib.rs new file mode 100644 index 0000000..ba4023a --- /dev/null +++ b/tests/autolib.rs @@ -0,0 +1,51 @@ +use cargo_manifest::Manifest; + +#[test] +fn test_bin() { + let m = Manifest::from_path("tests/autolib/bin/Cargo.toml").unwrap(); + assert!(m.lib.is_none()); +} + +#[test] +fn test_lib_rs() { + let m = Manifest::from_path("tests/autolib/lib_rs/Cargo.toml").unwrap(); + + let lib = m.lib.unwrap(); + assert_eq!(lib.path.as_deref(), Some("src/lib.rs")); + assert_eq!(lib.name.as_deref(), Some("auto_lib")); + + insta::assert_debug_snapshot!(lib); +} + +#[test] +fn test_name_override() { + let m = Manifest::from_path("tests/autolib/name_override/Cargo.toml").unwrap(); + + let lib = m.lib.unwrap(); + assert_eq!(lib.path.as_deref(), Some("src/lib.rs")); + assert_eq!(lib.name.as_deref(), Some("foo")); + + insta::assert_debug_snapshot!(lib); +} + +#[test] +fn test_path_override() { + let m = Manifest::from_path("tests/autolib/path_override/Cargo.toml").unwrap(); + + let lib = m.lib.unwrap(); + assert_eq!(lib.path.as_deref(), Some("src/foo.rs")); + assert_eq!(lib.name.as_deref(), Some("auto_lib")); + + insta::assert_debug_snapshot!(lib); +} + +#[test] +fn test_other_override() { + let m = Manifest::from_path("tests/autolib/other_override/Cargo.toml").unwrap(); + + let lib = m.lib.unwrap(); + assert!(!lib.test); + assert!(lib.proc_macro); + + insta::assert_debug_snapshot!(lib); +} diff --git a/tests/autolib/Cargo.toml b/tests/autolib/Cargo.toml deleted file mode 100644 index 95d4a3d..0000000 --- a/tests/autolib/Cargo.toml +++ /dev/null @@ -1,11 +0,0 @@ -[package] -name = "auto-lib" -version = "0.1.0" -authors = ["Kornel "] -autoexamples = false -publish = false - -[dependencies] - -[badges] -coveralls = {} diff --git a/tests/autolib/bin/Cargo.toml b/tests/autolib/bin/Cargo.toml new file mode 100644 index 0000000..0a083ca --- /dev/null +++ b/tests/autolib/bin/Cargo.toml @@ -0,0 +1,3 @@ +[package] +name = "auto-lib" +version = "0.1.0" diff --git a/tests/autolib/bin/src/main.rs b/tests/autolib/bin/src/main.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/tests/autolib/bin/src/main.rs @@ -0,0 +1 @@ + diff --git a/tests/autolib/lib_rs/Cargo.toml b/tests/autolib/lib_rs/Cargo.toml new file mode 100644 index 0000000..0a083ca --- /dev/null +++ b/tests/autolib/lib_rs/Cargo.toml @@ -0,0 +1,3 @@ +[package] +name = "auto-lib" +version = "0.1.0" diff --git a/tests/autolib/src/lib.rs b/tests/autolib/lib_rs/src/lib.rs similarity index 100% rename from tests/autolib/src/lib.rs rename to tests/autolib/lib_rs/src/lib.rs diff --git a/tests/autolib/name_override/Cargo.toml b/tests/autolib/name_override/Cargo.toml new file mode 100644 index 0000000..dca1704 --- /dev/null +++ b/tests/autolib/name_override/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "auto-lib" +version = "0.1.0" + +[lib] +name = "foo" diff --git a/tests/autolib/name_override/src/lib.rs b/tests/autolib/name_override/src/lib.rs new file mode 100644 index 0000000..e69de29 diff --git a/tests/autolib/other_override/Cargo.toml b/tests/autolib/other_override/Cargo.toml new file mode 100644 index 0000000..1f91b9f --- /dev/null +++ b/tests/autolib/other_override/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "auto-lib" +version = "0.1.0" +edition = "2018" + +[lib] +proc-macro = true +test = false diff --git a/tests/autolib/other_override/src/lib.rs b/tests/autolib/other_override/src/lib.rs new file mode 100644 index 0000000..e69de29 diff --git a/tests/autolib/path_override/Cargo.toml b/tests/autolib/path_override/Cargo.toml new file mode 100644 index 0000000..bb10500 --- /dev/null +++ b/tests/autolib/path_override/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "auto-lib" +version = "0.1.0" + +[lib] +path = "src/foo.rs" \ No newline at end of file diff --git a/tests/autolib/path_override/src/foo.rs b/tests/autolib/path_override/src/foo.rs new file mode 100644 index 0000000..e69de29 diff --git a/tests/autolib/path_override/src/lib.rs b/tests/autolib/path_override/src/lib.rs new file mode 100644 index 0000000..e69de29 diff --git a/tests/parse.rs b/tests/parse.rs index 3a5efcc..1e92223 100644 --- a/tests/parse.rs +++ b/tests/parse.rs @@ -36,12 +36,6 @@ fn autobin() { insta::assert_debug_snapshot!(m); } -#[test] -fn autolib() { - let m = Manifest::from_path("tests/autolib/Cargo.toml").expect("load autolib"); - insta::assert_debug_snapshot!(m); -} - #[test] fn autobuild() { let m = Manifest::from_path("tests/autobuild/Cargo.toml").expect("load autobuild"); diff --git a/tests/snapshots/autolib__lib_rs.snap b/tests/snapshots/autolib__lib_rs.snap new file mode 100644 index 0000000..eea3495 --- /dev/null +++ b/tests/snapshots/autolib__lib_rs.snap @@ -0,0 +1,26 @@ +--- +source: tests/autolib.rs +expression: lib +--- +Product { + path: Some( + "src/lib.rs", + ), + name: Some( + "auto_lib", + ), + test: true, + doctest: true, + bench: true, + doc: true, + plugin: false, + proc_macro: false, + harness: true, + edition: None, + required_features: [], + crate_type: Some( + [ + "lib", + ], + ), +} diff --git a/tests/snapshots/autolib__name_override.snap b/tests/snapshots/autolib__name_override.snap new file mode 100644 index 0000000..c0c7cb8 --- /dev/null +++ b/tests/snapshots/autolib__name_override.snap @@ -0,0 +1,26 @@ +--- +source: tests/autolib.rs +expression: lib +--- +Product { + path: Some( + "src/lib.rs", + ), + name: Some( + "foo", + ), + test: true, + doctest: true, + bench: true, + doc: true, + plugin: false, + proc_macro: false, + harness: true, + edition: None, + required_features: [], + crate_type: Some( + [ + "lib", + ], + ), +} diff --git a/tests/snapshots/autolib__other_override.snap b/tests/snapshots/autolib__other_override.snap new file mode 100644 index 0000000..bdf5b52 --- /dev/null +++ b/tests/snapshots/autolib__other_override.snap @@ -0,0 +1,28 @@ +--- +source: tests/autolib.rs +expression: lib +--- +Product { + path: Some( + "src/lib.rs", + ), + name: Some( + "auto_lib", + ), + test: false, + doctest: true, + bench: true, + doc: true, + plugin: false, + proc_macro: true, + harness: true, + edition: Some( + E2018, + ), + required_features: [], + crate_type: Some( + [ + "lib", + ], + ), +} diff --git a/tests/snapshots/autolib__path_override.snap b/tests/snapshots/autolib__path_override.snap new file mode 100644 index 0000000..0e20991 --- /dev/null +++ b/tests/snapshots/autolib__path_override.snap @@ -0,0 +1,26 @@ +--- +source: tests/autolib.rs +expression: lib +--- +Product { + path: Some( + "src/foo.rs", + ), + name: Some( + "auto_lib", + ), + test: true, + doctest: true, + bench: true, + doc: true, + plugin: false, + proc_macro: false, + harness: true, + edition: None, + required_features: [], + crate_type: Some( + [ + "lib", + ], + ), +} diff --git a/tests/snapshots/parse__autolib.snap b/tests/snapshots/parse__autolib.snap deleted file mode 100644 index 6c6d368..0000000 --- a/tests/snapshots/parse__autolib.snap +++ /dev/null @@ -1,107 +0,0 @@ ---- -source: tests/parse.rs -expression: m ---- -Manifest { - package: Some( - Package { - name: "auto-lib", - edition: None, - version: Some( - Local( - "0.1.0", - ), - ), - build: None, - workspace: None, - authors: Some( - Local( - [ - "Kornel ", - ], - ), - ), - links: None, - description: None, - homepage: None, - documentation: None, - readme: None, - keywords: None, - categories: None, - license: None, - license_file: None, - repository: None, - metadata: None, - rust_version: None, - exclude: None, - include: None, - default_run: None, - autobins: true, - autoexamples: false, - autotests: true, - autobenches: true, - publish: Some( - Local( - Flag( - false, - ), - ), - ), - resolver: None, - }, - ), - cargo_features: None, - workspace: None, - dependencies: Some( - {}, - ), - dev_dependencies: None, - build_dependencies: None, - target: None, - features: None, - bin: [], - bench: [], - test: [], - example: [], - patch: None, - lib: Some( - Product { - path: Some( - "src/lib.rs", - ), - name: Some( - "auto_lib", - ), - test: true, - doctest: true, - bench: true, - doc: true, - plugin: false, - proc_macro: false, - harness: true, - edition: None, - required_features: [], - crate_type: Some( - [ - "lib", - ], - ), - }, - ), - profile: None, - badges: Some( - Badges { - appveyor: None, - circle_ci: None, - gitlab: None, - travis_ci: None, - codecov: None, - coveralls: None, - is_it_maintained_issue_resolution: None, - is_it_maintained_open_issues: None, - maintenance: Maintenance { - status: None, - }, - }, - ), -} diff --git a/tests/snapshots/parse__metadata.snap b/tests/snapshots/parse__metadata.snap index 542c8ec..caa245e 100644 --- a/tests/snapshots/parse__metadata.snap +++ b/tests/snapshots/parse__metadata.snap @@ -59,7 +59,9 @@ Manifest { path: Some( "lib.rs", ), - name: None, + name: Some( + "metadata", + ), test: true, doctest: true, bench: true, @@ -69,7 +71,11 @@ Manifest { harness: true, edition: None, required_features: [], - crate_type: None, + crate_type: Some( + [ + "lib", + ], + ), }, ), profile: None, diff --git a/tests/snapshots/parse__opt_version.snap b/tests/snapshots/parse__opt_version.snap index f39981c..b728256 100644 --- a/tests/snapshots/parse__opt_version.snap +++ b/tests/snapshots/parse__opt_version.snap @@ -51,7 +51,9 @@ Manifest { path: Some( "lib.rs", ), - name: None, + name: Some( + "opt_version", + ), test: true, doctest: true, bench: true, @@ -61,7 +63,11 @@ Manifest { harness: true, edition: None, required_features: [], - crate_type: None, + crate_type: Some( + [ + "lib", + ], + ), }, ), profile: None,