Skip to content

Commit

Permalink
support before :all and block after :all + around :all
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Jun 18, 2023
1 parent 5055c0f commit 4ef87f5
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Next
- support `before :all`
- block invalid `after :all` and `around :all`

# v5.0.0
- add minitest 5.14 support
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Features
- **Ctrl+c** stops tests and prints failures
- **pastable rerun snippet** for failures (disabled/integrated on rails 5)
- multiple before & after blocks
- `before :all` blocks
- **around** blocks `around { |t| Dir.chdir(...) { t.call } }`
- **red-green** output (disabled/integrated on rails 5)
- `mtest` executable to **run by line number** and by folder (disabled/integrated on rails 5)
Expand Down
5 changes: 4 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ task :update do
# replace ruby with mtest
raise unless code.sub!(%{output = "ruby \#{file} -l \#{line}"}, %{output = "mtest \#{file}:\#{line}"})
elsif url.end_with?('/around/spec.rb')
# do not fail with resume for nill class when before was never called
# do not fail with resume for nil class when before was never called
# for example when putting <% raise %> into a fixture file
raise unless code.sub!(%{fib.resume unless fib == :failed}, %{fib.resume if fib && fib != :failed})

# make `after :all` blow up to avoid confusion
raise unless code.sub!(%{fib = nil}, %{raise ArgumentError, "only :each or no argument is supported" if args != [] && args != [:each]\n fib = nil})
elsif url.end_with?('/rg_plugin.rb')
# support disabling/enabling colors
# https://github.com/blowmage/minitest-rg/pull/15
Expand Down
1 change: 1 addition & 0 deletions lib/maxitest/autorun.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require "maxitest/trap"
require "maxitest/let_bang"
require "maxitest/let_all"
require "maxitest/hook_all"
require "maxitest/pending"
require "maxitest/xit"
require "maxitest/static_class_order"
Expand Down
29 changes: 29 additions & 0 deletions lib/maxitest/hook_all.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Maxitest
class << self
attr_accessor :hook_all_counter
end
end
Maxitest.hook_all_counter = 0

module Maxitest
module HookAll
[:before, :after].each do |hook|
# minitest discards the type argument, so we are not sending it along
define_method(hook) do |type = :each, &block|
case type
when :each then super(&block)
when :all
raise ArgumentError, ":all is not supported in after" if hook == :after
c = (Maxitest.hook_all_counter += 1)
callback = :"maxitest_hook_all_#{c}"
let_all(callback, &block)
super() { send callback }
else
raise ArgumentError, "only :each and :all are supported"
end
end
end
end
end

Minitest::Spec::DSL.prepend(Maxitest::HookAll)
1 change: 1 addition & 0 deletions lib/maxitest/let_all.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def let_all(name, &block)
cache.first
end
end

def self.included(base)
base.extend(self)
end
Expand Down
1 change: 1 addition & 0 deletions lib/maxitest/vendor/around.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def run(*args)
# - execute test
# - resume fiber to execute last part
def around(*args, &block)
raise ArgumentError, "only :each or no argument is supported" if args != [] && args != [:each]
fib = nil
before do
fib = Fiber.new do |context, resume|
Expand Down
2 changes: 1 addition & 1 deletion spec/cases/around.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
let(:calls) { [] }

before { calls << 1 }
around { |test| calls << 2; test.call }
around((ENV["HOOK_TYPE"] || "each").to_sym) { |test| calls << 2; test.call }
around { |test| calls << 3; test.call }
after { calls.must_equal [1,2,3,4] }
after { calls << 4 }
Expand Down
42 changes: 42 additions & 0 deletions spec/cases/hook_all.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require "./spec/cases/helper"

hook_method = (ENV["HOOK_METHOD"] || "before").to_sym
hook_type = (ENV["HOOK_TYPE"] || "all").to_sym

# need this globally or classes don't sort
r = Minitest::Runnable.runnables
def r.shuffle
self
end

describe 2 do
order_dependent!

send hook_method, hook_type do
puts "ALL"
end

it "works" do
puts "T1"
end

describe "subclass" do
order_dependent!

send hook_method, hook_type do
puts "ALL-SUB"
end

it "still works" do
puts "TS1"
end

it "yes it does" do
puts "TS2"
end
end

it "works after subclass" do
puts "T2"
end
end
37 changes: 32 additions & 5 deletions spec/maxitest_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
run_cmd("ruby spec/cases/plain.rb").should include "\n2 runs, 2 assertions"
end

it "supports around" do
run_cmd("ruby spec/cases/around.rb").should include "\n2 runs, 3 assertions"
end

it "supports context" do
run_cmd("ruby spec/cases/context.rb").should include "\n2 runs, 2 assertions"
end
Expand Down Expand Up @@ -75,6 +71,38 @@
output_in.should include 'spec/cases/raise.rb:11'
end

describe "before/after/around" do
it "works" do
out = run_cmd("ruby spec/cases/hook_all.rb")
out.should include "Running:\n\nALL\nT1\n.T2\n.ALL-SUB\nTS1\n.TS2\n.\n\nFinished"
end

it "fails when using unsupported type" do
with_env HOOK_TYPE: "foo" do
out = run_cmd("ruby spec/cases/hook_all.rb", fail: true)
out.should include "only :each and :all are supported (ArgumentError)"
end
end

it "informs user about missing after :all" do
with_env HOOK_METHOD: "after" do
out = run_cmd("ruby spec/cases/hook_all.rb --seed 123", fail: true)
out.should include ":all is not supported in after (ArgumentError)"
end
end

it "supports around" do
run_cmd("ruby spec/cases/around.rb").should include "\n2 runs, 3 assertions"
end

it "informs user about missing around :all" do
with_env HOOK_TYPE: "all" do
out = run_cmd("ruby spec/cases/around.rb", fail: true)
out.should include "only :each or no argument is supported (ArgumentError)"
end
end
end

describe "color" do
it "is color-less without tty" do
run_cmd("ruby spec/cases/plain.rb").should include "\n2 runs, 2 assertions"
Expand Down Expand Up @@ -315,7 +343,6 @@ def run_cmd(command, options = {})
stdout.strip
end


# copied from https://github.com/grosser/parallel/blob/master/spec/parallel_spec.rb#L10-L15
def kill_process_with_name(file, signal='INT')
running_processes = `ps -f`.split("\n").map{ |line| line.split(/\s+/) }
Expand Down

0 comments on commit 4ef87f5

Please sign in to comment.