| 1 |
extern crate cargo_metadata; |
| 2 |
extern crate semver; |
| 3 |
#[macro_use] |
| 4 |
extern crate serde_json; |
| 5 |
|
| 6 |
use camino::Utf8PathBuf; |
| 7 |
use cargo_metadata::{ |
| 8 |
ArtifactDebuginfo, CargoOpt, DependencyKind, Edition, Message, Metadata, MetadataCommand, |
| 9 |
}; |
| 10 |
|
| 11 |
/// Output from oldest version ever supported (1.24). |
| 12 |
/// |
| 13 |
/// This intentionally has as many null fields as possible. |
| 14 |
/// 1.8 is when metadata was introduced. |
| 15 |
/// Older versions not supported because the following are required: |
| 16 |
/// - `workspace_members` added in 1.13 |
| 17 |
/// - `target_directory` added in 1.19 |
| 18 |
/// - `workspace_root` added in 1.24 |
| 19 |
const JSON_OLD_MINIMAL: &str = r#" |
| 20 |
{ |
| 21 |
"packages": [ |
| 22 |
{ |
| 23 |
"name": "foo", |
| 24 |
"version": "0.1.0", |
| 25 |
"id": "foo 0.1.0 (path+file:///foo)", |
| 26 |
"license": null, |
| 27 |
"license_file": null, |
| 28 |
"description": null, |
| 29 |
"source": null, |
| 30 |
"dependencies": [ |
| 31 |
{ |
| 32 |
"name": "somedep", |
| 33 |
"source": null, |
| 34 |
"req": "^1.0", |
| 35 |
"kind": null, |
| 36 |
"optional": false, |
| 37 |
"uses_default_features": true, |
| 38 |
"features": [], |
| 39 |
"target": null |
| 40 |
} |
| 41 |
], |
| 42 |
"targets": [ |
| 43 |
{ |
| 44 |
"kind": [ |
| 45 |
"bin" |
| 46 |
], |
| 47 |
"crate_types": [ |
| 48 |
"bin" |
| 49 |
], |
| 50 |
"name": "foo", |
| 51 |
"src_path": "/foo/src/main.rs" |
| 52 |
} |
| 53 |
], |
| 54 |
"features": {}, |
| 55 |
"manifest_path": "/foo/Cargo.toml" |
| 56 |
} |
| 57 |
], |
| 58 |
"workspace_members": [ |
| 59 |
"foo 0.1.0 (path+file:///foo)" |
| 60 |
], |
| 61 |
"resolve": null, |
| 62 |
"target_directory": "/foo/target", |
| 63 |
"version": 1, |
| 64 |
"workspace_root": "/foo" |
| 65 |
} |
| 66 |
"#; |
| 67 |
|
| 68 |
#[test] |
| 69 |
fn old_minimal() { |
| 70 |
let meta: Metadata = serde_json::from_str(JSON_OLD_MINIMAL).unwrap(); |
| 71 |
assert_eq!(meta.packages.len(), 1); |
| 72 |
let pkg = &meta.packages[0]; |
| 73 |
assert_eq!(pkg.name, "foo"); |
| 74 |
assert_eq!(pkg.version, semver::Version::parse("0.1.0").unwrap()); |
| 75 |
assert_eq!(pkg.authors.len(), 0); |
| 76 |
assert_eq!(pkg.id.to_string(), "foo 0.1.0 (path+file:///foo)"); |
| 77 |
assert_eq!(pkg.description, None); |
| 78 |
assert_eq!(pkg.license, None); |
| 79 |
assert_eq!(pkg.license_file, None); |
| 80 |
assert_eq!(pkg.default_run, None); |
| 81 |
assert_eq!(pkg.rust_version, None); |
| 82 |
assert_eq!(pkg.dependencies.len(), 1); |
| 83 |
let dep = &pkg.dependencies[0]; |
| 84 |
assert_eq!(dep.name, "somedep"); |
| 85 |
assert_eq!(dep.source, None); |
| 86 |
assert_eq!(dep.req, semver::VersionReq::parse("^1.0").unwrap()); |
| 87 |
assert_eq!(dep.kind, DependencyKind::Normal); |
| 88 |
assert!(!dep.optional); |
| 89 |
assert!(dep.uses_default_features); |
| 90 |
assert_eq!(dep.features.len(), 0); |
| 91 |
assert!(dep.target.is_none()); |
| 92 |
assert_eq!(dep.rename, None); |
| 93 |
assert_eq!(dep.registry, None); |
| 94 |
assert_eq!(pkg.targets.len(), 1); |
| 95 |
let target = &pkg.targets[0]; |
| 96 |
assert_eq!(target.name, "foo"); |
| 97 |
assert_eq!(target.kind, vec!["bin".into()]); |
| 98 |
assert_eq!(target.crate_types, vec!["bin".into()]); |
| 99 |
assert_eq!(target.required_features.len(), 0); |
| 100 |
assert_eq!(target.src_path, "/foo/src/main.rs"); |
| 101 |
assert_eq!(target.edition, Edition::E2015); |
| 102 |
assert!(target.doctest); |
| 103 |
assert!(target.test); |
| 104 |
assert!(target.doc); |
| 105 |
assert_eq!(pkg.features.len(), 0); |
| 106 |
assert_eq!(pkg.manifest_path, "/foo/Cargo.toml"); |
| 107 |
assert_eq!(pkg.categories.len(), 0); |
| 108 |
assert_eq!(pkg.keywords.len(), 0); |
| 109 |
assert_eq!(pkg.readme, None); |
| 110 |
assert_eq!(pkg.repository, None); |
| 111 |
assert_eq!(pkg.homepage, None); |
| 112 |
assert_eq!(pkg.documentation, None); |
| 113 |
assert_eq!(pkg.edition, Edition::E2015); |
| 114 |
assert_eq!(pkg.metadata, serde_json::Value::Null); |
| 115 |
assert_eq!(pkg.links, None); |
| 116 |
assert_eq!(pkg.publish, None); |
| 117 |
assert_eq!(meta.workspace_members.len(), 1); |
| 118 |
assert_eq!( |
| 119 |
meta.workspace_members[0].to_string(), |
| 120 |
"foo 0.1.0 (path+file:///foo)" |
| 121 |
); |
| 122 |
assert!(meta.resolve.is_none()); |
| 123 |
assert_eq!(meta.workspace_root, "/foo"); |
| 124 |
assert_eq!(meta.workspace_metadata, serde_json::Value::Null); |
| 125 |
assert_eq!(meta.target_directory, "/foo/target"); |
| 126 |
|
| 127 |
assert!(!meta.workspace_default_members.is_available()); |
| 128 |
assert!(meta.workspace_default_members.is_missing()); |
| 129 |
|
| 130 |
let serialized = serde_json::to_value(meta).unwrap(); |
| 131 |
assert!(!serialized |
| 132 |
.as_object() |
| 133 |
.unwrap() |
| 134 |
.contains_key("workspace_default_members")); |
| 135 |
} |
| 136 |
|
| 137 |
macro_rules! sorted { |
| 138 |
($e:expr) => {{ |
| 139 |
let mut v = $e.clone(); |
| 140 |
v.sort(); |
| 141 |
v |
| 142 |
}}; |
| 143 |
} |
| 144 |
|
| 145 |
fn cargo_version() -> semver::Version { |
| 146 |
let output = std::process::Command::new("cargo") |
| 147 |
.arg("-V") |
| 148 |
.output() |
| 149 |
.expect("Failed to exec cargo."); |
| 150 |
let out = std::str::from_utf8(&output.stdout) |
| 151 |
.expect("invalid utf8") |
| 152 |
.trim(); |
| 153 |
let split: Vec<&str> = out.split_whitespace().collect(); |
| 154 |
assert!(split.len() >= 2, "cargo -V output is unexpected: {}", out); |
| 155 |
let mut ver = semver::Version::parse(split[1]).expect("cargo -V semver could not be parsed"); |
| 156 |
// Don't care about metadata, it is awkward to compare. |
| 157 |
ver.pre = semver::Prerelease::EMPTY; |
| 158 |
ver.build = semver::BuildMetadata::EMPTY; |
| 159 |
ver |
| 160 |
} |
| 161 |
|
| 162 |
#[derive(serde::Deserialize, PartialEq, Eq, Debug)] |
| 163 |
struct WorkspaceMetadata { |
| 164 |
testobject: TestObject, |
| 165 |
} |
| 166 |
|
| 167 |
#[derive(serde::Deserialize, PartialEq, Eq, Debug)] |
| 168 |
struct TestObject { |
| 169 |
myvalue: String, |
| 170 |
} |
| 171 |
|
| 172 |
#[test] |
| 173 |
fn all_the_fields() { |
| 174 |
// All the fields currently generated as of 1.60. This tries to exercise as |
| 175 |
// much as possible. |
| 176 |
let ver = cargo_version(); |
| 177 |
let minimum = semver::Version::parse("1.56.0").unwrap(); |
| 178 |
if ver < minimum { |
| 179 |
// edition added in 1.30 |
| 180 |
// rename added in 1.31 |
| 181 |
// links added in 1.33 |
| 182 |
// doctest added in 1.37 |
| 183 |
// publish added in 1.39 |
| 184 |
// dep_kinds added in 1.41 |
| 185 |
// test added in 1.47 |
| 186 |
// homepage added in 1.49 |
| 187 |
// documentation added in 1.49 |
| 188 |
// doc added in 1.50 |
| 189 |
// path added in 1.51 |
| 190 |
// default_run added in 1.55 |
| 191 |
// rust_version added in 1.58 |
| 192 |
// workspace_default_members added in 1.71 |
| 193 |
eprintln!("Skipping all_the_fields test, cargo {} is too old.", ver); |
| 194 |
return; |
| 195 |
} |
| 196 |
let meta = MetadataCommand::new() |
| 197 |
.manifest_path("tests/all/Cargo.toml") |
| 198 |
.exec() |
| 199 |
.unwrap(); |
| 200 |
assert_eq!(meta.workspace_root.file_name().unwrap(), "all"); |
| 201 |
assert_eq!( |
| 202 |
serde_json::from_value::<WorkspaceMetadata>(meta.workspace_metadata.clone()).unwrap(), |
| 203 |
WorkspaceMetadata { |
| 204 |
testobject: TestObject { |
| 205 |
myvalue: "abc".to_string() |
| 206 |
} |
| 207 |
} |
| 208 |
); |
| 209 |
assert_eq!(meta.workspace_members.len(), 1); |
| 210 |
assert!(meta.workspace_members[0].to_string().contains("all")); |
| 211 |
if ver >= semver::Version::parse("1.71.0").unwrap() { |
| 212 |
assert_eq!(&*meta.workspace_default_members, &meta.workspace_members); |
| 213 |
} |
| 214 |
|
| 215 |
assert_eq!(meta.packages.len(), 9); |
| 216 |
let all = meta.packages.iter().find(|p| p.name == "all").unwrap(); |
| 217 |
assert_eq!(all.version, semver::Version::parse("0.1.0").unwrap()); |
| 218 |
assert_eq!(all.authors, vec!["Jane Doe <[email protected]>"]); |
| 219 |
assert!(all.id.to_string().contains("all")); |
| 220 |
assert_eq!(all.description, Some("Package description.".to_string())); |
| 221 |
assert_eq!(all.license, Some("MIT/Apache-2.0".to_string())); |
| 222 |
assert_eq!(all.license_file, Some(Utf8PathBuf::from("LICENSE"))); |
| 223 |
assert!(all.license_file().unwrap().ends_with("tests/all/LICENSE")); |
| 224 |
assert_eq!(all.publish, Some(vec![])); |
| 225 |
assert_eq!(all.links, Some("foo".to_string())); |
| 226 |
assert_eq!(all.default_run, Some("otherbin".to_string())); |
| 227 |
if ver >= semver::Version::parse("1.58.0").unwrap() { |
| 228 |
assert_eq!( |
| 229 |
all.rust_version, |
| 230 |
Some(semver::Version::parse("1.56.0").unwrap()) |
| 231 |
); |
| 232 |
} |
| 233 |
|
| 234 |
assert_eq!(all.dependencies.len(), 8); |
| 235 |
let bitflags = all |
| 236 |
.dependencies |
| 237 |
.iter() |
| 238 |
.find(|d| d.name == "bitflags") |
| 239 |
.unwrap(); |
| 240 |
assert_eq!( |
| 241 |
bitflags.source, |
| 242 |
Some("registry+https://github.com/rust-lang/crates.io-index".to_string()) |
| 243 |
); |
| 244 |
assert!(bitflags.optional); |
| 245 |
assert_eq!(bitflags.req, semver::VersionReq::parse("^1.0").unwrap()); |
| 246 |
|
| 247 |
let path_dep = all |
| 248 |
.dependencies |
| 249 |
.iter() |
| 250 |
.find(|d| d.name == "path-dep") |
| 251 |
.unwrap(); |
| 252 |
assert_eq!(path_dep.source, None); |
| 253 |
assert_eq!(path_dep.kind, DependencyKind::Normal); |
| 254 |
assert_eq!(path_dep.req, semver::VersionReq::parse("*").unwrap()); |
| 255 |
assert_eq!( |
| 256 |
path_dep.path.as_ref().map(|p| p.ends_with("path-dep")), |
| 257 |
Some(true), |
| 258 |
); |
| 259 |
|
| 260 |
all.dependencies |
| 261 |
.iter() |
| 262 |
.find(|d| d.name == "namedep") |
| 263 |
.unwrap(); |
| 264 |
|
| 265 |
let featdep = all |
| 266 |
.dependencies |
| 267 |
.iter() |
| 268 |
.find(|d| d.name == "featdep") |
| 269 |
.unwrap(); |
| 270 |
assert_eq!(featdep.features, vec!["i128"]); |
| 271 |
assert!(!featdep.uses_default_features); |
| 272 |
|
| 273 |
let renamed = all |
| 274 |
.dependencies |
| 275 |
.iter() |
| 276 |
.find(|d| d.name == "oldname") |
| 277 |
.unwrap(); |
| 278 |
assert_eq!(renamed.rename, Some("newname".to_string())); |
| 279 |
|
| 280 |
let devdep = all |
| 281 |
.dependencies |
| 282 |
.iter() |
| 283 |
.find(|d| d.name == "devdep") |
| 284 |
.unwrap(); |
| 285 |
assert_eq!(devdep.kind, DependencyKind::Development); |
| 286 |
|
| 287 |
let bdep = all.dependencies.iter().find(|d| d.name == "bdep").unwrap(); |
| 288 |
assert_eq!(bdep.kind, DependencyKind::Build); |
| 289 |
|
| 290 |
let windep = all |
| 291 |
.dependencies |
| 292 |
.iter() |
| 293 |
.find(|d| d.name == "windep") |
| 294 |
.unwrap(); |
| 295 |
assert_eq!( |
| 296 |
windep.target.as_ref().map(|x| x.to_string()), |
| 297 |
Some("cfg(windows)".to_string()) |
| 298 |
); |
| 299 |
|
| 300 |
macro_rules! get_file_name { |
| 301 |
($v:expr) => { |
| 302 |
all.targets |
| 303 |
.iter() |
| 304 |
.find(|t| t.src_path.file_name().unwrap() == $v) |
| 305 |
.unwrap() |
| 306 |
}; |
| 307 |
} |
| 308 |
assert_eq!(all.targets.len(), 8); |
| 309 |
let lib = get_file_name!("lib.rs"); |
| 310 |
assert_eq!(lib.name, "all"); |
| 311 |
assert_eq!( |
| 312 |
sorted!(lib.kind), |
| 313 |
vec!["cdylib".into(), "rlib".into(), "staticlib".into()] |
| 314 |
); |
| 315 |
assert_eq!( |
| 316 |
sorted!(lib.crate_types), |
| 317 |
vec!["cdylib".into(), "rlib".into(), "staticlib".into()] |
| 318 |
); |
| 319 |
assert_eq!(lib.required_features.len(), 0); |
| 320 |
assert_eq!(lib.edition, Edition::E2018); |
| 321 |
assert!(lib.doctest); |
| 322 |
assert!(lib.test); |
| 323 |
assert!(lib.doc); |
| 324 |
|
| 325 |
let main = get_file_name!("main.rs"); |
| 326 |
assert_eq!(main.crate_types, vec!["bin".into()]); |
| 327 |
assert_eq!(main.kind, vec!["bin".into()]); |
| 328 |
assert!(!main.doctest); |
| 329 |
assert!(main.test); |
| 330 |
assert!(main.doc); |
| 331 |
|
| 332 |
let otherbin = get_file_name!("otherbin.rs"); |
| 333 |
assert_eq!(otherbin.edition, Edition::E2015); |
| 334 |
assert!(!otherbin.doc); |
| 335 |
|
| 336 |
let reqfeat = get_file_name!("reqfeat.rs"); |
| 337 |
assert_eq!(reqfeat.required_features, vec!["feat2"]); |
| 338 |
|
| 339 |
let ex1 = get_file_name!("ex1.rs"); |
| 340 |
assert_eq!(ex1.kind, vec!["example".into()]); |
| 341 |
assert!(!ex1.test); |
| 342 |
|
| 343 |
let t1 = get_file_name!("t1.rs"); |
| 344 |
assert_eq!(t1.kind, vec!["test".into()]); |
| 345 |
|
| 346 |
let b1 = get_file_name!("b1.rs"); |
| 347 |
assert_eq!(b1.kind, vec!["bench".into()]); |
| 348 |
|
| 349 |
let build = get_file_name!("build.rs"); |
| 350 |
assert_eq!(build.kind, vec!["custom-build".into()]); |
| 351 |
|
| 352 |
if ver >= semver::Version::parse("1.60.0").unwrap() { |
| 353 |
// 1.60 now reports optional dependencies within the features table |
| 354 |
assert_eq!(all.features.len(), 4); |
| 355 |
assert_eq!(all.features["bitflags"], vec!["dep:bitflags"]); |
| 356 |
} else { |
| 357 |
assert_eq!(all.features.len(), 3); |
| 358 |
} |
| 359 |
assert_eq!(all.features["feat1"].len(), 0); |
| 360 |
assert_eq!(all.features["feat2"].len(), 0); |
| 361 |
assert_eq!(sorted!(all.features["default"]), vec!["bitflags", "feat1"]); |
| 362 |
|
| 363 |
assert!(all.manifest_path.ends_with("all/Cargo.toml")); |
| 364 |
assert_eq!(all.categories, vec!["command-line-utilities"]); |
| 365 |
assert_eq!(all.keywords, vec!["cli"]); |
| 366 |
assert_eq!(all.readme, Some(Utf8PathBuf::from("README.md"))); |
| 367 |
assert!(all.readme().unwrap().ends_with("tests/all/README.md")); |
| 368 |
assert_eq!( |
| 369 |
all.repository, |
| 370 |
Some("https://github.com/oli-obk/cargo_metadata/".to_string()) |
| 371 |
); |
| 372 |
assert_eq!( |
| 373 |
all.homepage, |
| 374 |
Some("https://github.com/oli-obk/cargo_metadata/".to_string()) |
| 375 |
); |
| 376 |
assert_eq!( |
| 377 |
all.documentation, |
| 378 |
Some("https://docs.rs/cargo_metadata/".to_string()) |
| 379 |
); |
| 380 |
assert_eq!(all.edition, Edition::E2018); |
| 381 |
assert_eq!( |
| 382 |
all.metadata, |
| 383 |
json!({ |
| 384 |
"docs": { |
| 385 |
"rs": { |
| 386 |
"all-features": true, |
| 387 |
"default-target": "x86_64-unknown-linux-gnu", |
| 388 |
"rustc-args": ["--example-rustc-arg"] |
| 389 |
} |
| 390 |
} |
| 391 |
}) |
| 392 |
); |
| 393 |
|
| 394 |
let resolve = meta.resolve.as_ref().unwrap(); |
| 395 |
assert!(resolve.root.as_ref().unwrap().to_string().contains("all")); |
| 396 |
|
| 397 |
assert_eq!(resolve.nodes.len(), 9); |
| 398 |
let path_dep = resolve |
| 399 |
.nodes |
| 400 |
.iter() |
| 401 |
.find(|n| n.id.to_string().contains("path-dep")) |
| 402 |
.unwrap(); |
| 403 |
assert_eq!(path_dep.deps.len(), 0); |
| 404 |
assert_eq!(path_dep.dependencies.len(), 0); |
| 405 |
assert_eq!(path_dep.features.len(), 0); |
| 406 |
|
| 407 |
let bitflags = resolve |
| 408 |
.nodes |
| 409 |
.iter() |
| 410 |
.find(|n| n.id.to_string().contains("bitflags")) |
| 411 |
.unwrap(); |
| 412 |
assert_eq!(bitflags.features, vec!["default"]); |
| 413 |
|
| 414 |
let featdep = resolve |
| 415 |
.nodes |
| 416 |
.iter() |
| 417 |
.find(|n| n.id.to_string().contains("featdep")) |
| 418 |
.unwrap(); |
| 419 |
assert_eq!(featdep.features, vec!["i128"]); |
| 420 |
|
| 421 |
let all = resolve |
| 422 |
.nodes |
| 423 |
.iter() |
| 424 |
.find(|n| n.id.to_string().contains("all")) |
| 425 |
.unwrap(); |
| 426 |
assert_eq!(all.dependencies.len(), 8); |
| 427 |
assert_eq!(all.deps.len(), 8); |
| 428 |
let newname = all.deps.iter().find(|d| d.name == "newname").unwrap(); |
| 429 |
assert!(newname.pkg.to_string().contains("oldname")); |
| 430 |
// Note the underscore here. |
| 431 |
let path_dep = all.deps.iter().find(|d| d.name == "path_dep").unwrap(); |
| 432 |
assert!(path_dep.pkg.to_string().contains("path-dep")); |
| 433 |
assert_eq!(path_dep.dep_kinds.len(), 1); |
| 434 |
let kind = &path_dep.dep_kinds[0]; |
| 435 |
assert_eq!(kind.kind, DependencyKind::Normal); |
| 436 |
assert!(kind.target.is_none()); |
| 437 |
|
| 438 |
let namedep = all |
| 439 |
.deps |
| 440 |
.iter() |
| 441 |
.find(|d| d.name == "different_name") |
| 442 |
.unwrap(); |
| 443 |
assert!(namedep.pkg.to_string().contains("namedep")); |
| 444 |
assert_eq!(sorted!(all.features), vec!["bitflags", "default", "feat1"]); |
| 445 |
|
| 446 |
let bdep = all.deps.iter().find(|d| d.name == "bdep").unwrap(); |
| 447 |
assert_eq!(bdep.dep_kinds.len(), 1); |
| 448 |
let kind = &bdep.dep_kinds[0]; |
| 449 |
assert_eq!(kind.kind, DependencyKind::Build); |
| 450 |
assert!(kind.target.is_none()); |
| 451 |
|
| 452 |
let devdep = all.deps.iter().find(|d| d.name == "devdep").unwrap(); |
| 453 |
assert_eq!(devdep.dep_kinds.len(), 1); |
| 454 |
let kind = &devdep.dep_kinds[0]; |
| 455 |
assert_eq!(kind.kind, DependencyKind::Development); |
| 456 |
assert!(kind.target.is_none()); |
| 457 |
|
| 458 |
let windep = all.deps.iter().find(|d| d.name == "windep").unwrap(); |
| 459 |
assert_eq!(windep.dep_kinds.len(), 1); |
| 460 |
let kind = &windep.dep_kinds[0]; |
| 461 |
assert_eq!(kind.kind, DependencyKind::Normal); |
| 462 |
assert_eq!( |
| 463 |
kind.target.as_ref().map(|x| x.to_string()), |
| 464 |
Some("cfg(windows)".to_string()) |
| 465 |
); |
| 466 |
|
| 467 |
let serialized = serde_json::to_value(meta).unwrap(); |
| 468 |
if ver >= semver::Version::parse("1.71.0").unwrap() { |
| 469 |
assert!(serialized.as_object().unwrap()["workspace_default_members"] |
| 470 |
.as_array() |
| 471 |
.is_some()); |
| 472 |
} else { |
| 473 |
assert!(!serialized |
| 474 |
.as_object() |
| 475 |
.unwrap() |
| 476 |
.contains_key("workspace_default_members")); |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
#[test] |
| 481 |
fn alt_registry() { |
| 482 |
// This is difficult to test (would need to set up a custom index). |
| 483 |
// Just manually check the JSON is handled. |
| 484 |
let json = r#" |
| 485 |
{ |
| 486 |
"packages": [ |
| 487 |
{ |
| 488 |
"name": "alt", |
| 489 |
"version": "0.1.0", |
| 490 |
"id": "alt 0.1.0 (path+file:///alt)", |
| 491 |
"source": null, |
| 492 |
"dependencies": [ |
| 493 |
{ |
| 494 |
"name": "alt2", |
| 495 |
"source": "registry+https://example.com", |
| 496 |
"req": "^0.1", |
| 497 |
"kind": null, |
| 498 |
"rename": null, |
| 499 |
"optional": false, |
| 500 |
"uses_default_features": true, |
| 501 |
"features": [], |
| 502 |
"target": null, |
| 503 |
"registry": "https://example.com" |
| 504 |
} |
| 505 |
], |
| 506 |
"targets": [ |
| 507 |
{ |
| 508 |
"kind": [ |
| 509 |
"lib" |
| 510 |
], |
| 511 |
"crate_types": [ |
| 512 |
"lib" |
| 513 |
], |
| 514 |
"name": "alt", |
| 515 |
"src_path": "/alt/src/lib.rs", |
| 516 |
"edition": "2018" |
| 517 |
} |
| 518 |
], |
| 519 |
"features": {}, |
| 520 |
"manifest_path": "/alt/Cargo.toml", |
| 521 |
"metadata": null, |
| 522 |
"authors": [], |
| 523 |
"categories": [], |
| 524 |
"keywords": [], |
| 525 |
"readme": null, |
| 526 |
"repository": null, |
| 527 |
"edition": "2018", |
| 528 |
"links": null |
| 529 |
} |
| 530 |
], |
| 531 |
"workspace_members": [ |
| 532 |
"alt 0.1.0 (path+file:///alt)" |
| 533 |
], |
| 534 |
"resolve": null, |
| 535 |
"target_directory": "/alt/target", |
| 536 |
"version": 1, |
| 537 |
"workspace_root": "/alt" |
| 538 |
} |
| 539 |
"#; |
| 540 |
let meta: Metadata = serde_json::from_str(json).unwrap(); |
| 541 |
assert_eq!(meta.packages.len(), 1); |
| 542 |
let alt = &meta.packages[0]; |
| 543 |
let deps = &alt.dependencies; |
| 544 |
assert_eq!(deps.len(), 1); |
| 545 |
let dep = &deps[0]; |
| 546 |
assert_eq!(dep.registry, Some("https://example.com".to_string())); |
| 547 |
} |
| 548 |
|
| 549 |
#[test] |
| 550 |
fn current_dir() { |
| 551 |
let meta = MetadataCommand::new() |
| 552 |
.current_dir("tests/all/namedep") |
| 553 |
.exec() |
| 554 |
.unwrap(); |
| 555 |
let namedep = meta.packages.iter().find(|p| p.name == "namedep").unwrap(); |
| 556 |
assert!(namedep.name.starts_with("namedep")); |
| 557 |
} |
| 558 |
|
| 559 |
#[test] |
| 560 |
fn parse_stream_is_robust() { |
| 561 |
// Proc macros can print stuff to stdout, which naturally breaks JSON messages. |
| 562 |
// Let's check that we don't die horribly in this case, and report an error. |
| 563 |
let json_output = r##"{"reason":"compiler-artifact","package_id":"chatty 0.1.0 (path+file:///chatty-macro/chatty)","manifest_path":"chatty-macro/Cargo.toml","target":{"kind":["proc-macro"],"crate_types":["proc-macro"],"name":"chatty","src_path":"/chatty-macro/chatty/src/lib.rs","edition":"2018","doctest":true},"profile":{"opt_level":"0","debuginfo":2,"debug_assertions":true,"overflow_checks":true,"test":false},"features":[],"filenames":["/chatty-macro/target/debug/deps/libchatty-f2adcff24cdf3bb2.so"],"executable":null,"fresh":false} |
| 564 |
Evil proc macro was here! |
| 565 |
{"reason":"compiler-artifact","package_id":"chatty-macro 0.1.0 (path+file:///chatty-macro)","manifest_path":"chatty-macro/Cargo.toml","target":{"kind":["lib"],"crate_types":["lib"],"name":"chatty-macro","src_path":"/chatty-macro/src/lib.rs","edition":"2018","doctest":true},"profile":{"opt_level":"0","debuginfo":2,"debug_assertions":true,"overflow_checks":true,"test":false},"features":[],"filenames":["/chatty-macro/target/debug/libchatty_macro.rlib","/chatty-macro/target/debug/deps/libchatty_macro-cb5956ed52a11fb6.rmeta"],"executable":null,"fresh":false} |
| 566 |
"##; |
| 567 |
let mut n_messages = 0; |
| 568 |
let mut text = String::new(); |
| 569 |
for message in cargo_metadata::Message::parse_stream(json_output.as_bytes()) { |
| 570 |
let message = message.unwrap(); |
| 571 |
match message { |
| 572 |
cargo_metadata::Message::TextLine(line) => text = line, |
| 573 |
_ => n_messages += 1, |
| 574 |
} |
| 575 |
} |
| 576 |
assert_eq!(n_messages, 2); |
| 577 |
assert_eq!(text, "Evil proc macro was here!"); |
| 578 |
} |
| 579 |
|
| 580 |
#[test] |
| 581 |
fn advanced_feature_configuration() { |
| 582 |
fn build_features<F: FnOnce(&mut MetadataCommand) -> &mut MetadataCommand>( |
| 583 |
func: F, |
| 584 |
) -> Vec<String> { |
| 585 |
let mut meta = MetadataCommand::new(); |
| 586 |
let meta = meta.manifest_path("tests/all/Cargo.toml"); |
| 587 |
|
| 588 |
let meta = func(meta); |
| 589 |
let meta = meta.exec().unwrap(); |
| 590 |
|
| 591 |
let resolve = meta.resolve.as_ref().unwrap(); |
| 592 |
|
| 593 |
let all = resolve |
| 594 |
.nodes |
| 595 |
.iter() |
| 596 |
.find(|n| !n.features.is_empty()) |
| 597 |
.unwrap(); |
| 598 |
|
| 599 |
all.features.clone() |
| 600 |
} |
| 601 |
|
| 602 |
// Default behavior; tested above |
| 603 |
let default_features = build_features(|meta| meta); |
| 604 |
assert_eq!( |
| 605 |
sorted!(default_features), |
| 606 |
vec!["bitflags", "default", "feat1"] |
| 607 |
); |
| 608 |
|
| 609 |
// Manually specify the same default features |
| 610 |
let manual_features = build_features(|meta| { |
| 611 |
meta.features(CargoOpt::NoDefaultFeatures) |
| 612 |
.features(CargoOpt::SomeFeatures(vec![ |
| 613 |
"feat1".into(), |
| 614 |
"bitflags".into(), |
| 615 |
])) |
| 616 |
}); |
| 617 |
assert_eq!(sorted!(manual_features), vec!["bitflags", "feat1"]); |
| 618 |
|
| 619 |
// Multiple SomeFeatures is same as one longer SomeFeatures |
| 620 |
let manual_features = build_features(|meta| { |
| 621 |
meta.features(CargoOpt::NoDefaultFeatures) |
| 622 |
.features(CargoOpt::SomeFeatures(vec!["feat1".into()])) |
| 623 |
.features(CargoOpt::SomeFeatures(vec!["feat2".into()])) |
| 624 |
}); |
| 625 |
assert_eq!(sorted!(manual_features), vec!["feat1", "feat2"]); |
| 626 |
|
| 627 |
// No features + All features == All features |
| 628 |
let all_features = build_features(|meta| { |
| 629 |
meta.features(CargoOpt::AllFeatures) |
| 630 |
.features(CargoOpt::NoDefaultFeatures) |
| 631 |
}); |
| 632 |
assert_eq!( |
| 633 |
sorted!(all_features), |
| 634 |
vec!["bitflags", "default", "feat1", "feat2"] |
| 635 |
); |
| 636 |
|
| 637 |
// The '--all-features' flag supersedes other feature flags |
| 638 |
let all_flag_variants = build_features(|meta| { |
| 639 |
meta.features(CargoOpt::SomeFeatures(vec!["feat2".into()])) |
| 640 |
.features(CargoOpt::NoDefaultFeatures) |
| 641 |
.features(CargoOpt::AllFeatures) |
| 642 |
}); |
| 643 |
assert_eq!(sorted!(all_flag_variants), sorted!(all_features)); |
| 644 |
} |
| 645 |
|
| 646 |
#[test] |
| 647 |
fn depkind_to_string() { |
| 648 |
assert_eq!(DependencyKind::Normal.to_string(), "normal"); |
| 649 |
assert_eq!(DependencyKind::Development.to_string(), "dev"); |
| 650 |
assert_eq!(DependencyKind::Build.to_string(), "build"); |
| 651 |
assert_eq!(DependencyKind::Unknown.to_string(), "Unknown"); |
| 652 |
} |
| 653 |
|
| 654 |
#[test] |
| 655 |
fn basic_workspace_root_package_exists() { |
| 656 |
// First try with dependencies |
| 657 |
let meta = MetadataCommand::new() |
| 658 |
.manifest_path("tests/basic_workspace/Cargo.toml") |
| 659 |
.exec() |
| 660 |
.unwrap(); |
| 661 |
assert_eq!(meta.root_package().unwrap().name, "ex_bin"); |
| 662 |
// Now with no_deps, it should still work exactly the same |
| 663 |
let meta = MetadataCommand::new() |
| 664 |
.manifest_path("tests/basic_workspace/Cargo.toml") |
| 665 |
.no_deps() |
| 666 |
.exec() |
| 667 |
.unwrap(); |
| 668 |
assert_eq!( |
| 669 |
meta.root_package() |
| 670 |
.expect("workspace root still exists when no_deps used") |
| 671 |
.name, |
| 672 |
"ex_bin" |
| 673 |
); |
| 674 |
} |
| 675 |
|
| 676 |
#[test] |
| 677 |
fn debuginfo_variants() { |
| 678 |
// Checks behavior for the different debuginfo variants. |
| 679 |
let variants = [ |
| 680 |
("0", ArtifactDebuginfo::None), |
| 681 |
("1", ArtifactDebuginfo::Limited), |
| 682 |
("2", ArtifactDebuginfo::Full), |
| 683 |
( |
| 684 |
"\"line-directives-only\"", |
| 685 |
ArtifactDebuginfo::LineDirectivesOnly, |
| 686 |
), |
| 687 |
("\"line-tables-only\"", ArtifactDebuginfo::LineTablesOnly), |
| 688 |
("3", ArtifactDebuginfo::UnknownInt(3)), |
| 689 |
( |
| 690 |
"\"abc\"", |
| 691 |
ArtifactDebuginfo::UnknownString("abc".to_string()), |
| 692 |
), |
| 693 |
("null", ArtifactDebuginfo::None), |
| 694 |
]; |
| 695 |
for (value, expected) in variants { |
| 696 |
let s = r#"{"reason":"compiler-artifact","package_id":"cargo_metadata 0.16.0 (path+file:////cargo_metadata)","manifest_path":"/cargo_metadata/Cargo.toml","target":{"kind":["lib"],"crate_types":["lib"],"name":"cargo_metadata","src_path":"/cargo_metadata/src/lib.rs","edition":"2018","doc":true,"doctest":true,"test":true},"profile":{"opt_level":"0","debuginfo":DEBUGINFO,"debug_assertions":true,"overflow_checks":true,"test":false},"features":["default"],"filenames":["/cargo_metadata/target/debug/deps/libcargo_metadata-27f582f7187b9a2c.rmeta"],"executable":null,"fresh":false}"#; |
| 697 |
let message: Message = serde_json::from_str(&s.replace("DEBUGINFO", value)).unwrap(); |
| 698 |
match message { |
| 699 |
Message::CompilerArtifact(artifact) => { |
| 700 |
assert_eq!(artifact.profile.debuginfo, expected); |
| 701 |
let de_s = serde_json::to_string(&artifact.profile.debuginfo).unwrap(); |
| 702 |
// Note: Roundtrip does not retain null value. |
| 703 |
if value == "null" { |
| 704 |
assert_eq!(artifact.profile.debuginfo.to_string(), "0"); |
| 705 |
assert_eq!(de_s, "0"); |
| 706 |
} else { |
| 707 |
assert_eq!( |
| 708 |
artifact.profile.debuginfo.to_string(), |
| 709 |
value.trim_matches('"') |
| 710 |
); |
| 711 |
assert_eq!(de_s, value); |
| 712 |
} |
| 713 |
} |
| 714 |
_ => panic!("unexpected {:?}", message), |
| 715 |
} |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
#[test] |
| 720 |
#[should_panic = "WorkspaceDefaultMembers should only be dereferenced on Cargo versions >= 1.71"] |
| 721 |
fn missing_workspace_default_members() { |
| 722 |
let meta: Metadata = serde_json::from_str(JSON_OLD_MINIMAL).unwrap(); |
| 723 |
let _ = &*meta.workspace_default_members; |
| 724 |
} |
| 725 |
|
| 726 |
#[test] |
| 727 |
fn workspace_default_members_is_available() { |
| 728 |
// generated with cargo +1.71.0 metadata --format-version 1 |
| 729 |
let json = r#" |
| 730 |
{ |
| 731 |
"packages": [ |
| 732 |
{ |
| 733 |
"name": "basic", |
| 734 |
"version": "0.1.0", |
| 735 |
"id": "basic 0.1.0 (path+file:///example)", |
| 736 |
"license": null, |
| 737 |
"license_file": null, |
| 738 |
"description": null, |
| 739 |
"source": null, |
| 740 |
"dependencies": [], |
| 741 |
"targets": [ |
| 742 |
{ |
| 743 |
"kind": [ |
| 744 |
"lib" |
| 745 |
], |
| 746 |
"crate_types": [ |
| 747 |
"lib" |
| 748 |
], |
| 749 |
"name": "basic", |
| 750 |
"src_path": "/example/src/lib.rs", |
| 751 |
"edition": "2021", |
| 752 |
"doc": true, |
| 753 |
"doctest": true, |
| 754 |
"test": true |
| 755 |
} |
| 756 |
], |
| 757 |
"features": {}, |
| 758 |
"manifest_path": "/example/Cargo.toml", |
| 759 |
"metadata": null, |
| 760 |
"publish": null, |
| 761 |
"authors": [], |
| 762 |
"categories": [], |
| 763 |
"keywords": [], |
| 764 |
"readme": null, |
| 765 |
"repository": null, |
| 766 |
"homepage": null, |
| 767 |
"documentation": null, |
| 768 |
"edition": "2021", |
| 769 |
"links": null, |
| 770 |
"default_run": null, |
| 771 |
"rust_version": null |
| 772 |
} |
| 773 |
], |
| 774 |
"workspace_members": [ |
| 775 |
"basic 0.1.0 (path+file:///example)" |
| 776 |
], |
| 777 |
"workspace_default_members": [ |
| 778 |
"basic 0.1.0 (path+file:///example)" |
| 779 |
], |
| 780 |
"resolve": { |
| 781 |
"nodes": [ |
| 782 |
{ |
| 783 |
"id": "basic 0.1.0 (path+file:///example)", |
| 784 |
"dependencies": [], |
| 785 |
"deps": [], |
| 786 |
"features": [] |
| 787 |
} |
| 788 |
], |
| 789 |
"root": "basic 0.1.0 (path+file:///example)" |
| 790 |
}, |
| 791 |
"target_directory": "/example/target", |
| 792 |
"version": 1, |
| 793 |
"workspace_root": "/example", |
| 794 |
"metadata": null |
| 795 |
} |
| 796 |
"#; |
| 797 |
|
| 798 |
let meta: Metadata = serde_json::from_str(json).unwrap(); |
| 799 |
|
| 800 |
assert!(meta.workspace_default_members.is_available()); |
| 801 |
assert!(!meta.workspace_default_members.is_missing()); |
| 802 |
} |
| 803 |
|
| 804 |
#[test] |
| 805 |
fn workspace_default_members_is_missing() { |
| 806 |
// generated with cargo +1.70.0 metadata --format-version 1 |
| 807 |
let json = r#" |
| 808 |
{ |
| 809 |
"packages": [ |
| 810 |
{ |
| 811 |
"name": "basic", |
| 812 |
"version": "0.1.0", |
| 813 |
"id": "basic 0.1.0 (path+file:///example)", |
| 814 |
"license": null, |
| 815 |
"license_file": null, |
| 816 |
"description": null, |
| 817 |
"source": null, |
| 818 |
"dependencies": [], |
| 819 |
"targets": [ |
| 820 |
{ |
| 821 |
"kind": [ |
| 822 |
"lib" |
| 823 |
], |
| 824 |
"crate_types": [ |
| 825 |
"lib" |
| 826 |
], |
| 827 |
"name": "basic", |
| 828 |
"src_path": "/example/src/lib.rs", |
| 829 |
"edition": "2021", |
| 830 |
"doc": true, |
| 831 |
"doctest": true, |
| 832 |
"test": true |
| 833 |
} |
| 834 |
], |
| 835 |
"features": {}, |
| 836 |
"manifest_path": "/example/Cargo.toml", |
| 837 |
"metadata": null, |
| 838 |
"publish": null, |
| 839 |
"authors": [], |
| 840 |
"categories": [], |
| 841 |
"keywords": [], |
| 842 |
"readme": null, |
| 843 |
"repository": null, |
| 844 |
"homepage": null, |
| 845 |
"documentation": null, |
| 846 |
"edition": "2021", |
| 847 |
"links": null, |
| 848 |
"default_run": null, |
| 849 |
"rust_version": null |
| 850 |
} |
| 851 |
], |
| 852 |
"workspace_members": [ |
| 853 |
"basic 0.1.0 (path+file:///example)" |
| 854 |
], |
| 855 |
"resolve": { |
| 856 |
"nodes": [ |
| 857 |
{ |
| 858 |
"id": "basic 0.1.0 (path+file:///example)", |
| 859 |
"dependencies": [], |
| 860 |
"deps": [], |
| 861 |
"features": [] |
| 862 |
} |
| 863 |
], |
| 864 |
"root": "basic 0.1.0 (path+file:///example)" |
| 865 |
}, |
| 866 |
"target_directory": "/example/target", |
| 867 |
"version": 1, |
| 868 |
"workspace_root": "/example", |
| 869 |
"metadata": null |
| 870 |
} |
| 871 |
"#; |
| 872 |
|
| 873 |
let meta: Metadata = serde_json::from_str(json).unwrap(); |
| 874 |
|
| 875 |
assert!(!meta.workspace_default_members.is_available()); |
| 876 |
assert!(meta.workspace_default_members.is_missing()); |
| 877 |
} |
| 878 |
|
| 879 |
#[test] |
| 880 |
fn test_unknown_target_kind_and_crate_type() { |
| 881 |
// Both kind and crate_type set to a type not yet known |
| 882 |
let json = r#" |
| 883 |
{ |
| 884 |
"packages": [ |
| 885 |
{ |
| 886 |
"name": "alt", |
| 887 |
"version": "0.1.0", |
| 888 |
"id": "alt 0.1.0 (path+file:///alt)", |
| 889 |
"source": null, |
| 890 |
"dependencies": [], |
| 891 |
"targets": [ |
| 892 |
{ |
| 893 |
"kind": [ |
| 894 |
"future-kind" |
| 895 |
], |
| 896 |
"crate_types": [ |
| 897 |
"future-type" |
| 898 |
], |
| 899 |
"name": "alt", |
| 900 |
"src_path": "/alt/src/lib.rs", |
| 901 |
"edition": "2018" |
| 902 |
} |
| 903 |
], |
| 904 |
"features": {}, |
| 905 |
"manifest_path": "/alt/Cargo.toml", |
| 906 |
"metadata": null, |
| 907 |
"authors": [], |
| 908 |
"categories": [], |
| 909 |
"keywords": [], |
| 910 |
"readme": null, |
| 911 |
"repository": null, |
| 912 |
"edition": "2018", |
| 913 |
"links": null |
| 914 |
} |
| 915 |
], |
| 916 |
"workspace_members": [ |
| 917 |
"alt 0.1.0 (path+file:///alt)" |
| 918 |
], |
| 919 |
"resolve": null, |
| 920 |
"target_directory": "/alt/target", |
| 921 |
"version": 1, |
| 922 |
"workspace_root": "/alt" |
| 923 |
} |
| 924 |
"#; |
| 925 |
let meta: Metadata = serde_json::from_str(json).unwrap(); |
| 926 |
assert_eq!(meta.packages.len(), 1); |
| 927 |
assert_eq!(meta.packages[0].targets.len(), 1); |
| 928 |
let target = &meta.packages[0].targets[0]; |
| 929 |
assert_eq!(target.kind[0], "future-kind".into()); |
| 930 |
assert_eq!(target.crate_types[0], "future-type".into()); |
| 931 |
} |