1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
//! Tests for corrupt git repos.
use std::fs;
use std::path::{Path, PathBuf};
use cargo_test_support::paths;
use cargo_test_support::prelude::*;
use cargo_test_support::{basic_manifest, git, project};
use cargo_util::paths as cargopaths;
#[cargo_test]
fn deleting_database_files() {
let project = project();
let git_project = git::new("bar", |project| {
project
.file("Cargo.toml", &basic_manifest("bar", "0.5.0"))
.file("src/lib.rs", "")
});
let project = project
.file(
"Cargo.toml",
&format!(
r#"
[package]
name = "foo"
version = "0.5.0"
authors = []
[dependencies]
bar = {{ git = '{}' }}
"#,
git_project.url()
),
)
.file("src/lib.rs", "")
.build();
project.cargo("check").run();
let mut files = Vec::new();
find_files(&paths::home().join(".cargo/git/db"), &mut files);
assert!(!files.is_empty());
let log = "cargo::sources::git=trace";
for file in files {
if !file.exists() {
continue;
}
println!("deleting {}", file.display());
cargopaths::remove_file(&file).unwrap();
project.cargo("check -v").env("CARGO_LOG", log).run();
if !file.exists() {
continue;
}
println!("truncating {}", file.display());
make_writable(&file);
fs::OpenOptions::new()
.write(true)
.open(&file)
.unwrap()
.set_len(2)
.unwrap();
project.cargo("check -v").env("CARGO_LOG", log).run();
}
}
#[cargo_test]
fn deleting_checkout_files() {
let project = project();
let git_project = git::new("bar", |project| {
project
.file("Cargo.toml", &basic_manifest("bar", "0.5.0"))
.file("src/lib.rs", "")
});
let project = project
.file(
"Cargo.toml",
&format!(
r#"
[package]
name = "foo"
version = "0.5.0"
authors = []
[dependencies]
bar = {{ git = '{}' }}
"#,
git_project.url()
),
)
.file("src/lib.rs", "")
.build();
project.cargo("check").run();
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());
let log = "cargo::sources::git=trace";
for file in files {
if !file.exists() {
continue;
}
println!("deleting {}", file.display());
cargopaths::remove_file(&file).unwrap();
project.cargo("check -v").env("CARGO_LOG", log).run();
if !file.exists() {
continue;
}
println!("truncating {}", file.display());
make_writable(&file);
fs::OpenOptions::new()
.write(true)
.open(&file)
.unwrap()
.set_len(2)
.unwrap();
project.cargo("check -v").env("CARGO_LOG", log).run();
}
}
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);
}
}
}
|