From ecf867177929adc0fc3e57897e66e73a23a50ead Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Sun, 19 Nov 2023 20:55:50 +0800 Subject: [PATCH] Add metadata test to make sure we can get the features Signed-off-by: hi-rustin --- tests/testsuite/metadata.rs | 197 ++++++++++++++++++++++++++++++++++-- 1 file changed, 188 insertions(+), 9 deletions(-) diff --git a/tests/testsuite/metadata.rs b/tests/testsuite/metadata.rs index 888cdce8c0e2..0bcaeba1c101 100644 --- a/tests/testsuite/metadata.rs +++ b/tests/testsuite/metadata.rs @@ -2,7 +2,7 @@ use cargo_test_support::install::cargo_home; use cargo_test_support::paths::CargoPathExt; -use cargo_test_support::registry::Package; +use cargo_test_support::registry::{self, Package}; use cargo_test_support::{basic_bin_manifest, basic_lib_manifest, main_file, project, rustc_host}; use serde_json::json; @@ -86,6 +86,185 @@ fn cargo_metadata_simple() { .run(); } +#[cargo_test] +fn cargo_metadata_with_registered_dep() { + registry::init(); + registry::Package::new("my-package", "0.1.1+my-package") + .feature("default", &["feature1", "feature2"]) + .feature("feature1", &[]) + .feature("feature2", &[]) + .publish(); + let p = project() + .file("src/foo.rs", "") + .file( + "Cargo.toml", + r#" +[package] +name = "foo" +version = "0.5.0" + +[[bin]] +name = "foo" + +[dependencies] +my-package = "0.1.1+my-package" + "#, + ) + .build(); + + p.cargo("metadata") + .with_json( + r#" + { + "packages": [ + { + "authors": [], + "categories": [], + "default_run": null, + "dependencies": [ + { + "features": [], + "kind": null, + "name": "my-package", + "optional": false, + "registry": null, + "rename": null, + "req": "^0.1.1", + "source": "registry+https://github.com/rust-lang/crates.io-index", + "target": null, + "uses_default_features": true + } + ], + "description": null, + "documentation": null, + "edition": "2015", + "features": {}, + "homepage": null, + "id": "foo 0.5.0 (path+file:[..]foo)", + "keywords": [], + "license": null, + "license_file": null, + "links": null, + "manifest_path": "[..]foo/Cargo.toml", + "metadata": null, + "name": "foo", + "publish": null, + "readme": null, + "repository": null, + "rust_version": null, + "source": null, + "targets": [ + { + "crate_types": [ + "bin" + ], + "doc": true, + "doctest": false, + "edition": "2015", + "kind": [ + "bin" + ], + "name": "foo", + "src_path": "[..]foo/src/foo.rs", + "test": true + } + ], + "version": "0.5.0" + }, + { + "authors": [], + "categories": [], + "default_run": null, + "dependencies": [], + "description": null, + "documentation": null, + "edition": "2015", + "features": { + "default": [ + "feature1", + "feature2" + ], + "feature1": [], + "feature2": [] + }, + "homepage": null, + "id": "my-package 0.1.1+my-package (registry+https://github.com/rust-lang/crates.io-index)", + "keywords": [], + "license": null, + "license_file": null, + "links": null, + "manifest_path": "[..]my-package-0.1.1+my-package/Cargo.toml", + "metadata": null, + "name": "my-package", + "publish": null, + "readme": null, + "repository": null, + "rust_version": null, + "source": "registry+https://github.com/rust-lang/crates.io-index", + "targets": [ + { + "crate_types": [ + "lib" + ], + "doc": true, + "doctest": true, + "edition": "2015", + "kind": [ + "lib" + ], + "name": "my-package", + "src_path": "[..]my-package-0.1.1+my-package/src/lib.rs", + "test": true + } + ], + "version": "0.1.1+my-package" + } + ], + "workspace_members": ["foo 0.5.0 (path+file:[..]foo)"], + "workspace_default_members": ["foo 0.5.0 (path+file:[..]foo)"], + "resolve": { + "nodes": [ + { + "dependencies": [ + "my-package 0.1.1+my-package (registry+https://github.com/rust-lang/crates.io-index)" + ], + "deps": [ + { + "dep_kinds": [ + { + "kind": null, + "target": null + } + ], + "name": "my_package", + "pkg": "my-package 0.1.1+my-package (registry+https://github.com/rust-lang/crates.io-index)" + } + ], + "features": [], + "id": "foo 0.5.0 (path+file:[..]foo)" + }, + { + "dependencies": [], + "deps": [], + "features": [ + "default", + "feature1", + "feature2" + ], + "id": "my-package 0.1.1+my-package (registry+https://github.com/rust-lang/crates.io-index)" + } + ], + "root": "foo 0.5.0 (path+file:[..]foo)" + }, + "target_directory": "[..]foo/target", + "version": 1, + "workspace_root": "[..]/foo", + "metadata": null + }"#, + ) + .run(); +} + #[cargo_test] fn cargo_metadata_warns_on_implicit_version() { let p = project() @@ -956,7 +1135,7 @@ fn workspace_metadata_with_dependencies_no_deps() { name = "bar" version = "0.5.0" authors = ["wycats@example.com"] - + [dependencies] baz = { path = "../baz/" } artifact = { path = "../artifact/", artifact = "bin" } @@ -1012,7 +1191,7 @@ fn workspace_metadata_with_dependencies_no_deps() { "lib": false, "target": null } - }, + }, { "features": [], "kind": null, @@ -1196,13 +1375,13 @@ fn workspace_metadata_with_dependencies_and_resolve() { name = "artifact" version = "0.5.0" authors = [] - + [lib] crate-type = ["staticlib", "cdylib", "rlib"] - + [[bin]] name = "bar-name" - + [[bin]] name = "baz-name" "#, @@ -1216,10 +1395,10 @@ fn workspace_metadata_with_dependencies_and_resolve() { name = "bin-only-artifact" version = "0.5.0" authors = [] - + [[bin]] name = "a-name" - + [[bin]] name = "b-name" "#, @@ -4040,7 +4219,7 @@ fn workspace_metadata_with_dependencies_no_deps_artifact() { name = "bar" version = "0.5.0" authors = ["wycats@example.com"] - + [dependencies] baz = { path = "../baz/" } baz-renamed = { path = "../baz/" }