Skip to content

Commit

Permalink
Add success message when rspectre succeeds (#35)
Browse files Browse the repository at this point in the history
- Closes #30
- Tweaks a few overly aggressive rubocop settings as well.
  • Loading branch information
dgollahon committed Sep 9, 2020
1 parent 36e8c18 commit 975a058
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: 2
jobs:
build:
docker:
- image: circleci/ruby:2.5
- image: circleci/ruby:2.5.8
environment:
CODECLIMATE_REPO_TOKEN: c8c9bf91b1e168a3f507a2ef2d2d891eb2e9cf37c06ffd4d0c6ba4b7caf618ab
steps:
Expand Down
28 changes: 17 additions & 11 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,31 @@ Layout/MultilineMethodCallIndentation:
EnforcedStyle: indented
Layout/MultilineMethodDefinitionBraceLayout:
Enabled: true
Metrics/AbcSize:
Enabled: false
Metrics/MethodLength:
Max: 20
Metrics/LineLength:
Max: 100
Metrics/BlockLength:
Exclude:
# Ignore RSpec DSL
- spec/**/*
# Ignore RSpec DSL
- spec/**/*
Naming/UncommunicativeMethodParamName:
Enabled: false
Style/Next:
EnforcedStyle: always
Style/PercentLiteralDelimiters:
PreferredDelimiters:
'%i': '[]'
'%I': '[]'
'%q': '{}'
'%Q': '{}'
'%r': '{}'
'%s': ()
'%w': '[]'
'%W': '[]'
'%x': ()
"%i": "[]"
"%I": "[]"
"%q": "{}"
"%Q": "{}"
"%r": "{}"
"%s": ()
"%w": "[]"
"%W": "[]"
"%x": ()
Style/TrivialAccessors:
ExactNameMatch: false
Style/SymbolArray:
Expand Down Expand Up @@ -89,6 +93,8 @@ RSpec/MessageExpectation:
Enabled: true
RSpec/ExampleLength:
Enabled: false
RSpec/MultipleExpectations:
Enabled: false
RSpec/VerifiedDoubles:
IgnoreSymbolicNames: true
RSpec/FilePath:
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## [Master (Unreleased)]

- Drop support for ruby 2.3 and 2.4 - [#34](https://github.com/dgollahon/rspectre/pull/34) ([@dgollahon])
- Added success message if no unused setup is found - [#35](https://github.com/dgollahon/rspectre/pull/35) ([@dgollahon])
- Dropped support for ruby 2.3 and 2.4 - [#34](https://github.com/dgollahon/rspectre/pull/34) ([@dgollahon])
- Fixed a bug where `stringio` was not being required - [#32](https://github.com/dgollahon/rspectre/pull/32) ([@dgollahon])
- Removed `unparser` dependency - [#31](https://github.com/dgollahon/rspectre/pull/31) ([@dgollahon])

Expand Down
2 changes: 1 addition & 1 deletion lib/rspectre/auto_corrector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def removal_range(node)
)
end

def range_end(node) # rubocop:disable Metrics/MethodLength
def range_end(node)
location = node.location.expression

last_line = location.last_line
Expand Down
2 changes: 1 addition & 1 deletion lib/rspectre/linter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def self.example_group
RSpec::Core::ExampleGroup
end

def self.register(selector, locations) # rubocop:disable Metrics/MethodLength
def self.register(selector, locations)
location = locations.first

file = File.realpath(location.path)
Expand Down
2 changes: 1 addition & 1 deletion lib/rspectre/linter/unused_shared_setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Linter
class UnusedSharedSetup < self
TAG = 'UnusedSharedSetup'

def self.redefine_shared(receiver, method) # rubocop:disable Metrics/MethodLength
def self.redefine_shared(receiver, method)
# Capture the original class method
original_method = receiver.method(method)

Expand Down
2 changes: 2 additions & 0 deletions lib/rspectre/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def lint
def handle_offenses
if TRACKER.offenses?
auto_correct ? TRACKER.correct_offenses : TRACKER.report_offenses
else
puts 'No unused test setup detected.'
end
end

Expand Down
22 changes: 22 additions & 0 deletions spec/runner_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

RSpec.describe RSpectre::Runner do
include_context 'rspectre runner'

it 'outputs a success message' do
source = <<~RUBY
RSpec.describe 'nothing of interest here' do
let(:a) { 1 }
it 'is 1' do
expect(a).to be(1)
end
end
RUBY

run_rspectre(source) do |(stdout, _stderr, status), _spec_file|
expect(status.to_i).to be(0)
expect(stdout).to eql("No unused test setup detected.\n")
end
end
end
73 changes: 26 additions & 47 deletions spec/shared/highlighted_offenses.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
# frozen_string_literal: true

RSpec.shared_examples 'highlighted offenses' do |src|
subject(:lint) do
rspectre_path = File.expand_path('bin/rspectre')
RSpec.shared_examples 'highlighted offenses' do |source|
include_context 'rspectre runner'

Dir.chdir(File.dirname(spec_file.path)) do
Open3.capture3("#{rspectre_path} --rspec #{spec_file.path}")
end
end

let(:spec_file) do
Tempfile.new.tap do |file|
file.write(src.gsub(/^\s*\^+.+\n/, ''))
file.flush
end
end

let(:expected_offenses) do
def expected_offenses(source, spec_file) # rubocop:disable Metrics/MethodLength
line_count = 0
last_line = ''

src.split("\n").map do |current_line|
source.split("\n").map do |current_line|
header_pattern = /^(?<leading_space>\s*)(?<highlight>\^+)\s(?<tag>\w+):\s(?<message>.+)$/

if (match = current_line.match(header_pattern))
Expand All @@ -45,42 +32,34 @@
end.compact
end

before do
spec_file
end

it 'highlights the offenses' do
it 'highlights the expected offenses' do
aggregate_failures do
stdout, _stderr, status = lint

offenses = stdout.split("\n").each_slice(4)
run_rspectre(source) do |(stdout, _stderr, status), file|
offenses = stdout.split("\n").each_slice(4)
expected_offenses = expected_offenses(source, file)

expect(status.to_i).to be > 0
expect(offenses.count).to eql(expected_offenses.count)
expect(status.to_i).to be > 0
expect(offenses.count).to eql(expected_offenses.count)

offenses.zip(expected_offenses) do |message_parts, (offense, description)|
expected_parts = offense.to_s.split("\n")
offenses.zip(expected_offenses) do |message_parts, (offense, description)|
expected_parts = offense.to_s.split("\n")

expect(message_parts).to eql(expected_parts), <<~MSG
Expected:
#{expected_parts[0]}
#{expected_parts[1]}
#{expected_parts[2]}
#{expected_parts[3]}
Found:
#{message_parts[0]}
#{message_parts[1]}
#{message_parts[2]}
#{message_parts[3]}
MSG
expect(message_parts).to eql(expected_parts), <<~MSG
Expected:
#{expected_parts[0]}
#{expected_parts[1]}
#{expected_parts[2]}
#{expected_parts[3]}
Found:
#{message_parts[0]}
#{message_parts[1]}
#{message_parts[2]}
#{message_parts[3]}
MSG

expect(offense.description).to eql(description)
expect(offense.description).to eql(description)
end
end
end
end

after do
spec_file.close
spec_file.unlink
end
end
23 changes: 23 additions & 0 deletions spec/shared/rspectre_runner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

RSpec.shared_context 'rspectre runner' do # rubocop:disable RSpec/ContextWording
def run_rspectre(source)
spec_file =
Tempfile.new.tap do |file|
file.write(source.gsub(/^\s*\^+.+\n/, ''))
file.flush
end

rspectre_path = File.expand_path('bin/rspectre')

output =
Dir.chdir(File.dirname(spec_file.path)) do
Open3.capture3("#{rspectre_path} --rspec #{spec_file.path}")
end

yield([output, spec_file])
ensure
spec_file.close
spec_file.unlink
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
require 'rspectre'

require 'shared/highlighted_offenses'
require 'shared/rspectre_runner'

RSpec.configure do |config|
# Forbid RSpec from monkey patching any of our objects
Expand Down

0 comments on commit 975a058

Please sign in to comment.