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,41 +27,39 @@ module Hbc
module Artifact module Artifact
# NOTE: order is important here, since we want to extract nested containers # NOTE: order is important here, since we want to extract nested containers
# before we handle any other artifacts # before we handle any other artifacts
def self.artifacts TYPES = [
[ PreflightBlock,
PreflightBlock, NestedContainer,
NestedContainer, Installer,
Installer, App,
App, Suite,
Suite, Artifact, # generic 'artifact' stanza
Artifact, # generic 'artifact' stanza Colorpicker,
Colorpicker, Pkg,
Pkg, Prefpane,
Prefpane, Qlplugin,
Qlplugin, Dictionary,
Dictionary, Font,
Font, Service,
Service, StageOnly,
StageOnly, Binary,
Binary, InputMethod,
InputMethod, InternetPlugin,
InternetPlugin, AudioUnitPlugin,
AudioUnitPlugin, VstPlugin,
VstPlugin, Vst3Plugin,
Vst3Plugin, ScreenSaver,
ScreenSaver, Uninstall,
Uninstall, PostflightBlock,
PostflightBlock, Zap,
Zap, ].freeze
]
end
def self.for_cask(cask) def self.for_cask(cask, command: SystemCommand, force: false)
odebug "Determining which artifacts are present in Cask #{cask}" odebug "Determining which artifacts are present in Cask #{cask}"
artifacts.select do |artifact|
odebug "Checking for artifact class #{artifact}" TYPES
artifact.me?(cask) .select { |klass| klass.me?(cask) }
end .map { |klass| klass.new(cask, command: command, force: force) }
end end
end end
end end

View File

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

View File

@ -270,14 +270,17 @@ module Hbc
ORDINARY_ARTIFACT_TYPES.each do |type| ORDINARY_ARTIFACT_TYPES.each do |type|
define_method(type) do |*args| define_method(type) do |*args|
if type == :stage_only && args != [true] if type == :stage_only
raise CaskInvalidError.new(token, "'stage_only' takes a single argument: true") if args != [true]
end raise CaskInvalidError.new(token, "'stage_only' takes a single argument: true")
artifacts[type] << args end
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") raise CaskInvalidError.new(token, "'stage_only' must be the only activatable artifact")
end
end end
artifacts[type].add(args)
end end
end end

View File

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

View File

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