#[path = "../src/test_helpers.rs"]
mod test_helpers;
use std::fs::{create_dir_all, File};
use std::path::*;
use std::process::Command;
use crate::test_helpers::bin_path;
use regex::Regex;
use walkdir::WalkDir;
#[test]
#[cfg_attr(feature = "offline_tests", ignore)]
fn spurious_files_in_cache_test() {
let crate_path = PathBuf::from("tests/size_test/");
let fchp = "target/spurious_files_test"; let status = Command::new("cargo")
.arg("fetch")
.current_dir(&crate_path)
.env("CARGO_HOME", "../../target/spurious_files_test")
.output();
assert!(status.is_ok(), "fetch of dummy crate did not succeed");
assert!(
status.unwrap().status.success(),
"exit status of dummy crate fetch != 0"
);
assert!(
PathBuf::from(&fchp).is_dir(),
"fake cargo home was not created!"
);
File::create("target/spurious_files_test/file1.txt").unwrap();
File::create("target/spurious_files_test/file2").unwrap();
File::create("target/spurious_files_test/registry/file3.ogg").unwrap();
File::create("target/spurious_files_test/registry/cache/file4").unwrap();
File::create("target/spurious_files_test/registry/index/file5").unwrap();
File::create("target/spurious_files_test/registry/src/file6").unwrap();
File::create("target/spurious_files_test/registry/cache/github.com-1ecc6299db9ec823/file7")
.unwrap();
File::create("target/spurious_files_test/registry/index/github.com-1ecc6299db9ec823/file8")
.unwrap();
File::create("target/spurious_files_test/registry/src/github.com-1ecc6299db9ec823/file9")
.unwrap();
create_dir_all("target/spurious_files_test/registry/cache/github.com-1ecc6299db9ec823/foo_dir")
.unwrap();
create_dir_all("target/spurious_files_test/git/checkouts").unwrap();
create_dir_all("target/spurious_files_test/git/db").unwrap();
File::create("target/spurious_files_test/git/.DS_STORE").unwrap();
File::create("target/spurious_files_test/git/foo").unwrap();
File::create("target/spurious_files_test/git/checkouts/.DS_STORE").unwrap();
File::create("target/spurious_files_test/git/checkouts/bar").unwrap();
File::create("target/spurious_files_test/git/db/.DS_STORE").unwrap();
File::create("target/spurious_files_test/git/db/bla").unwrap();
let mut registry_pkg_cache_path = PathBuf::from(&fchp);
registry_pkg_cache_path.push("registry");
registry_pkg_cache_path.push("cache");
assert!(registry_pkg_cache_path.is_dir(), "no registry cache found");
let mut filenames = WalkDir::new(registry_pkg_cache_path)
.min_depth(2)
.into_iter()
.map(|dir| dir.unwrap().path().file_name().unwrap().to_owned())
.collect::<Vec<_>>();
filenames.sort();
assert_eq!(filenames.len(), 6);
assert_eq!(
filenames,
[
"cc-1.0.18.crate",
"file7", "foo_dir", "libc-0.2.42.crate",
"pkg-config-0.3.12.crate",
"unicode-xid-0.0.4.crate"
]
);
let cargo_cache = Command::new(bin_path()).env("CARGO_HOME", fchp).output();
assert!(cargo_cache.is_ok(), "cargo cache failed to run");
let cmd_out = &cargo_cache.unwrap();
let cc_output = String::from_utf8_lossy(&cmd_out.stdout).into_owned();
let mut desired_output = String::from("Cargo cache .*spurious_files_test.*:\n\n");
desired_output.push_str(
"Total: .* MB
0 installed binaries: .* B
Registry: .* MB
Registry index: .* MB
.. crate archives: .* KB
.. crate source checkouts: .* MB
Git db: 0 B
0 bare git repos: 0 B
0 git repo checkouts: 0 B",
);
let regex = Regex::new(&desired_output);
assert!(
regex.unwrap().is_match(&cc_output),
"regex: {:?}, cc_output: {}",
desired_output,
cc_output
);
}