Skip to content

Commit 8599e31

Browse files
author
Alex Evanczuk
authored
Tidy up tests (#48)
This commit is intended to mostly colocate references to the codeowners file in one place within tests.
1 parent 272efd1 commit 8599e31

File tree

5 files changed

+37
-31
lines changed

5 files changed

+37
-31
lines changed

spec/lib/code_ownership/private/extension_loader_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def bust_caches!
6666
end
6767
RUBY
6868

69-
expect_any_instance_of(codeowners_validation).to receive(:`).with("git add #{Pathname.pwd.join('.github/CODEOWNERS')}") # rubocop:disable RSpec/AnyInstance
69+
expect_any_instance_of(codeowners_validation).to receive(:`).with("git add #{codeowners_path}") # rubocop:disable RSpec/AnyInstance
7070
end
7171

7272
after(:all) do
@@ -90,7 +90,7 @@ def bust_caches!
9090

9191
it 'allows extensions to add to codeowners list' do
9292
expect { CodeOwnership.validate! }.to raise_error(CodeOwnership::InvalidCodeOwnershipConfigurationError)
93-
expect(Pathname.new('.github/CODEOWNERS').read).to eq <<~EXPECTED
93+
expect(codeowners_path.read).to eq <<~EXPECTED
9494
# STOP! - DO NOT EDIT THIS FILE MANUALLY
9595
# This file was automatically generated by "bin/codeownership validate".
9696
#

spec/lib/code_ownership/private/ownership_mappers/package_ownership_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ module CodeOwnership
1313
owner: Bar
1414
CONTENTS
1515

16+
write_file('packs/my_other_package/my_file.rb')
17+
1618
write_file('package.yml', <<~CONTENTS)
1719
enforce_dependency: true
1820
enforce_privacy: true

spec/lib/code_ownership/private/validations/github_codeowners_up_to_date_spec.rb

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ module CodeOwnership
1010

1111
context 'in an empty application' do
1212
it 'automatically regenerates the codeowners file' do
13-
expect(Pathname.new('.github/CODEOWNERS')).to_not exist
14-
expect_any_instance_of(codeowners_validation).to receive(:`).with("git add #{Pathname.pwd.join('.github/CODEOWNERS')}") # rubocop:disable RSpec/AnyInstance
13+
expect(codeowners_path).to_not exist
14+
expect_any_instance_of(codeowners_validation).to receive(:`).with("git add #{codeowners_path}") # rubocop:disable RSpec/AnyInstance
1515
expect { CodeOwnership.validate! }.to_not raise_error
16-
expect(Pathname.new('.github/CODEOWNERS').read).to eq <<~EXPECTED
16+
expect(codeowners_path.read).to eq <<~EXPECTED
1717
# STOP! - DO NOT EDIT THIS FILE MANUALLY
1818
# This file was automatically generated by "bin/codeownership validate".
1919
#
@@ -30,10 +30,10 @@ module CodeOwnership
3030
before { create_non_empty_application }
3131

3232
it 'automatically regenerates the codeowners file' do
33-
expect(Pathname.new('.github/CODEOWNERS')).to_not exist
34-
expect_any_instance_of(codeowners_validation).to receive(:`).with("git add #{Pathname.pwd.join('.github/CODEOWNERS')}") # rubocop:disable RSpec/AnyInstance
33+
expect(codeowners_path).to_not exist
34+
expect_any_instance_of(codeowners_validation).to receive(:`).with("git add #{codeowners_path}") # rubocop:disable RSpec/AnyInstance
3535
expect { CodeOwnership.validate! }.to_not raise_error
36-
expect(Pathname.new('.github/CODEOWNERS').read).to eq <<~EXPECTED
36+
expect(codeowners_path.read).to eq <<~EXPECTED
3737
# STOP! - DO NOT EDIT THIS FILE MANUALLY
3838
# This file was automatically generated by "bin/codeownership validate".
3939
#
@@ -64,10 +64,10 @@ module CodeOwnership
6464

6565
context 'the user has passed in specific input files into the validate method' do
6666
it 'still automatically regenerates the codeowners file, since we look at all files when regenerating CODEOWNERS' do
67-
expect(Pathname.new('.github/CODEOWNERS')).to_not exist
68-
expect_any_instance_of(codeowners_validation).to receive(:`).with("git add #{Pathname.pwd.join('.github/CODEOWNERS')}") # rubocop:disable RSpec/AnyInstance
67+
expect(codeowners_path).to_not exist
68+
expect_any_instance_of(codeowners_validation).to receive(:`).with("git add #{codeowners_path}") # rubocop:disable RSpec/AnyInstance
6969
expect { CodeOwnership.validate! }.to_not raise_error
70-
expect(Pathname.new('.github/CODEOWNERS').read).to eq <<~EXPECTED
70+
expect(codeowners_path.read).to eq <<~EXPECTED
7171
# STOP! - DO NOT EDIT THIS FILE MANUALLY
7272
# This file was automatically generated by "bin/codeownership validate".
7373
#
@@ -108,10 +108,10 @@ module CodeOwnership
108108
end
109109

110110
it 'does not include the team in the output' do
111-
expect(Pathname.new('.github/CODEOWNERS')).to_not exist
111+
expect(codeowners_path).to_not exist
112112
expect { CodeOwnership.validate! }.to_not raise_error
113113
expect_any_instance_of(codeowners_validation).to_not receive(:`) # rubocop:disable RSpec/AnyInstance
114-
expect(Pathname.new('.github/CODEOWNERS').read).to eq <<~EXPECTED
114+
expect(codeowners_path.read).to eq <<~EXPECTED
115115
# STOP! - DO NOT EDIT THIS FILE MANUALLY
116116
# This file was automatically generated by "bin/codeownership validate".
117117
#
@@ -138,10 +138,10 @@ module CodeOwnership
138138
end
139139

140140
it 'does not include the team in the output' do
141-
expect(Pathname.new('.github/CODEOWNERS')).to_not exist
141+
expect(codeowners_path).to_not exist
142142
expect { CodeOwnership.validate! }.to_not raise_error
143143
expect_any_instance_of(codeowners_validation).to_not receive(:`) # rubocop:disable RSpec/AnyInstance
144-
expect(Pathname.new('.github/CODEOWNERS').read).to eq <<~EXPECTED
144+
expect(codeowners_path.read).to eq <<~EXPECTED
145145
# STOP! - DO NOT EDIT THIS FILE MANUALLY
146146
# This file was automatically generated by "bin/codeownership validate".
147147
#
@@ -161,10 +161,10 @@ module CodeOwnership
161161
end
162162

163163
it 'does not stage the changes to the codeowners file' do
164-
expect(Pathname.new('.github/CODEOWNERS')).to_not exist
164+
expect(codeowners_path).to_not exist
165165
expect_any_instance_of(codeowners_validation).to_not receive(:`) # rubocop:disable RSpec/AnyInstance
166166
expect { CodeOwnership.validate!(stage_changes: false) }.to_not raise_error
167-
expect(Pathname.new('.github/CODEOWNERS').read).to eq <<~EXPECTED
167+
expect(codeowners_path.read).to eq <<~EXPECTED
168168
# STOP! - DO NOT EDIT THIS FILE MANUALLY
169169
# This file was automatically generated by "bin/codeownership validate".
170170
#
@@ -185,7 +185,7 @@ module CodeOwnership
185185

186186
context 'in an empty application' do
187187
it 'automatically regenerates the codeowners file' do
188-
expect(Pathname.new('.github/CODEOWNERS')).to_not exist
188+
expect(codeowners_path).to_not exist
189189
expect_any_instance_of(codeowners_validation).to_not receive(:`) # rubocop:disable RSpec/AnyInstance
190190
expect { CodeOwnership.validate!(autocorrect: false) }.to raise_error do |e|
191191
expect(e).to be_a CodeOwnership::InvalidCodeOwnershipConfigurationError
@@ -196,15 +196,15 @@ module CodeOwnership
196196
See https://github.com/rubyatscale/code_ownership#README.md for more details
197197
EXPECTED
198198
end
199-
expect(Pathname.new('.github/CODEOWNERS')).to_not exist
199+
expect(codeowners_path).to_not exist
200200
end
201201
end
202202

203203
context 'in an non-empty application' do
204204
before { create_non_empty_application }
205205

206206
it 'automatically regenerates the codeowners file' do
207-
expect(Pathname.new('.github/CODEOWNERS')).to_not exist
207+
expect(codeowners_path).to_not exist
208208
expect_any_instance_of(codeowners_validation).to_not receive(:`) # rubocop:disable RSpec/AnyInstance
209209
expect { CodeOwnership.validate!(autocorrect: false) }.to raise_error do |e|
210210
expect(e).to be_a CodeOwnership::InvalidCodeOwnershipConfigurationError
@@ -215,7 +215,7 @@ module CodeOwnership
215215
See https://github.com/rubyatscale/code_ownership#README.md for more details
216216
EXPECTED
217217
end
218-
expect(Pathname.new('.github/CODEOWNERS')).to_not exist
218+
expect(codeowners_path).to_not exist
219219
end
220220

221221
context 'team does not have a github team listed' do
@@ -229,7 +229,7 @@ module CodeOwnership
229229
end
230230

231231
it 'does not include the team in the output' do
232-
expect(Pathname.new('.github/CODEOWNERS')).to_not exist
232+
expect(codeowners_path).to_not exist
233233
expect_any_instance_of(codeowners_validation).to_not receive(:`) # rubocop:disable RSpec/AnyInstance
234234
expect { CodeOwnership.validate!(autocorrect: false) }.to raise_error do |e|
235235
expect(e).to be_a CodeOwnership::InvalidCodeOwnershipConfigurationError
@@ -240,7 +240,7 @@ module CodeOwnership
240240
See https://github.com/rubyatscale/code_ownership#README.md for more details
241241
EXPECTED
242242
end
243-
expect(Pathname.new('.github/CODEOWNERS')).to_not exist
243+
expect(codeowners_path).to_not exist
244244
end
245245
end
246246

@@ -258,7 +258,7 @@ module CodeOwnership
258258
end
259259

260260
it 'does not include the team in the output' do
261-
expect(Pathname.new('.github/CODEOWNERS')).to_not exist
261+
expect(codeowners_path).to_not exist
262262
expect_any_instance_of(codeowners_validation).to_not receive(:`) # rubocop:disable RSpec/AnyInstance
263263
expect { CodeOwnership.validate!(autocorrect: false) }.to raise_error do |e|
264264
expect(e).to be_a CodeOwnership::InvalidCodeOwnershipConfigurationError
@@ -269,7 +269,7 @@ module CodeOwnership
269269
See https://github.com/rubyatscale/code_ownership#README.md for more details
270270
EXPECTED
271271
end
272-
expect(Pathname.new('.github/CODEOWNERS')).to_not exist
272+
expect(codeowners_path).to_not exist
273273
end
274274
end
275275
end
@@ -279,7 +279,7 @@ module CodeOwnership
279279

280280
it 'prints out the diff' do
281281
FileUtils.mkdir('.github')
282-
Pathname.new('.github/CODEOWNERS').write <<~CODEOWNERS
282+
codeowners_path.write <<~CODEOWNERS
283283
# STOP! - DO NOT EDIT THIS FILE MANUALLY
284284
# This file was automatically generated by "bin/codeownership validate".
285285
#
@@ -335,7 +335,7 @@ module CodeOwnership
335335

336336
it 'prints out the diff' do
337337
FileUtils.mkdir('.github')
338-
Pathname.new('.github/CODEOWNERS').write <<~CODEOWNERS
338+
codeowners_path.write <<~CODEOWNERS
339339
# STOP! - DO NOT EDIT THIS FILE MANUALLY
340340
# This file was automatically generated by "bin/codeownership validate".
341341
#
@@ -388,7 +388,7 @@ module CodeOwnership
388388

389389
it 'prints out the diff' do
390390
FileUtils.mkdir('.github')
391-
Pathname.new('.github/CODEOWNERS').write <<~CODEOWNERS
391+
codeowners_path.write <<~CODEOWNERS
392392
# STOP! - DO NOT EDIT THIS FILE MANUALLY
393393
# This file was automatically generated by "bin/codeownership validate".
394394
#
@@ -443,10 +443,10 @@ module CodeOwnership
443443
end
444444

445445
it 'skips validating the codeowners file' do
446-
expect(Pathname.new('.github/CODEOWNERS')).to_not exist
446+
expect(codeowners_path).to_not exist
447447
expect_any_instance_of(codeowners_validation).to_not receive(:`) # rubocop:disable RSpec/AnyInstance
448448
expect { CodeOwnership.validate!(autocorrect: false) }.to_not raise_error
449-
expect(Pathname.new('.github/CODEOWNERS')).to_not exist
449+
expect(codeowners_path).to_not exist
450450
end
451451
end
452452
end

spec/spec_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
config.include_context 'application fixtures'
2020

2121
config.before do |c|
22+
codeowners_path.delete if codeowners_path.exist?
23+
2224
unless c.metadata[:do_not_bust_cache]
2325
CodeOwnership.bust_caches!
2426
CodeTeams.bust_caches!

spec/support/application_fixtures.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
RSpec.shared_context 'application fixtures' do
2+
let(:codeowners_path) { Pathname.pwd.join('.github/CODEOWNERS') }
3+
24
let(:create_configuration) do
35
write_file('config/code_ownership.yml', <<~YML)
46
owned_globs:
@@ -9,7 +11,7 @@
911
let(:create_minimal_configuration) do
1012
write_file('config/code_ownership.yml', <<~YML)
1113
owned_globs:
12-
- app/**/*.rb
14+
- app/**/*.{rb,jsx}
1315
YML
1416
end
1517

0 commit comments

Comments
 (0)