1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
use std::env::current_dir;
use std::path::PathBuf;
use semver::Version;
use cargo_metadata::{CargoOpt, Error, MetadataCommand};
use serde::Deserialize;
#[derive(Debug, PartialEq, Eq, Deserialize)]
struct TestPackageMetadata {
some_field: bool,
other_field: String,
}
#[test]
fn metadata() {
let metadata = MetadataCommand::new().no_deps().exec().unwrap();
let this = &metadata.packages[0];
assert_eq!(this.name, "cargo_metadata");
assert_eq!(this.targets.len(), 3);
let lib = this
.targets
.iter()
.find(|t| t.name == "cargo_metadata")
.unwrap();
assert_eq!(lib.kind[0], "lib");
assert_eq!(lib.crate_types[0], "lib");
let selftest = this.targets.iter().find(|t| t.name == "selftest").unwrap();
assert_eq!(selftest.name, "selftest");
assert_eq!(selftest.kind[0], "test");
assert_eq!(selftest.crate_types[0], "bin");
let package_metadata = &metadata.packages[0]
.metadata
.as_object()
.expect("package.metadata must be a table.");
assert_eq!(package_metadata.len(), 1);
let value = package_metadata.get("cargo_metadata_test").unwrap();
let test_package_metadata: TestPackageMetadata = serde_json::from_value(value.clone()).unwrap();
assert_eq!(
test_package_metadata,
TestPackageMetadata {
some_field: true,
other_field: "foo".into(),
}
);
}
#[test]
fn builder_interface() {
let _ = MetadataCommand::new()
.manifest_path("Cargo.toml")
.exec()
.unwrap();
let _ = MetadataCommand::new()
.manifest_path(String::from("Cargo.toml"))
.exec()
.unwrap();
let _ = MetadataCommand::new()
.manifest_path(PathBuf::from("Cargo.toml"))
.exec()
.unwrap();
let _ = MetadataCommand::new()
.manifest_path("Cargo.toml")
.no_deps()
.exec()
.unwrap();
let _ = MetadataCommand::new()
.manifest_path("Cargo.toml")
.features(CargoOpt::AllFeatures)
.exec()
.unwrap();
let _ = MetadataCommand::new()
.manifest_path("Cargo.toml")
.current_dir(current_dir().unwrap())
.exec()
.unwrap();
}
#[test]
fn error1() {
match MetadataCommand::new().manifest_path("foo").exec() {
Err(Error::CargoMetadata { stderr }) => assert_eq!(
stderr.trim(),
"error: the manifest-path must be a path to a Cargo.toml file"
),
_ => unreachable!(),
}
}
#[test]
fn error2() {
match MetadataCommand::new()
.manifest_path("foo/Cargo.toml")
.exec()
{
Err(Error::CargoMetadata { stderr }) => assert_eq!(
stderr.trim(),
"error: manifest path `foo/Cargo.toml` does not exist"
),
_ => unreachable!(),
}
}
#[test]
fn cargo_path() {
match MetadataCommand::new()
.cargo_path("this does not exist")
.exec()
{
Err(Error::Io(e)) => assert_eq!(e.kind(), std::io::ErrorKind::NotFound),
_ => unreachable!(),
}
}
#[test]
fn metadata_deps() {
std::env::set_var("CARGO_PROFILE", "3");
let metadata = MetadataCommand::new()
.manifest_path("Cargo.toml")
.exec()
.unwrap();
let this_id = metadata
.workspace_members
.first()
.expect("Did not find ourselves");
let this = &metadata[this_id];
assert_eq!(this.name, "cargo_metadata");
let workspace_packages = metadata.workspace_packages();
assert_eq!(workspace_packages.len(), 1);
assert_eq!(&workspace_packages[0].id, this_id);
let lib = this
.targets
.iter()
.find(|t| t.name == "cargo_metadata")
.unwrap();
assert_eq!(lib.kind[0], "lib");
assert_eq!(lib.crate_types[0], "lib");
let selftest = this.targets.iter().find(|t| t.name == "selftest").unwrap();
assert_eq!(selftest.name, "selftest");
assert_eq!(selftest.kind[0], "test");
assert_eq!(selftest.crate_types[0], "bin");
let dependencies = &this.dependencies;
let serde = dependencies
.iter()
.find(|dep| dep.name == "serde")
.expect("Did not find serde dependency");
assert_eq!(serde.kind, cargo_metadata::DependencyKind::Normal);
assert!(!serde.req.matches(&Version::parse("1.0.0").unwrap()));
assert!(serde.req.matches(&Version::parse("1.99.99").unwrap()));
assert!(!serde.req.matches(&Version::parse("2.0.0").unwrap()));
}
#[test]
fn workspace_default_packages() {
use cargo_metadata::workspace_default_members_is_missing;
let metadata = MetadataCommand::new()
.manifest_path("Cargo.toml")
.exec()
.unwrap();
let workspace_packages = metadata.workspace_packages();
// this will only trigger on cargo versions that expose
// workspace_default_members (that is, cargo >= 1.71)
if !workspace_default_members_is_missing(&metadata.workspace_default_members) {
let default_packages = metadata.workspace_default_packages();
assert_eq!(default_packages, workspace_packages);
}
}
|