Skip to content

Commit

Permalink
Merge pull request #5975 from rolandwalker/gpg_dsl_revise
Browse files Browse the repository at this point in the history
Revise gpg stanza order and parameters
  • Loading branch information
rolandwalker committed Sep 8, 2014
2 parents 294f890 + 07158e4 commit fea75ed
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 48 deletions.
44 changes: 20 additions & 24 deletions lib/cask/dsl/gpg.rb
Original file line number Diff line number Diff line change
@@ -1,40 +1,36 @@
class Cask::DSL::Gpg

VALID_TYPES = Set.new [
:id, # first one is the default
:url,
]
KEY_PARAMETERS = Set.new [
:key_id,
:key_url,
]

REQUIRED_PARAMETERS = [
:signature
]
VALID_PARAMETERS = Set.new [ ]
VALID_PARAMETERS.merge KEY_PARAMETERS

attr_reader :key, :parameters, :type, :signature
attr_accessor *VALID_PARAMETERS
attr_accessor :signature

def initialize(key, parameters={})
def initialize(signature, parameters={})
@parameters = parameters
REQUIRED_PARAMETERS.each do |param|
unless @parameters.key?(param) and not @parameters[param].nil?
raise "gpg #{param.inspect} parameter is required"
end
@signature = Cask::UnderscoreSupportingURI.parse(signature)
parameters.each do |hkey, hvalue|
raise "invalid 'gpg' parameter: '#{hkey.inspect}'" unless VALID_PARAMETERS.include?(hkey)
writer_method = "#{hkey}=".to_sym
hvalue = Cask::UnderscoreSupportingURI.parse(hvalue) if hkey == :key_url
send(writer_method, hvalue)
end
@key = key
@signature = Cask::UnderscoreSupportingURI.parse(@parameters[:signature])
@type = @parameters[:type]
@type = VALID_TYPES.first if @type.nil?
unless VALID_TYPES.include?(@type)
raise "invalid gpg type: '#{@type.inspect}'"
end
if @type == :url
@key = Cask::UnderscoreSupportingURI.parse(@key)
unless KEY_PARAMETERS.intersection(parameters.keys).length == 1
raise "'gpg' stanza must include exactly one of: '#{KEY_PARAMETERS.to_a}'"
end
end

def to_yaml
[@key, @parameters].to_yaml
# bug, :key_url value is not represented as an instance of Cask::UnderscoreSupportingURI
[@signature, @parameters].to_yaml
end

def to_s
@key.to_s
@signature.to_s
end
end
27 changes: 23 additions & 4 deletions test/cask/dsl_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,32 @@ def caveats; <<-EOS.undent
cask.gpg.to_s.must_match %r{\S}
end

it "prevents specifying gpg multiple times" do
it "allows gpg stanza to be specified with :key_url" do
cask = Cask.load('with-gpg-key-url')
cask.gpg.to_s.must_match %r{\S}
end

it "prevents specifying gpg stanza multiple times" do
err = lambda {
invalid_cask = Cask.load('invalid/invalid-gpg-multiple')
invalid_cask = Cask.load('invalid/invalid-gpg-multiple-stanzas')
}.must_raise(CaskInvalidError)
err.message.must_include "'gpg' stanza may only appear once"
end

it "prevents missing gpg key parameters" do
err = lambda {
invalid_cask = Cask.load('invalid/invalid-gpg-missing-key')
}.must_raise(CaskInvalidError)
err.message.must_include "'gpg' stanza must include exactly one"
end

it "prevents conflicting gpg key parameters" do
err = lambda {
invalid_cask = Cask.load('invalid/invalid-gpg-conflicting-keys')
}.must_raise(CaskInvalidError)
err.message.must_include "'gpg' stanza must include exactly one"
end

it "refuses to load invalid gpg signature URLs" do
err = lambda {
invalid_cask = Cask.load('invalid/invalid-gpg-signature-url')
Expand All @@ -178,9 +197,9 @@ def caveats; <<-EOS.undent
}.must_raise(CaskInvalidError)
end

