Skip to content

Commit

Permalink
Merge pull request #287 from lesniakania/master
Browse files Browse the repository at this point in the history
Fixed watchdir option
  • Loading branch information
e2 committed Nov 6, 2014
2 parents 073dfa8 + bafc4ea commit c7889f1
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 74 deletions.
19 changes: 12 additions & 7 deletions lib/guard/rspec/command.rb
@@ -1,5 +1,5 @@
require 'rspec/core'
require 'pathname'
require "rspec/core"
require "pathname"

module Guard
class RSpec
Expand All @@ -11,7 +11,7 @@ class Command < String
def initialize(paths, options = {})
@paths = paths
@options = options
super(_parts.join(' '))
super(_parts.join(" "))
end

private
Expand All @@ -22,12 +22,17 @@ def _parts
parts << _guard_formatter
parts << "--failure-exit-code #{FAILURE_EXIT_CODE}"
parts << options[:cmd_additional_args] || ""
parts << paths.join(' ')
if chdir = options[:chdir]
paths.each do |path|
path.sub!("#{chdir}#{File::SEPARATOR}", "")
end
end
parts << paths.join(" ")
end

def _visual_formatter
return if _cmd_include_formatter?
_rspec_formatters || '-f progress'
_rspec_formatters || "-f progress"
end

def _rspec_formatters
Expand All @@ -36,9 +41,9 @@ def _rspec_formatters
config = ::RSpec::Core::ConfigurationOptions.new([])
config.parse_options if config.respond_to?(:parse_options)
formatters = config.options[:formatters] || nil
# RSpec's parser returns an array in the format [[formatter, output], ...], so match their format
# RSpec"s parser returns an array in the format [[formatter, output], ...], so match their format
# Construct a matching command line option, including output target
formatters && formatters.map { |formatter| "-f #{formatter.join ' -o '}" }.join(' ')
formatters && formatters.map { |formatter| "-f #{formatter.join " -o "}" }.join(" ")
end

def _cmd_include_formatter?
Expand Down
37 changes: 27 additions & 10 deletions lib/guard/rspec/formatter.rb
@@ -1,13 +1,13 @@
require 'guard/rspec'
require 'rspec/core/formatters/base_formatter'
require "guard/rspec"
require "rspec/core/formatters/base_formatter"

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"
::RSpec::Core::Version::STRING.split(".").first == "3"
end

if rspec_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 All @@ -35,7 +51,7 @@ def self.extract_spec_location(metadata)
return root_metadata[:location]
end

location = (metadata[:location] || "").split(':').first # rspec issue https://github.com/rspec/rspec-core/issues/1243
location = (metadata[:location] || "").split(":").first # rspec issue https://github.com/rspec/rspec-core/issues/1243
end

location
Expand All @@ -47,7 +63,7 @@ def self.spec_path?(path)
if File.const_defined?(:FNM_EXTGLOB) # ruby >= 2
flags |= File::FNM_EXTGLOB
end
File.fnmatch(::RSpec.configuration.pattern, path.sub(/:\d+\z/, ''), flags)
File.fnmatch(::RSpec.configuration.pattern, path.sub(/:\d+\z/, ""), flags)
end

def dump_summary(*args)
Expand All @@ -63,7 +79,7 @@ def dump_summary(*args)
write_summary(*args)
end
rescue
# nothing really we can do, at least don't kill the test runner
# nothing really we can do, at least don"t kill the test runner
end

# Write summary to temporary file for runner
Expand All @@ -77,12 +93,13 @@ 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
failed = examples.select { |e| e.execution_result[:status].to_s == 'failed' }
failed = examples.select { |e| e.execution_result[:status].to_s == "failed" }
failed.map { |e| self.class.extract_spec_location(e.metadata) }.sort.uniq
end

Expand Down
29 changes: 24 additions & 5 deletions lib/guard/rspec/inspectors/base_inspector.rb
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
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
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

0 comments on commit c7889f1

Please sign in to comment.