Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bail on first error flag -b. #28

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* `assert` can now receive a custom failure message, which should help write
better custom assertions.

* Use `-b` to bail after first test failure

1.2.2 - 2014-11-05
==================

Expand Down
5 changes: 3 additions & 2 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ Description
-----------

Each test file is run in a forked process to avoid shared state. Once a failure
is found, you get a report detailing what failed and how to locate the error
and the rest of the file is skipped.
is found, you get a report detailing what failed and how to locate the error.

You can use the `scope` command around tests: it guarantees that no instance
variables are shared between tests.
Expand Down Expand Up @@ -149,6 +148,8 @@ with test helpers, use the `-r` flag:

If you want to check which version you are running, try the `-v` flag.

To bail after first test failure, use the `-b` flag.

Installation
------------

Expand Down
5 changes: 3 additions & 2 deletions bin/cutest
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby

if ARGV.empty?
puts "usage: cutest [-v] [-r lib] [-o test] [-s scope] file ..."
puts "usage: cutest [-v] [-b] [-r lib] [-o test] [-s scope] file ..."
exit
end

Expand All @@ -12,10 +12,11 @@ files = Clap.run ARGV,
"-r" => lambda { |file| require file },
"-o" => lambda { |name| cutest[:only] = name },
"-s" => lambda { |name| cutest[:scope] = name },
"-b" => lambda { cutest[:bail] = true },
"-v" => lambda { puts Cutest::VERSION }

if files.any?
success = Cutest.run(Dir[*files])

puts
exit(1) unless success
end
9 changes: 4 additions & 5 deletions lib/cutest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ class Cutest
end

def self.run(files)
status = files.all? do |file|
test_results = -> (file) do
run_file(file)

Process.wait2.last.success?
end

puts

status
cutest[:bail] ?
files.all?(&test_results) :
files.map(&test_results).all?
end

def self.run_file(file)
Expand Down
31 changes: 31 additions & 0 deletions test/run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,34 @@

assert_equal 0, $?.to_i
end

scope "bail on first error" do
test "exit on the first error" do
expected = ".\n" +
" test: \n" +
" line: assert false\n" +
" file: test/fixtures/outside_block.rb +5\n\n" +
"Cutest::AssertionFailed: expression returned false\n\n"

out = %x{./bin/cutest -b test/fixtures/outside_block.rb test/fixtures/failure.rb}

assert_equal(expected, out)
end

test "don't exit on the first error" do
expected = ".\n" +
" test: \n" +
" line: assert false\n" +
" file: test/fixtures/outside_block.rb +5\n\n" +
"Cutest::AssertionFailed: expression returned false\n" +
"\n" +
" test: failed assertion\n" +
" line: assert false\n" +
" file: test/fixtures/failure.rb +2\n\n" +
"Cutest::AssertionFailed: expression returned false\n\n"

out = %x{./bin/cutest test/fixtures/outside_block.rb test/fixtures/failure.rb}

assert_equal(expected, out)
end
end