Merge pull request #1956 from reitermarkus/refactor-artifacts

Refactor `Artifact`.
This commit is contained in:
Markus Reiter 2017-02-07 22:22:46 +01:00 committed by GitHub
commit bef2c6c9bd
5 changed files with 121 additions and 145 deletions

View File

@ -27,8 +27,7 @@ module Hbc
module Artifact
# NOTE: order is important here, since we want to extract nested containers
# before we handle any other artifacts
def self.artifacts
[
TYPES = [
PreflightBlock,
NestedContainer,
Installer,
@ -53,15 +52,14 @@ module Hbc
Uninstall,
PostflightBlock,
Zap,
]
end
].freeze
def self.for_cask(cask)
def self.for_cask(cask, command: SystemCommand, force: false)
odebug "Determining which artifacts are present in Cask #{cask}"
artifacts.select do |artifact|
odebug "Checking for artifact class #{artifact}"
artifact.me?(cask)
end
TYPES
.select { |klass| klass.me?(cask) }
.map { |klass| klass.new(cask, command: command, force: force) }
end
end
end

View File

@ -55,7 +55,7 @@ module Hbc
def self.list_artifacts(cask)
Artifact.for_cask(cask).each do |artifact|
summary = artifact.new(cask).summary
summary = artifact.summary
ohai summary[:english_description], summary[:contents] unless summary.empty?
end
end

View File

@ -270,15 +270,18 @@ module Hbc
ORDINARY_ARTIFACT_TYPES.each do |type|
define_method(type) do |*args|
if type == :stage_only && args != [true]
if type == :stage_only
if args != [true]
raise CaskInvalidError.new(token, "'stage_only' takes a single argument: true")
end
artifacts[type] << args
if artifacts.key?(:stage_only) && artifacts.keys.count > 1 &&
!(artifacts.keys & ACTIVATABLE_ARTIFACT_TYPES).empty?
unless (artifacts.keys & ACTIVATABLE_ARTIFACT_TYPES).empty?
raise CaskInvalidError.new(token, "'stage_only' must be the only activatable artifact")
end
end
artifacts[type].add(args)
end
end
def installer(*args)

View File

@ -133,16 +133,14 @@ module Hbc
def install_artifacts
already_installed_artifacts = []
options = { command: @command, force: force }
odebug "Installing artifacts"
artifacts = Artifact.for_cask(@cask)
artifacts = Artifact.for_cask(@cask, command: @command, force: force)
odebug "#{artifacts.length} artifact/s defined", artifacts
artifacts.each do |artifact|
artifact = artifact.new(@cask, options)
next unless artifact.respond_to?(:install_phase)
odebug "Installing artifact of class #{artifact}"
odebug "Installing artifact of class #{artifact.class}"
artifact.install_phase
already_installed_artifacts.unshift(artifact)
end
@ -150,7 +148,7 @@ module Hbc
begin
already_installed_artifacts.each do |artifact|
next unless artifact.respond_to?(:uninstall_phase)
odebug "Reverting installation of artifact of class #{artifact}"
odebug "Reverting installation of artifact of class #{artifact.class}"
artifact.uninstall_phase
end
ensure
@ -319,13 +317,11 @@ module Hbc
def uninstall_artifacts
odebug "Un-installing artifacts"
artifacts = Artifact.for_cask(@cask)
artifacts = Artifact.for_cask(@cask, command: @command, force: force)
odebug "#{artifacts.length} artifact/s defined", artifacts
artifacts.each do |artifact|
options = { command: @command, force: force }
artifact = artifact.new(@cask, options)
next unless artifact.respond_to?(:uninstall_phase)
odebug "Un-installing artifact of class #{artifact}"
odebug "Un-installing artifact of class #{artifact.class}"
artifact.uninstall_phase
end
end

View File

