Skip to content

Commit

Permalink
Implemented chdir option
Browse files Browse the repository at this point in the history
  • Loading branch information
Ania Slimak committed Oct 18, 2014
1 parent afe429c commit a64bf07
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 25 deletions.
5 changes: 5 additions & 0 deletions lib/guard/rspec/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ def _parts
parts << _guard_formatter
parts << "--failure-exit-code #{FAILURE_EXIT_CODE}"
parts << options[:cmd_additional_args] || ""
if chdir = options[:chdir]
paths.each do |path|
path.sub!("#{chdir}#{File::SEPARATOR}", '')
end
end
parts << paths.join(' ')
end

Expand Down
23 changes: 20 additions & 3 deletions lib/guard/rspec/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
module Guard
class RSpec
class Formatter < ::RSpec::Core::Formatters::BaseFormatter
TEMPORARY_FILE_PATH ||= File.expand_path('./tmp/rspec_guard_result')
TEMPORARY_FILE_PATH ||= './tmp/rspec_guard_result'

def self.rspec_3?
::RSpec::Core::Version::STRING.split('.').first == "3"
Expand All @@ -22,6 +22,22 @@ def examples
end
end

def self.paths_with_chdir(paths, chdir)
paths.map do |path|
path_with_chdir(path, chdir)
end
end

def self.path_with_chdir(path, chdir)
return path unless chdir

File.join(chdir, path)
end

def self.tmp_file(chdir)
path_with_chdir(TEMPORARY_FILE_PATH, chdir)
end

# rspec issue https://github.com/rspec/rspec-core/issues/793
def self.extract_spec_location(metadata)
root_metadata = metadata
Expand Down Expand Up @@ -77,8 +93,9 @@ def write_summary(duration, total, failures, pending)
private

def _write(&block)
FileUtils.mkdir_p(File.dirname(TEMPORARY_FILE_PATH))
File.open(TEMPORARY_FILE_PATH, 'w', &block)
file = File.expand_path(TEMPORARY_FILE_PATH)
FileUtils.mkdir_p(File.dirname(file))
File.open(file, 'w', &block)
end

def _failed_paths
Expand Down
29 changes: 24 additions & 5 deletions lib/guard/rspec/inspectors/base_inspector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class BaseInspector
def initialize(options = {})
@options = options
@spec_paths = @options[:spec_paths]
@chdir = @options[:chdir]
end

def paths(paths)
Expand All @@ -33,18 +34,36 @@ def _clean(paths)
paths.compact!
spec_dirs = _select_only_spec_dirs(paths)
spec_files = _select_only_spec_files(paths)
spec_dirs + spec_files
(spec_dirs + spec_files).uniq
end

def _select_only_spec_dirs(paths)
paths.select { |p| File.directory?(p) || spec_paths.include?(p) }
paths.select do |path|
File.directory?(path) ||
_spec_paths_with_chdir.include?(path)
end
end

def _select_only_spec_files(paths)
spec_files = spec_paths.collect { |path| Dir[File.join(path, "**{,/*/**}", "*[_.]spec.rb")] }
feature_files = spec_paths.collect { |path| Dir[File.join(path, "**{,/*/**}", "*.feature")] }
spec_files = _collect_files("*[_.]spec.rb")
feature_files = _collect_files("*.feature")
files = (spec_files + feature_files).flatten
paths.select { |p| files.include?(p) }

paths.select do |path|
files.any? do |file|
file == Formatter.path_with_chdir(path, @chdir)
end
end
end

def _spec_paths_with_chdir
Formatter.paths_with_chdir(spec_paths, @chdir)
end

def _collect_files(pattern)
_spec_paths_with_chdir.collect do |path|
Dir[File.join(path, "**{,/*/**}", pattern)]
end
end
end
end
Expand Down
9 changes: 6 additions & 3 deletions lib/guard/rspec/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,16 @@ def _command_success?(success)
end

def _command_output
formatter_tmp_file = Formatter::TEMPORARY_FILE_PATH
formatter_tmp_file = Formatter.tmp_file(options[:chdir])
lines = File.readlines(formatter_tmp_file)
[lines.first.strip, lines[1..11].map(&:strip).compact]
summary = lines.first.strip
failed_paths = lines[1..11].map(&:strip).compact

[summary, failed_paths]
rescue
[nil, nil]
ensure
File.exist?(formatter_tmp_file) && File.delete(formatter_tmp_file)
File.delete(formatter_tmp_file) if File.exists?(formatter_tmp_file)
end

def _open_launchy
Expand Down
20 changes: 20 additions & 0 deletions spec/lib/guard/rspec/command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@
expect(command).to match %r{-f progress}
end
end

context ":chdir option present" do
let(:chdir) { "moduleA" }
let(:paths) do
%w[path1 path2].map { |p| "#{chdir}#{File::Separator}#{p}" }
end

let(:options) do
{
cmd: "cd #{chdir} && rspec",
chdir: chdir
}
end

it "removes chdir part from the path
as it should be present in the cmd" do

expect(command).to match %r{path1 path2}
end
end
end

end
69 changes: 58 additions & 11 deletions spec/lib/guard/rspec/formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,49 @@
require 'guard/rspec/formatter'

describe Guard::RSpec::Formatter do

describe '::TEMPORARY_FILE_PATH' do
it 'is absolute path' do
require 'pathname'
temporary_file_path = described_class.const_get(:TEMPORARY_FILE_PATH)
expect(Pathname.new(temporary_file_path).absolute?).to eq(true)
require 'pathname'
let(:temporary_file_path) { described_class::TEMPORARY_FILE_PATH }

subject { Pathname.new(temporary_file_path).absolute? }

it 'is relative path' do
expect(subject).to eq(false)
end
end

