Skip to content

Commit

Permalink
enforce a valid minimum DSL version in headers
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandwalker committed Nov 23, 2014
1 parent a1c1fc2 commit e2a0214
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/cask/source/path_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,17 @@ def load
# munge text
cask_string.sub!(%r{\A(\s*\#[^\n]*\n)+}, '');
if %r{\A\s*cask\s+:v([\d_]+)(test)?\s+=>\s+([\'\"])(\S+?)\3(?:\s*,\s*|\s+)do\s*\n}.match(cask_string)
dsl_version = $1
dsl_version_string = $1
test_cask = ! $2.nil?
header_name = $4
dsl_version = Gem::Version.new(dsl_version_string.gsub('_','.'))
superclass_name = test_cask ? 'TestCask' : 'Cask'
cask_string.sub!(%r{\A[^\n]+\n}, "class #{cask_class_name} < #{superclass_name}\n")
# todo enforce valid DSL version
# todo the minimum DSL version should be defined globally elsewhere
minimum_dsl_version = Gem::Version.new('1.0')
unless dsl_version >= minimum_dsl_version
raise CaskInvalidError.new(cask_name, "Bad header line: 'v#{dsl_version_string}' is less than required minimum version '#{minimum_dsl_version}'")
end
if header_name != cask_name
raise CaskInvalidError.new(cask_name, "Bad header line: '#{header_name}' does not match file name")
end
Expand Down
10 changes: 10 additions & 0 deletions test/cask/dsl_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@
end
end

describe "header line" do
it "requires a valid minimum DSL version in the header" do
err = lambda {
invalid_cask = Cask.load('invalid/invalid-header-version')
}.must_raise(CaskInvalidError)
err.message.must_include 'Bad header line'
err.message.must_include 'is less than required minimum version'
end
end

describe "sha256 stanza" do
it "lets you set checksum via sha256" do
ChecksumCask = Class.new(Cask)
Expand Down
9 changes: 9 additions & 0 deletions test/support/Casks/invalid/invalid-header-version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
cask :v0_9test => 'invalid-header-version' do
version '1.2.3'
sha256 '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853'

url TestHelper.local_binary_url('caffeine.zip')
homepage 'http://example.com/local-caffeine'

app 'Caffeine.app'
end

0 comments on commit e2a0214

Please sign in to comment.