@ -13,10 +13,8 @@ describe Hbc::Installer do
Hbc::Installer.new(caffeine).install
end
dest_path = Hbc.caskroom.join("local-caffeine", caffeine.version)
dest_path.must_be :directory?
application = Hbc.appdir.join("Caffeine.app")
application.must_be :directory?
expect(Hbc.caskroom.join("local-caffeine", caffeine.version)).must_be :directory?
expect(Hbc.appdir.join("Caffeine.app")).must_be :directory?
end
it "works with dmg-based Casks" do
@ -26,10 +24,8 @@ describe Hbc::Installer do
Hbc::Installer.new(asset).install
end
dest_path = Hbc.caskroom.join("container-dmg", asset.version)
dest_path.must_be :directory?
file = Hbc.appdir.join("container")
file.must_be :file?
expect(Hbc.caskroom.join("container-dmg", asset.version)).must_be :directory?
expect(Hbc.appdir.join("container")).must_be :file?
end
it "works with tar-gz-based Casks" do
@ -39,10 +35,8 @@ describe Hbc::Installer do
Hbc::Installer.new(asset).install
end
dest_path = Hbc.caskroom.join("container-tar-gz", asset.version)
dest_path.must_be :directory?
application = Hbc.appdir.join("container")
application.must_be :file?
expect(Hbc.caskroom.join("container-tar-gz", asset.version)).must_be :directory?
expect(Hbc.appdir.join("container")).must_be :file?
end
it "works with cab-based Casks" do
@ -55,10 +49,8 @@ describe Hbc::Installer do
end
end
dest_path = Hbc.caskroom.join("container-cab", asset.version)
dest_path.must_be :directory?
application = Hbc.appdir.join("container")
application.must_be :file?
expect(Hbc.caskroom.join("container-cab", asset.version)).must_be :directory?
expect(Hbc.appdir.join("container")).must_be :file?
end
it "works with Adobe AIR-based Casks" do
@ -69,10 +61,8 @@ describe Hbc::Installer do
Hbc::Installer.new(asset).install
end
dest_path = Hbc.caskroom.join("container-air", asset.version)
dest_path.must_be :directory?
application = Hbc.appdir.join("container.app")
application.must_be :directory?
expect(Hbc.caskroom.join("container-air", asset.version)).must_be :directory?
expect(Hbc.appdir.join("container.app")).must_be :directory?
end
it "works with 7z-based Casks" do
@ -85,10 +75,8 @@ describe Hbc::Installer do
end
end
dest_path = Hbc.caskroom.join("container-7z", asset.version)
dest_path.must_be :directory?
file = Hbc.appdir.join("container")
file.must_be :file?
expect(Hbc.caskroom.join("container-7z", asset.version)).must_be :directory?
expect(Hbc.appdir.join("container")).must_be :file?
end
it "works with xar-based Casks" do
@ -98,10 +86,8 @@ describe Hbc::Installer do
Hbc::Installer.new(asset).install
end
dest_path = Hbc.caskroom.join("container-xar", asset.version)
dest_path.must_be :directory?
file = Hbc.appdir.join("container")
file.must_be :file?
expect(Hbc.caskroom.join("container-xar", asset.version)).must_be :directory?
expect(Hbc.appdir.join("container")).must_be :file?
end
it "works with Stuffit-based Casks" do
@ -114,10 +100,8 @@ describe Hbc::Installer do
end
end
dest_path = Hbc.caskroom.join("container-sit", asset.version)
dest_path.must_be :directory?
application = Hbc.appdir.join("container")
application.must_be :file?
expect(Hbc.caskroom.join("container-sit", asset.version)).must_be :directory?
expect(Hbc.appdir.join("container")).must_be :file?
end
it "works with RAR-based Casks" do
@ -130,10 +114,8 @@ describe Hbc::Installer do
end
end
dest_path = Hbc.caskroom.join("container-rar", asset.version)
dest_path.must_be :directory?
application = Hbc.appdir.join("container")
application.must_be :file?
expect(Hbc.caskroom.join("container-rar", asset.version)).must_be :directory?
expect(Hbc.appdir.join("container")).must_be :file?
end
it "works with pure bzip2-based Casks" do
@ -143,10 +125,8 @@ describe Hbc::Installer do
Hbc::Installer.new(asset).install
end
dest_path = Hbc.caskroom.join("container-bzip2", asset.version)
dest_path.must_be :directory?
file = Hbc.appdir.join("container-bzip2--#{asset.version}")
file.must_be :file?
expect(Hbc.caskroom.join("container-bzip2", asset.version)).must_be :directory?
expect(Hbc.appdir.join("container-bzip2--#{asset.version}")).must_be :file?
end
it "works with pure gzip-based Casks" do
@ -156,10 +136,8 @@ describe Hbc::Installer do
Hbc::Installer.new(asset).install
end
dest_path = Hbc.caskroom.join("container-gzip", asset.version)
dest_path.must_be :directory?
file = Hbc.appdir.join("container")
file.must_be :file?
expect(Hbc.caskroom.join("container-gzip", asset.version)).must_be :directory?
expect(Hbc.appdir.join("container")).must_be :file?
end
it "works with pure xz-based Casks" do
@ -172,10 +150,8 @@ describe Hbc::Installer do
end
end
dest_path = Hbc.caskroom.join("container-xz", asset.version)
dest_path.must_be :directory?
file = Hbc.appdir.join("container-xz--#{asset.version}")
file.must_be :file?
expect(Hbc.caskroom.join("container-xz", asset.version)).must_be :directory?
expect(Hbc.appdir.join("container-xz--#{asset.version}")).must_be :file?
end
it "works with lzma-based Casks" do
@ -188,15 +164,13 @@ describe Hbc::Installer do
end
end
dest_path = Hbc.caskroom.join("container-lzma", asset.version)
dest_path.must_be :directory?
file = Hbc.appdir.join("container-lzma--#{asset.version}")
file.must_be :file?
expect(Hbc.caskroom.join("container-lzma", asset.version)).must_be :directory?
expect(Hbc.appdir.join("container-lzma--#{asset.version}")).must_be :file?
end
it "blows up on a bad checksum" do
bad_checksum = Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/bad-checksum.rb")
lambda {
expect {
shutup do
Hbc::Installer.new(bad_checksum).install
end
@ -205,7 +179,7 @@ describe Hbc::Installer do
it "blows up on a missing checksum" do
missing_checksum = Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/missing-checksum.rb")
lambda {
expect {
shutup do
Hbc::Installer.new(missing_checksum).install
end
@ -219,12 +193,12 @@ describe Hbc::Installer do
Hbc::Installer.new(no_checksum).install
end
no_checksum.must_be :installed?
expect(no_checksum).must_be :installed?
end
it "fails to install if sha256 :no_check is used with --require-sha" do
no_checksum = Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/no-checksum.rb")
lambda {
expect {
Hbc::Installer.new(no_checksum, require_sha: true).install
}.must_raise(Hbc::CaskNoShasumError)
end
@ -236,23 +210,27 @@ describe Hbc::Installer do
Hbc::Installer.new(no_checksum, require_sha: true, force: true).install
end
no_checksum.must_be :installed?
expect(no_checksum).must_be :installed?
end
it "prints caveats if they're present" do
with_caveats = Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/with-caveats.rb")
lambda {
expect {
Hbc::Installer.new(with_caveats).install
}.must_output(/Here are some things you might want to know/)
with_caveats.must_be :installed?
expect(with_caveats).must_be :installed?
end
it "prints installer :manual instructions when present" do
with_installer_manual = Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/with-installer-manual.rb")
lambda {
expect {
Hbc::Installer.new(with_installer_manual).install
}.must_output(/To complete the installation of Cask with-installer-manual, you must also\nrun the installer at\n\n '#{with_installer_manual.staged_path.join('Caffeine.app')}'/)
with_installer_manual.must_be :installed?
expect(with_installer_manual).must_be :installed?
end
it "does not extract __MACOSX directories from zips" do
@ -262,54 +240,60 @@ describe Hbc::Installer do
Hbc::Installer.new(with_macosx_dir).install
end
with_macosx_dir.staged_path.join("__MACOSX").wont_be :directory?
expect(with_macosx_dir.staged_path.join("__MACOSX")).wont_be :directory?
end
it "installer method raises an exception when already-installed Casks which auto-update are attempted" do
auto_updates = Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/auto-updates.rb")
auto_updates.installed?.must_equal false
installer = Hbc::Installer.new(auto_updates)
with_auto_updates = Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/auto-updates.rb")
expect(with_auto_updates).wont_be :installed?
installer = Hbc::Installer.new(with_auto_updates)
shutup do
installer.install
end
lambda {
expect {
installer.install
}.must_raise(Hbc::CaskAlreadyInstalledAutoUpdatesError)
end
it "allows already-installed Casks which auto-update to be installed if force is provided" do
auto_updates = Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/auto-updates.rb")
auto_updates.installed?.must_equal false
with_auto_updates = Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/auto-updates.rb")
expect(with_auto_updates).wont_be :installed?
shutup do
Hbc::Installer.new(auto_updates).install
Hbc::Installer.new(with_auto_updates).install
end
shutup do
Hbc::Installer.new(auto_updates, force: true).install
Hbc::Installer.new(with_auto_updates, force: true).install
end # wont_raise
end
# unlike the CLI, the internal interface throws exception on double-install
it "installer method raises an exception when already-installed Casks are attempted" do
transmission = Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/local-transmission.rb")
transmission.installed?.must_equal false
expect(transmission).wont_be :installed?
installer = Hbc::Installer.new(transmission)
shutup do
installer.install
end
lambda {
expect {
installer.install
}.must_raise(Hbc::CaskAlreadyInstalledError)
end
it "allows already-installed Casks to be installed if force is provided" do
transmission = Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/local-transmission.rb")
transmission.installed?.must_equal false
expect(transmission).wont_be :installed?
shutup do
Hbc::Installer.new(transmission).install
@ -327,9 +311,7 @@ describe Hbc::Installer do
Hbc::Installer.new(naked_pkg).install
end
dest_path = Hbc.caskroom.join("container-pkg", naked_pkg.version)
pkg = dest_path.join("container.pkg")
pkg.must_be :file?
expect(Hbc.caskroom.join("container-pkg", naked_pkg.version, "container.pkg")).must_be :file?
end
it "works properly with an overridden container :type" do
@ -339,9 +321,7 @@ describe Hbc::Installer do
Hbc::Installer.new(naked_executable).install
end
dest_path = Hbc.caskroom.join("naked-executable", naked_executable.version)
executable = dest_path.join("naked_executable")
executable.must_be :file?
expect(Hbc.caskroom.join("naked-executable", naked_executable.version, "naked_executable")).must_be :file?
end
it "works fine with a nested container" do
@ -351,8 +331,7 @@ describe Hbc::Installer do
Hbc::Installer.new(nested_app).install
end
dest_path = Hbc.appdir.join("MyNestedApp.app")
dest_path.must_be :directory?
expect(Hbc.appdir.join("MyNestedApp.app")).must_be :directory?
end
it "generates and finds a timestamped metadata directory for an installed Cask" do
@ -363,8 +342,8 @@ describe Hbc::Installer do
end
m_path = caffeine.metadata_path(:now, true)
caffeine.metadata_path(:now, false).must_equal(m_path)
caffeine.metadata_path(:latest).must_equal(m_path)
expect(caffeine.metadata_path(:now, false)).must_equal(m_path)
expect(caffeine.metadata_path(:latest)).must_equal(m_path)
end
it "generates and finds a metadata subdirectory for an installed Cask" do
@ -376,8 +355,8 @@ describe Hbc::Installer do
subdir_name = "Casks"
m_subdir = caffeine.metadata_subdir(subdir_name, :now, true)
caffeine.metadata_subdir(subdir_name, :now, false).must_equal(m_subdir)
caffeine.metadata_subdir(subdir_name, :latest).must_equal(m_subdir)
expect(caffeine.metadata_subdir(subdir_name, :now, false)).must_equal(m_subdir)
expect(caffeine.metadata_subdir(subdir_name, :latest)).must_equal(m_subdir)
end
end
@ -391,9 +370,9 @@ describe Hbc::Installer do
installer.uninstall
end
Hbc.caskroom.join("local-caffeine", caffeine.version, "Caffeine.app").wont_be :directory?
Hbc.caskroom.join("local-caffeine", caffeine.version).wont_be :directory?
Hbc.caskroom.join("local-caffeine").wont_be :directory?
expect(Hbc.caskroom.join("local-caffeine", caffeine.version, "Caffeine.app")).wont_be :directory?
expect(Hbc.caskroom.join("local-caffeine", caffeine.version)).wont_be :directory?
expect(Hbc.caskroom.join("local-caffeine")).wont_be :directory?
end
it "uninstalls all versions if force is set" do
@ -404,19 +383,19 @@ describe Hbc::Installer do
Hbc::Installer.new(caffeine).install
end
Hbc.caskroom.join("local-caffeine", caffeine.version).must_be :directory?
Hbc.caskroom.join("local-caffeine", mutated_version).wont_be :directory?
expect(Hbc.caskroom.join("local-caffeine", caffeine.version)).must_be :directory?
expect(Hbc.caskroom.join("local-caffeine", mutated_version)).wont_be :directory?
FileUtils.mv(Hbc.caskroom.join("local-caffeine", caffeine.version), Hbc.caskroom.join("local-caffeine", mutated_version))
Hbc.caskroom.join("local-caffeine", caffeine.version).wont_be :directory?
Hbc.caskroom.join("local-caffeine", mutated_version).must_be :directory?
expect(Hbc.caskroom.join("local-caffeine", caffeine.version)).wont_be :directory?
expect(Hbc.caskroom.join("local-caffeine", mutated_version)).must_be :directory?
shutup do
Hbc::Installer.new(caffeine, force: true).uninstall
end
Hbc.caskroom.join("local-caffeine", caffeine.version).wont_be :directory?
Hbc.caskroom.join("local-caffeine", mutated_version).wont_be :directory?
Hbc.caskroom.join("local-caffeine").wont_be :directory?
expect(Hbc.caskroom.join("local-caffeine", caffeine.version)).wont_be :directory?
expect(Hbc.caskroom.join("local-caffeine", mutated_version)).wont_be :directory?
expect(Hbc.caskroom.join("local-caffeine")).wont_be :directory?
end
end
end