Skip to content

Commit

Permalink
[Fix rubocop#8804] Save actual status to cache (rubocop#8841)
Browse files Browse the repository at this point in the history
* `Unsupported` status should be saved to cache, later to be used by total_correctable_count
* `Corrected` should not be saved to cache [rubocop#5857]

Co-authored-by: Ofir Petrushka <hatkyinc@gmail.com>
Co-authored-by: Bozhidar Batsov <bozhidar@batsov.com>
  • Loading branch information
3 people committed Oct 5, 2020
1 parent c2b4c37 commit 90997a4
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -21,6 +21,7 @@
* [#8807](https://github.com/rubocop-hq/rubocop/pull/8807): Fix a false positive for `Style/RedundantCondition` when using assignment by hash key access. ([@koic][])
* [#8848](https://github.com/rubocop-hq/rubocop/issues/8848): Fix a false positive for `Style/CombinableLoops` when using the same method with different arguments. ([@dvandersluis][])
* [#8843](https://github.com/rubocop-hq/rubocop/issues/8843): Fix an incorrect autocorrect for `Lint/AmbiguousRegexpLiteral` when sending method to regexp literal receiver. ([@koic][])
* [#8842](https://github.com/rubocop-hq/rubocop/issues/8842): Save actual status to cache, except corrected. ([@hatkyinc2][])
* [#8835](https://github.com/rubocop-hq/rubocop/issues/8835): Fix an incorrect autocorrect for `Style/RedundantInterpolation` when using string interpolation for non-operator methods. ([@koic][])

### Changes
Expand Down
3 changes: 2 additions & 1 deletion lib/rubocop/cached_data.rb
Expand Up @@ -21,6 +21,7 @@ def to_json(offenses)
private

def serialize_offense(offense)
status = :uncorrected if %i[corrected corrected_with_todo].include?(offense.status)
{
# Calling #to_s here ensures that the serialization works when using
# other json serializers such as Oj. Some of these gems do not call
Expand All @@ -32,7 +33,7 @@ def serialize_offense(offense)
},
message: message(offense),
cop_name: offense.cop_name,
status: :uncorrected
status: status || offense.status
}
end

Expand Down
77 changes: 61 additions & 16 deletions spec/rubocop/result_cache_spec.rb
Expand Up @@ -60,25 +60,70 @@ def abs(path)
end

# Fixes https://github.com/rubocop-hq/rubocop/issues/6274
context 'when offenses are saved by autocorrect run' do
let(:corrected_offense) do
RuboCop::Cop::Offense.new(
:warning, location, 'unused var', 'Lint/UselessAssignment', :corrected
)
context 'when offenses are saved' do
context 'an offence with status corrected' do
let(:offense) do
RuboCop::Cop::Offense.new(
:warning, location, 'unused var', 'Lint/UselessAssignment', :corrected
)
end

it 'serializes them with uncorrected status' do
cache.save([offense])
expect(cache.load[0].status).to eq(:uncorrected)
end
end
let(:uncorrected_offense) do
RuboCop::Cop::Offense.new(
corrected_offense.severity.name,
corrected_offense.location,
corrected_offense.message,
corrected_offense.cop_name,
:uncorrected
)

context 'an offence with status corrected_with_todo' do
let(:offense) do
RuboCop::Cop::Offense.new(
:warning, location, 'unused var', 'Lint/UselessAssignment', :corrected_with_todo
)
end

it 'serializes them with uncorrected status' do
cache.save([offense])
expect(cache.load[0].status).to eq(:uncorrected)
end
end

it 'serializes them with :uncorrected status' do
cache.save([corrected_offense])
expect(cache.load).to match_array([uncorrected_offense])
context 'an offence with status uncorrected' do
let(:offense) do
RuboCop::Cop::Offense.new(
:warning, location, 'unused var', 'Lint/UselessAssignment', :uncorrected
)
end

it 'serializes them with uncorrected status' do
cache.save([offense])
expect(cache.load[0].status).to eq(:uncorrected)
end
end

context 'an offence with status unsupported' do
let(:offense) do
RuboCop::Cop::Offense.new(
:warning, location, 'unused var', 'Lint/UselessAssignment', :unsupported
)
end

it 'serializes them with unsupported status' do
cache.save([offense])
expect(cache.load[0].status).to eq(:unsupported)
end
end

context 'an offence with status new_status' do
let(:offense) do
RuboCop::Cop::Offense.new(
:warning, location, 'unused var', 'Lint/UselessAssignment', :new_status
)
end

it 'serializes them with new_status status' do
cache.save([offense])
expect(cache.load[0].status).to eq(:new_status)
end
end
end

Expand Down

0 comments on commit 90997a4

Please sign in to comment.