Skip to content

Commit

Permalink
Add success message when rspectre succeeds
Browse files Browse the repository at this point in the history
- Closes #30
  • Loading branch information
dgollahon committed Sep 9, 2020
1 parent 36e8c18 commit f0489bd
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 49 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
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: 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)
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
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 f0489bd

Please sign in to comment.