Skip to content

Commit 2fc0f8f

Browse files
committed
Add test fixtures and specs for pks JSON integration
Create JSON fixture files representing pks check output for various violation scenarios (privacy, dependency, multiple, strict mode). Extend pks_offense_spec with fixture-based tests to verify parsing. Add pks_wrapper_spec with test patterns ready for PksWrapper impl. All 90 existing tests continue to pass.
1 parent 74be2e3 commit 2fc0f8f

File tree

6 files changed

+179
-0
lines changed

6 files changed

+179
-0
lines changed

spec/danger_packwerk/pks_offense_spec.rb

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module DangerPackwerk
44
RSpec.describe PksOffense do
55
# PksOffense doesn't need Danger plugin context, but spec_helper includes it
66
let(:plugin) { dangerfile.packwerk }
7+
let(:fixtures_path) { File.join(__dir__, '..', 'fixtures', 'pks_output') }
78
let(:privacy_offense_hash) do
89
{
910
'violation_type' => 'privacy',
@@ -152,5 +153,98 @@ module DangerPackwerk
152153
expect(hash[offense1]).to eq('second')
153154
end
154155
end
156+
157+
describe 'fixture-based tests' do
158+
describe 'no_violations.json' do
159+
let(:json) { File.read(File.join(fixtures_path, 'no_violations.json')) }
160+
161+
it 'parses empty offenses array' do
162+
offenses = described_class.from_json(json)
163+
expect(offenses).to eq([])
164+
end
165+
end
166+
167+
describe 'privacy_violation.json' do
168+
let(:json) { File.read(File.join(fixtures_path, 'privacy_violation.json')) }
169+
170+
it 'parses a single privacy violation' do
171+
offenses = described_class.from_json(json)
172+
173+
expect(offenses.length).to eq(1)
174+
offense = offenses.first
175+
expect(offense.violation_type).to eq('privacy')
176+
expect(offense.file).to eq('packs/my_pack/app/models/user.rb')
177+
expect(offense.line).to eq(42)
178+
expect(offense.column).to eq(10)
179+
expect(offense.constant_name).to eq('::OtherPack::PrivateClass')
180+
expect(offense.referencing_pack_name).to eq('packs/my_pack')
181+
expect(offense.defining_pack_name).to eq('packs/other_pack')
182+
expect(offense.strict).to eq(false)
183+
expect(offense.privacy?).to eq(true)
184+
expect(offense.dependency?).to eq(false)
185+
end
186+
end
187+
188+
describe 'dependency_violation.json' do
189+
let(:json) { File.read(File.join(fixtures_path, 'dependency_violation.json')) }
190+
191+
it 'parses a single dependency violation with strict mode' do
192+
offenses = described_class.from_json(json)
193+
194+
expect(offenses.length).to eq(1)
195+
offense = offenses.first
196+
expect(offense.violation_type).to eq('dependency')
197+
expect(offense.file).to eq('packs/my_pack/app/services/user_service.rb')
198+
expect(offense.line).to eq(15)
199+
expect(offense.column).to eq(5)
200+
expect(offense.constant_name).to eq('::ThirdPack::SomeConstant')
201+
expect(offense.referencing_pack_name).to eq('packs/my_pack')
202+
expect(offense.defining_pack_name).to eq('packs/third_pack')
203+
expect(offense.strict).to eq(true)
204+
expect(offense.privacy?).to eq(false)
205+
expect(offense.dependency?).to eq(true)
206+
end
207+
end
208+
209+
describe 'multiple_violations.json' do
210+
let(:json) { File.read(File.join(fixtures_path, 'multiple_violations.json')) }
211+
212+
it 'parses multiple violations of different types' do
213+
offenses = described_class.from_json(json)
214+
215+
expect(offenses.length).to eq(3)
216+
217+
privacy_offenses = offenses.select(&:privacy?)
218+
dependency_offenses = offenses.select(&:dependency?)
219+
220+
expect(privacy_offenses.length).to eq(2)
221+
expect(dependency_offenses.length).to eq(1)
222+
end
223+
224+
it 'preserves all offense details' do
225+
offenses = described_class.from_json(json)
226+
227+
files = offenses.map(&:file)
228+
expect(files).to contain_exactly(
229+
'packs/my_pack/app/models/user.rb',
230+
'packs/my_pack/app/services/user_service.rb',
231+
'packs/another_pack/app/models/order.rb'
232+
)
233+
end
234+
end
235+
236+
describe 'strict_mode_violation.json' do
237+
let(:json) { File.read(File.join(fixtures_path, 'strict_mode_violation.json')) }
238+
239+
it 'parses strict mode privacy violation' do
240+
offenses = described_class.from_json(json)
241+
242+
expect(offenses.length).to eq(1)
243+
offense = offenses.first
244+
expect(offense.strict).to eq(true)
245+
expect(offense.privacy?).to eq(true)
246+
end
247+
end
248+
end
155249
end
156250
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"offenses": [
3+
{
4+
"violation_type": "dependency",
5+
"file": "packs/my_pack/app/services/user_service.rb",
6+
"line": 15,
7+
"column": 5,
8+
"constant_name": "::ThirdPack::SomeConstant",
9+
"referencing_pack_name": "packs/my_pack",
10+
"defining_pack_name": "packs/third_pack",
11+
"strict": true,
12+
"message": "Dependency violation: packs/my_pack does not declare packs/third_pack as a dependency"
13+
}
14+
]
15+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"offenses": [
3+
{
4+
"violation_type": "privacy",
5+
"file": "packs/my_pack/app/models/user.rb",
6+
"line": 42,
7+
"column": 10,
8+
"constant_name": "::OtherPack::PrivateClass",
9+
"referencing_pack_name": "packs/my_pack",
10+
"defining_pack_name": "packs/other_pack",
11+
"strict": false,
12+
"message": "Privacy violation: ::OtherPack::PrivateClass is private to packs/other_pack"
13+
},
14+
{
15+
"violation_type": "dependency",
16+
"file": "packs/my_pack/app/services/user_service.rb",
17+
"line": 15,
18+
"column": 5,
19+
"constant_name": "::ThirdPack::SomeConstant",
20+
"referencing_pack_name": "packs/my_pack",
21+
"defining_pack_name": "packs/third_pack",
22+
"strict": true,
23+
"message": "Dependency violation: packs/my_pack does not declare packs/third_pack as a dependency"
24+
},
25+
{
26+
"violation_type": "privacy",
27+
"file": "packs/another_pack/app/models/order.rb",
28+
"line": 88,
29+
"column": 15,
30+
"constant_name": "::OtherPack::SecretHelper",
31+
"referencing_pack_name": "packs/another_pack",
32+
"defining_pack_name": "packs/other_pack",
33+
"strict": false,
34+
"message": "Privacy violation: ::OtherPack::SecretHelper is private to packs/other_pack"
35+
}
36+
]
37+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"offenses": []
3+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"offenses": [
3+
{
4+
"violation_type": "privacy",
5+
"file": "packs/my_pack/app/models/user.rb",
6+
"line": 42,
7+
"column": 10,
8+
"constant_name": "::OtherPack::PrivateClass",
9+
"referencing_pack_name": "packs/my_pack",
10+
"defining_pack_name": "packs/other_pack",
11+
"strict": false,
12+
"message": "Privacy violation: ::OtherPack::PrivateClass is private to packs/other_pack"
13+
}
14+
]
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"offenses": [
3+
{
4+
"violation_type": "privacy",
5+
"file": "packs/strict_pack/app/services/api_client.rb",
6+
"line": 23,
7+
"column": 8,
8+
"constant_name": "::CorePack::InternalApi",
9+
"referencing_pack_name": "packs/strict_pack",
10+
"defining_pack_name": "packs/core_pack",
11+
"strict": true,
12+
"message": "Privacy violation (strict mode): ::CorePack::InternalApi is private to packs/core_pack"
13+
}
14+
]
15+
}

0 commit comments

Comments
 (0)