Skip to content

Commit

Permalink
Add test to verify functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
CosmicHorrorDev committed Oct 30, 2022
1 parent 738f084 commit a6d0e96
Showing 1 changed file with 52 additions and 11 deletions.
63 changes: 52 additions & 11 deletions tests/testsuite/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
use cargo_test_support::registry::Package;
use cargo_test_support::{
basic_bin_manifest, basic_manifest, git, main_file, project, project_in, rustc_host,
basic_bin_manifest, basic_lib_manifest, basic_manifest, git, main_file, project, project_in,
rustc_host,
};
use glob::GlobError;
use std::env;
Expand Down Expand Up @@ -96,26 +97,66 @@ fn clean_multiple_packages_in_glob_char_path() {
.build();
let foo_path = &p.build_dir().join("debug").join("deps");

#[cfg(not(target_env = "msvc"))]
let file_glob = "foo-*";

#[cfg(target_env = "msvc")]
let file_glob = "foo.pdb";

// Assert that build artifacts are produced
p.cargo("build").run();
assert_ne!(get_build_artifacts(foo_path).len(), 0);
assert_ne!(get_build_artifacts(foo_path, file_glob).len(), 0);

// Assert that build artifacts are destroyed
p.cargo("clean -p foo").run();
assert_eq!(get_build_artifacts(foo_path).len(), 0);
assert_eq!(get_build_artifacts(foo_path, file_glob).len(), 0);
}

fn get_build_artifacts(path: &PathBuf) -> Vec<Result<PathBuf, GlobError>> {
let pattern = path.to_str().expect("expected utf-8 path");
let pattern = glob::Pattern::escape(pattern);
#[cargo_test]
fn clean_p_only_cleans_specified_package() {
let p = project()
.file(
"Cargo.toml",
r#"
[workspace]
members = [
"foo",
"foo_core",
]
"#,
)
.file("foo/Cargo.toml", &basic_lib_manifest("foo"))
.file("foo/src/lib.rs", "//! foo")
.file("foo_core/Cargo.toml", &basic_lib_manifest("foo_core"))
.file("foo_core/src/lib.rs", "//! foo_core")
.build();

#[cfg(not(target_env = "msvc"))]
const FILE: &str = "foo-*";
let deps_path = &p.build_dir().join("debug").join("deps");
let foo_glob = "foo-*";
let foo_core_glob = "foo_core-*";

#[cfg(target_env = "msvc")]
const FILE: &str = "foo.pdb";
p.cargo("build -p foo -p foo_core").run();

// 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();
assert_ne!(num_foo_core_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());
assert_eq!(
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);
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")
Expand Down

0 comments on commit a6d0e96

Please sign in to comment.