forked from trailofbits/dylint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
272 lines (247 loc) · 9.77 KB
/
Copy pathlib.rs
File metadata and controls
272 lines (247 loc) · 9.77 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#[cfg(all(not(coverage), test))]
mod test {
use cargo_metadata::MetadataCommand;
use dylint_internal::{CommandExt, clippy_utils::toolchain_channel, examples::iter};
use regex::Regex;
use std::{
collections::BTreeSet,
ffi::OsStr,
fs::{read_dir, read_to_string},
path::Path,
process::Command,
};
use toml_edit::{DocumentMut, Item};
use walkdir::WalkDir;
#[test]
fn examples() {
// smoelius: Pass `--lib --tests` to `cargo test` to avoid the potential filename
// collision associated with building the examples.
nested_workspace::test().args(["--lib", "--tests"]).unwrap();
}
#[test]
fn examples_have_same_version_as_workspace() {
for path in iter(false).unwrap() {
let path = path.unwrap();
if path.file_name() == Some(OsStr::new("restriction")) {
continue;
}
let metadata = MetadataCommand::new()
.current_dir(&path)
.no_deps()
.exec()
.unwrap();
let package = dylint_internal::cargo::package_with_root(&metadata, &path).unwrap();
assert_eq!(env!("CARGO_PKG_VERSION"), package.version.to_string());
}
}
#[test]
fn examples_have_equivalent_cargo_configs() {
let mut prev = None;
for path in iter(true).unwrap() {
let path = path.unwrap();
if path.file_name() == Some(OsStr::new("straggler")) {
continue;
}
let config_toml = path.join(".cargo/config.toml");
let contents = read_to_string(config_toml).unwrap();
let mut document = contents.parse::<DocumentMut>().unwrap();
// smoelius: Hack. `build.target-dir` is expected to be a relative path. Replace it with
// an absolute one. However, the directory might not exist when this test is run. So use
// `cargo_util::paths::normalize_path` rather than `Path::canonicalize`.
document
.as_table_mut()
.get_mut("build")
.and_then(Item::as_table_mut)
.and_then(|table| table.get_mut("target-dir"))
.and_then(Item::as_value_mut)
.and_then(|value| {
let target_dir = value.as_str()?;
*value = cargo_util::paths::normalize_path(&path.join(target_dir))
.to_string_lossy()
.as_ref()
.into();
Some(())
})
.unwrap();
let curr = document.to_string();
if let Some(prev) = &prev {
assert_eq!(*prev, curr);
} else {
prev = Some(curr);
}
}
}
#[test]
fn examples_use_edition_2024() {
for result in iter(false).unwrap() {
let manifest_dir = result.unwrap();
let manifest_path = manifest_dir.join("Cargo.toml");
let contents = read_to_string(&manifest_path).unwrap();
let table = toml::from_str::<toml::Table>(&contents).unwrap();
let Some(package) = table.get("package").and_then(|value| value.as_table()) else {
continue;
};
let edition = package.get("edition").and_then(toml::Value::as_str);
assert_eq!(
Some("2024"),
edition,
"failed for `{}`",
manifest_path.display()
);
}
}
#[test]
fn examples_use_same_toolchain_channel() {
let mut prev = None;
for path in iter(true).unwrap() {
let path = path.unwrap();
if path.file_name() == Some(OsStr::new("straggler")) {
continue;
}
let curr = toolchain_channel(&path).unwrap();
if let Some(prev) = &prev {
assert_eq!(*prev, curr);
} else {
prev = Some(curr);
}
}
}
#[test]
fn examples_do_not_require_rust_src() {
for path in iter(true).unwrap() {
let path = path.unwrap();
let contents = read_to_string(path.join("rust-toolchain")).unwrap();
let table = toml::from_str::<toml::Table>(&contents).unwrap();
let array = table
.get("toolchain")
.and_then(toml::Value::as_table)
.and_then(|toolchain| toolchain.get("components"))
.and_then(toml::Value::as_array)
.unwrap();
let components = array
.iter()
.map(toml::Value::as_str)
.collect::<Option<Vec<_>>>()
.unwrap();
assert!(!components.contains(&"rust-src"));
}
}
#[test]
fn examples_do_not_contain_forbidden_paths() {
let forbidden_files_general = [".gitignore"];
let forbidden_files_specific = [".cargo/config.toml", "rust-toolchain"];
let allowed_dirs = ["experimental", "testing"];
let root_dirs_with_exceptions = ["general", "supplementary", "restriction"];
for entry in WalkDir::new(".").into_iter().flatten() {
let path = entry.path();
if let Some(file_name) = path.file_name().and_then(OsStr::to_str) {
if forbidden_files_general.contains(&file_name) {
assert!(
!forbidden_files_general.contains(&file_name),
"Forbidden file `.gitignore` found in examples directory: {}",
path.display()
);
}
if forbidden_files_specific.contains(&file_name) {
let is_in_allowed_directory = allowed_dirs
.iter()
.any(|&allowed| path.starts_with(allowed));
let is_in_root_of_exception_dirs =
root_dirs_with_exceptions.iter().any(|&exception| {
path.starts_with(exception) && path.components().count() == 2
});
assert!(
!(is_in_allowed_directory || is_in_root_of_exception_dirs),
"Forbidden file {} found in non-allowed directory: {}",
file_name,
path.display()
);
}
}
}
}
#[test]
fn check_examples_formatting() {
let mut failed_files = Vec::new();
for entry in WalkDir::new(".")
.into_iter()
.filter_map(Result::ok)
.filter(|e| e.path().extension() == Some(OsStr::new("rs")))
{
let path = entry.path();
let output = Command::new("rustfmt")
.args([
"+nightly",
"--check",
"--edition=2024",
path.to_str().unwrap(),
])
.logged_output(false)
.unwrap_or_else(|error| panic!("Failed to execute rustfmt: {error}"));
if !output.status.success() {
failed_files.push(path.to_path_buf());
eprintln!(
"rustfmt check failed for: {}\nstdout:\n```\n{}\n```\nstderr:\n```\n{}\n```",
path.display(),
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
}
}
assert!(
failed_files.is_empty(),
"rustfmt check failed for the following files:\n{}",
failed_files
.iter()
.map(|p| p.display().to_string())
.collect::<Vec<_>>()
.join("\n")
);
}
#[test]
fn verify_registered_lints() {
const CATEGORIES: &[&str] = &["general", "supplementary"];
let register_lints_re =
Regex::new(r"([a-zA-Z_][a-zA-Z0-9_]*)::register_lints\s*\(").unwrap();
for category in CATEGORIES {
let category_path = Path::new(category);
let lib_rs_path = category_path.join("src/lib.rs");
let file_contents = read_to_string(&lib_rs_path)
.unwrap_or_else(|e| panic!("Failed to read {}: {}", lib_rs_path.display(), e));
let actual_lints: BTreeSet<_> = register_lints_re
.captures_iter(&file_contents)
.map(|cap| cap.get(1).unwrap().as_str().to_string())
.collect();
let expected_lints: BTreeSet<_> = read_dir(category_path)
.unwrap_or_else(|e| {
panic!(
"Failed to read directory {}: {}",
category_path.display(),
e
)
})
.filter_map(|entry| {
let entry = entry.ok()?;
let path = entry.path();
if path.is_dir() {
let file_name = path.file_name()?;
let dir_name = file_name.to_str()?;
if dir_name != "src" && !dir_name.starts_with('.') {
return Some(dir_name.to_string());
}
}
None
})
.collect();
let missing: Vec<_> = expected_lints.difference(&actual_lints).collect();
assert!(
missing.is_empty(),
"Mismatch in {}\n\nMissing registered lints: {:?}\n\nExpected: {:?}\nActual: {:?}",
category_path.display(),
missing,
expected_lints,
actual_lints
);
}
}
}