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

Support custom result files #331

Merged
merged 2 commits into from
Jun 26, 2015
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ notification: false # Display notification after the specs are done running,
run_all: { cmd: 'custom rspec command', message: 'custom message' } # Custom options to use when running all specs
title: 'My project' # Display a custom title for the notification, default: 'RSpec results'
chdir: 'directory' # run rspec from within a given subdirectory (useful if project has separate specs for submodules)
results_file: 'some/path' # use the given file for storing results (instead of default relative path)
```

### Using Launchy to view rspec results
Expand Down
3 changes: 2 additions & 1 deletion lib/guard/rspec/rspec_process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def _run
end

def _really_run
pid = Kernel.spawn(command) # use spawn to stub in JRuby
env = { "GUARD_RSPEC_RESULTS_FILE" => formatter_tmp_file }
pid = Kernel.spawn(env, command) # use spawn to stub in JRuby
result = Process.wait2(pid)
result.last.exitstatus
rescue Errno::ENOENT => ex
Expand Down
15 changes: 9 additions & 6 deletions lib/guard/rspec/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,20 @@ def reload

def _run(paths, options, &block)
fail NoCmdOptionError unless options[:cmd]
_really_run(paths, options, &block)
command = Command.new(paths, options)
_really_run(command, options, &block)
true
rescue RSpecProcess::Failure, NoCmdOptionError => ex
Compat::UI.error(ex.to_s)
notifier.notify_failure
false
end

def _really_run(paths, options)
def _really_run(cmd, options)
# TODO: add option to specify the file
file = _tmp_file(options[:chdir])
file = _results_file(options[:results_file], options[:chdir])

process = RSpecProcess.new(Command.new(paths, options), file)
process = RSpecProcess.new(cmd, file)
results = process.results

inspector.failed(results.failed_paths)
Expand All @@ -78,8 +79,10 @@ def _open_launchy
::Launchy.open(options[:launchy]) if pn.exist?
end

def _tmp_file(chdir)
chdir ? File.join(chdir, TEMPORARY_FILE_PATH) : TEMPORARY_FILE_PATH
def _results_file(results_file, chdir)
results_file ||= TEMPORARY_FILE_PATH
return results_file unless Pathname(results_file).relative?
chdir ? File.join(chdir, results_file) : results_file
end
end
end
Expand Down
10 changes: 7 additions & 3 deletions lib/guard/rspec_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

module Guard
class RSpecFormatter < ::RSpec::Core::Formatters::BaseFormatter
TEMPORARY_FILE_PATH ||= "tmp/rspec_guard_result"

def self.rspec_3?
::RSpec::Core::Version::STRING.split(".").first == "3"
end
Expand Down Expand Up @@ -82,7 +80,7 @@ def write_summary(duration, total, failures, pending)
end

def _write(&block)
file = File.expand_path(TEMPORARY_FILE_PATH)
file = _results_file
FileUtils.mkdir_p(File.dirname(file))
File.open(file, "w", &block)
end
Expand All @@ -109,5 +107,11 @@ def _status_failed?(example)
example.execution_result[:status].to_s == "failed"
end
end

def _results_file
path = ENV["GUARD_RSPEC_RESULTS_FILE"]
fail "Fatal: No output file given for Guard::RSpec formatter!" unless path
File.expand_path(path)
end
end
end
7 changes: 7 additions & 0 deletions spec/lib/guard/rspec/command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@
end

context "with no RSpec defined formatter" do
before do
allow(RSpec::Core::ConfigurationOptions).to receive(:new) do
instance_double(RSpec::Core::ConfigurationOptions,
options: { formatters: nil })
end
end

it "sets default progress formatter" do
expect(command).to match %r{-f progress}
end
Expand Down
9 changes: 5 additions & 4 deletions spec/lib/guard/rspec/rspec_process_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
end

before do
allow(Kernel).to receive(:spawn).
with({ "GUARD_RSPEC_RESULTS_FILE" => file }, cmd).and_return(pid)

allow(Guard::RSpec::Results).to receive(:new).
with("foobar.txt").and_return(results)
with(file).and_return(results)
end

context "with an non-existing command" do
Expand All @@ -41,7 +44,6 @@

context "with an existing command" do
before do
allow(Kernel).to receive(:spawn).with(cmd).and_return(pid)
allow(Process).to receive(:wait2).with(pid).and_return(wait_result)
end

Expand All @@ -65,8 +67,7 @@
end

context "with no failures" do
it "spawns and waits" do
expect(Kernel).to receive(:spawn).with(cmd).and_return(pid)
it "waits for process to end" do
expect(Process).to receive(:wait2).with(pid).and_return(wait_result)
subject
end
Expand Down
75 changes: 60 additions & 15 deletions spec/lib/guard/rspec/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,66 @@
end
end

context "with a custom results file" do
let(:options) do
{ cmd: "rspec", results_file: results_file }.merge(chdir_options)
end

context "with no chdir option" do
let(:chdir_options) { {} }

context "when the path is relative" do
let(:results_file) { "foobar.txt" }
it "uses the given file" do
expect(Guard::RSpec::RSpecProcess).to receive(:new).
with(anything, results_file).and_return(process)
runner.run(paths)
end
end

context "when the path is absolute" do
let(:results_file) { "/foo/foobar.txt" }
it "uses the given path" do
expect(Guard::RSpec::RSpecProcess).to receive(:new).
with(anything, results_file).and_return(process)
runner.run(paths)
end
end
end

context "with chdir option" do
let(:chdir_options) { { chdir: "moduleA" } }

context "when the path is relative" do
let(:results_file) { "foobar.txt" }

it "uses a path relative to chdir" do
expect(Guard::RSpec::RSpecProcess).to receive(:new).
with(anything, "moduleA/foobar.txt").and_return(process)
runner.run(paths)
end
end

context "when the path is absolute" do
let(:results_file) { "/foo/foobar.txt" }
it "uses the full given path anyway" do
expect(Guard::RSpec::RSpecProcess).to receive(:new).
with(anything, results_file).and_return(process)
runner.run(paths)
end
end
end
end

context "with no custom results file" do
let(:options) { { cmd: "rspec" } }
it "uses the default" do
expect(Guard::RSpec::RSpecProcess).to receive(:new).
with(anything, "tmp/rspec_guard_result").and_return(process)
runner.run(paths)
end
end

it "notifies inspector about failed paths" do
expect(inspector).to receive(:failed).with([])
runner.run(paths)
Expand Down Expand Up @@ -225,19 +285,4 @@
runner.run(paths)
end
end

# TODO: remove / cleanup
describe "_tmp_file" do
subject { described_class.new.send(:_tmp_file, chdir) }

context "with no chdir option" do
let(:chdir) { nil }
it { is_expected.to eq("tmp/rspec_guard_result") }
end

context "chdir option" do
let(:chdir) { "moduleA" }
it { is_expected.to eq("moduleA/tmp/rspec_guard_result") }
end
end
end
14 changes: 8 additions & 6 deletions spec/lib/guard/rspec_formatter_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
require "guard/rspec_formatter"

RSpec.describe Guard::RSpecFormatter do
describe "::TEMPORARY_FILE_PATH" do
subject { Pathname.new(described_class::TEMPORARY_FILE_PATH) }
it { is_expected.to be_relative }
end

describe "#dump_summary" do
def rspec_summary_args(*args)
return args unless ::RSpec::Core::Version::STRING.start_with?("3.")
Expand Down Expand Up @@ -53,8 +48,15 @@ def rspec_summary_args(*args)
context "without stubbed IO" do
let(:stub_formatter) { false }

around do |example|
env_var = "GUARD_RSPEC_RESULTS_FILE"
old, ENV[env_var] = ENV[env_var], "foobar.txt"
example.run
ENV[env_var] = old
end

it "creates temporary file and and writes to it" do
file = File.expand_path(described_class::TEMPORARY_FILE_PATH)
file = File.expand_path("foobar.txt")

expect(FileUtils).to receive(:mkdir_p).
with(File.dirname(file)) {}
Expand Down