Skip to content

Commit

Permalink
Merge pull request #167 from ehuss/new-fields
Browse files Browse the repository at this point in the history
Add recently added fields.
  • Loading branch information
oli-obk authored Oct 25, 2021
2 parents 312b8a5 + 1ca3f6c commit 77277e8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
16 changes: 15 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ use std::process::Command;
use std::str::from_utf8;

pub use camino;
pub use semver::Version;
pub use semver::{Version, VersionReq};

pub use dependency::{Dependency, DependencyKind};
use diagnostic::Diagnostic;
Expand Down Expand Up @@ -345,6 +345,14 @@ pub struct Package {
///
/// This is always `None` if running with a version of Cargo older than 1.39.
pub publish: Option<Vec<String>>,
/// The default binary to run by `cargo run`.
///
/// This is always `None` if running with a version of Cargo older than 1.55.
pub default_run: Option<String>,
/// The minimum supported Rust version of this package.
///
/// This is always `None` if running with a version of Cargo older than 1.58.
pub rust_version: Option<VersionReq>,
}

impl Package {
Expand Down Expand Up @@ -428,6 +436,12 @@ pub struct Target {
#[serde(default = "default_true")]
#[cfg_attr(feature = "builder", builder(default = "true"))]
pub test: bool,
/// Whether or not this target is documented by `cargo doc`.
///
/// This is always `true` if running with a version of Cargo older than 1.50.
#[serde(default = "default_true")]
#[cfg_attr(feature = "builder", builder(default = "true"))]
pub doc: bool,
}

fn default_true() -> bool {
Expand Down
3 changes: 3 additions & 0 deletions tests/all/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ homepage = "https://github.com/oli-obk/cargo_metadata/"
documentation = "https://docs.rs/cargo_metadata/"
links = "foo"
publish = false
default-run = "otherbin"
rust-version = "1.56"

[package.metadata.docs.rs]
all-features = true
Expand Down Expand Up @@ -47,6 +49,7 @@ crate-type = ["rlib", "cdylib", "dylib", "staticlib"]
[[bin]]
name = "otherbin"
edition = '2015'
doc = false

[[bin]]
name = "reqfeat"
Expand Down
30 changes: 22 additions & 8 deletions tests/test_samples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ fn old_minimal() {
assert_eq!(pkg.description, None);
assert_eq!(pkg.license, None);
assert_eq!(pkg.license_file, None);
assert_eq!(pkg.default_run, None);
assert_eq!(pkg.rust_version, None);
assert_eq!(pkg.dependencies.len(), 1);
let dep = &pkg.dependencies[0];
assert_eq!(dep.name, "somedep");
Expand All @@ -95,6 +97,7 @@ fn old_minimal() {
assert_eq!(target.edition, "2015");
assert_eq!(target.doctest, true);
assert_eq!(target.test, true);
assert_eq!(target.doc, true);
assert_eq!(pkg.features.len(), 0);
assert_eq!(pkg.manifest_path, "/foo/Cargo.toml");
assert_eq!(pkg.categories.len(), 0);
Expand Down Expand Up @@ -155,10 +158,10 @@ struct TestObject {

#[test]
fn all_the_fields() {
// All the fields currently generated as of 1.49. This tries to exercise as
// All the fields currently generated as of 1.58. This tries to exercise as
// much as possible.
let ver = cargo_version();
let minimum = semver::Version::parse("1.49.0").unwrap();
let minimum = semver::Version::parse("1.56.0").unwrap();
if ver < minimum {
// edition added in 1.30
// rename added in 1.31
Expand All @@ -169,7 +172,10 @@ fn all_the_fields() {
// test added in 1.47
// homepage added in 1.49
// documentation added in 1.49
// doc added in 1.50
// path added in 1.51
// default_run added in 1.55
// rust_version added in 1.58
eprintln!("Skipping all_the_fields test, cargo {} is too old.", ver);
return;
}
Expand Down Expand Up @@ -200,6 +206,13 @@ fn all_the_fields() {
assert!(all.license_file().unwrap().ends_with("tests/all/LICENSE"));
assert_eq!(all.publish, Some(vec![]));
assert_eq!(all.links, Some("foo".to_string()));
assert_eq!(all.default_run, Some("otherbin".to_string()));
if ver >= semver::Version::parse("1.58.0").unwrap() {
assert_eq!(
all.rust_version,
Some(semver::VersionReq::parse("1.56").unwrap())
);
}

assert_eq!(all.dependencies.len(), 8);
let bitflags = all
Expand All @@ -222,12 +235,10 @@ fn all_the_fields() {
assert_eq!(path_dep.source, None);
assert_eq!(path_dep.kind, DependencyKind::Normal);
assert_eq!(path_dep.req, semver::VersionReq::parse("*").unwrap());
if ver >= semver::Version::parse("1.51.0").unwrap() {
assert_eq!(
path_dep.path.as_ref().map(|p| p.ends_with("path-dep")),
Some(true),
);
}
assert_eq!(
path_dep.path.as_ref().map(|p| p.ends_with("path-dep")),
Some(true),
);

all.dependencies
.iter()
Expand Down Expand Up @@ -292,15 +303,18 @@ fn all_the_fields() {
assert_eq!(lib.edition, "2018");
assert_eq!(lib.doctest, true);
assert_eq!(lib.test, true);
assert_eq!(lib.doc, 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);
assert_eq!(main.test, true);
assert_eq!(main.doc, true);

let otherbin = get_file_name!("otherbin.rs");
assert_eq!(otherbin.edition, "2015");
assert_eq!(otherbin.doc, false);

let reqfeat = get_file_name!("reqfeat.rs");
assert_eq!(reqfeat.required_features, vec!["feat2"]);
Expand Down

0 comments on commit 77277e8

Please sign in to comment.