describe '.tmp_file' do
let(:chdir) { nil }

subject { described_class.tmp_file(chdir) }

it { expect(subject).to eq(described_class::TEMPORARY_FILE_PATH) }

context "chdir option present" do
let(:chdir) { 'moduleA' }

it do
expect(subject).to eq(
"#{chdir}/#{described_class::TEMPORARY_FILE_PATH}")
end
end
end

describe '.paths_with_chdir' do
let(:paths) { %w[path1 path2] }
let(:chdir) { nil }

subject { described_class.paths_with_chdir(paths, chdir) }

it { expect(subject).to eq(paths) }

context "chdir option present" do
let(:chdir) { 'moduleA' }

it do
expect(subject).to eq(paths.map { |p| "#{chdir}/#{p}" })
end
end
end

Expand All @@ -32,9 +70,13 @@
}

it 'creates temporary file and and writes to it' do
temporary_file_path = described_class.const_get(:TEMPORARY_FILE_PATH)
expect(FileUtils).to receive(:mkdir_p).with(File.dirname(temporary_file_path)) {}
expect(File).to receive(:open).with(temporary_file_path, 'w') { |filename, mode, &block| block.call writer }
file = File.expand_path(described_class::TEMPORARY_FILE_PATH)
expect(FileUtils).to receive(:mkdir_p).
with(File.dirname(file)) {}
expect(File).to receive(:open).
with(file, 'w') do |filename, mode, &block|
block.call writer
end
formatter.write_summary(123, 1, 2, 3)
end
end
Expand All @@ -48,16 +90,20 @@
metadata: { location: spec_filename }
) }

def expected_output(spec_filename)
/^3 examples, 1 failures in 123\.0 seconds\n#{spec_filename}\n$/
end

it 'writes summary line and failed location in tmp dir' do
allow(formatter).to receive(:examples) { [failed_example] }
formatter.write_summary(123, 3, 1, 0)
expect(result).to match /^3 examples, 1 failures in 123\.0 seconds\n#{spec_filename}\n$/
expect(result).to match expected_output(spec_filename)
end

it 'writes only uniq filenames out' do
allow(formatter).to receive(:examples) { [failed_example, failed_example] }
formatter.write_summary(123, 3, 1, 0)
expect(result).to match /^3 examples, 1 failures in 123\.0 seconds\n#{spec_filename}\n$/
expect(result).to match expected_output(spec_filename)
end

context "for rspec 3" do
Expand All @@ -71,7 +117,7 @@
it 'writes summary line and failed location' do
allow(formatter).to receive(:examples) { [failed_example] }
formatter.dump_summary(notification)
expect(result).to match /^3 examples, 1 failures in 123\.0 seconds\n#{spec_filename}\n$/
expect(result).to match expected_output(spec_filename)
end
end
end
Expand All @@ -82,7 +128,8 @@
example_group: { location: './spec/requests/breadcrumbs_spec.rb:218' }
}

expect(described_class.extract_spec_location(metadata)).to start_with './spec/requests/breadcrumbs_spec.rb'
result = described_class.extract_spec_location(metadata)
expect(result).to start_with './spec/requests/breadcrumbs_spec.rb'
end

# Skip location because of rspec issue https://github.com/rspec/rspec-core/issues/1243
Expand Down
81 changes: 78 additions & 3 deletions spec/lib/guard/rspec/inspectors/base_inspector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,94 @@
end

describe '#paths' do
it 'should not be emplemented here' do
it 'should not be implemented here' do
expect { inspector.paths(paths) }.to raise_error(abstract_error)
end

context "specific inspector" do
class FooInspector < Guard::RSpec::Inspectors::BaseInspector
def paths(paths)
_clean(paths)
end
end

let(:options) do
{
chdir: chdir,
spec_paths: ['spec']
}
end
let(:chdir) { nil }
let(:inspector) { FooInspector.new(options) }

subject { inspector.paths(paths) }

context "_select_only_spec_dirs" do
let(:paths) { ['spec'] }

it "returns matching paths" do
allow(File).to receive(:directory?).
with('spec').and_return(false)

expect(subject).to eq(paths)
end

context "chdir option present" do
let(:chdir) { 'moduleA' }
let(:paths) { ["#{chdir}/spec"] }

it "returns matching paths" do
allow(File).to receive(:directory?).
with('moduleA/spec').and_return(false)

expect(subject).to eq(paths)
end
end
end

context "_select_only_spec_files" do
let(:paths) do
["app/models/a_foo.rb", "spec/models/a_foo_spec.rb"]
end
let(:spec_files) do
[["spec/models/a_foo_spec.rb",
"spec/models/b_foo_spec.rb"]]
end

before do
allow(Dir).to receive(:[]).and_return(spec_files)
end

it "returns matching paths" do
expect(subject).to eq(['spec/models/a_foo_spec.rb'])
end

context "chdir option present" do
let(:chdir) { 'moduleA' }
let(:paths) do
["moduleA/models/a_foo.rb", "spec/models/a_foo_spec.rb"]
end
let(:spec_files) do
[["moduleA/spec/models/a_foo_spec.rb",
"moduleA/spec/models/b_foo_spec.rb"]]
end

it "returns matching paths" do
expect(subject).to eq(['spec/models/a_foo_spec.rb'])
end
end
end
end
end

describe '#failed' do
it 'should not be emplemented here' do
it 'should not be implemented here' do
expect { inspector.failed(paths) }.to raise_error(abstract_error)
end
end

describe '#reload' do
it 'should not be emplemented here' do
it 'should not be implemented here' do
expect { inspector.reload }.to raise_error(abstract_error)
end
end
Expand Down

0 comments on commit a64bf07

Please sign in to comment.