Skip to content

Commit

Permalink
Merge pull request #4847 from rolandwalker/extend_appcast_stanza
Browse files Browse the repository at this point in the history
DSL: extend `appcast` stanza
  • Loading branch information
rolandwalker committed Jun 28, 2014
2 parents 000487b + 1828116 commit cc7db15
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 58 deletions.
1 change: 1 addition & 0 deletions lib/cask.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Cask; end
require 'cmd/update'
require 'rubygems'

require 'cask/appcast'
require 'cask/artifact'
require 'cask/audit'
require 'cask/auditor'
Expand Down
33 changes: 33 additions & 0 deletions lib/cask/appcast.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Cask::Appcast

# note :latest_version is considered experimental
# and may be removed

APPCAST_FORMATS = Set.new [
:sparkle, # first one is the default
:plaintext,
:unknown,
]

attr_reader :parameters, :sha256, :format, :latest_version

def initialize(uri, parameters={})
@parameters = parameters
@uri = Cask::UnderscoreSupportingURI.parse(uri)
@sha256 = @parameters[:sha256]
@latest_version = @parameters[:latest_version]
@format = @parameters[:format]
@format = APPCAST_FORMATS.first if @format.nil?
unless APPCAST_FORMATS.include?(@format)
raise "invalid appcast format: '#{@format.inspect}'"
end
end

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

def to_s
@uri.to_s
end
end
4 changes: 3 additions & 1 deletion lib/cask/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ def appcast(*args)
raise CaskInvalidError.new(self.title, "'appcast' stanza may only appear once")
end
@appcast ||= begin
Cask::UnderscoreSupportingURI.parse(*args) unless args.empty?
Cask::Appcast.new(*args) unless args.empty?
rescue StandardError => e
raise CaskInvalidError.new(self.title, e)
end
end

Expand Down
154 changes: 97 additions & 57 deletions test/cask/dsl_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,6 @@
test_cask.version.must_equal '1.2.3'
end

it "lets you set checksum via sha256" do
ChecksumCask = Class.new(Cask)
ChecksumCask.class_eval do
sha256 'imasha2'
end
instance = ChecksumCask.new
instance.sums.must_equal [
Checksum.new(:sha2, 'imasha2')
]
end

it "prevents the entire world from crashing when a cask includes an unknown method" do
UnexpectedMethodCask = Class.new(Cask)
begin
Expand All @@ -43,73 +32,124 @@
end
end

it "allows you to specify linkables" do
CaskWithLinkables = Class.new(Cask)
CaskWithLinkables.class_eval do
link 'Foo.app'
link 'Bar.app'
describe "sha256 stanza" do
it "lets you set checksum via sha256" do
ChecksumCask = Class.new(Cask)
ChecksumCask.class_eval do
sha256 'imasha2'
end
instance = ChecksumCask.new
instance.sums.must_equal [
Checksum.new(:sha2, 'imasha2')
]
end

instance = CaskWithLinkables.new
Array(instance.artifacts[:link]).sort.must_equal [['Bar.app'], ['Foo.app']]
end

it "allow linkables to be set to empty" do
CaskWithNoLinkables = Class.new(Cask)
describe "link stanza" do
it "allows you to specify linkables" do
CaskWithLinkables = Class.new(Cask)
CaskWithLinkables.class_eval do
link 'Foo.app'
link 'Bar.app'
end

instance = CaskWithNoLinkables.new
Array(instance.artifacts[:link]).must_equal %w[]
instance = CaskWithLinkables.new
Array(instance.artifacts[:link]).sort.must_equal [['Bar.app'], ['Foo.app']]
end

it "allow linkables to be set to empty" do
CaskWithNoLinkables = Class.new(Cask)

instance = CaskWithNoLinkables.new
Array(instance.artifacts[:link]).must_equal %w[]
end
end

it "allows caveats to be specified via a method define" do
PlainCask = Class.new(Cask)
describe "caveats stanza" do
it "allows caveats to be specified via a method define" do
PlainCask = Class.new(Cask)

instance = PlainCask.new
instance = PlainCask.new

instance.caveats.must_be :empty?
instance.caveats.must_be :empty?

CaskWithCaveats = Class.new(Cask)
CaskWithCaveats.class_eval do
def caveats; <<-EOS.undent
When you install this cask, you probably want to know this.
EOS
CaskWithCaveats = Class.new(Cask)
CaskWithCaveats.class_eval do
def caveats; <<-EOS.undent
When you install this cask, you probably want to know this.
EOS
end
end
end

instance = CaskWithCaveats.new
instance = CaskWithCaveats.new

