Skip to content

Commit

Permalink
detect unsupported RSpec glob pattern [fix #338]
Browse files Browse the repository at this point in the history
  • Loading branch information
e2 committed Jul 23, 2015
1 parent 07eb68e commit e71f765
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lib/guard/rspec_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ class RSpecFormatter < ::RSpec::Core::Formatters::BaseFormatter
NO_ENV_WARNING_MSG = "no environment passed - see #{WIKI_ENV_WARN_URL}"
NO_RESULTS_VALUE_MSG = ":results_file value unknown (using defaults)"

UNSUPPORTED_PATTERN = "Your RSpec.configuration.pattern uses characters "\
"unsupported by your Ruby version (File::FNM_EXTGLOB is undefined)"

class Error < RuntimeError
class UnsupportedPattern < Error
def initialize(msg = UNSUPPORTED_PATTERN)
super
end
end
end

def self.rspec_3?
::RSpec::Core::Version::STRING.split(".").first == "3"
end
Expand Down Expand Up @@ -54,12 +65,16 @@ def self.extract_spec_location(metadata)
end

def self.spec_path?(path)
path ||= ""
pattern = ::RSpec.configuration.pattern

flags = File::FNM_PATHNAME | File::FNM_DOTMATCH
if File.const_defined?(:FNM_EXTGLOB) # ruby >= 2
flags |= File::FNM_EXTGLOB
elsif pattern =~ /[{}]/
fail Error::UnsupportedPattern
end
pattern = ::RSpec.configuration.pattern

path ||= ""
path = path.sub(/:\d+\z/, "")
path = Pathname.new(path).cleanpath.to_s
File.fnmatch(pattern, path, flags)
Expand Down
24 changes: 24 additions & 0 deletions spec/lib/guard/rspec_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,5 +228,29 @@ def expected_output(spec_filename)
)
end
end

context "when RSpec 3.0 uses ext globs" do
before do
allow(::RSpec.configuration).to receive(:pattern).
and_return("**{,/*/**}/*_spec.rb")
end

context "when Ruby does not support ext glob matcher" do
before do
allow(File).to receive(:const_defined?).with(:FNM_EXTGLOB) { false }
end

let(:metadata) { { location: "./spec/foo_spec.rb:75" } }

it "fails" do
expect do
described_class.extract_spec_location(metadata)
end.to raise_error(
described_class::Error::UnsupportedPattern,
"Your RSpec.configuration.pattern uses characters unsupported "\
"by your Ruby version (File::FNM_EXTGLOB is undefined)")
end
end
end
end
end

0 comments on commit e71f765

Please sign in to comment.