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

DSL: add gpg stanza #4848

Merged
merged 1 commit into from
Jun 28, 2014
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/cask.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Cask; end
require 'cask/dsl'
require 'cask/exceptions'
require 'cask/fetcher'
require 'cask/gpg'
require 'cask/installer'
require 'cask/link_checker'
require 'cask/locations'
Expand Down
13 changes: 13 additions & 0 deletions lib/cask/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ def url; self.class.url; end

def appcast; self.class.appcast; end

def gpg; self.class.gpg; end

def version; self.class.version; end

def depends_on_formula; self.class.depends_on_formula; end
Expand Down Expand Up @@ -50,6 +52,17 @@ def appcast(*args)
end
end

def gpg(*args)
if @gpg and !args.empty?
raise CaskInvalidError.new(self.title, "'gpg' stanza may only appear once")
end
@gpg ||= begin
Cask::Gpg.new(*args) unless args.empty?
rescue StandardError => e
raise CaskInvalidError.new(self.title, e)
end
end

def container_type(type=nil)
if @container_type and !type.nil?
raise CaskInvalidError.new(self.title, "'container_type' stanza may only appear once")
Expand Down
40 changes: 40 additions & 0 deletions lib/cask/gpg.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Cask::Gpg

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

REQUIRED_PARAMETERS = [
:signature
]

attr_reader :key, :parameters, :type, :signature

def initialize(key, 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
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)
end
end

def to_yaml
[@key, @parameters].to_yaml
end

def to_s
@key.to_s
end
end
1 change: 1 addition & 0 deletions lib/cask/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def dumpcask
:caveats,
:depends_on_formula,
:container_type,
:gpg,
].each do |method|
odebug "Cask instance method '#{method}':", self.send(method).to_yaml
end
Expand Down
32 changes: 32 additions & 0 deletions test/cask/dsl_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,36 @@ def caveats; <<-EOS.undent
}.must_raise(CaskInvalidError)
err.message.must_include "'version' stanza may only appear once"
end

describe "gpg stanza" do
it "allows gpg stanza to be specified" do
cask = Cask.load('with-gpg')
cask.gpg.to_s.must_match %r{\S}
end

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

it "refuses to load invalid gpg signature URLs" do
err = lambda {
invalid_cask = Cask.load('invalid/invalid-gpg-signature-url')
}.must_raise(CaskInvalidError)
end

it "refuses to load invalid gpg key URLs" do
err = lambda {
invalid_cask = Cask.load('invalid/invalid-gpg-key-url')
}.must_raise(CaskInvalidError)
end

it "refuses to load if gpg :type is invalid" do
err = lambda {
invalid_cask = Cask.load('invalid/invalid-gpg-type')
}.must_raise(CaskInvalidError)
end
end
end
9 changes: 9 additions & 0 deletions test/support/Casks/invalid/invalid-gpg-key-url.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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'
sha256 '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853'
version '1.2.3'
link 'Caffeine.app'
end
11 changes: 11 additions & 0 deletions test/support/Casks/invalid/invalid-gpg-multiple.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class InvalidGpgMultiple < TestCask
url TestHelper.local_binary('caffeine.zip')
homepage 'http://example.com/invalid-gpg-multiple'
gpg 'ID', :type => :id,
:signature => 'http://example.com/gpg-signature.asc'
gpg 'ID', :type => :id,
:signature => 'http://example.com/gpg-signature.asc'
sha256 '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853'
version '1.2.3'
link 'Caffeine.app'
end
9 changes: 9 additions & 0 deletions test/support/Casks/invalid/invalid-gpg-signature-url.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class InvalidGpgSignatureUrl < TestCask
url TestHelper.local_binary('caffeine.zip')
homepage 'http://example.com/invalid-gpg-signature-url'
gpg 'ID', :type => :id,
:signature => 1
sha256 '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853'
version '1.2.3'
link 'Caffeine.app'
end
9 changes: 9 additions & 0 deletions test/support/Casks/invalid/invalid-gpg-type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class InvalidGpgType < 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'
sha256 '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853'
version '1.2.3'
link 'Caffeine.app'
end
9 changes: 9 additions & 0 deletions test/support/Casks/with-gpg.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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'
sha256 '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853'
version '1.2.3'
link 'Caffeine.app'
end