Skip to content

Commit

Permalink
Replace test/unit + shoulda with RSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Apr 19, 2015
1 parent 82920ca commit 739cf4d
Show file tree
Hide file tree
Showing 58 changed files with 808 additions and 744 deletions.
4 changes: 3 additions & 1 deletion .rspec
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
--require rspec/legacy_formatters
--color
--order random
--warning
10 changes: 5 additions & 5 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
AllCops:
Exclude:
- 'test/fixtures/iso-8859.rb'
- 'spec/fixtures/iso-8859.rb'
- 'tmp/**/*'
- 'vendor/bundle/**/*'

Lint/AmbiguousRegexpLiteral:
Exclude:
- 'features/**/*_steps.rb'
- 'test/**/*_steps.rb'
- 'spec/**/*_steps.rb'
- 'tmp/**/*_steps.rb'

Metrics/AbcSize:
Expand Down Expand Up @@ -55,15 +55,15 @@ Style/SpaceInsideHashLiteralBraces:

Style/SpecialGlobalVars:
Exclude:
- 'test/test_deleted_source.rb'
- 'test/test_return_codes.rb'
- 'spec/deleted_source_spec.rb'
- 'spec/return_codes_spec.rb'

Style/StringLiterals:
EnforcedStyle: double_quotes

Style/FileName:
Exclude:
- 'test/fixtures/utf-8.rb'
- 'spec/fixtures/utf-8.rb'

Style/TrailingComma:
EnforcedStyleForMultiline: 'comma'
25 changes: 10 additions & 15 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,21 @@ source "https://rubygems.org"
gem "rake", ">= 10.3"

group :test do
gem "rspec", ">= 3.2"
# Older versions of some gems required for Ruby 1.8.7 support
platform :ruby_18 do
gem "activesupport", "~> 3.2.21"
gem "shoulda-matchers", "~> 2.0.0"
gem "i18n", "~> 0.6.11"
end
gem "rubocop", ">= 0.30", :platforms => [:ruby_19, :ruby_20, :ruby_21, :ruby_22]
gem "minitest", ">= 5.5"
gem "rspec", ">= 3.0"
gem "rspec-legacy_formatters", ">= 1.0"
gem "shoulda", ">= 3.5"
end

platform :jruby, :ruby_19, :ruby_20, :ruby_21, :ruby_22 do
gem "aruba", "~> 0.6"
gem "capybara", "~> 2.0.0"
gem "cucumber", "~> 2.0"
gem "phantomjs", "~> 1.9"
gem "poltergeist", "~> 1.1"
gem "test-unit", "~> 3.0"
platform :jruby, :ruby_19, :ruby_20, :ruby_21, :ruby_22 do
gem "aruba", "~> 0.6"
gem "capybara", "~> 2.0.0"
gem "cucumber", "~> 2.0"
gem "phantomjs", "~> 1.9"
gem "poltergeist", "~> 1.1"
gem "rubocop", ">= 0.30"
gem "test-unit", "~> 3.0"
end
end

gemspec
13 changes: 5 additions & 8 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@ end
# Enforce proper permissions on each build
Rake::Task[:build].prerequisites.unshift :fix_permissions

require "rake/testtask"
Rake::TestTask.new(:test) do |test|
test.libs << "lib" << "test"
test.test_files = FileList["test/test_*.rb"]
test.verbose = true
test.warning = true
require "rspec/core/rake_task"
RSpec::Core::RakeTask.new(:spec) do |spec|
spec.pattern = FileList["spec/*_spec.rb"]
end

begin
Expand All @@ -33,7 +30,7 @@ end
if RUBY_VERSION >= "1.9"
require "cucumber/rake/task"
Cucumber::Rake::Task.new
task :default => [:test, :cucumber, :rubocop]
task :default => [:spec, :cucumber, :rubocop]
else
task :default => [:test]
task :default => [:spec]
end
3 changes: 1 addition & 2 deletions features/support/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
require "aruba/cucumber"
require "aruba/jruby" if RUBY_ENGINE == "jruby"
require "capybara/cucumber"
require "minitest/autorun"
require "phantomjs/poltergeist"

# Fake rack app for capybara that just returns the latest coverage report from aruba temp project dir
Expand All @@ -33,7 +32,7 @@
# Clean up and create blank state for fake project
in_current_dir do
FileUtils.rm_rf "project"
FileUtils.cp_r File.join(this_dir, "../../test/faked_project/"), "project"
FileUtils.cp_r File.join(this_dir, "../../spec/faked_project/"), "project"
end

step 'I cd to "project"'
Expand Down
29 changes: 29 additions & 0 deletions spec/1_8_fallbacks_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require "helper"

# Tests that verify that on 1.8 versions of ruby, simplecov simply
# does not launch and does not cause errors on the way
#
# TODO: This should be expanded upon all methods that could potentially
# be called in a test/spec-helper simplecov config block
#
describe "Ruby 1.8 fallback" do
it "return false when calling SimpleCov.start" do
expect(SimpleCov.start).to be false
end

it "return false when calling SimpleCov.start with a block" do
expect(SimpleCov.start { fail "Shouldn't reach this!" }).to be false
end

it "return false when calling SimpleCov.configure with a block" do
expect(SimpleCov.configure { fail "Shouldn't reach this!" }).to be false
end

it "allow to define a profile" do
expect do
SimpleCov.profiles.define "testprofile" do
add_filter "/config/"
end
end.not_to raise_error
end
end if RUBY_VERSION.start_with? "1.8"
46 changes: 46 additions & 0 deletions spec/command_guesser_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require "helper"

describe SimpleCov::CommandGuesser do
subject { SimpleCov::CommandGuesser }
it 'correctly guesses "Unit Tests" for unit tests' do
subject.original_run_command = "/some/path/test/units/foo_bar_test.rb"
expect(subject.guess).to eq("Unit Tests")
subject.original_run_command = "test/units/foo.rb"
expect(subject.guess).to eq("Unit Tests")
subject.original_run_command = "test/foo.rb"
expect(subject.guess).to eq("Unit Tests")
subject.original_run_command = "test/{models,helpers,unit}/**/*_test.rb"
expect(subject.guess).to eq("Unit Tests")
end

it 'correctly guesses "Functional Tests" for functional tests' do
subject.original_run_command = "/some/path/test/functional/foo_bar_controller_test.rb"
expect(subject.guess).to eq("Functional Tests")
subject.original_run_command = "test/{controllers,mailers,functional}/**/*_test.rb"
expect(subject.guess).to eq("Functional Tests")
end

it 'correctly guesses "Integration Tests" for integration tests' do
subject.original_run_command = "/some/path/test/integration/foo_bar_controller_test.rb"
expect(subject.guess).to eq("Integration Tests")
subject.original_run_command = "test/integration/**/*_test.rb"
expect(subject.guess).to eq("Integration Tests")
end

it 'correctly guesses "Cucumber Features" for cucumber features' do
subject.original_run_command = "features"
expect(subject.guess).to eq("Cucumber Features")
subject.original_run_command = "cucumber"
expect(subject.guess).to eq("Cucumber Features")
end

it 'correctly guesses "RSpec" for RSpec' do
subject.original_run_command = "/some/path/spec/foo.rb"
expect(subject.guess).to eq("RSpec")
end

it "defaults to RSpec because RSpec constant is defined" do
subject.original_run_command = "some_arbitrary_command with arguments"
expect(subject.guess).to eq("RSpec")
end
end if SimpleCov.usable?
12 changes: 12 additions & 0 deletions spec/deleted_source_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require "helper"

# Test to verify correct handling of deleted files
# See https://github.com/colszowka/simplecov/issues/9
describe "A source file which is subsequently deleted" do
it "does not cause an error" do
Dir.chdir(File.join(File.dirname(__FILE__), "fixtures")) do
`ruby deleted_source_sample.rb`
expect($?.exitstatus).to be_zero
end
end
end
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
40 changes: 40 additions & 0 deletions spec/file_list_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require "helper"

describe SimpleCov::Result do
subject do
original_result = {
source_fixture("sample.rb") => [nil, 1, 1, 1, nil, nil, 1, 1, nil, nil],
source_fixture("app/models/user.rb") => [nil, 1, 1, 1, nil, nil, 1, 0, nil, nil],
source_fixture("app/controllers/sample_controller.rb") => [nil, 2, 2, 0, nil, nil, 0, nil, nil, nil],
}
SimpleCov::Result.new(original_result).files
end

it "has 11 covered lines" do
expect(subject.covered_lines).to eq(11)
end

it "has 3 missed lines" do
expect(subject.missed_lines).to eq(3)
end

it "has 19 never lines" do
expect(subject.never_lines).to eq(19)
end

it "has 14 lines of code" do
expect(subject.lines_of_code).to eq(14)
end

it "has 3 skipped lines" do
expect(subject.skipped_lines).to eq(3)
end

it "has the correct covered percent" do
expect(subject.covered_percent).to eq(78.57142857142857)
end

