Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix regexes in CaskLoader. #3289

Merged
merged 2 commits into from
Oct 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 15 additions & 8 deletions Library/Homebrew/cask/lib/hbc/cask_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ module CaskLoader
class FromContentLoader
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)
@content = content
end
Expand Down Expand Up @@ -56,7 +68,8 @@ def cask(header_token, **options, &block)

class FromURILoader < FromPathLoader
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

attr_reader :url
Expand Down Expand Up @@ -156,15 +169,9 @@ def self.load(ref)
end

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,
FromContentLoader,
FromURILoader,
FromTapLoader,
FromTapPathLoader,
Expand Down
57 changes: 57 additions & 0 deletions Library/Homebrew/test/cask/cask_loader/from_content_loader_spec.rb
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions Library/Homebrew/test/cask/cask_loader/from_uri_loader_spec.rb
Original file line number Diff line number Diff line change
@@ -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