2019-11-25 02:42:45 +00:00
|
|
|
//! Tests for corrupt git repos.
|
|
|
|
|
2019-09-12 17:14:29 +00:00
|
|
|
use cargo_test_support::paths;
|
|
|
|
use cargo_test_support::{basic_manifest, git, project};
|
2021-03-20 20:43:33 +00:00
|
|
|
use cargo_util::paths as cargopaths;
|
|
|
|
use std::fs;
|
|
|
|
use std::path::{Path, PathBuf};
|
2018-03-02 17:44:47 +00:00
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
#[cargo_test]
|
2018-03-02 17:44:47 +00:00
|
|
|
fn deleting_database_files() {
|
2018-07-20 11:47:47 +00:00
|
|
|
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", "")
|
2019-08-13 05:25:36 +00:00
|
|
|
});
|
2018-03-02 17:44:47 +00:00
|
|
|
|
|
|
|
let project = project
|
2018-03-14 15:17:44 +00:00
|
|
|
.file(
|
|
|
|
"Cargo.toml",
|
|
|
|
&format!(
|
|
|
|
r#"
|
2022-09-22 19:50:54 +00:00
|
|
|
[package]
|
2020-09-27 00:59:58 +00:00
|
|
|
name = "foo"
|
|
|
|
version = "0.5.0"
|
|
|
|
authors = []
|
2018-03-02 17:44:47 +00:00
|
|
|
|
2020-09-27 00:59:58 +00:00
|
|
|
[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();
|
|
|
|
|
2023-02-15 22:46:35 +00:00
|
|
|
project.cargo("check").run();
|
2018-03-02 17:44:47 +00:00
|
|
|
|
|
|
|
let mut files = Vec::new();
|
|
|
|
find_files(&paths::home().join(".cargo/git/db"), &mut files);
|
2018-08-08 22:57:20 +00:00
|
|
|
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();
|
2023-02-15 22:46:35 +00:00
|
|
|
project.cargo("check -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();
|
2023-02-15 22:46:35 +00:00
|
|
|
project.cargo("check -v").env("CARGO_LOG", log).run();
|
2018-03-02 17:44:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
#[cargo_test]
|
2018-03-02 17:44:47 +00:00
|
|
|
fn deleting_checkout_files() {
|
2018-07-20 11:47:47 +00:00
|
|
|
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", "")
|
2019-08-13 05:25:36 +00:00
|
|
|
});
|
2018-03-02 17:44:47 +00:00
|
|
|
|
|
|
|
let project = project
|
2018-03-14 15:17:44 +00:00
|
|
|
.file(
|
|
|
|
"Cargo.toml",
|
|
|
|
&format!(
|
|
|
|
r#"
|
2022-09-22 19:50:54 +00:00
|
|
|
[package]
|
2020-09-27 00:59:58 +00:00
|
|
|
name = "foo"
|
|
|
|
version = "0.5.0"
|
|
|
|
authors = []
|
2018-03-02 17:44:47 +00:00
|
|
|
|
2020-09-27 00:59:58 +00:00
|
|
|
[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();
|
|
|
|
|
2023-02-15 22:46:35 +00:00
|
|
|
project.cargo("check").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);
|
2018-08-08 22:57:20 +00:00
|
|
|
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();
|
2023-02-15 22:46:35 +00:00
|
|
|
project.cargo("check -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();
|
2023-02-15 22:46:35 +00:00
|
|
|
project.cargo("check -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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|