Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/packs/checker/privacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ impl CheckerInterface for Checker {
&format!("{}::", private_constant);
reference.constant_name.starts_with(namespaced_constant)
});
dbg!(constant_is_private, constant_is_in_private_namespace);
if !constant_is_private && !constant_is_in_private_namespace {
return Ok(None);
}
Expand Down
33 changes: 33 additions & 0 deletions tests/check_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,39 @@ fn test_check_with_privacy_dependency_error_template_overrides(
common::teardown();
Ok(())
}
#[test]
fn test_check_with_private_constants() -> Result<(), Box<dyn Error>> {
let output = cargo_bin_cmd!("pks")
.arg("--project-root")
.arg("tests/fixtures/privacy_violations_with_private_constants")
.arg("check")
.assert()
.code(1)
.get_output()
.clone();

let stripped_stdout = stripped_output(output.stdout);

// A non-empty `private_constants` list narrows privacy to that list: the listed
// constant and its namespace are private, everything else in the pack is public.
assert!(stripped_stdout.contains("2 violation(s) detected:"));
assert!(stripped_stdout.contains("packs/foo/app/services/foo.rb:3:4\nPrivacy violation: `::Bar` is private to `packs/bar`, but referenced from `packs/foo`"));
assert!(stripped_stdout.contains("packs/foo/app/services/foo.rb:7:4\nPrivacy violation: `::Bar::Inner` is private to `packs/bar`, but referenced from `packs/foo`"));
assert!(!stripped_stdout.contains("::SomeConcern"));

// Regression guard: this branch once carried a live `dbg!`, which wrote a pair of
// lines to stderr for every reference checked against `private_constants`.
let stripped_stderr = stripped_output(output.stderr);
assert_eq!(
stripped_stderr, "",
"expected no stderr output, got:\n{}",
stripped_stderr
);

common::teardown();
Ok(())
}

#[test]
fn test_check() -> Result<(), Box<dyn Error>> {
let output = cargo_bin_cmd!("pks")
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# ::Bar is listed in private_constants, so referencing it is a violation.
module Bar
def bar; end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# ::Bar::Inner is not listed itself, but sits inside the private ::Bar namespace,
# so referencing it is also a violation.
module Bar
module Inner
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# ::SomeConcern is defined in packs/bar but is absent from private_constants.
# A non-empty private_constants list makes everything outside it public, so
# referencing this is NOT a violation.
module SomeConcern; end
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
enforce_privacy: true
private_constants:
- "::Bar"
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Foo
def references_a_private_constant
::Bar
end

def references_a_constant_in_the_private_namespace
::Bar::Inner
end

def references_a_constant_left_public_by_omission
::SomeConcern
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Whether or not you want the cache enabled (disabled by default)
cache: false
Loading