Skip to content

Commit

Permalink
Revert "Revert "Start converting Minitest specs to RSpec ""
Browse files Browse the repository at this point in the history
  • Loading branch information
hannako authored Feb 9, 2021
1 parent 967f8d5 commit 7a2bb96
Show file tree
Hide file tree
Showing 22 changed files with 761 additions and 501 deletions.
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ group :development, :test do
gem "jasmine"
gem "jasmine_selenium_runner"
gem "pry-byebug"
gem "rspec-rails"
gem "rubocop-govuk"
gem "timecop"
gem "unparser", require: false
end
52 changes: 52 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
GEM
remote: https://rubygems.org/
specs:
abstract_type (0.0.7)
actioncable (6.0.3.4)
actionpack (= 6.0.3.4)
nio4r (~> 2.0)
Expand Down Expand Up @@ -56,8 +57,15 @@ GEM
minitest (~> 5.1)
tzinfo (~> 1.1)
zeitwerk (~> 2.2, >= 2.2.2)
adamantium (0.2.0)
ice_nine (~> 0.11.0)
memoizable (~> 0.4.0)
addressable (2.7.0)
public_suffix (>= 2.0.2, < 5.0)
anima (0.3.2)
abstract_type (~> 0.0.7)
adamantium (~> 0.2)
equalizer (~> 0.0.11)
ast (2.4.1)
better_errors (2.7.1)
coderay (>= 1.0.0)
Expand All @@ -78,6 +86,9 @@ GEM
xpath (~> 3.2)
childprocess (3.0.0)
coderay (1.1.3)
concord (0.1.6)
adamantium (~> 0.2.0)
equalizer (~> 0.0.9)
concurrent-ruby (1.1.8)
crack (0.4.3)
safe_yaml (~> 1.0.0)
Expand Down Expand Up @@ -125,6 +136,7 @@ GEM
docile (1.3.2)
domain_name (0.5.20190701)
unf (>= 0.0.5, < 1.0.0)
equalizer (0.0.11)
erubi (1.10.0)
execjs (2.7.0)
faraday (1.3.0)
Expand Down Expand Up @@ -183,6 +195,7 @@ GEM
domain_name (~> 0.5)
i18n (1.8.8)
concurrent-ruby (~> 1.0)
ice_nine (0.11.2)
jasmine (3.6.0)
jasmine-core (~> 3.6.0)
phantomjs
Expand All @@ -209,6 +222,8 @@ GEM
mini_mime (>= 0.1.1)
marcel (0.3.3)
mimemagic (~> 0.3.2)
memoizable (0.4.2)
thread_safe (~> 0.3, >= 0.3.1)
method_source (1.0.0)
middleware (0.1.0)
mime-types (3.3.1)
Expand All @@ -222,6 +237,13 @@ GEM
minitest (>= 5.0)
railties (>= 4.1)
mocha (1.11.2)
mprelude (0.1.0)
abstract_type (~> 0.0.7)
adamantium (~> 0.2.0)
concord (~> 0.1.5)
equalizer (~> 0.0.9)
ice_nine (~> 0.11.1)
procto (~> 0.0.2)
multi_test (0.1.2)
multipart-post (2.1.1)
netrc (0.11.0)
Expand All @@ -237,6 +259,7 @@ GEM
ast (~> 2.4.1)
phantomjs (2.1.1.0)
plek (4.0.0)
procto (0.0.3)
protobuf-cucumber (3.10.8)
activesupport (>= 3.2)
middleware
Expand Down Expand Up @@ -299,6 +322,23 @@ GEM
rexml (3.2.4)
rinku (2.0.6)
rouge (3.26.0)
rspec-core (3.10.1)
rspec-support (~> 3.10.0)
rspec-expectations (3.10.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-mocks (3.10.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-rails (4.0.2)
actionpack (>= 4.2)
activesupport (>= 4.2)
railties (>= 4.2)
rspec-core (~> 3.10)
rspec-expectations (~> 3.10)
rspec-mocks (~> 3.10)
rspec-support (~> 3.10)
rspec-support (3.10.2)
rubocop (0.87.1)
parallel (~> 1.10)
parser (>= 2.7.1.1)
Expand Down Expand Up @@ -385,6 +425,16 @@ GEM
unicorn (5.8.0)
kgio (~> 2.6)
raindrops (~> 0.7)
unparser (0.5.5)
abstract_type (~> 0.0.7)
adamantium (~> 0.2.0)
anima (~> 0.3.1)
concord (~> 0.1.5)
diff-lcs (~> 1.3)
equalizer (~> 0.0.9)
mprelude (~> 0.1.0)
parser (>= 2.6.5)
procto (~> 0.0.2)
webdrivers (4.4.1)
nokogiri (~> 1.6)
rubyzip (>= 1.3.0)
Expand Down Expand Up @@ -426,6 +476,7 @@ DEPENDENCIES
rails (= 6.0.3.4)
rails-controller-testing
rinku
rspec-rails
rubocop-govuk
sassc-rails
simplecov
Expand All @@ -434,6 +485,7 @@ DEPENDENCIES
timecop
uglifier
uk_postcode
unparser
webdrivers
webmock

Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ require File.expand_path("config/application", __dir__)
Rails.application.load_tasks

Rake::Task[:default].clear if Rake::Task.task_defined?(:default)
task default: %i[lint cucumber test jasmine:ci]
task default: %i[lint cucumber test spec jasmine:ci]
17 changes: 17 additions & 0 deletions features/support/minitest_rspec_patch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Whilst collections has two test suites (minitest and rspec) we need to
# patch the minitest assertions for the cucumber feature tests to pass.
# Without this patch, the feature tests error with "undefined method `+' for nil:NilClass (NoMethodError)"
# each time assert is called eg assert page.has_text?("Foo").

require "minitest"

module MiniTestAssertions
def self.extended(base)
base.extend(MiniTest::Assertions)
base.assertions = 0
end

attr_accessor :assertions
end

World(MiniTestAssertions)
219 changes: 219 additions & 0 deletions lib/parsers/convert_to_rspec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
# Taken from https://gist.github.com/seven1m/e375bcdf2864da0022f1

require "parser/current"
require "unparser"
require "fileutils"

class ConvertToRspec
attr_reader :minitest_folder, :rspec_folder

def initialize(minitest_folder, rspec_folder)
@minitest_folder = minitest_folder
@rspec_folder = rspec_folder
end

def go!
Dir["#{minitest_folder}/*_test.rb"].each do |test|
puts test
body = File.read(test)
test.gsub!(minitest_folder.to_s, rspec_folder.to_s)
new_path = test.gsub!(/_test\.rb$/, "_spec.rb")
# custom_fixes_before!(body, new_path)
describe!(body)
before!(body)
context_to_describe!(body)
should_to_it!(body)
test_to_it!(body)
assert_question!(body)
assert_cannot!(body)
assert_can!(body)
assert_equal!(body)
assert_empty!(body)
assert_include!(body)
assert_match!(body)
assert_response!(body)
assert_redirected_to!(body)
assert_nil!(body)
assert_not!(body)
assert!(body)
custom_fixes_after!(body, new_path)
FileUtils.mkdir_p(rspec_folder)
File.open(new_path, "w") { |f| f.write(body) }
puts
end
end

def describe!(body)
replace_line!(body) do |line|
if line =~ /class ([:\w]+)Test < .*::(TestCase|IntegrationTest)\s*$/
"RSpec.describe #{Regexp.last_match(1)} do"
end
if line =~ / ([:\w]+) < ComponentTestCase/
"RSpec.describe #{Regexp.last_match(1)} do"
end
end
end

def before!(body)
replace_line!(body) do |line|
if line =~ /(\s*)(setup do|def setup)/
"#{Regexp.last_match(1)}before do"
end
end
end

def context_to_describe!(body)
replace_line!(body) do |line|
if line =~ /^(\s*)context (['"])(\w+)(['"]) do\s*$/
"#{Regexp.last_match(1)}describe #{Regexp.last_match(2)}#{Regexp.last_match(3)}#{Regexp.last_match(4)} do"
end
end
end

def should_to_it!(body)
replace_line!(body) do |line|
if line =~ /^(\s*)should (['"])(.*)(['"]) do\s*$/
"#{Regexp.last_match(1)}it #{Regexp.last_match(2)}should #{Regexp.last_match(3)}#{Regexp.last_match(4)} do"
end
end
end

def test_to_it!(body)
replace_line!(body) do |line|
if line =~ /^(\s*)test (['"])(.*)(['"]) do\s*$/
"#{Regexp.last_match(1)}it #{Regexp.last_match(2)}#{Regexp.last_match(3)}#{Regexp.last_match(4)} do"
end
end
end

def assert_question!(body)
replace_line!(body) do |line|
if line =~ /^(\s*)assert (\!|not )(.*)\.(\w+)\?\s*$/
"#{Regexp.last_match(1)}expect(#{Regexp.last_match(3)}).to_not be_#{Regexp.last_match(4)}"
elsif line =~ /^(\s*)assert (.*)\.(\w+)\?\s*$/
"#{Regexp.last_match(1)}expect(#{Regexp.last_match(2)}).to be_#{Regexp.last_match(3)}"
end
end
end

def assert_equal!(body)
body.gsub!(/assert_equal(.*),\s*\n(.*)$/, "assert_equal\\1, \\2")
replace_line!(body) do |line|
if line =~ /^(\s*)(assert_equal.*)$/
(arg1, arg2) = get_args(line)
"#{Regexp.last_match(1)}expect(#{arg2}).to eq(#{arg1})"
end
end
end

def assert_cannot!(body)
replace_line!(body) do |line|
if line =~ /^(\s*)(assert_cannot.*)$/
(arg1, arg2, arg3) = get_args(line)
"#{Regexp.last_match(1)}expect(#{arg1}).to_not be_able_to(#{arg2}, #{arg3})"
end
end
end

def assert_can!(body)
replace_line!(body) do |line|
if line =~ /^(\s*)(assert_can.*)$/
(arg1, arg2, arg3) = get_args(line)
"#{Regexp.last_match(1)}expect(#{arg1}).to be_able_to(#{arg2}, #{arg3})"
end
end
end

def assert_include!(body)
replace_line!(body) do |line|
if line =~ /^(\s*)assert (!|not )?(.*)\.include\?\((.*)\)$/
negate = Regexp.last_match(2) ? "_not" : ""
"#{Regexp.last_match(1)}expect(#{Regexp.last_match(3)}).to#{negate} include(#{Regexp.last_match(4)})"
end
end
end

def assert_match!(body)
body.gsub!(/assert_match(.*),\s*\n(.*)$/, "assert_match\\1, \\2")
replace_line!(body) do |line|
if line =~ /^(\s*)(assert_match.*)$/
(arg1, arg2) = get_args(line)
"#{Regexp.last_match(1)}expect(#{arg2}).to match(#{arg1})"
end
end
end

def assert_response!(body)
replace_line!(body) do |line|
if line =~ /^(\s*)assert_response :(.*)$/
"#{Regexp.last_match(1)}expect(response).to be_#{Regexp.last_match(2)}"
end
end
end

def assert_redirected_to!(body)
replace_line!(body) do |line|
if line =~ /^(\s*)assert_redirected_to (.*)$/
"#{Regexp.last_match(1)}expect(response).to redirect_to(#{Regexp.last_match(2)})"
end
end
end

def assert_nil!(body)
replace_line!(body) do |line|
if line =~ /^(\s*)assert_nil (.*)$/
"#{Regexp.last_match(1)}expect(#{Regexp.last_match(2)}).to be_nil"
end
end
end

def assert_empty!(body)
replace_line!(body) do |line|
if line =~ /^(\s*)assert_empty (.*)$/
"#{Regexp.last_match(1)}expect(#{Regexp.last_match(2)}).to be_empty"
end
end
end

def assert_not!(body)
replace_line!(body) do |line|
if line =~ /^(\s*)assert (!|not )(.*)\s*$/
"#{Regexp.last_match(1)}expect(#{Regexp.last_match(3)}).not_to be"
end
end
end

def assert!(body)
replace_line!(body) do |line|
if line =~ /^(\s*)assert (.*)\s*$/
"#{Regexp.last_match(1)}expect(#{Regexp.last_match(2)}).to be"
end
end
end

def replace_line!(body)
lines = body.split(/\n/)
lines.each_with_index do |line, index|
begin
new_line = yield(line, index)
rescue StandardError
puts "----------------------------------------"
puts "line number #{index + 1}:"
puts line.strip
puts "----------------------------------------"
raise
end
lines[index] = new_line if new_line
end
body.replace(lines.join("\n"))
end

# custom fixes to be run _after_ all the transformations
def custom_fixes_after!(body, path); end

def get_args(line)
ast = Parser::CurrentRuby.parse(line)
args = ast.to_a[2..-1]
args.map { |a| Unparser.unparse(a) }
end
end
Loading

0 comments on commit 7a2bb96

Please sign in to comment.