-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchecker.rs
More file actions
563 lines (507 loc) · 18 KB
/
checker.rs
File metadata and controls
563 lines (507 loc) · 18 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
// Module declarations
mod dependency;
pub(crate) mod layer;
mod todo_format;
mod common_test;
mod folder_privacy;
mod output_helper;
pub(crate) mod pack_checker;
mod privacy;
pub(crate) mod reference;
mod visibility;
use crate::packs::checker_configuration::CheckerType;
// Internal imports
use crate::packs::pack::write_pack_to_disk;
use crate::packs::pack::Pack;
use crate::packs::package_todo;
use crate::packs::Configuration;
use anyhow::bail;
// External imports
use rayon::prelude::IntoParallelIterator;
use rayon::prelude::IntoParallelRefIterator;
use rayon::prelude::ParallelIterator;
use reference::Reference;
use std::collections::HashMap;
use std::fmt;
use std::fmt::Display;
use std::fmt::Formatter;
use std::{collections::HashSet, path::PathBuf};
use tracing::debug;
use super::bin_locater;
use super::reference_extractor::get_all_references;
#[derive(PartialEq, Clone, Eq, Hash, Debug)]
pub struct ViolationIdentifier {
pub violation_type: String,
pub strict: bool,
pub file: String,
pub constant_name: String,
pub referencing_pack_name: String,
pub defining_pack_name: String,
}
#[derive(PartialEq, Clone, Eq, Hash, Debug)]
pub struct Violation {
pub message: String,
pub identifier: ViolationIdentifier,
}
pub(crate) trait CheckerInterface {
fn check(
&self,
reference: &Reference,
configuration: &Configuration,
) -> anyhow::Result<Option<Violation>>;
fn violation_type(&self) -> String;
}
pub(crate) trait ValidatorInterface {
fn validate(&self, configuration: &Configuration) -> Option<Vec<String>>;
}
#[derive(Debug, PartialEq)]
pub struct CheckAllResult {
pub reportable_violations: HashSet<Violation>,
pub stale_violations: Vec<ViolationIdentifier>,
pub strict_mode_violations: HashSet<Violation>,
}
impl CheckAllResult {
pub fn has_violations(&self) -> bool {
!self.reportable_violations.is_empty()
|| !self.stale_violations.is_empty()
|| !self.strict_mode_violations.is_empty()
}
fn write_violations(&self, f: &mut Formatter<'_>) -> fmt::Result {
if !self.reportable_violations.is_empty() {
let mut sorted_violations: Vec<&Violation> =
self.reportable_violations.iter().collect();
sorted_violations.sort_by(|a, b| a.message.cmp(&b.message));
writeln!(f, "{} violation(s) detected:", sorted_violations.len())?;
for violation in sorted_violations {
writeln!(f, "{}\n", violation.message)?;
}
}
if !self.stale_violations.is_empty() {
writeln!(
f,
"There were stale violations found, please run `{} update`",
bin_locater::packs_bin_name(),
)?;
}
if !self.strict_mode_violations.is_empty() {
for v in self.strict_mode_violations.iter() {
let error_message =
build_strict_violation_message(&v.identifier);
writeln!(f, "{}", error_message)?;
}
}
Ok(())
}
}
impl Display for CheckAllResult {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
if self.has_violations() {
self.write_violations(f)
} else {
write!(f, "No violations detected!")
}
}
}
struct CheckAllBuilder<'a> {
configuration: &'a Configuration,
found_violations: &'a FoundViolations,
}
#[derive(Debug)]
struct FoundViolations {
absolute_paths: HashSet<PathBuf>,
violations: HashSet<Violation>,
}
impl<'a> CheckAllBuilder<'a> {
fn new(
configuration: &'a Configuration,
found_violations: &'a FoundViolations,
) -> Self {
Self {
configuration,
found_violations,
}
}
pub fn build(mut self) -> anyhow::Result<CheckAllResult> {
let recorded_violations = &self.configuration.pack_set.all_violations;
Ok(CheckAllResult {
reportable_violations: self
.build_reportable_violations(recorded_violations)
.into_iter()
.cloned()
.collect(),
stale_violations: self
.build_stale_violations(recorded_violations)?
.into_iter()
.cloned()
.collect(),
strict_mode_violations: self
.build_strict_mode_violations()
.into_iter()
.collect(),
})
}
fn build_reportable_violations(
&mut self,
recorded_violations: &HashSet<ViolationIdentifier>,
) -> HashSet<&'a Violation> {
let reportable_violations =
if self.configuration.ignore_recorded_violations {
debug!("Filtering recorded violations is disabled in config");
self.found_violations.violations.iter().collect()
} else {
self.found_violations
.violations
.iter()
.filter(|v| !recorded_violations.contains(&v.identifier))
.collect()
};
reportable_violations
}
fn build_stale_violations(
&mut self,
recorded_violations: &'a HashSet<ViolationIdentifier>,
) -> anyhow::Result<Vec<&'a ViolationIdentifier>> {
let found_violation_identifiers: HashSet<&ViolationIdentifier> = self
.found_violations
.violations
.par_iter()
.map(|v| &v.identifier)
.collect();
let relative_files = self
.found_violations
.absolute_paths
.iter()
.map(|p| {
p.strip_prefix(&self.configuration.absolute_root)
.map_err(|e| {
anyhow::Error::new(e).context(format!(
"Failed to strip prefix from {:?}",
&self.configuration.absolute_root
))
})
.and_then(|path| {
path.to_str().ok_or_else(|| {
anyhow::Error::new(std::fmt::Error).context(
format!(
"Path ({:?}) cannot be converted to &str",
&path
),
)
})
})
})
.collect::<anyhow::Result<HashSet<&str>>>()?;
let stale_violations = recorded_violations
.par_iter()
.filter(|v_identifier| {
Self::is_stale_violation(
&relative_files,
&found_violation_identifiers,
v_identifier,
)
})
.collect::<Vec<&ViolationIdentifier>>();
Ok(stale_violations)
}
fn is_stale_violation(
relative_files: &HashSet<&str>,
found_violation_identifiers: &HashSet<&ViolationIdentifier>,
todo_violation_identifier: &ViolationIdentifier,
) -> bool {
let violation_path_exists =
relative_files.contains(todo_violation_identifier.file.as_str());
if violation_path_exists {
!found_violation_identifiers.contains(todo_violation_identifier)
} else {
true // The todo violation references a file that no longer exists
}
}
fn build_strict_mode_violations(&self) -> Vec<Violation> {
self.found_violations
.violations
.iter()
.filter(|v| v.identifier.strict)
.cloned()
.collect()
}
}
pub(crate) fn check_all(
configuration: &Configuration,
files: Vec<String>,
) -> anyhow::Result<CheckAllResult> {
let checkers = get_checkers(configuration);
debug!("Intersecting input files with configuration included files");
let absolute_paths: HashSet<PathBuf> =
configuration.intersect_files(files.clone());
let violations: HashSet<Violation> =
get_all_violations(configuration, &absolute_paths, &checkers)?;
let found_violations = FoundViolations {
absolute_paths,
violations,
};
CheckAllBuilder::new(configuration, &found_violations).build()
}
fn validate(configuration: &Configuration) -> Vec<String> {
debug!("Running validators against packages");
let validators: Vec<Box<dyn ValidatorInterface + Send + Sync>> = vec![
Box::new(dependency::Checker {
checker_configuration: configuration.checker_configuration
[&CheckerType::Dependency]
.clone(),
}),
Box::new(layer::Checker {
layers: configuration.layers.clone(),
checker_configuration: configuration.checker_configuration
[&CheckerType::Layer]
.clone(),
}),
Box::new(todo_format::Checker),
];
let mut validation_errors: Vec<String> = validators
.iter()
.filter_map(|v| v.validate(configuration))
.flatten()
.collect();
validation_errors.dedup();
debug!("Finished validators against packages");
validation_errors
}
pub(crate) fn build_strict_violation_message(
violation_identifier: &ViolationIdentifier,
) -> String {
format!("{} cannot have {} violations on {} because strict mode is enabled for {} violations in the enforcing pack's package.yml file",
violation_identifier.referencing_pack_name,
violation_identifier.violation_type,
violation_identifier.defining_pack_name,
violation_identifier.violation_type,)
}
pub(crate) fn validate_all(
configuration: &Configuration,
) -> anyhow::Result<()> {
let validation_errors = validate(configuration);
if !validation_errors.is_empty() {
println!("{} validation error(s) detected:", validation_errors.len());
for validation_error in validation_errors.iter() {
println!("{}\n", validation_error);
}
bail!("Packwerk validate failed")
} else {
println!("Packwerk validate succeeded!");
Ok(())
}
}
pub(crate) fn update(configuration: &Configuration) -> anyhow::Result<()> {
let checkers = get_checkers(configuration);
let violations = get_all_violations(
configuration,
&configuration.included_files,
&checkers,
)?;
let strict_violations = &violations
.iter()
.filter(|v| v.identifier.strict)
.collect::<Vec<&Violation>>();
if !strict_violations.is_empty() {
for violation in strict_violations {
let strict_message =
build_strict_violation_message(&violation.identifier);
println!("{}", strict_message);
}
println!(
"{} strict mode violation(s) detected. These violations must be fixed for `check` to succeed.",
&strict_violations.len()
);
}
package_todo::write_violations_to_disk(configuration, violations);
println!("Successfully updated package_todo.yml files!");
Ok(())
}
pub(crate) fn remove_unnecessary_dependencies(
configuration: &Configuration,
) -> anyhow::Result<()> {
let unnecessary_dependencies = get_unnecessary_dependencies(configuration)?;
for (pack, dependency_names) in unnecessary_dependencies.iter() {
remove_reference_to_dependency(pack, dependency_names)?;
}
Ok(())
}
pub(crate) fn check_unnecessary_dependencies(
configuration: &Configuration,
) -> anyhow::Result<()> {
let unnecessary_dependencies = get_unnecessary_dependencies(configuration)?;
if unnecessary_dependencies.is_empty() {
Ok(())
} else {
for (pack, dependency_names) in unnecessary_dependencies.iter() {
for dependency_name in dependency_names {
println!(
"{} depends on {} but does not use it",
pack.name, dependency_name
)
}
}
let found_message = if unnecessary_dependencies.len() == 1 {
"Found 1 unnecessary dependency".to_string()
} else {
format!(
"Found {} unused dependencies",
unnecessary_dependencies.len()
)
};
bail!(
"{}. Run `{} check-unused-dependencies --auto-correct` to remove them.",
found_message,
bin_locater::packs_bin_name(),
);
}
}
fn get_unnecessary_dependencies(
configuration: &Configuration,
) -> anyhow::Result<HashMap<Pack, Vec<String>>> {
let references =
get_all_references(configuration, &configuration.included_files)?;
let mut edge_counts: HashMap<(String, String), i32> = HashMap::new();
for reference in references {
let defining_pack_name = reference.defining_pack_name;
if let Some(defining_pack_name) = defining_pack_name {
let edge_key =
(reference.referencing_pack_name, defining_pack_name);
edge_counts
.entry(edge_key)
.and_modify(|f| *f += 1)
.or_insert(1);
}
}
let mut unnecessary_dependencies: HashMap<Pack, Vec<String>> =
HashMap::new();
for pack in &configuration.pack_set.packs {
for dependency_name in &pack.dependencies {
let edge_key = (pack.name.clone(), dependency_name.clone());
let edge_count = edge_counts.get(&edge_key).unwrap_or(&0);
if edge_count == &0 {
unnecessary_dependencies
.entry(pack.clone())
.or_default()
.push(dependency_name.clone());
}
}
}
Ok(unnecessary_dependencies)
}
fn get_all_violations(
configuration: &Configuration,
absolute_paths: &HashSet<PathBuf>,
checkers: &Vec<Box<dyn CheckerInterface + Send + Sync>>,
) -> anyhow::Result<HashSet<Violation>> {
let references = get_all_references(configuration, absolute_paths)?;
debug!("Running checkers on resolved references");
let violations = checkers
.into_par_iter()
.try_fold(HashSet::new, |mut acc, c| {
for reference in &references {
if let Some(violation) = c.check(reference, configuration)? {
acc.insert(violation);
}
}
Ok(acc)
})
.try_reduce(HashSet::new, |mut acc, v| {
acc.extend(v);
Ok(acc)
});
debug!("Finished running checkers");
violations
}
fn get_checkers(
configuration: &Configuration,
) -> Vec<Box<dyn CheckerInterface + Send + Sync>> {
vec![
Box::new(dependency::Checker {
checker_configuration: configuration.checker_configuration
[&CheckerType::Dependency]
.clone(),
}),
Box::new(privacy::Checker {
checker_configuration: configuration.checker_configuration
[&CheckerType::Privacy]
.clone(),
}),
Box::new(visibility::Checker {
checker_configuration: configuration.checker_configuration
[&CheckerType::Visibility]
.clone(),
}),
Box::new(layer::Checker {
layers: configuration.layers.clone(),
checker_configuration: configuration.checker_configuration
[&CheckerType::Layer]
.clone(),
}),
Box::new(folder_privacy::Checker {
checker_configuration: configuration.checker_configuration
[&CheckerType::FolderPrivacy]
.clone(),
}),
]
}
fn remove_reference_to_dependency(
pack: &Pack,
dependency_names: &[String],
) -> anyhow::Result<()> {
let without_dependency = pack
.dependencies
.iter()
.filter(|dependency| !dependency_names.contains(dependency));
let updated_pack = Pack {
dependencies: without_dependency.cloned().collect(),
..pack.clone()
};
write_pack_to_disk(&updated_pack)?;
Ok(())
}
#[cfg(test)]
mod tests {
use std::collections::HashSet;
use crate::packs::checker::{
CheckAllResult, Violation, ViolationIdentifier,
};
#[test]
fn test_write_violations() {
let chec_result = CheckAllResult {
reportable_violations: vec![
Violation {
message: "foo/bar/file1.rb:10:5\nPrivacy violation: `::Foo::PrivateClass` is private to `foo`, but referenced from `bar`".to_string(),
identifier: ViolationIdentifier {
violation_type: "Privacy".to_string(),
strict: false,
file: "foo/bar/file1.rb".to_string(),
constant_name: "::Foo::PrivateClass".to_string(),
referencing_pack_name: "bar".to_string(),
defining_pack_name: "foo".to_string(),
}
},
Violation {
message: "foo/bar/file2.rb:15:3\nDependency violation: `::Foo::AnotherClass` is not allowed to depend on `::Bar::SomeClass`".to_string(),
identifier: ViolationIdentifier {
violation_type: "Dependency".to_string(),
strict: false,
file: "foo/bar/file2.rb".to_string(),
constant_name: "::Foo::AnotherClass".to_string(),
referencing_pack_name: "foo".to_string(),
defining_pack_name: "bar".to_string(),
}
}
].iter().cloned().collect(),
stale_violations: Vec::new(),
strict_mode_violations: HashSet::new(),
};
let expected_output = "2 violation(s) detected:
foo/bar/file1.rb:10:5
Privacy violation: `::Foo::PrivateClass` is private to `foo`, but referenced from `bar`
foo/bar/file2.rb:15:3
Dependency violation: `::Foo::AnotherClass` is not allowed to depend on `::Bar::SomeClass`
";
let actual = format!("{}", chec_result);
assert_eq!(actual, expected_output);
}
}