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

add before_install and before_uninstall #2742

Merged
merged 1 commit into from
Feb 13, 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
2 changes: 2 additions & 0 deletions lib/cask/artifact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Cask::Artifact; end
require 'cask/artifact/app'
require 'cask/artifact/binary'
require 'cask/artifact/after_block'
require 'cask/artifact/before_block'
require 'cask/artifact/colorpicker'
require 'cask/artifact/font'
require 'cask/artifact/nested_container'
Expand All @@ -27,6 +28,7 @@ module Cask::Artifact
#
def self.artifacts
[
Cask::Artifact::BeforeBlock,
Cask::Artifact::NestedContainer,
Cask::Artifact::App,
Cask::Artifact::Colorpicker,
Expand Down
14 changes: 14 additions & 0 deletions lib/cask/artifact/before_block.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Cask::Artifact::BeforeBlock < Cask::Artifact::Base
def self.me?(cask)
cask.artifacts[:before_install].any? ||
cask.artifacts[:before_uninstall].any?
end

def install
@cask.artifacts[:before_install].each { |block| @cask.instance_eval &block }
end

def uninstall
@cask.artifacts[:before_uninstall].each { |block| @cask.instance_eval &block }
end
end
4 changes: 3 additions & 1 deletion lib/cask/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ def caveats(*string, &block)

ARTIFACT_BLOCK_TYPES = [
:after_install,
:after_uninstall
:after_uninstall,
:before_install,
:before_uninstall,
]

ARTIFACT_BLOCK_TYPES.each do |type|
Expand Down
45 changes: 45 additions & 0 deletions test/cask/artifact/before_block_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'test_helper'

describe Cask::Artifact::BeforeBlock do
describe 'install' do
it 'calls the specified block before installing' do
called = false
yielded_arg = nil

CaskWithBeforeInstall = Class.new(Cask)
CaskWithBeforeInstall.class_eval do
before_install do |c|
called = true
yielded_arg = c
end
end

cask = CaskWithBeforeInstall.new
Cask::Artifact::BeforeBlock.new(cask).install

called.must_equal true
yielded_arg.must_equal cask
end
end

describe 'uninstall' do
it 'calls the specified block before uninstalling, passing the cask' do
called = false
yielded_arg = nil

CaskWithBeforeUninstall = Class.new(Cask)
CaskWithBeforeUninstall.class_eval do
before_uninstall do |c|
called = true
yielded_arg = c
end
end

cask = CaskWithBeforeUninstall.new
Cask::Artifact::BeforeBlock.new(cask).uninstall

called.must_equal true
yielded_arg.must_equal cask
end
end
end