Skip to content

Commit

Permalink
Add publish and doctest fields. (#88)
Browse files Browse the repository at this point in the history
Add `publish` and `doctest` fields.
  • Loading branch information
oli-obk authored Oct 21, 2019
2 parents 9a722c9 + 77aa7e9 commit f049869
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,12 @@ pub struct Package {
pub metadata: serde_json::Value,
/// The name of a native library the package is linking to.
pub links: Option<String>,
/// List of registries to which this package may be published.
///
/// Publishing is unrestricted if `None`, and forbidden if the `Vec` is empty.
///
/// This is always `None` if running with a version of Cargo older than 1.39.
pub publish: Option<Vec<String>>,
#[doc(hidden)]
#[serde(skip)]
__do_not_match_exhaustively: (),
Expand Down Expand Up @@ -400,11 +406,21 @@ pub struct Target {
/// Rust edition for this target
#[serde(default = "edition_default")]
pub edition: String,
/// Whether or not this target has doc tests enabled, and the target is
/// compatible with doc testing.
///
/// This is always `true` if running with a version of Cargo older than 1.37.
#[serde(default = "default_true")]
pub doctest: bool,
#[doc(hidden)]
#[serde(skip)]
__do_not_match_exhaustively: (),
}

fn default_true() -> bool {
true
}

fn edition_default() -> String {
"2015".to_string()
}
Expand Down
2 changes: 2 additions & 0 deletions tests/all/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/all/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ keywords = ["cli"]
readme = "README.md"
repository = "https://github.com/oli-obk/cargo_metadata/"
links = "foo"
publish = false

[package.metadata.docs.rs]
all-features = true
Expand Down
15 changes: 12 additions & 3 deletions tests/test_samples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ fn old_minimal() {
assert_eq!(target.required_features.len(), 0);
assert_eq!(target.src_path, PathBuf::from("/foo/src/main.rs"));
assert_eq!(target.edition, "2015");
assert_eq!(target.doctest, true);
assert_eq!(pkg.features.len(), 0);
assert_eq!(pkg.manifest_path, PathBuf::from("/foo/Cargo.toml"));
assert_eq!(pkg.categories.len(), 0);
Expand All @@ -102,6 +103,7 @@ fn old_minimal() {
assert_eq!(pkg.edition, "2015");
assert_eq!(pkg.metadata, serde_json::Value::Null);
assert_eq!(pkg.links, None);
assert_eq!(pkg.publish, None);
assert_eq!(meta.workspace_members.len(), 1);
assert_eq!(
meta.workspace_members[0].to_string(),
Expand All @@ -128,15 +130,19 @@ fn cargo_version() -> semver::Version {
let out = std::str::from_utf8(&output.stdout).expect("invalid utf8").trim();
let split: Vec<&str> = out.split_whitespace().collect();
assert!(split.len() >= 2, "cargo -V output is unexpected: {}", out);
semver::Version::parse(split[1]).expect("cargo -V semver could not be parsed")
let mut ver = semver::Version::parse(split[1]).expect("cargo -V semver could not be parsed");
// Don't care about metadata, it is awkward to compare.
ver.pre = Vec::new();
ver.build = Vec::new();
ver
}

#[test]
fn all_the_fields() {
// All the fields currently generated as of 1.31. This tries to exercise as
// All the fields currently generated as of 1.39. This tries to exercise as
// much as possible.
let ver = cargo_version();
let minimum = semver::Version::parse("1.31.0").unwrap();
let minimum = semver::Version::parse("1.39.0").unwrap();
if ver < minimum {
// edition added in 1.30
// rename added in 1.31
Expand Down Expand Up @@ -166,6 +172,7 @@ fn all_the_fields() {
assert_eq!(all.description, Some("Package description.".to_string()));
assert_eq!(all.license, Some("MIT/Apache-2.0".to_string()));
assert_eq!(all.license_file, Some(PathBuf::from("LICENSE")));
assert_eq!(all.publish, Some(vec![]));

assert_eq!(all.dependencies.len(), 8);
let bitflags = all
Expand Down Expand Up @@ -250,10 +257,12 @@ fn all_the_fields() {
);
assert_eq!(lib.required_features.len(), 0);
assert_eq!(lib.edition, "2018");
assert_eq!(lib.doctest, true);

let main = get_file_name!("main.rs");
assert_eq!(main.crate_types, vec!["bin"]);
assert_eq!(main.kind, vec!["bin"]);
assert_eq!(main.doctest, false);

let otherbin = get_file_name!("otherbin.rs");
assert_eq!(otherbin.edition, "2015");
Expand Down

0 comments on commit f049869

Please sign in to comment.