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