Skip to content

Instantly share code, notes, and snippets.

@ssaunier
Created March 22, 2014 19:43
Show Gist options
  • Save ssaunier/9713130 to your computer and use it in GitHub Desktop.
Save ssaunier/9713130 to your computer and use it in GitHub Desktop.
A rake runner for minitest, capturing the outcome of the test
# app/services/minitest_rake_runner.rb
require 'open3'
class MinitestRakeRunner
attr_reader :stdout_lines
class RakefileMissingError < StandardError; end
PATTERN = /(?<tests>\d+) (tests|runs), (?<assertions>\d+) assertions, (?<failures>\d+) failures, (?<errors>\d+) errors, (?<skips>\d+) skips/
def run(directory)
check_for_rakefile! directory
stdin, stdout, stderr = Open3.popen3('rake', chdir: directory)
@stdout_lines = stdout.readlines
parse_stdout
end
private
def parse_stdout
@stdout_lines.reverse.each do |line|
result = PATTERN.match line
if result
return Hash[result.names.collect(&:to_sym).
zip(result.captures.collect(&:to_i))]
end
end
end
def check_for_rakefile!(directory)
unless File.exists?(File.join(directory, 'Rakefile'))
raise RakefileMissingError.new("Could not find any Rakefile in #{directory}")
end
end
end
describe MinitestRakeRunner do
let(:runner) { MinitestRakeRunner.new }
let(:rake_directory) { File.expand_path("../../support/minitest_rake_runner/has_rake_file", __FILE__)}
let(:no_rake_directory) { File.expand_path("../../support/minitest_rake_runner/no_rake_file", __FILE__)}
describe "#run" do
it "should raise if the directory does not have a rakefile" do
expect { runner.run(no_rake_directory) }.to raise_error MinitestRakeRunner::RakefileMissingError
end
it "should not raise if the directory has a rakefile" do
expect { runner.run(rake_directory) }.not_to raise_error
end
it "should return the minitest results" do
result = runner.run(rake_directory)
expect(result[:tests]).to eq 1
expect(result[:assertions]).to eq 1
expect(result[:failures]).to eq 0
expect(result[:errors]).to eq 0
expect(result[:skips]).to eq 0
end
it "should expose the stdout of the rake job" do
runner.run(rake_directory)
expect(runner.stdout_lines).not_to be_blank
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment