Merge pull request #3289 from reitermarkus/cask-loader
Fix regexes in `CaskLoader`.
This commit is contained in:
commit
91ab116ace
@ -3,6 +3,18 @@ module Hbc
|
|||||||
class FromContentLoader
|
class FromContentLoader
|
||||||
attr_reader :content
|
attr_reader :content
|
||||||
|
|
||||||
|
def self.can_load?(ref)
|
||||||
|
return false unless ref.respond_to?(:to_str)
|
||||||
|
content = ref.to_str
|
||||||
|
|
||||||
|
token = /(?:"[^"]*"|'[^']*')/
|
||||||
|
curly = /\(\s*#{token}\s*\)\s*\{.*\}/
|
||||||
|
do_end = /\s+#{token}\s+do(?:\s*;\s*|\s+).*end/
|
||||||
|
regex = /\A\s*cask(?:#{curly.source}|#{do_end.source})\s*\Z/m
|
||||||
|
|
||||||
|
content.match?(regex)
|
||||||
|
end
|
||||||
|
|
||||||
def initialize(content)
|
def initialize(content)
|
||||||
@content = content
|
@content = content
|
||||||
end
|
end
|
||||||
@ -56,7 +68,8 @@ module Hbc
|
|||||||
|
|
||||||
class FromURILoader < FromPathLoader
|
class FromURILoader < FromPathLoader
|
||||||
def self.can_load?(ref)
|
def self.can_load?(ref)
|
||||||
ref.to_s.match?(::URI::DEFAULT_PARSER.make_regexp)
|
uri_regex = ::URI::DEFAULT_PARSER.make_regexp
|
||||||
|
ref.to_s.match?(Regexp.new('\A' + uri_regex.source + '\Z', uri_regex.options))
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_reader :url
|
attr_reader :url
|
||||||
@ -156,15 +169,9 @@ module Hbc
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.for(ref)
|
def self.for(ref)
|
||||||
if ref.respond_to?(:to_str)
|
|
||||||
content = ref.to_str
|
|
||||||
if content.match?(/\A\s*cask\s+(?:"[^"]*"|'[^']*')\s+do(?:\s+.*\s+|;?\s+)end\s*\Z/)
|
|
||||||
return FromContentLoader.new(content)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
[
|
[
|
||||||
FromInstanceLoader,
|
FromInstanceLoader,
|
||||||
|
FromContentLoader,
|
||||||
FromURILoader,
|
FromURILoader,
|
||||||
FromTapLoader,
|
FromTapLoader,
|
||||||
FromTapPathLoader,
|
FromTapPathLoader,
|
||||||
|
|||||||
@ -0,0 +1,57 @@
|
|||||||
|
describe Hbc::CaskLoader::FromContentLoader do
|
||||||
|
alias_matcher :be_able_to_load, :be_can_load
|
||||||
|
|
||||||
|
describe "::can_load?" do
|
||||||
|
it "returns true for Casks specified with `cask \"token\" do … end`" do
|
||||||
|
expect(described_class).to be_able_to_load <<~EOS
|
||||||
|
cask "token" do
|
||||||
|
end
|
||||||
|
EOS
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns true for Casks specified with `cask \"token\" do; end`" do
|
||||||
|
expect(described_class).to be_able_to_load <<~EOS
|
||||||
|
cask "token" do; end
|
||||||
|
EOS
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns true for Casks specified with `cask 'token' do … end`" do
|
||||||
|
expect(described_class).to be_able_to_load <<~EOS
|
||||||
|
cask 'token' do
|
||||||
|
end
|
||||||
|
EOS
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns true for Casks specified with `cask 'token' do; end`" do
|
||||||
|
expect(described_class).to be_able_to_load <<~EOS
|
||||||
|
cask 'token' do; end
|
||||||
|
EOS
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns true for Casks specified with `cask(\"token\") { … }`" do
|
||||||
|
expect(described_class).to be_able_to_load <<~EOS
|
||||||
|
cask("token") {
|
||||||
|
}
|
||||||
|
EOS
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns true for Casks specified with `cask(\"token\") {}`" do
|
||||||
|
expect(described_class).to be_able_to_load <<~EOS
|
||||||
|
cask("token") {}
|
||||||
|
EOS
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns true for Casks specified with `cask('token') { … }`" do
|
||||||
|
expect(described_class).to be_able_to_load <<~EOS
|
||||||
|
cask('token') {
|
||||||
|
}
|
||||||
|
EOS
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns true for Casks specified with `cask('token') {}`" do
|
||||||
|
expect(described_class).to be_able_to_load <<~EOS
|
||||||
|
cask('token') {}
|
||||||
|
EOS
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -0,0 +1,21 @@
|
|||||||
|
describe Hbc::CaskLoader::FromURILoader do
|
||||||
|
alias_matcher :be_able_to_load, :be_can_load
|
||||||
|
|
||||||
|
describe "::can_load?" do
|
||||||
|
it "returns true when given an URI" do
|
||||||
|
expect(described_class).to be_able_to_load(URI("http://example.com/"))
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns true when given a String which can be parsed to a URI" do
|
||||||
|
expect(described_class).to be_able_to_load("http://example.com/")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns false when given a String with Cask contents containing a URL" do
|
||||||
|
expect(described_class).not_to be_able_to_load <<~EOS
|
||||||
|
cask 'token' do
|
||||||
|
url 'http://example.com/'
|
||||||
|
end
|
||||||
|
EOS
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
x
Reference in New Issue
Block a user