Skip to content

Commit cd2540e

Browse files
author
Alex Evanczuk
authored
Fix bug with ignored_private_constants in a package.yml failing validation (#28)
* write failing test * renamespace test * make test pass * bump version
1 parent 880bfd3 commit cd2540e

4 files changed

Lines changed: 120 additions & 111 deletions

File tree

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ GIT
1717
PATH
1818
remote: .
1919
specs:
20-
packwerk-extensions (0.1.6)
20+
packwerk-extensions (0.1.7)
2121
packwerk (>= 2.2.1)
2222
railties (>= 6.0.0)
2323
sorbet-runtime

lib/packwerk/privacy/validator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def call(package_set, configuration)
3131

3232
sig { override.returns(T::Array[String]) }
3333
def permitted_keys
34-
%w[public_path enforce_privacy private_constants]
34+
%w[public_path enforce_privacy private_constants ignored_private_constants]
3535
end
3636

3737
private

packwerk-extensions.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |spec|
22
spec.name = 'packwerk-extensions'
3-
spec.version = '0.1.6'
3+
spec.version = '0.1.7'
44
spec.authors = ['Gusto Engineers']
55
spec.email = ['dev@gusto.com']
66

test/unit/privacy/validator_test.rb

Lines changed: 117 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -7,114 +7,123 @@
77
require 'fixtures/skeleton/components/timeline/app/models/private_thing'
88

99
module Packwerk
10-
class ValidatorTest < Minitest::Test
11-
extend T::Sig
12-
include ApplicationFixtureHelper
13-
include RailsApplicationFixtureHelper
14-
15-
setup do
16-
setup_application_fixture
17-
end
18-
19-
teardown do
20-
teardown_application_fixture
21-
end
22-
23-
test 'check_all returns an error for invalid enforce_privacy value' do
24-
use_template(:minimal)
25-
merge_into_app_yaml_file('package.yml', { 'enforce_privacy' => 'yes, please.' })
26-
27-
result = Packwerk::Privacy::Validator.new.call(package_set, config)
28-
29-
refute result.ok?
30-
assert_match(/Invalid 'enforce_privacy' option/, result.error_value)
31-
end
32-
33-
test 'check_all returns success for when enforce_privacy is set to strict' do
34-
use_template(:minimal)
35-
merge_into_app_yaml_file('package.yml', { 'enforce_privacy' => 'strict' })
36-
37-
result = Packwerk::Privacy::Validator.new.call(package_set, config)
38-
39-
assert result.ok?
40-
end
41-
42-
test 'check_all returns success when inflector defines acronym' do
43-
use_template(:skeleton)
44-
45-
result = Packwerk::Privacy::Validator.new.call(package_set, config)
46-
47-
assert result.ok?
48-
assert_nil result.error_value
49-
end
50-
51-
test 'check_all returns an error for invalid public_path value' do
52-
use_template(:minimal)
53-
merge_into_app_yaml_file('package.yml', { 'public_path' => [] })
54-
55-
result = Packwerk::Privacy::Validator.new.call(package_set, config)
56-
57-
refute result.ok?
58-
assert_match(/'public_path' option must be a string/, result.error_value)
59-
end
60-
61-
test 'check_package_manifests_for_privacy returns an error for unresolvable privatized constants' do
62-
use_template(:skeleton)
63-
ConstantResolver.expects(:new).returns(stub('resolver', resolve: nil))
64-
65-
result = Packwerk::Privacy::Validator.new.call(package_set, config)
66-
refute result.ok?, result.error_value
67-
assert_match(
68-
/'::PrivateThing', listed in #{to_app_path('components\/timeline\/package.yml')}, could not be resolved/,
69-
result.error_value
70-
)
71-
assert_match(
72-
/Add a private_thing.rb file/,
73-
result.error_value
74-
)
75-
end
76-
77-
test 'check_package_manifests_for_privacy returns an error for privatized constants in other packages' do
78-
use_template(:skeleton)
79-
context = ConstantResolver::ConstantContext.new('::PrivateThing', 'private_thing.rb')
80-
81-
ConstantResolver.expects(:new).returns(stub('resolver', resolve: context))
82-
83-
result = Packwerk::Privacy::Validator.new.call(package_set, config)
84-
85-
refute result.ok?, result.error_value
86-
assert_match(
87-
%r{'::PrivateThing' is declared as private in the 'components/timeline' package},
88-
result.error_value
89-
)
90-
assert_match(
91-
/but appears to be defined\sin the '.' package/,
92-
result.error_value
93-
)
94-
end
95-
96-
test 'check_package_manifests_for_privacy returns an error for constants without `::` prefix' do
97-
use_template(:minimal)
98-
merge_into_app_yaml_file('package.yml', { 'private_constants' => ['::PrivateThing', 'OtherThing'] })
99-
100-
result = Packwerk::Privacy::Validator.new.call(package_set, config)
101-
102-
refute result.ok?, result.error_value
103-
assert_match(
104-
/'OtherThing', listed in the 'private_constants' option in .*package.yml, is invalid./,
105-
result.error_value
106-
)
107-
assert_match(
108-
/Private constants need to be prefixed with the top-level namespace operator `::`/,
109-
result.error_value
110-
)
111-
end
112-
113-
private
114-
115-
sig { returns(Packwerk::ApplicationValidator) }
116-
def validator
117-
@validator ||= Packwerk::ApplicationValidator.new
10+
module Privacy
11+
class ValidatorTest < Minitest::Test
12+
extend T::Sig
13+
include ApplicationFixtureHelper
14+
include RailsApplicationFixtureHelper
15+
16+
setup do
17+
setup_application_fixture
18+
end
19+
20+
teardown do
21+
teardown_application_fixture
22+
end
23+
24+
test 'check_all returns an error for invalid enforce_privacy value' do
25+
use_template(:minimal)
26+
merge_into_app_yaml_file('package.yml', { 'enforce_privacy' => 'yes, please.' })
27+
28+
result = Packwerk::Privacy::Validator.new.call(package_set, config)
29+
30+
refute result.ok?
31+
assert_match(/Invalid 'enforce_privacy' option/, result.error_value)
32+
end
33+
34+
test 'check_all returns success for when enforce_privacy is set to strict' do
35+
use_template(:minimal)
36+
merge_into_app_yaml_file('package.yml', { 'enforce_privacy' => 'strict' })
37+
38+
result = Packwerk::Privacy::Validator.new.call(package_set, config)
39+
40+
assert result.ok?
41+
end
42+
43+
test 'check_all returns success when inflector defines acronym' do
44+
use_template(:skeleton)
45+
46+
result = Packwerk::Privacy::Validator.new.call(package_set, config)
47+
48+
assert result.ok?
49+
assert_nil result.error_value
50+
end
51+
52+
test 'check_all returns an error for invalid public_path value' do
53+
use_template(:minimal)
54+
merge_into_app_yaml_file('package.yml', { 'public_path' => [] })
55+
56+
result = Packwerk::Privacy::Validator.new.call(package_set, config)
57+
58+
refute result.ok?
59+
assert_match(/'public_path' option must be a string/, result.error_value)
60+
end
61+
62+
test 'check_package_manifests_for_privacy returns an error for unresolvable privatized constants' do
63+
use_template(:skeleton)
64+
ConstantResolver.expects(:new).returns(stub('resolver', resolve: nil))
65+
66+
result = Packwerk::Privacy::Validator.new.call(package_set, config)
67+
refute result.ok?, result.error_value
68+
assert_match(
69+
/'::PrivateThing', listed in #{to_app_path('components\/timeline\/package.yml')}, could not be resolved/,
70+
result.error_value
71+
)
72+
assert_match(
73+
/Add a private_thing.rb file/,
74+
result.error_value
75+
)
76+
end
77+
78+
test 'check_package_manifests_for_privacy returns an error for privatized constants in other packages' do
79+
use_template(:skeleton)
80+
context = ConstantResolver::ConstantContext.new('::PrivateThing', 'private_thing.rb')
81+
82+
ConstantResolver.expects(:new).returns(stub('resolver', resolve: context))
83+
84+
result = Packwerk::Privacy::Validator.new.call(package_set, config)
85+
86+
refute result.ok?, result.error_value
87+
assert_match(
88+
%r{'::PrivateThing' is declared as private in the 'components/timeline' package},
89+
result.error_value
90+
)
91+
assert_match(
92+
/but appears to be defined\sin the '.' package/,
93+
result.error_value
94+
)
95+
end
96+
97+
test 'check_package_manifests_for_privacy returns an error for constants without `::` prefix' do
98+
use_template(:minimal)
99+
merge_into_app_yaml_file('package.yml', { 'private_constants' => ['::PrivateThing', 'OtherThing'] })
100+
101+
result = Packwerk::Privacy::Validator.new.call(package_set, config)
102+
103+
refute result.ok?, result.error_value
104+
assert_match(
105+
/'OtherThing', listed in the 'private_constants' option in .*package.yml, is invalid./,
106+
result.error_value
107+
)
108+
assert_match(
109+
/Private constants need to be prefixed with the top-level namespace operator `::`/,
110+
result.error_value
111+
)
112+
end
113+
114+
test 'does not create a validation error when using ignored_private_constants' do
115+
use_template(:minimal)
116+
merge_into_app_yaml_file('package.yml', { 'ignored_private_constants' => ['packs/my_other_pack'] })
117+
result = validator.call(package_set, config)
118+
assert result.ok?
119+
end
120+
121+
private
122+
123+
sig { returns(ApplicationValidator) }
124+
def validator
125+
@validator ||= ApplicationValidator.new
126+
end
118127
end
119128
end
120129
end

0 commit comments

Comments
 (0)