it "refuses to load if gpg :type is invalid" do
it "refuses to load if gpg parameter is unknown" do
err = lambda {
invalid_cask = Cask.load('invalid/invalid-gpg-type')
invalid_cask = Cask.load('invalid/invalid-gpg-parameter')
}.must_raise(CaskInvalidError)
end
end
Expand Down
10 changes: 10 additions & 0 deletions test/support/Casks/invalid/invalid-gpg-conflicting-keys.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class InvalidGpgConflictingKeys < TestCask
url TestHelper.local_binary('caffeine.zip')
homepage 'http://example.com/invalid-gpg-conflicting-keys'
gpg 'http://example.com/gpg-signature.asc',
:key_id => 'ID',
:key_url => 'http://example.com/gpg-key-url'
sha256 '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853'
version '1.2.3'
link 'Caffeine.app'
end
4 changes: 2 additions & 2 deletions test/support/Casks/invalid/invalid-gpg-key-url.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class InvalidGpgKeyUrl < TestCask
url TestHelper.local_binary('caffeine.zip')
homepage 'http://example.com/invalid-gpg-key-url'
gpg 1, :type => :url,
:signature => 'http://example.com/gpg-signature.asc'
gpg 'http://example.com/gpg-signature.asc',
:key_url => 1
sha256 '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853'
version '1.2.3'
link 'Caffeine.app'
Expand Down
8 changes: 8 additions & 0 deletions test/support/Casks/invalid/invalid-gpg-missing-key.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class InvalidGpgMissingKeys < TestCask
url TestHelper.local_binary('caffeine.zip')
homepage 'http://example.com/invalid-gpg-missing-keys'
gpg 'http://example.com/gpg-signature.asc'
sha256 '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853'
version '1.2.3'
link 'Caffeine.app'
end
11 changes: 11 additions & 0 deletions test/support/Casks/invalid/invalid-gpg-multiple-stanzas.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class InvalidGpgMultipleStanzas < TestCask
url TestHelper.local_binary('caffeine.zip')
homepage 'http://example.com/invalid-gpg-multiple-stanzas'
gpg 'http://example.com/gpg-signature.asc',
:key_id => 'ID'
gpg 'http://example.com/gpg-signature.asc',
:key_id => 'ID'
sha256 '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853'
version '1.2.3'
link 'Caffeine.app'
end
11 changes: 0 additions & 11 deletions test/support/Casks/invalid/invalid-gpg-multiple.rb

This file was deleted.

9 changes: 9 additions & 0 deletions test/support/Casks/invalid/invalid-gpg-parameter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class InvalidGpgParameter < TestCask
url TestHelper.local_binary('caffeine.zip')
homepage 'http://example.com/invalid-gpg-type'
gpg 'http://example.com/gpg-signature.asc',
:no_such_parameter => :value
sha256 '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853'
version '1.2.3'
link 'Caffeine.app'
end
4 changes: 2 additions & 2 deletions test/support/Casks/invalid/invalid-gpg-signature-url.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class InvalidGpgSignatureUrl < TestCask
url TestHelper.local_binary('caffeine.zip')
homepage 'http://example.com/invalid-gpg-signature-url'
gpg 'ID', :type => :id,
:signature => 1
gpg 1,
:key_id => 'ID'
sha256 '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853'
version '1.2.3'
link 'Caffeine.app'
Expand Down
6 changes: 3 additions & 3 deletions test/support/Casks/invalid/invalid-gpg-type.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class InvalidGpgType < TestCask
class InvalidGpgParameter < TestCask
url TestHelper.local_binary('caffeine.zip')
homepage 'http://example.com/invalid-gpg-type'
gpg 'ID', :type => :no_such_type,
:signature => 'http://example.com/gpg-signature.asc'
gpg 'http://example.com/gpg-signature.asc',
:no_such_parameter => :value
sha256 '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853'
version '1.2.3'
link 'Caffeine.app'
Expand Down
9 changes: 9 additions & 0 deletions test/support/Casks/with-gpg-key-url.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class WithGpgKeyUrl < TestCask
url TestHelper.local_binary('caffeine.zip')
homepage 'http://example.com/with-gpg-key-url'
gpg 'http://example.com/gpg-signature.asc',
:key_url => 'http://example.com/gpg-key-url'
sha256 '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853'
version '1.2.3'
link 'Caffeine.app'
end
4 changes: 2 additions & 2 deletions test/support/Casks/with-gpg.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class WithGpg < TestCask
url TestHelper.local_binary('caffeine.zip')
homepage 'http://example.com/with-gpg'
gpg 'ID', :type => :id,
:signature => 'http://example.com/gpg-signature.asc'
gpg 'http://example.com/gpg-signature.asc',
:key_id => 'ID'
sha256 '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853'
version '1.2.3'
link 'Caffeine.app'
Expand Down

0 comments on commit fea75ed

Please sign in to comment.