Skip to content

Commit 2ebcc75

Browse files
Actually make test effective
1 parent 604aeb5 commit 2ebcc75

File tree

1 file changed

+63
-24
lines changed

1 file changed

+63
-24
lines changed

tests/testsuite/clean.rs

Lines changed: 63 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
33
use cargo_test_support::registry::Package;
44
use cargo_test_support::{
5-
basic_bin_manifest, basic_lib_manifest, basic_manifest, git, main_file, project, project_in,
6-
rustc_host,
5+
basic_bin_manifest, basic_manifest, git, main_file, project, project_in, rustc_host,
76
};
87
use glob::GlobError;
98
use std::env;
@@ -112,6 +111,18 @@ fn clean_multiple_packages_in_glob_char_path() {
112111
assert_eq!(get_build_artifacts(foo_path, file_glob).len(), 0);
113112
}
114113

114+
fn get_build_artifacts(path: &PathBuf, file_glob: &str) -> Vec<Result<PathBuf, GlobError>> {
115+
let pattern = path.to_str().expect("expected utf-8 path");
116+
let pattern = glob::Pattern::escape(pattern);
117+
118+
let path = PathBuf::from(pattern).join(file_glob);
119+
let path = path.to_str().expect("expected utf-8 path");
120+
glob::glob(path)
121+
.expect("expected glob to run")
122+
.into_iter()
123+
.collect::<Vec<Result<PathBuf, GlobError>>>()
124+
}
125+
115126
#[cargo_test]
116127
fn clean_p_only_cleans_specified_package() {
117128
let p = project()
@@ -122,46 +133,74 @@ fn clean_p_only_cleans_specified_package() {
122133
members = [
123134
"foo",
124135
"foo_core",
136+
"foo-base",
125137
]
126138
"#,
127139
)
128-
.file("foo/Cargo.toml", &basic_lib_manifest("foo"))
140+
.file("foo/Cargo.toml", &basic_manifest("foo", "0.1.0"))
129141
.file("foo/src/lib.rs", "//! foo")
130-
.file("foo_core/Cargo.toml", &basic_lib_manifest("foo_core"))
142+
.file("foo_core/Cargo.toml", &basic_manifest("foo_core", "0.1.0"))
131143
.file("foo_core/src/lib.rs", "//! foo_core")
144+
.file("foo-base/Cargo.toml", &basic_manifest("foo-base", "0.1.0"))
145+
.file("foo-base/src/lib.rs", "//! foo-base")
132146
.build();
133147

134-
let deps_path = &p.build_dir().join("debug").join("deps");
135-
let foo_glob = "foo-*";
136-
let foo_core_glob = "foo_core-*";
148+
let fingerprint_path = &p.build_dir().join("debug").join(".fingerprint");
149+
150+
p.cargo("build -p foo -p foo_core -p foo-base").run();
137151

138-
p.cargo("build -p foo -p foo_core").run();
152+
let mut fingerprint_names = get_fingerprints_without_hashes(fingerprint_path);
139153

140-
// Artifacts present for both after building
141-
assert!(!get_build_artifacts(deps_path, foo_glob).is_empty());
142-
let num_foo_core_artifacts = get_build_artifacts(deps_path, foo_core_glob).len();
154+
// Artifacts present for all after building
155+
assert!(fingerprint_names.iter().any(|e| e == "foo"));
156+
let num_foo_core_artifacts = fingerprint_names
157+
.iter()
158+
.filter(|&e| e == "foo_core")
159+
.count();
143160
assert_ne!(num_foo_core_artifacts, 0);
161+
let num_foo_base_artifacts = fingerprint_names
162+
.iter()
163+
.filter(|&e| e == "foo-base")
164+
.count();
165+
assert_ne!(num_foo_base_artifacts, 0);
144166

145167
p.cargo("clean -p foo").run();
146168

147-
// Cleaning `foo` leaves artifacts for `foo_core`
148-
assert!(get_build_artifacts(deps_path, foo_glob).is_empty());
169+
fingerprint_names = get_fingerprints_without_hashes(fingerprint_path);
170+
171+
// Cleaning `foo` leaves artifacts for the others
172+
assert!(!fingerprint_names.iter().any(|e| e == "foo"));
149173
assert_eq!(
174+
fingerprint_names
175+
.iter()
176+
.filter(|&e| e == "foo_core")
177+
.count(),
178+
num_foo_core_artifacts,
179+
);
180+
assert_eq!(
181+
fingerprint_names
182+
.iter()
183+
.filter(|&e| e == "foo-base")
184+
.count(),
150185
num_foo_core_artifacts,
151-
get_build_artifacts(deps_path, foo_core_glob).len()
152186
);
153187
}
154188

155-
fn get_build_artifacts(path: &PathBuf, file_glob: &str) -> Vec<Result<PathBuf, GlobError>> {
156-
let pattern = path.to_str().expect("expected utf-8 path");
157-
let pattern = glob::Pattern::escape(pattern);
158-
159-
let path = PathBuf::from(pattern).join(file_glob);
160-
let path = path.to_str().expect("expected utf-8 path");
161-
glob::glob(path)
162-
.expect("expected glob to run")
163-
.into_iter()
164-
.collect::<Vec<Result<PathBuf, GlobError>>>()
189+
fn get_fingerprints_without_hashes(fingerprint_path: &Path) -> Vec<String> {
190+
std::fs::read_dir(fingerprint_path)
191+
.expect("Build dir should be readable")
192+
.filter_map(|entry| entry.ok())
193+
.map(|entry| {
194+
let name = entry.file_name();
195+
let name = name
196+
.into_string()
197+
.expect("fingerprint name should be UTF-8");
198+
name.rsplit_once('-')
199+
.expect("Name should contain at least one hyphen")
200+
.0
201+
.to_owned()
202+
})
203+
.collect()
165204
}
166205

167206
#[cargo_test]

0 commit comments

Comments
 (0)