instance.caveats.must_equal "When you install this cask, you probably want to know this.\n"
instance.caveats.must_equal "When you install this cask, you probably want to know this.\n"
end
end

it "allows installable pkgs to be specified" do
CaskWithInstallables = Class.new(Cask)
CaskWithInstallables.class_eval do
install 'Foo.pkg'
install 'Bar.pkg'
describe "pkg stanza" do
it "allows installable pkgs to be specified" do
CaskWithInstallables = Class.new(Cask)
CaskWithInstallables.class_eval do
install 'Foo.pkg'
install 'Bar.pkg'
end

instance = CaskWithInstallables.new
Array(instance.artifacts[:install]).sort.must_equal [['Bar.pkg'], ['Foo.pkg']]
end
end

instance = CaskWithInstallables.new
Array(instance.artifacts[:install]).sort.must_equal [['Bar.pkg'], ['Foo.pkg']]
describe "url stanza" do
it "prevents defining multiple urls" do
err = lambda {
invalid_cask = Cask.load('invalid/invalid-two-url')
}.must_raise(CaskInvalidError)
err.message.must_include "'url' stanza may only appear once"
end
end

it "prevents defining multiple urls" do
err = lambda {
invalid_cask = Cask.load('invalid/invalid-two-url')
}.must_raise(CaskInvalidError)
err.message.must_include "'url' stanza may only appear once"
describe "homepage stanza" do
it "prevents defining multiple homepages" do
err = lambda {
invalid_cask = Cask.load('invalid/invalid-two-homepage')
}.must_raise(CaskInvalidError)
err.message.must_include "'homepage' stanza may only appear once"
end
end

it "prevents defining multiple homepages" do
err = lambda {
invalid_cask = Cask.load('invalid/invalid-two-homepage')
}.must_raise(CaskInvalidError)
err.message.must_include "'homepage' stanza may only appear once"
describe "version stanza" do
it "prevents defining multiple versions" do
err = lambda {
invalid_cask = Cask.load('invalid/invalid-two-version')
}.must_raise(CaskInvalidError)
err.message.must_include "'version' stanza may only appear once"
end
end

it "prevents defining multiple versions" do
err = lambda {
invalid_cask = Cask.load('invalid/invalid-two-version')
}.must_raise(CaskInvalidError)
err.message.must_include "'version' stanza may only appear once"
describe "appcast stanza" do
it "allows appcasts to be specified" do
cask = Cask.load('with-appcast')
cask.appcast.to_s.must_match %r{^http}
end

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

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

it "refuses to load if appcast :format is invalid" do
err = lambda {
invalid_cask = Cask.load('invalid/invalid-appcast-format')
}.must_raise(CaskInvalidError)
end
end
end
10 changes: 10 additions & 0 deletions test/support/Casks/invalid/invalid-appcast-format.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class InvalidAppcastFormat < TestCask
url TestHelper.local_binary('caffeine.zip')
homepage 'http://example.com/invalid-appcast-format'
appcast 'http://example.com/appcast.xml',
:sha256 => '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853',
:format => :no_such_format
sha256 '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853'
version '1.2.3'
link 'Caffeine.app'
end
13 changes: 13 additions & 0 deletions test/support/Casks/invalid/invalid-appcast-multiple.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class InvalidAppcastMultiple < TestCask
url TestHelper.local_binary('caffeine.zip')
homepage 'http://example.com/invalid-appcast-multiple'
appcast 'http://example.com/appcast1.xml',
:sha256 => '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853',
:format => :sparkle
appcast 'http://example.com/appcast2.xml',
:sha256 => '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853',
:format => :sparkle
sha256 '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853'
version '1.2.3'
link 'Caffeine.app'
end
10 changes: 10 additions & 0 deletions test/support/Casks/invalid/invalid-appcast-url.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class InvalidAppcastUrl < TestCask
url TestHelper.local_binary('caffeine.zip')
homepage 'http://example.com/invalid-appcast-url'
appcast 1,
:sha256 => '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853',
:format => :sparkle
sha256 '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853'
version '1.2.3'
link 'Caffeine.app'
end
10 changes: 10 additions & 0 deletions test/support/Casks/with-appcast.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class WithAppcast < TestCask
url TestHelper.local_binary('caffeine.zip')
homepage 'http://example.com/with-appcast'
appcast 'http://example.com/appcast.xml',
:sha256 => '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853',
:format => :sparkle
sha256 '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853'
version '1.2.3'
link 'Caffeine.app'
end

0 comments on commit cc7db15

Please sign in to comment.