-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfiguration.rs
459 lines (420 loc) · 16.9 KB
/
configuration.rs
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
use super::caching::{
cache::Cache, create_cache_dir_idempotently, noop_cache::NoopCache,
per_file_cache::PerFileCache,
};
use super::checker::layer::Layers;
use super::checker_configuration::{CheckerConfiguration, CheckerType};
use super::file_utils::user_inputted_paths_to_absolute_filepaths;
use super::raw_configuration::CheckerOverrides;
use super::{
constant_resolver::ConstantResolverConfiguration, raw_configuration,
raw_configuration::RawConfiguration, walk_directory,
walk_directory::WalkDirectoryResult, PackSet,
};
use std::collections::HashMap;
use std::{
collections::HashSet,
path::{Path, PathBuf},
};
use tracing::debug;
use walk_directory::walk_directory;
pub struct Configuration {
pub included_files: HashSet<PathBuf>,
pub absolute_root: PathBuf,
pub cache_enabled: bool,
pub cache_directory: PathBuf,
pub pack_set: PackSet,
pub layers: Layers,
pub experimental_parser: bool,
pub ignored_definitions: HashMap<String, HashSet<PathBuf>>,
pub autoload_roots: HashMap<PathBuf, String>,
pub inflections_path: PathBuf,
pub custom_associations: Vec<String>,
pub stdin_file_path: Option<PathBuf>,
// Note that it'd probably be better to use the logger library, `tracing` (see logger.rs)
// and configure logging in one place. As the complexity of how/why we want to see different logs
// grows, we can refactor this.
pub print_files: bool,
pub packs_first_mode: bool,
pub ignore_recorded_violations: bool,
pub disable_enforce_dependencies: bool,
pub disable_enforce_folder_privacy: bool,
pub disable_enforce_layers: bool,
pub disable_enforce_privacy: bool,
pub disable_enforce_visibility: bool,
pub checker_configuration: HashMap<CheckerType, CheckerConfiguration>,
}
impl Configuration {
pub(crate) fn intersect_files(
&self,
input_files: Vec<String>,
) -> HashSet<PathBuf> {
if input_files.is_empty() {
self.included_files.clone()
} else {
let absolute_filepaths = user_inputted_paths_to_absolute_filepaths(
&self.absolute_root,
input_files,
);
self.included_files
.intersection(&absolute_filepaths)
.cloned()
.collect::<HashSet<PathBuf>>()
}
}
pub(crate) fn get_cache(&self) -> Box<dyn Cache + Send + Sync> {
if self.cache_enabled {
let cache_dir = if self.experimental_parser {
self.cache_directory.join("experimental")
} else {
self.cache_directory.join("zeitwerk")
};
create_cache_dir_idempotently(&cache_dir);
Box::new(PerFileCache { cache_dir })
} else {
Box::new(NoopCache {})
}
}
pub(crate) fn constant_resolver_configuration(
&self,
) -> ConstantResolverConfiguration {
ConstantResolverConfiguration {
absolute_root: &self.absolute_root,
cache_directory: &self.cache_directory,
cache_enabled: self.cache_enabled,
autoload_roots: &self.autoload_roots,
inflections_path: &self.inflections_path,
}
}
}
pub(crate) fn get(absolute_root: &Path) -> anyhow::Result<Configuration> {
debug!("Beginning to build configuration");
let raw_config = raw_configuration::get(absolute_root)?;
let walk_directory_result =
walk_directory(absolute_root.to_path_buf(), &raw_config)?;
from_raw(absolute_root, raw_config, walk_directory_result)
}
pub(crate) fn from_raw(
absolute_root: &Path,
raw_config: RawConfiguration,
walk_directory_result: WalkDirectoryResult,
) -> anyhow::Result<Configuration> {
let WalkDirectoryResult {
included_files,
included_packs,
owning_package_yml_for_file,
} = walk_directory_result;
let absolute_root = absolute_root.to_path_buf();
let pack_set = PackSet::build(included_packs, owning_package_yml_for_file)?;
let cache_directory = absolute_root.join(raw_config.cache_directory);
let cache_enabled = raw_config.cache;
let experimental_parser = raw_config.experimental_parser;
let layers = Layers {
layers: raw_config.layers,
};
let ignored_definitions = raw_config.ignored_definitions;
let autoload_roots: HashMap<PathBuf, String> = raw_config.autoload_roots;
let packs_first_mode = raw_config.packs_first_mode;
let inflections_path = absolute_root.join(
raw_config
.inflections_path
.unwrap_or(PathBuf::from("config/initializers/inflections.rb")),
);
let custom_associations = raw_config
.custom_associations
.iter()
// In packwerk, custom_associations are an array of symbols. We strip the leading : so this configuration is compatible with the rust implementation.
.map(|a| a.trim_start_matches(':').to_owned())
.collect();
let violation_checker_configuration = build_violation_checker_configuration(
raw_config.checker_overrides.as_ref(),
);
debug!("Finished building configuration");
Ok(Configuration {
included_files,
absolute_root,
cache_enabled,
cache_directory,
pack_set,
layers,
experimental_parser,
ignored_definitions,
autoload_roots,
inflections_path,
custom_associations,
stdin_file_path: None,
print_files: false,
packs_first_mode,
ignore_recorded_violations: false,
disable_enforce_dependencies: false,
disable_enforce_folder_privacy: false,
disable_enforce_layers: false,
disable_enforce_privacy: false,
disable_enforce_visibility: false,
checker_configuration: violation_checker_configuration,
})
}
fn build_violation_checker_configuration(
violation_checker_overrides: Option<&CheckerOverrides>,
) -> HashMap<CheckerType, CheckerConfiguration> {
let mut checker_configurations = HashMap::new();
let mut folder_privacy_checker_configuration =
CheckerConfiguration::new(CheckerType::FolderPrivacy);
let mut privacy_checker_configuration =
CheckerConfiguration::new(CheckerType::Privacy);
let mut dependency_checker_configuration =
CheckerConfiguration::new(CheckerType::Dependency);
let mut layer_checker_configuration =
CheckerConfiguration::new(CheckerType::Layer);
let mut visibility_checker_configuration =
CheckerConfiguration::new(CheckerType::Visibility);
if let Some(violation_checker_overrides) = violation_checker_overrides {
if let Some(error_template) = violation_checker_overrides
.folder_privacy_error_template
.clone()
{
folder_privacy_checker_configuration.override_error_template =
Some(error_template);
}
if let Some(error_template) =
violation_checker_overrides.privacy_error_template.clone()
{
privacy_checker_configuration.override_error_template =
Some(error_template);
}
if let Some(error_template) =
violation_checker_overrides.layer_error_template.clone()
{
layer_checker_configuration.override_error_template =
Some(error_template);
}
if let Some(error_template) = violation_checker_overrides
.visibility_error_template
.clone()
{
visibility_checker_configuration.override_error_template =
Some(error_template);
}
if let Some(error_template) = violation_checker_overrides
.dependency_error_template
.clone()
{
dependency_checker_configuration.override_error_template =
Some(error_template);
}
}
checker_configurations.insert(
CheckerType::FolderPrivacy,
folder_privacy_checker_configuration,
);
checker_configurations
.insert(CheckerType::Dependency, dependency_checker_configuration);
checker_configurations
.insert(CheckerType::Privacy, privacy_checker_configuration);
checker_configurations
.insert(CheckerType::Layer, layer_checker_configuration);
checker_configurations
.insert(CheckerType::Visibility, visibility_checker_configuration);
checker_configurations
}
#[cfg(test)]
mod tests {
use super::*;
use crate::packs::{
configuration,
pack::{CheckerSetting, Pack},
PackageTodo,
};
use pretty_assertions::assert_eq;
#[test]
fn default_options() {
let absolute_root = PathBuf::from("tests/fixtures/simple_app");
let actual = configuration::get(&absolute_root).unwrap();
assert_eq!(actual.absolute_root, absolute_root);
let expected_included_files = vec![
absolute_root.join("frontend/ui_helper.rb"),
absolute_root.join("packs/bar/app/services/bar.rb"),
absolute_root.join("packs/foo/app/services/foo.rb"),
absolute_root.join("packs/foo/app/services/foo/bar.rb"),
absolute_root.join("packs/foo/app/views/foo.erb"),
absolute_root.join("packs/baz/app/services/baz.rb"),
absolute_root.join("packs/bar/app/models/concerns/some_concern.rb"),
absolute_root.join("app/services/some_root_class.rb"),
absolute_root.join("app/company_data/widget.rb"),
]
.into_iter()
.collect::<HashSet<PathBuf>>();
assert_eq!(actual.included_files, expected_included_files);
let expected_packs = vec![
Pack {
enforce_dependencies: None,
enforce_privacy: Some(CheckerSetting::True),
enforce_visibility: None,
enforce_folder_privacy: None,
enforce_folder_visibility: None,
enforce_layers: None,
owner: None,
yml: absolute_root.join("packs/bar/package.yml"),
name: String::from("packs/bar"),
relative_path: PathBuf::from("packs/bar"),
dependencies: HashSet::new(),
visible_to: None,
package_todo: PackageTodo::default(),
ignored_dependencies: HashSet::new(),
ignored_private_constants: HashSet::new(),
private_constants: HashSet::new(),
public_folder: None,
layer: None,
client_keys: HashMap::new(),
enforcement_globs_ignore: None,
},
Pack {
enforce_dependencies: None,
enforce_privacy: None,
enforce_visibility: None,
enforce_folder_privacy: None,
enforce_folder_visibility: None,
enforce_layers: None,
owner: None,
yml: absolute_root.join("packs/baz/package.yml"),
name: String::from("packs/baz"),
relative_path: PathBuf::from("packs/baz"),
dependencies: HashSet::new(),
visible_to: None,
package_todo: PackageTodo::default(),
ignored_dependencies: HashSet::new(),
ignored_private_constants: HashSet::new(),
private_constants: HashSet::new(),
public_folder: None,
layer: None,
client_keys: HashMap::new(),
enforcement_globs_ignore: None,
},
Pack {
enforce_dependencies: Some(CheckerSetting::True),
enforce_privacy: Some(CheckerSetting::True),
enforce_visibility: None,
enforce_folder_privacy: None,
enforce_folder_visibility: None,
enforce_layers: None,
owner: None,
yml: absolute_root.join("packs/foo/package.yml"),
name: String::from("packs/foo"),
relative_path: PathBuf::from("packs/foo"),
dependencies: HashSet::from_iter(vec![String::from(
"packs/baz",
)]),
visible_to: None,
package_todo: PackageTodo::default(),
ignored_dependencies: HashSet::new(),
ignored_private_constants: HashSet::new(),
private_constants: HashSet::new(),
public_folder: None,
layer: None,
client_keys: HashMap::new(),
enforcement_globs_ignore: None,
},
Pack {
enforce_dependencies: None,
enforce_privacy: None,
enforce_visibility: None,
enforce_folder_privacy: None,
enforce_folder_visibility: None,
enforce_layers: None,
owner: None,
yml: absolute_root.join("package.yml"),
name: String::from("."),
relative_path: PathBuf::from("."),
dependencies: HashSet::new(),
visible_to: None,
package_todo: PackageTodo::default(),
ignored_dependencies: HashSet::new(),
ignored_private_constants: HashSet::new(),
private_constants: HashSet::new(),
public_folder: None,
layer: None,
client_keys: HashMap::new(),
enforcement_globs_ignore: None,
},
];
assert_eq!(expected_packs, actual.pack_set.packs);
assert!(!actual.cache_enabled)
}
#[test]
fn filtered_absolute_paths_with_nonempty_input_paths() {
let absolute_root = PathBuf::from("tests/fixtures/simple_app");
let configuration = configuration::get(&absolute_root).unwrap();
let actual_paths = configuration.intersect_files(vec![
String::from("packs/foo/app/services/foo.rb"),
String::from("scripts/my_script.rb"),
String::from("packs/bar/app/services/bar.rb"),
String::from("vendor/some_gem/foo.rb"),
]);
let expected_paths = vec![
absolute_root.join("packs/bar/app/services/bar.rb"),
absolute_root.join("packs/foo/app/services/foo.rb"),
]
.into_iter()
.collect::<HashSet<PathBuf>>();
assert_eq!(actual_paths, expected_paths);
}
#[test]
fn filtered_absolute_paths_with_empty_input_paths() {
let absolute_root = PathBuf::from("tests/fixtures/simple_app");
let configuration = configuration::get(&absolute_root).unwrap();
let actual_paths = configuration.intersect_files(vec![]);
let expected_paths = vec![
absolute_root.join("frontend/ui_helper.rb"),
absolute_root.join("packs/bar/app/services/bar.rb"),
absolute_root.join("packs/foo/app/services/foo.rb"),
absolute_root.join("packs/foo/app/services/foo/bar.rb"),
absolute_root.join("packs/foo/app/views/foo.erb"),
absolute_root.join("packs/baz/app/services/baz.rb"),
absolute_root.join("packs/bar/app/models/concerns/some_concern.rb"),
absolute_root.join("app/services/some_root_class.rb"),
absolute_root.join("app/company_data/widget.rb"),
]
.into_iter()
.collect::<HashSet<PathBuf>>();
assert_eq!(actual_paths, expected_paths);
}
#[test]
fn filtered_absolute_paths_with_directory_input_paths() {
let absolute_root = PathBuf::from("tests/fixtures/simple_app");
let configuration = configuration::get(&absolute_root).unwrap();
let actual_paths =
configuration.intersect_files(vec![String::from("packs/bar")]);
let expected_paths = vec![
absolute_root.join("packs/bar/app/services/bar.rb"),
absolute_root.join("packs/bar/app/models/concerns/some_concern.rb"),
]
.into_iter()
.collect::<HashSet<PathBuf>>();
assert_eq!(actual_paths, expected_paths);
}
#[test]
fn with_symbols_as_custom_associations() {
let absolute_root = PathBuf::from("tests/fixtures/simple_app");
let raw = RawConfiguration {
custom_associations: vec![":my_association".to_owned()],
..RawConfiguration::default()
};
let included_packs: HashSet<Pack> = vec![Pack {
name: String::from("."),
..Pack::default()
}]
.into_iter()
.collect();
let walk_directory_result = WalkDirectoryResult {
included_files: Default::default(),
included_packs,
owning_package_yml_for_file: Default::default(),
};
let configuration =
configuration::from_raw(&absolute_root, raw, walk_directory_result)
.unwrap();
let actual_associations = configuration.custom_associations;
let expected_paths = vec!["my_association".to_owned()];
assert_eq!(actual_associations, expected_paths);
}
}