it "has the correct covered strength" do
expect(subject.covered_strength).to eq(0.9285714285714286)
end
end if SimpleCov.usable?
95 changes: 95 additions & 0 deletions spec/filters_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
require "helper"

describe SimpleCov::SourceFile do
subject do
SimpleCov::SourceFile.new(source_fixture("sample.rb"), [nil, 1, 1, 1, nil, nil, 1, 0, nil, nil])
end

it "doesn't match a new SimpleCov::StringFilter 'foobar'" do
expect(SimpleCov::StringFilter.new("foobar")).not_to be_matches subject
end

it "doesn't match a new SimpleCov::StringFilter 'some/path'" do
expect(SimpleCov::StringFilter.new("some/path")).not_to be_matches subject
end

it "matches a new SimpleCov::StringFilter 'spec/fixtures'" do
expect(SimpleCov::StringFilter.new("spec/fixtures")).to be_matches subject
end

it "matches a new SimpleCov::StringFilter 'spec/fixtures/sample.rb'" do
expect(SimpleCov::StringFilter.new("spec/fixtures/sample.rb")).to be_matches subject
end

it "matches a new SimpleCov::StringFilter 'sample.rb'" do
expect(SimpleCov::StringFilter.new("sample.rb")).to be_matches subject
end

it "doesn't match a new SimpleCov::BlockFilter that is not applicable" do
expect(SimpleCov::BlockFilter.new(proc { |s| File.basename(s.filename) == "foo.rb" })).not_to be_matches subject
end

it "matches a new SimpleCov::BlockFilter that is applicable" do
expect(SimpleCov::BlockFilter.new(proc { |s| File.basename(s.filename) == "sample.rb" })).to be_matches subject
end

it "matches a new SimpleCov::ArrayFilter when 'sample.rb' is passed as array" do
expect(SimpleCov::ArrayFilter.new(["sample.rb"])).to be_matches subject
end

it "doesn't match a new SimpleCov::ArrayFilter when a file path different than 'sample.rb' is passed as array" do
expect(SimpleCov::ArrayFilter.new(["other_file.rb"])).not_to be_matches subject
end

it "matches a new SimpleCov::ArrayFilter when two file paths including 'sample.rb' are passed as array" do
expect(SimpleCov::ArrayFilter.new(["sample.rb", "other_file.rb"])).to be_matches subject
end

context "with no filters set up and a basic source file in an array" do
before do
@prev_filters, SimpleCov.filters = SimpleCov.filters, []
end

subject do
[SimpleCov::SourceFile.new(source_fixture("sample.rb"), [nil, 1, 1, 1, nil, nil, 1, 0, nil, nil])]
end

after do
SimpleCov.filters = @prev_filters
end

it 'returns 0 items after executing SimpleCov.filtered on files when using a "sample" string filter' do
SimpleCov.add_filter "sample"
expect(SimpleCov.filtered(subject).count).to be_zero
end

it 'returns 0 items after executing SimpleCov.filtered on files when using a "spec/fixtures" string filter' do
SimpleCov.add_filter "spec/fixtures"
expect(SimpleCov.filtered(subject).count).to be_zero
end

it 'returns 1 item after executing SimpleCov.filtered on files when using a "fooo" string filter' do
SimpleCov.add_filter "fooo"
expect(SimpleCov.filtered(subject).count).to eq(1)
end

it "returns 0 items after executing SimpleCov.filtered on files when using a block filter that returns true" do
SimpleCov.add_filter do
true
end
expect(SimpleCov.filtered(subject).count).to be_zero
end

it "returns 1 item after executing SimpleCov.filtered on files when using an always-false block filter" do
SimpleCov.add_filter do
false
end
expect(SimpleCov.filtered(subject).count).to eq(1)
end

it "returns a FileList after filtering" do
SimpleCov.add_filter "fooo"
expect(SimpleCov.filtered(subject)).to be_a SimpleCov::FileList
end
end
end if SimpleCov.usable?
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 1 addition & 6 deletions test/helper.rb → spec/helper.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
require "bundler/setup"
require "simplecov"
require "minitest/autorun"
require "shoulda"
require "rspec"

SimpleCov.coverage_dir("tmp/coverage")

def source_fixture(filename)
File.expand_path(File.join(File.dirname(__FILE__), "fixtures", filename))
end

require "shoulda_macros"
Minitest::Test.send :extend, ShouldaMacros

# Taken from http://stackoverflow.com/questions/4459330/how-do-i-temporarily-redirect-stderr-in-ruby
require "stringio"

Expand Down
Loading

0 comments on commit 739cf4d

Please sign in to comment.