Skip to content

Commit

Permalink
Actually make test effective
Browse files Browse the repository at this point in the history
  • Loading branch information
CosmicHorrorDev committed Nov 1, 2022
1 parent 604aeb5 commit 2ebcc75
Showing 1 changed file with 63 additions and 24 deletions.
87 changes: 63 additions & 24 deletions tests/testsuite/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

use cargo_test_support::registry::Package;
use cargo_test_support::{
basic_bin_manifest, basic_lib_manifest, basic_manifest, git, main_file, project, project_in,
rustc_host,
basic_bin_manifest, basic_manifest, git, main_file, project, project_in, rustc_host,
};
use glob::GlobError;
use std::env;
Expand Down Expand Up @@ -112,6 +111,18 @@ fn clean_multiple_packages_in_glob_char_path() {
assert_eq!(get_build_artifacts(foo_path, file_glob).len(), 0);
}

fn get_build_artifacts(path: &PathBuf, file_glob: &str) -> Vec<Result<PathBuf, GlobError>> {
let pattern = path.to_str().expect("expected utf-8 path");
let pattern = glob::Pattern::escape(pattern);

let path = PathBuf::from(pattern).join(file_glob);
let path = path.to_str().expect("expected utf-8 path");
glob::glob(path)
.expect("expected glob to run")
.into_iter()
.collect::<Vec<Result<PathBuf, GlobError>>>()
}

#[cargo_test]
fn clean_p_only_cleans_specified_package() {
let p = project()
Expand All @@ -122,46 +133,74 @@ fn clean_p_only_cleans_specified_package() {
members = [
"foo",
"foo_core",
"foo-base",
]
"#,
)
.file("foo/Cargo.toml", &basic_lib_manifest("foo"))
.file("foo/Cargo.toml", &basic_manifest("foo", "0.1.0"))
.file("foo/src/lib.rs", "//! foo")
.file("foo_core/Cargo.toml", &basic_lib_manifest("foo_core"))
.file("foo_core/Cargo.toml", &basic_manifest("foo_core", "0.1.0"))
.file("foo_core/src/lib.rs", "//! foo_core")
.file("foo-base/Cargo.toml", &basic_manifest("foo-base", "0.1.0"))
.file("foo-base/src/lib.rs", "//! foo-base")
.build();

let deps_path = &p.build_dir().join("debug").join("deps");
let foo_glob = "foo-*";
let foo_core_glob = "foo_core-*";
let fingerprint_path = &p.build_dir().join("debug").join(".fingerprint");

p.cargo("build -p foo -p foo_core -p foo-base").run();

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

// Artifacts present for both after building
assert!(!get_build_artifacts(deps_path, foo_glob).is_empty());
let num_foo_core_artifacts = get_build_artifacts(deps_path, foo_core_glob).len();
// Artifacts present for all after building
assert!(fingerprint_names.iter().any(|e| e == "foo"));
let num_foo_core_artifacts = fingerprint_names
.iter()
.filter(|&e| e == "foo_core")
.count();
assert_ne!(num_foo_core_artifacts, 0);
let num_foo_base_artifacts = fingerprint_names
.iter()
.filter(|&e| e == "foo-base")
.count();
assert_ne!(num_foo_base_artifacts, 0);

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

// Cleaning `foo` leaves artifacts for `foo_core`
assert!(get_build_artifacts(deps_path, foo_glob).is_empty());
fingerprint_names = get_fingerprints_without_hashes(fingerprint_path);

// Cleaning `foo` leaves artifacts for the others
assert!(!fingerprint_names.iter().any(|e| e == "foo"));
assert_eq!(
fingerprint_names
.iter()
.filter(|&e| e == "foo_core")
.count(),
num_foo_core_artifacts,
);
assert_eq!(
fingerprint_names
.iter()
.filter(|&e| e == "foo-base")
.count(),
num_foo_core_artifacts,
get_build_artifacts(deps_path, foo_core_glob).len()
);
}

fn get_build_artifacts(path: &PathBuf, file_glob: &str) -> Vec<Result<PathBuf, GlobError>> {
let pattern = path.to_str().expect("expected utf-8 path");
let pattern = glob::Pattern::escape(pattern);

let path = PathBuf::from(pattern).join(file_glob);
let path = path.to_str().expect("expected utf-8 path");
glob::glob(path)
.expect("expected glob to run")
.into_iter()
.collect::<Vec<Result<PathBuf, GlobError>>>()
fn get_fingerprints_without_hashes(fingerprint_path: &Path) -> Vec<String> {
std::fs::read_dir(fingerprint_path)
.expect("Build dir should be readable")
.filter_map(|entry| entry.ok())
.map(|entry| {
let name = entry.file_name();
let name = name
.into_string()
.expect("fingerprint name should be UTF-8");
name.rsplit_once('-')
.expect("Name should contain at least one hyphen")
.0
.to_owned()
})
.collect()
}

#[cargo_test]
Expand Down

0 comments on commit 2ebcc75

Please sign in to comment.