cargo/tests/testsuite/corrupt_git.rs

161 lines
4 KiB
Rust
Raw Normal View History

2019-11-25 02:42:45 +00:00
//! Tests for corrupt git repos.
2018-03-02 17:44:47 +00:00
use std::fs;
use std::path::{Path, PathBuf};
2019-09-12 19:52:46 +00:00
use cargo::util::paths as cargopaths;
use cargo_test_support::paths;
use cargo_test_support::{basic_manifest, git, project};
2018-03-02 17:44:47 +00:00
#[cargo_test]
2018-03-02 17:44:47 +00:00
fn deleting_database_files() {
let project = project();
2018-03-02 17:44:47 +00:00
let git_project = git::new("bar", |project| {
project
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_manifest("bar", "0.5.0"))
2018-03-02 17:44:47 +00:00
.file("src/lib.rs", "")
});
2018-03-02 17:44:47 +00:00
let project = project
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
&format!(
r#"
2018-03-02 17:44:47 +00:00
[project]
name = "foo"
version = "0.5.0"
authors = []
[dependencies]
bar = {{ git = '{}' }}
2018-03-14 15:17:44 +00:00
"#,
git_project.url()
),
2018-12-08 11:19:47 +00:00
)
.file("src/lib.rs", "")
2018-03-02 17:44:47 +00:00
.build();
project.cargo("build").run();
2018-03-02 17:44:47 +00:00
let mut files = Vec::new();
find_files(&paths::home().join(".cargo/git/db"), &mut files);
assert!(!files.is_empty());
2018-03-02 17:44:47 +00:00
let log = "cargo::sources::git=trace";
for file in files {
if !file.exists() {
2018-03-14 15:17:44 +00:00
continue;
2018-03-02 17:44:47 +00:00
}
println!("deleting {}", file.display());
cargopaths::remove_file(&file).unwrap();
project.cargo("build -v").env("CARGO_LOG", log).run();
2018-03-02 17:44:47 +00:00
if !file.exists() {
2018-03-14 15:17:44 +00:00
continue;
2018-03-02 17:44:47 +00:00
}
println!("truncating {}", file.display());
make_writable(&file);
fs::OpenOptions::new()
.write(true)
.open(&file)
.unwrap()
.set_len(2)
.unwrap();
project.cargo("build -v").env("CARGO_LOG", log).run();
2018-03-02 17:44:47 +00:00
}
}
#[cargo_test]
2018-03-02 17:44:47 +00:00
fn deleting_checkout_files() {
let project = project();
2018-03-02 17:44:47 +00:00
let git_project = git::new("bar", |project| {
project
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_manifest("bar", "0.5.0"))
2018-03-02 17:44:47 +00:00
.file("src/lib.rs", "")
});
2018-03-02 17:44:47 +00:00
let project = project
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
&format!(
r#"
2018-03-02 17:44:47 +00:00
[project]
name = "foo"
version = "0.5.0"
authors = []
[dependencies]
bar = {{ git = '{}' }}
2018-03-14 15:17:44 +00:00
"#,
git_project.url()
),
2018-12-08 11:19:47 +00:00
)
.file("src/lib.rs", "")
2018-03-02 17:44:47 +00:00
.build();
project.cargo("build").run();
2018-03-02 17:44:47 +00:00
let dir = paths::home()
.join(".cargo/git/checkouts")
// get the first entry in the checkouts dir for the package's location
.read_dir()
.unwrap()
.next()
.unwrap()
.unwrap()
.path()
// get the first child of that checkout dir for our checkout
.read_dir()
.unwrap()
.next()
.unwrap()
.unwrap()
.path()
// and throw on .git to corrupt things
.join(".git");
let mut files = Vec::new();
find_files(&dir, &mut files);
assert!(!files.is_empty());
2018-03-02 17:44:47 +00:00
let log = "cargo::sources::git=trace";
for file in files {
if !file.exists() {
2018-03-14 15:17:44 +00:00
continue;
2018-03-02 17:44:47 +00:00
}
println!("deleting {}", file.display());
cargopaths::remove_file(&file).unwrap();
project.cargo("build -v").env("CARGO_LOG", log).run();
2018-03-02 17:44:47 +00:00
if !file.exists() {
2018-03-14 15:17:44 +00:00
continue;
2018-03-02 17:44:47 +00:00
}
println!("truncating {}", file.display());
make_writable(&file);
fs::OpenOptions::new()
.write(true)
.open(&file)
.unwrap()
.set_len(2)
.unwrap();
project.cargo("build -v").env("CARGO_LOG", log).run();
2018-03-02 17:44:47 +00:00
}
}
fn make_writable(path: &Path) {
let mut p = path.metadata().unwrap().permissions();
p.set_readonly(false);
fs::set_permissions(path, p).unwrap();
}
fn find_files(path: &Path, dst: &mut Vec<PathBuf>) {
for e in path.read_dir().unwrap() {
let e = e.unwrap();
let path = e.path();
if e.file_type().unwrap().is_dir() {
find_files(&path, dst);
} else {
dst.push(path);
}
}
}