Skip to content

Commit ea87ed9

Browse files
authored
Checker error template overrides (#2)
* adding checker error template overrides * bumping version * adding error template overrides description
1 parent 0261a10 commit ea87ed9

File tree

42 files changed

+318
-21
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+318
-21
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[package]
44
name = "pks"
5-
version = "0.2.16"
5+
version = "0.2.17"
66
edition = "2021"
77
description = "Welcome! Please see https://github.com/rubyatscale/pks for more information!"
88
license = "MIT"

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Instructions:
8282
- Confirm the output of `git diff` is empty
8383
- Please file an issue if it's not!
8484

85+
8586
# New to Rust?
8687
Me too! This is my first Rust project, so I'd love to have feedback, advice, and contributions!
8788

@@ -155,6 +156,32 @@ enforcement_globs_ignore:
155156
reason: "The other dependency violations are fine as those packs will be absorbed into this one."
156157
```
157158
159+
## "check" error messages
160+
The error messages resulting from running `pks check` can be customized with mustache-style interpolation. The available
161+
variables are:
162+
- violation_name
163+
- referencing_pack_name
164+
- defining_pack_name
165+
- constant_name
166+
- reference_location
167+
- referencing_pack_relative_yml
168+
169+
Layer violations also have
170+
- defining_layer
171+
- referencing_layer
172+
173+
Example:
174+
packwerk.yml
175+
```yml
176+
checker_overrides:
177+
folder_privacy_error_template: "{{reference_location}} {{violation_name}} / Product Service Privacy Violation: `{{constant_name}}` belongs to the `{{defining_pack_name}}` product service, which is not visible to `{{referencing_pack_name}}` as it is a different product service. See https://go/pks-folder-privacy"
178+
layer_error_template: "{{reference_location}}Layer violation: `{{constant_name}}` belongs to `{{defining_pack_name}}` (whose layer is `{{defining_layer}}`) cannot be accessed from `{{referencing_pack_name}}` (whose layer is `{{referencing_layer}}`). See https://go/pks-layer"
179+
visibility_error_template: "{{reference_location}}Visibility violation: `{{constant_name}}` belongs to `{{defining_pack_name}}`, which is not visible to `{{referencing_pack_name}}`. See https://go/pks-visibility"
180+
privacy_error_template: "{{reference_location}}Privacy violation: `{{constant_name}}` is private to `{{defining_pack_name}}`, but referenced from `{{referencing_pack_name}}`. See https://go/pks-privacy"
181+
dependency_error_template: "{{reference_location}}Dependency violation: `{{constant_name}}` belongs to `{{defining_pack_name}}`, but `{{referencing_pack_relative_yml}}` does not specify a dependency on `{{defining_pack_name}}`. See https://go/pks-dependency"
182+
```
183+
184+
158185
# Benchmarks
159186
See [BENCHMARKS.md](https://github.com/rubyatscale/pks/blob/main/BENCHMARKS.md)
160187

src/packs/configuration.rs

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -180,35 +180,64 @@ fn build_violation_checker_configuration(
180180
violation_checker_overrides: Option<&CheckerOverrides>,
181181
) -> HashMap<CheckerType, CheckerConfiguration> {
182182
let mut checker_configurations = HashMap::new();
183-
checker_configurations.insert(
184-
CheckerType::Dependency,
185-
CheckerConfiguration::new(CheckerType::Dependency),
186-
);
187-
checker_configurations.insert(
188-
CheckerType::Privacy,
189-
CheckerConfiguration::new(CheckerType::Privacy),
190-
);
191-
checker_configurations.insert(
192-
CheckerType::Layer,
193-
CheckerConfiguration::new(CheckerType::Layer),
194-
);
195-
checker_configurations.insert(
196-
CheckerType::Visibility,
197-
CheckerConfiguration::new(CheckerType::Visibility),
198-
);
199-
let mut checker_configuration =
183+
let mut folder_privacy_checker_configuration =
200184
CheckerConfiguration::new(CheckerType::FolderPrivacy);
185+
let mut privacy_checker_configuration =
186+
CheckerConfiguration::new(CheckerType::Privacy);
187+
let mut dependency_checker_configuration =
188+
CheckerConfiguration::new(CheckerType::Dependency);
189+
let mut layer_checker_configuration =
190+
CheckerConfiguration::new(CheckerType::Layer);
191+
let mut visibility_checker_configuration =
192+
CheckerConfiguration::new(CheckerType::Visibility);
193+
201194
if let Some(violation_checker_overrides) = violation_checker_overrides {
202195
if let Some(error_template) = violation_checker_overrides
203196
.folder_privacy_error_template
204197
.clone()
205198
{
206-
checker_configuration.override_error_template =
199+
folder_privacy_checker_configuration.override_error_template =
200+
Some(error_template);
201+
}
202+
if let Some(error_template) =
203+
violation_checker_overrides.privacy_error_template.clone()
204+
{
205+
privacy_checker_configuration.override_error_template =
206+
Some(error_template);
207+
}
208+
if let Some(error_template) =
209+
violation_checker_overrides.layer_error_template.clone()
210+
{
211+
layer_checker_configuration.override_error_template =
212+
Some(error_template);
213+
}
214+
if let Some(error_template) = violation_checker_overrides
215+
.visibility_error_template
216+
.clone()
217+
{
218+
visibility_checker_configuration.override_error_template =
219+
Some(error_template);
220+
}
221+
if let Some(error_template) = violation_checker_overrides
222+
.dependency_error_template
223+
.clone()
224+
{
225+
dependency_checker_configuration.override_error_template =
207226
Some(error_template);
208227
}
209228
}
229+
checker_configurations.insert(
230+
CheckerType::FolderPrivacy,
231+
folder_privacy_checker_configuration,
232+
);
233+
checker_configurations
234+
.insert(CheckerType::Dependency, dependency_checker_configuration);
235+
checker_configurations
236+
.insert(CheckerType::Privacy, privacy_checker_configuration);
237+
checker_configurations
238+
.insert(CheckerType::Layer, layer_checker_configuration);
210239
checker_configurations
211-
.insert(CheckerType::FolderPrivacy, checker_configuration);
240+
.insert(CheckerType::Visibility, visibility_checker_configuration);
212241

213242
checker_configurations
214243
}

src/packs/raw_configuration.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ pub(crate) struct RawConfiguration {
8080
pub struct CheckerOverrides {
8181
/// Mustache style error message template
8282
pub folder_privacy_error_template: Option<String>,
83+
pub privacy_error_template: Option<String>,
84+
pub layer_error_template: Option<String>,
85+
pub visibility_error_template: Option<String>,
86+
pub dependency_error_template: Option<String>,
8387
}
8488

8589
pub(crate) fn get(absolute_root: &Path) -> anyhow::Result<RawConfiguration> {

tests/check_test.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,29 @@ pub fn stripped_output(output: Vec<u8>) -> String {
88
String::from_utf8_lossy(&strip_ansi_escapes::strip(output)).to_string()
99
}
1010

11+
#[test]
12+
fn test_check_with_privacy_dependency_error_template_overrides(
13+
) -> Result<(), Box<dyn Error>> {
14+
let output = Command::cargo_bin("pks")?
15+
.arg("--project-root")
16+
.arg("tests/fixtures/privacy_violation_overrides")
17+
.arg("--debug")
18+
.arg("check")
19+
.assert()
20+
.failure()
21+
.get_output()
22+
.stdout
23+
.clone();
24+
25+
let stripped_output = stripped_output(output);
26+
27+
assert!(stripped_output.contains("2 violation(s) detected:"));
28+
assert!(stripped_output.contains("packs/foo/app/services/foo.rb:3:4\nDependency violation: `::Bar` belongs to `packs/bar`, but `packs/foo/package.yml` does not specify a dependency on `packs/bar`. See https://go/pks-dependency"));
29+
assert!(stripped_output.contains("packs/foo/app/services/foo.rb:3:4\nPrivacy violation: `::Bar` is private to `packs/bar`, but referenced from `packs/foo`. See https://go/pks-privacy"));
30+
31+
common::teardown();
32+
Ok(())
33+
}
1134
#[test]
1235
fn test_check() -> Result<(), Box<dyn Error>> {
1336
let output = Command::cargo_bin("pks")?
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
ActiveSupport::Inflector.inflections do |do_not_couple_implementation_to_this_string|
2+
do_not_couple_implementation_to_this_string.acronym 'API'
3+
4+
# Using single vs double quotes inconsistently
5+
do_not_couple_implementation_to_this_string.acronym "CSV"
6+
end

tests/fixtures/layer_violations_with_overrides/package.yml

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# feature_flags, a utility pack, should not rely on Payments, a product pack
2+
Payments
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
enforce_layers: true
2+
layer: utilities
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module Payments
2+
end

0 commit comments

Comments
 (0)