Fixed implementation of Middleman::Util#path_match, added tests for it
This commit is contained in:
parent
8989e27769
commit
ad4b441dc3
|
@ -96,13 +96,17 @@ module Middleman
|
||||||
# to #match or #call, and returns whether or not the
|
# to #match or #call, and returns whether or not the
|
||||||
# given path matches that matcher.
|
# given path matches that matcher.
|
||||||
#
|
#
|
||||||
# @param matcher A matcher string/regexp/proc/etc
|
# @param [String, #match, #call] matcher A matcher String, RegExp, Proc, etc.
|
||||||
# @param path A path as a string
|
# @param [String] path A path as a string
|
||||||
# @return [Boolean] Whether the path matches the matcher
|
# @return [Boolean] Whether the path matches the matcher
|
||||||
def path_match(matcher, path)
|
def path_match(matcher, path)
|
||||||
case
|
!!case
|
||||||
when matcher.is_a?(String)
|
when matcher.is_a?(String)
|
||||||
path.match(matcher)
|
if matcher.include? '*'
|
||||||
|
File.fnmatch(matcher, path)
|
||||||
|
else
|
||||||
|
path == matcher
|
||||||
|
end
|
||||||
when matcher.respond_to?(:match)
|
when matcher.respond_to?(:match)
|
||||||
matcher.match(path)
|
matcher.match(path)
|
||||||
when matcher.respond_to?(:call)
|
when matcher.respond_to?(:call)
|
||||||
|
|
37
middleman-core/spec/middleman-core/path_match_spec.rb
Normal file
37
middleman-core/spec/middleman-core/path_match_spec.rb
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
require 'middleman-core/util'
|
||||||
|
|
||||||
|
describe "Middleman::Util#path_match" do
|
||||||
|
it "matches a literal string" do
|
||||||
|
expect(Middleman::Util.path_match '/index.html', '/index.html').to be true
|
||||||
|
end
|
||||||
|
|
||||||
|
it "won't match a wrong string" do
|
||||||
|
expect(Middleman::Util.path_match '/foo.html', '/index.html').to be false
|
||||||
|
end
|
||||||
|
|
||||||
|
it "won't match a partial string" do
|
||||||
|
expect(Middleman::Util.path_match 'ind', '/index.html').to be false
|
||||||
|
end
|
||||||
|
|
||||||
|
it "works with a regex" do
|
||||||
|
expect(Middleman::Util.path_match /\.html$/, '/index.html').to be true
|
||||||
|
expect(Middleman::Util.path_match /\.js$/, '/index.html').to be false
|
||||||
|
end
|
||||||
|
|
||||||
|
it "works with a proc" do
|
||||||
|
matcher = lambda {|p| p.length > 5 }
|
||||||
|
|
||||||
|
expect(Middleman::Util.path_match matcher, '/index.html').to be true
|
||||||
|
expect(Middleman::Util.path_match matcher, '/i').to be false
|
||||||
|
end
|
||||||
|
|
||||||
|
it "works with globs" do
|
||||||
|
expect(Middleman::Util.path_match '/foo/*.html', '/foo/index.html').to be true
|
||||||
|
expect(Middleman::Util.path_match '/foo/*.html', '/foo/index.js').to be false
|
||||||
|
expect(Middleman::Util.path_match '/bar/*.html', '/foo/index.js').to be false
|
||||||
|
|
||||||
|
expect(Middleman::Util.path_match '/foo/*', '/foo/bar/index.html').to be true
|
||||||
|
expect(Middleman::Util.path_match '/foo/**/*', '/foo/bar/index.html').to be true
|
||||||
|
expect(Middleman::Util.path_match '/foo/**', '/foo/bar/index.html').to be true
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue