Skip to content

Commit 0fdc6a9

Browse files
author
Alex Evanczuk
authored
Refactor tests into their own files (#36)
* move for_file for file annotations mapper into its own spec * move files have owners validation tests into their own file * move files have unique owners test into its own file * add note about failing test * move no overlapping globs validation into its own file * add note about validation tests * add package ownership spec * add team YML ownership spec * fill out team globs spec and prevent false positives * delete duplicate for_package spec, which is already present in spec/lib/code_ownership/private/ownership_mappers/package_ownership_spec.rb * move remove_file_annotation spec to file annotation mapper spec * fix bad indentation * move "application has invalid JSON in package" test to JsPackageOwnership spec * remove unnecessary test setup var * move JS package ownership for_file test to mapper spec
1 parent f211e2c commit 0fdc6a9

11 files changed

+1075
-866
lines changed
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
module CodeOwnership
2+
RSpec.describe Private::OwnershipMappers::FileAnnotations do
3+
describe '.for_team' do
4+
before do
5+
create_configuration
6+
write_file('config/teams/bar.yml', <<~CONTENTS)
7+
name: Bar
8+
CONTENTS
9+
10+
write_file('packs/my_pack/owned_file.rb', <<~CONTENTS)
11+
# @team Bar
12+
CONTENTS
13+
end
14+
15+
it 'prints out ownership information for the given team' do
16+
expect(CodeOwnership.for_team('Bar')).to eq <<~OWNERSHIP
17+
# Code Ownership Report for `Bar` Team
18+
## Annotations at the top of file
19+
- packs/my_pack/owned_file.rb
20+
21+
## Team-specific owned globs
22+
This team owns nothing in this category.
23+
24+
## Owner metadata key in package.yml
25+
This team owns nothing in this category.
26+
27+
## Owner metadata key in package.json
28+
This team owns nothing in this category.
29+
30+
## Team YML ownership
31+
- config/teams/bar.yml
32+
OWNERSHIP
33+
end
34+
end
35+
36+
describe '.for_file' do
37+
context 'ruby owned file' do
38+
before do
39+
create_configuration
40+
write_file('config/teams/bar.yml', <<~CONTENTS)
41+
name: Bar
42+
CONTENTS
43+
44+
write_file('packs/my_pack/owned_file.rb', <<~CONTENTS)
45+
# @team Bar
46+
CONTENTS
47+
end
48+
49+
it 'can find the owner of a ruby file with file annotations' do
50+
expect(CodeOwnership.for_file('packs/my_pack/owned_file.rb').name).to eq 'Bar'
51+
end
52+
end
53+
54+
context 'javascript owned file' do
55+
before do
56+
create_configuration
57+
write_file('config/teams/bar.yml', <<~CONTENTS)
58+
name: Bar
59+
CONTENTS
60+
61+
write_file('frontend/javascripts/packages/my_package/owned_file.jsx', <<~CONTENTS)
62+
// @team Bar
63+
CONTENTS
64+
end
65+
66+
it 'can find the owner of a javascript file with file annotations' do
67+
expect(CodeOwnership.for_file('frontend/javascripts/packages/my_package/owned_file.jsx').name).to eq 'Bar'
68+
end
69+
end
70+
end
71+
72+
describe '.remove_file_annotation!' do
73+
subject(:remove_file_annotation) do
74+
CodeOwnership.remove_file_annotation!(filename)
75+
# Getting the owner gets stored in the cache, so after we remove the file annotation we want to bust the cache
76+
CodeOwnership.bust_caches!
77+
end
78+
79+
before do
80+
write_file('config/teams/foo.yml', <<~CONTENTS)
81+
name: Foo
82+
CONTENTS
83+
end
84+
85+
context 'ruby file has no annotation' do
86+
let(:filename) { 'app/my_file.rb' }
87+
88+
before do
89+
write_file(filename, <<~CONTENTS)
90+
# Empty file
91+
CONTENTS
92+
end
93+
94+
it 'has no effect' do
95+
expect(File.read(filename)).to eq "# Empty file\n"
96+
97+
remove_file_annotation
98+
99+
expect(File.read(filename)).to eq "# Empty file\n"
100+
end
101+
end
102+
103+
context 'ruby file has annotation' do
104+
let(:filename) { 'app/my_file.rb' }
105+
106+
before do
107+
write_file(filename, <<~CONTENTS)
108+
# @team Foo
109+
110+
# Some content
111+
CONTENTS
112+
113+
write_file('package.yml', <<~CONTENTS)
114+
enforce_dependency: true
115+
enforce_privacy: true
116+
CONTENTS
117+
end
118+
119+
it 'removes the annotation' do
120+
current_ownership = CodeOwnership.for_file(filename)
121+
expect(current_ownership&.name).to eq 'Foo'
122+
expect(File.read(filename)).to eq <<~RUBY
123+
# @team Foo
124+
125+
# Some content
126+
RUBY
127+
128+
remove_file_annotation
129+
130+
new_ownership = CodeOwnership.for_file(filename)
131+
expect(new_ownership).to eq nil
132+
expected_output = <<~RUBY
133+
# Some content
134+
RUBY
135+
136+
expect(File.read(filename)).to eq expected_output
137+
end
138+
end
139+
140+
context 'javascript file has annotation' do
141+
let(:filename) { 'app/my_file.jsx' }
142+
143+
before do
144+
write_file(filename, <<~CONTENTS)
145+
// @team Foo
146+
147+
// Some content
148+
CONTENTS
149+
150+
write_file('package.yml', <<~CONTENTS)
151+
enforce_dependency: true
152+
enforce_privacy: true
153+
CONTENTS
154+
end
155+
156+
it 'removes the annotation' do
157+
current_ownership = CodeOwnership.for_file(filename)
158+
expect(current_ownership&.name).to eq 'Foo'
159+
expect(File.read(filename)).to eq <<~JAVASCRIPT
160+
// @team Foo
161+
162+
// Some content
163+
JAVASCRIPT
164+
165+
remove_file_annotation
166+
167+
new_ownership = CodeOwnership.for_file(filename)
168+
expect(new_ownership).to eq nil
169+
expected_output = <<~JAVASCRIPT
170+
// Some content
171+
JAVASCRIPT
172+
173+
expect(File.read(filename)).to eq expected_output
174+
end
175+
end
176+
177+
context 'file has new lines after the annotation' do
178+
let(:filename) { 'app/my_file.rb' }
179+
180+
before do
181+
write_file(filename, <<~CONTENTS)
182+
# @team Foo
183+
184+
185+
# Some content
186+
187+
188+
# Some other content
189+
CONTENTS
190+
end
191+
192+
it 'removes the annotation and the leading new lines' do
193+
expect(File.read(filename)).to eq <<~RUBY
194+
# @team Foo
195+
196+
197+
# Some content
198+
199+
200+
# Some other content
201+
RUBY
202+
203+
remove_file_annotation
204+
205+
expected_output = <<~RUBY
206+
# Some content
207+
208+
209+
# Some other content
210+
RUBY
211+
212+
expect(File.read(filename)).to eq expected_output
213+
end
214+
end
215+
end
216+
217+
end
218+
end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
module CodeOwnership
2+
RSpec.describe Private::OwnershipMappers::JsPackageOwnership do
3+
describe 'CodeOwnership.validate!' do
4+
context 'application has invalid JSON in package' do
5+
before do
6+
write_file('config/code_ownership.yml', {}.to_yaml)
7+
8+
write_file('frontend/javascripts/my_package/package.json', <<~CONTENTS)
9+
{ syntax error!!!
10+
"metadata": {
11+
"owner": "Foo"
12+
}
13+
}
14+
CONTENTS
15+
end
16+
17+
it 'lets the user know the their package JSON is invalid' do
18+
expect { CodeOwnership.validate! }.to raise_error do |e|
19+
expect(e).to be_a CodeOwnership::InvalidCodeOwnershipConfigurationError
20+
expect(e.message).to match /JSON::ParserError.*?unexpected token/
21+
expect(e.message).to include 'frontend/javascripts/my_package/package.json has invalid JSON, so code ownership cannot be determined.'
22+
expect(e.message).to include 'Please either make the JSON in that file valid or specify `js_package_paths` in config/code_ownership.yml.'
23+
end
24+
end
25+
end
26+
end
27+
28+
describe 'CodeOwnershp.for_file' do
29+
before do
30+
create_configuration
31+
32+
write_file('frontend/javascripts/packages/my_other_package/package.json', <<~CONTENTS)
33+
{
34+
"name": "@gusto/my_package",
35+
"metadata": {
36+
"owner": "Bar"
37+
}
38+
}
39+
CONTENTS
40+
write_file('frontend/javascripts/packages/my_other_package/my_file.jsx')
41+
write_file('config/teams/bar.yml', <<~CONTENTS)
42+
name: Bar
43+
CONTENTS
44+
end
45+
46+
it 'can find the owner of files in team-owned javascript packages' do
47+
expect(CodeOwnership.for_file('frontend/javascripts/packages/my_other_package/my_file.jsx').name).to eq 'Bar'
48+
end
49+
end
50+
end
51+
end
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
module CodeOwnership
2+
RSpec.describe Private::OwnershipMappers::PackageOwnership do
3+
before do
4+
create_configuration
5+
write_file('config/teams/bar.yml', <<~CONTENTS)
6+
name: Bar
7+
CONTENTS
8+
9+
write_file('packs/my_other_package/package.yml', <<~CONTENTS)
10+
enforce_dependency: true
11+
enforce_privacy: true
12+
metadata:
13+
owner: Bar
14+
CONTENTS
15+
16+
write_file('package.yml', <<~CONTENTS)
17+
enforce_dependency: true
18+
enforce_privacy: true
19+
CONTENTS
20+
end
21+
22+
describe 'CodeOwnership.for_team' do
23+
it 'prints out ownership information for the given team' do
24+
expect(CodeOwnership.for_team('Bar')).to eq <<~OWNERSHIP
25+
# Code Ownership Report for `Bar` Team
26+
## Annotations at the top of file
27+
This team owns nothing in this category.
28+
29+
## Team-specific owned globs
30+
This team owns nothing in this category.
31+
32+
## Owner metadata key in package.yml
33+
- packs/my_other_package/**/**
34+
35+
## Owner metadata key in package.json
36+
This team owns nothing in this category.
37+
38+
## Team YML ownership
39+
- config/teams/bar.yml
40+
OWNERSHIP
41+
end
42+
end
43+
44+
describe 'CodeOwnership.for_file' do
45+
it 'can find the owner of files in team-owned pack' do
46+
expect(CodeOwnership.for_file('packs/my_other_package/my_file.rb').name).to eq 'Bar'
47+
end
48+
end
49+
50+
describe 'CodeOwnership.for_package' do
51+
it 'returns the right team' do
52+
team = CodeOwnership.for_package(Packs.find('packs/my_other_package'))
53+
expect(team.name).to eq 'Bar'
54+
end
55+
end
56+
end
57+
end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module CodeOwnership
2+
RSpec.describe Private::OwnershipMappers::TeamGlobs do
3+
before do
4+
create_configuration
5+
write_file('config/teams/bar.yml', <<~CONTENTS)
6+
name: Bar
7+
owned_globs:
8+
- app/services/bar_stuff/**/**
9+
- frontend/javascripts/bar_stuff/**/**
10+
CONTENTS
11+
12+
write_file('app/services/bar_stuff/thing.rb')
13+
write_file('frontend/javascripts/bar_stuff/thing.jsx')
14+
end
15+
16+
it 'can find the owner of ruby files in owned_globs' do
17+
expect(CodeOwnership.for_file('app/services/bar_stuff/thing.rb').name).to eq 'Bar'
18+
end
19+
20+
it 'can find the owner of javascript files in owned_globs' do
21+
expect(CodeOwnership.for_file('frontend/javascripts/bar_stuff/thing.jsx').name).to eq 'Bar'
22+
end
23+
end
24+
end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module CodeOwnership
2+
RSpec.describe Private::OwnershipMappers::TeamYmlOwnership do
3+
before do
4+
create_configuration
5+
write_file('config/teams/bar.yml', <<~CONTENTS)
6+
name: Bar
7+
CONTENTS
8+
end
9+
10+
describe 'CodeOwnership.for_team' do
11+
it 'prints out ownership information for the given team' do
12+
expect(CodeOwnership.for_team('Bar')).to eq <<~OWNERSHIP
13+
# Code Ownership Report for `Bar` Team
14+
## Annotations at the top of file
15+
This team owns nothing in this category.
16+
17+
## Team-specific owned globs
18+
This team owns nothing in this category.
19+
20+
## Owner metadata key in package.yml
21+
This team owns nothing in this category.
22+
23+
## Owner metadata key in package.json
24+
This team owns nothing in this category.
25+
26+
## Team YML ownership
27+
- config/teams/bar.yml
28+
OWNERSHIP
29+
end
30+
end
31+
32+
describe 'CodeOwnership.for_file' do
33+
it 'maps a team YML to be owned by the team itself' do
34+
expect(CodeOwnership.for_file('config/teams/bar.yml').name).to eq 'Bar'
35+
end
36+
end
37+
end
38+
end

0 commit comments

Comments
 (0)