Skip to content

Commit

Permalink
Fill explicit lib declaration with default values (#38)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
Turbo87 authored Jun 18, 2024
1 parent 93418b1 commit 2ff6080
Show file tree
Hide file tree
Showing 22 changed files with 233 additions and 129 deletions.
34 changes: 33 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,39 @@ impl<Metadata: for<'a> Deserialize<'a>> Manifest<Metadata> {
};

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 <https://doc.rust-lang.org/cargo/reference/cargo-targets.html#the-name-field>).
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('-', "_")),
Expand Down
51 changes: 51 additions & 0 deletions tests/autolib.rs
Original file line number Diff line number Diff line change
@@ -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);
}
11 changes: 0 additions & 11 deletions tests/autolib/Cargo.toml

This file was deleted.

3 changes: 3 additions & 0 deletions tests/autolib/bin/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[package]
name = "auto-lib"
version = "0.1.0"
1 change: 1 addition & 0 deletions tests/autolib/bin/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

3 changes: 3 additions & 0 deletions tests/autolib/lib_rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[package]
name = "auto-lib"
version = "0.1.0"
File renamed without changes.
6 changes: 6 additions & 0 deletions tests/autolib/name_override/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "auto-lib"
version = "0.1.0"

[lib]
name = "foo"
Empty file.
8 changes: 8 additions & 0 deletions tests/autolib/other_override/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "auto-lib"
version = "0.1.0"
edition = "2018"

[lib]
proc-macro = true
test = false
Empty file.
6 changes: 6 additions & 0 deletions tests/autolib/path_override/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "auto-lib"
version = "0.1.0"

[lib]
path = "src/foo.rs"
Empty file.
Empty file.
6 changes: 0 additions & 6 deletions tests/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
26 changes: 26 additions & 0 deletions tests/snapshots/autolib__lib_rs.snap
Original file line number Diff line number Diff line change
@@ -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",
],
),
}
26 changes: 26 additions & 0 deletions tests/snapshots/autolib__name_override.snap
Original file line number Diff line number Diff line change
@@ -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",
],
),
}
28 changes: 28 additions & 0 deletions tests/snapshots/autolib__other_override.snap
Original file line number Diff line number Diff line change
@@ -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",
],
),
}
26 changes: 26 additions & 0 deletions tests/snapshots/autolib__path_override.snap
Original file line number Diff line number Diff line change
@@ -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",
],
),
}
107 changes: 0 additions & 107 deletions tests/snapshots/parse__autolib.snap

This file was deleted.

10 changes: 8 additions & 2 deletions tests/snapshots/parse__metadata.snap
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ Manifest {
path: Some(
"lib.rs",
),
name: None,
name: Some(
"metadata",
),
test: true,
doctest: true,
bench: true,
Expand All @@ -69,7 +71,11 @@ Manifest {
harness: true,
edition: None,
required_features: [],
crate_type: None,
crate_type: Some(
[
"lib",
],
),
},
),
profile: None,
Expand Down
Loading

0 comments on commit 2ff6080

Please sign in to comment.