Skip to content

Commit

Permalink
Add spec for warning messages when cache location is invalid
Browse files Browse the repository at this point in the history
This PR suppresses the following warnings that are displayed
when running tests, and makes them executable test code.

```console
% cd path/to/rubocop
% bundle exec rspec ./spec/rubocop/result_cache_spec.rb:278
Run options: include {:focus=>true,
:locations=>{"./spec/rubocop/result_cache_spec.rb"=>[278]}}

Randomized with seed 7133
Couldn't create cache directory. Continuing without cache.
  Permission denied
.Couldn't create cache directory. Continuing without cache.
  Read-only file system
.

Finished in 0.20314 seconds (files took 1.93 seconds to load)
2 examples, 0 failures

Randomized with seed 7133
```
  • Loading branch information
koic authored and bbatsov committed Apr 15, 2020
1 parent e22c2e2 commit 85ecd05
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions spec/rubocop/result_cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,19 +271,33 @@ def abs(path)
end
end

shared_examples 'invalid cache location' do |error|
shared_examples 'invalid cache location' do |error, message|
before do
$stderr = StringIO.new
end

it 'doesn\'t raise an exception' do
expect(FileUtils).to receive(:mkdir_p).with(start_with(cache_root))
.and_raise(error)
expect { cache.save([]) }.not_to raise_error
expect($stderr.string).to eq(<<~WARN)
Couldn't create cache directory. Continuing without cache.
#{message}
WARN
end

after do
$stderr = STDERR
end
end

context 'when the @path is not writable' do
let(:cache_root) { '/permission_denied_dir' }

it_behaves_like 'invalid cache location', Errno::EACCES
it_behaves_like 'invalid cache location', Errno::EROFS
it_behaves_like 'invalid cache location',
Errno::EACCES, 'Permission denied'
it_behaves_like 'invalid cache location',
Errno::EROFS, 'Read-only file system'
end
end

Expand Down

0 comments on commit 85ecd05

Please sign in to comment.