|
7 | 7 | require 'fixtures/skeleton/components/timeline/app/models/private_thing' |
8 | 8 |
|
9 | 9 | 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 |
118 | 127 | end |
119 | 128 | end |
120 | 129 | end |
0 commit comments