Add system_command helpers.

This commit is contained in:
Markus Reiter 2018-07-22 23:13:32 +02:00
parent db299035ac
commit e5e8033145
21 changed files with 68 additions and 68 deletions

View File

@ -20,7 +20,7 @@ module Hbc
path.relative_path_from(cask.staged_path).to_s
end
def extract(command: nil, verbose: nil, **_)
def extract(verbose: nil, **_)
container = Container.for_path(path)
unless container
@ -28,7 +28,7 @@ module Hbc
end
ohai "Extracting nested container #{path.relative_path_from(cask.staged_path)}"
container.new(cask, path, command).extract(to: cask.staged_path, verbose: verbose)
container.new(cask, path).extract(to: cask.staged_path, verbose: verbose)
FileUtils.remove_entry_secure(path)
end
end

View File

@ -8,7 +8,7 @@ module Hbc
end
def extract_to_dir(unpack_dir, basename:, verbose:)
@command.run!(
system_command!(
"/Applications/Utilities/Adobe AIR Application Installer.app/Contents/MacOS/Adobe AIR Application Installer",
args: ["-silent", "-location", unpack_dir, path],
)

View File

@ -5,10 +5,9 @@ module Hbc
attr_reader :path
def initialize(cask, path, command, nested: false)
def initialize(cask, path, nested: false)
@cask = cask
@path = path
@command = command
end
def extract(to: nil, basename: nil, verbose: false)
@ -42,7 +41,7 @@ module Hbc
return false unless container
ohai "Extracting nested container #{source.basename}"
container.new(@cask, source, @command).extract(to: to, verbose: verbose)
container.new(@cask, source).extract(to: to, verbose: verbose)
true
end

View File

@ -12,7 +12,7 @@ module Hbc
tmp_unpack_dir = Pathname(tmp_unpack_dir)
FileUtils.cp path, tmp_unpack_dir/basename, preserve: true
@command.run!("bunzip2", args: ["--quiet", "--", tmp_unpack_dir/basename])
system_command!("bunzip2", args: ["--quiet", "--", tmp_unpack_dir/basename])
extract_nested_inside(tmp_unpack_dir, to: unpack_dir)
end

View File

@ -8,7 +8,7 @@ module Hbc
end
def extract_to_dir(unpack_dir, basename:, verbose:)
@command.run!("cabextract",
system_command!("cabextract",
args: ["-d", unpack_dir, "--", path],
env: { "PATH" => PATH.new(Formula["cabextract"].opt_bin, ENV["PATH"]) })
end

View File

@ -6,7 +6,7 @@ module Hbc
class Container
class Dmg < Base
def self.can_extract?(path:, magic_number:)
imageinfo = SystemCommand.run("/usr/bin/hdiutil",
imageinfo = system_command("/usr/bin/hdiutil",
# realpath is a failsafe against unusual filenames
args: ["imageinfo", path.realpath],
print_stderr: false).stdout
@ -34,7 +34,7 @@ module Hbc
Dir.mktmpdir do |unpack_dir|
cdr_path = Pathname.new(unpack_dir).join("#{path.basename(".dmg")}.cdr")
without_eula = @command.run("/usr/bin/hdiutil",
without_eula = system_command("/usr/bin/hdiutil",
args: ["attach", "-plist", "-nobrowse", "-readonly", "-noidme", "-mountrandom", unpack_dir, path],
input: "qn\n",
print_stderr: false)
@ -43,9 +43,9 @@ module Hbc
plist = if without_eula.success?
without_eula.plist
else
@command.run!("/usr/bin/hdiutil", args: ["convert", "-quiet", "-format", "UDTO", "-o", cdr_path, path])
system_command!("/usr/bin/hdiutil", args: ["convert", "-quiet", "-format", "UDTO", "-o", cdr_path, path])
with_eula = @command.run!("/usr/bin/hdiutil",
with_eula = system_command!("/usr/bin/hdiutil",
args: ["attach", "-plist", "-nobrowse", "-readonly", "-noidme", "-mountrandom", unpack_dir, cdr_path])
if verbose? && !(eula_text = without_eula.stdout).empty?
@ -68,11 +68,11 @@ module Hbc
begin
tries ||= 3
if tries > 1
@command.run("/usr/sbin/diskutil",
system_command("/usr/sbin/diskutil",
args: ["eject", mountpath],
print_stderr: false)
else
@command.run("/usr/sbin/diskutil",
system_command("/usr/sbin/diskutil",
args: ["unmount", "force", mountpath],
print_stderr: false)
end
@ -94,8 +94,8 @@ module Hbc
filelist.puts(bom_filelist_from_path(mount))
filelist.close
@command.run!("/usr/bin/mkbom", args: ["-s", "-i", filelist.path, "--", bomfile.path])
@command.run!("/usr/bin/ditto", args: ["--bom", bomfile.path, "--", mount, to])
system_command!("/usr/bin/mkbom", args: ["-s", "-i", filelist.path, "--", bomfile.path])
system_command!("/usr/bin/ditto", args: ["--bom", bomfile.path, "--", mount, to])
end
end
end
@ -103,7 +103,8 @@ module Hbc
def bom_filelist_from_path(mount)
# We need to use `find` here instead of Ruby in order to properly handle
# file names containing special characters, such as “e” + “´” vs. “é”.
@command.run("/usr/bin/find", args: [".", "-print0"], chdir: mount, print_stderr: false).stdout
system_command("/usr/bin/find", args: [".", "-print0"], chdir: mount, print_stderr: false)
.stdout
.split("\0")
.reject { |path| skip_path?(mount, path) }
.join("\n")

View File

@ -8,7 +8,7 @@ module Hbc
end
def extract_to_dir(unpack_dir, basename:, verbose:)
@command.run!("unar",
system_command!("unar",
args: ["-force-overwrite", "-quiet", "-no-directory", "-output-directory", unpack_dir, "--", path],
env: { "PATH" => PATH.new(Formula["unar"].opt_bin, ENV["PATH"]) })
end

View File

@ -12,7 +12,7 @@ module Hbc
tmp_unpack_dir = Pathname(tmp_unpack_dir)
FileUtils.cp path, tmp_unpack_dir/basename, preserve: true
@command.run!("gunzip", args: ["--quiet", "--name", "--", tmp_unpack_dir/basename])
system_command!("gunzip", args: ["--quiet", "--name", "--", tmp_unpack_dir/basename])
extract_nested_inside(tmp_unpack_dir, to: unpack_dir)
end

View File

@ -8,8 +8,8 @@ module Hbc
end
def extract_to_dir(unpack_dir, basename:, verbose:)
@command.run!("/usr/bin/ditto", args: ["--", path, unpack_dir])
@command.run!("unlzma",
system_command!("/usr/bin/ditto", args: ["--", path, unpack_dir])
system_command!("unlzma",
args: ["-q", "--", Pathname(unpack_dir).join(basename)],
env: { "PATH" => PATH.new(Formula["unlzma"].opt_bin, ENV["PATH"]) })
end

View File

@ -8,7 +8,7 @@ module Hbc
end
def extract_to_dir(unpack_dir, basename:, verbose:)
@command.run!("/usr/bin/ditto", args: ["--", path, unpack_dir/basename])
system_command!("/usr/bin/ditto", args: ["--", path, unpack_dir/basename])
end
end
end

View File

@ -8,7 +8,7 @@ module Hbc
end
def extract_to_dir(unpack_dir, basename:, verbose:)
@command.run!("unrar",
system_command!("unrar",
args: ["x", "-inul", path, unpack_dir],
env: { "PATH" => PATH.new(Formula["unrar"].opt_bin, ENV["PATH"]) })
end

View File

@ -8,7 +8,7 @@ module Hbc
end
def extract_to_dir(unpack_dir, basename:, verbose:)
@command.run!("7zr",
system_command!("7zr",
args: ["x", "-y", "-bd", "-bso0", path, "-o#{unpack_dir}"],
env: { "PATH" => PATH.new(Formula["p7zip"].opt_bin, ENV["PATH"]) })
end

View File

@ -6,7 +6,7 @@ module Hbc
end
def extract_to_dir(unpack_dir, basename:, verbose:)
@command.run!("svn", args: ["export", "--force", path, unpack_dir])
system_command!("svn", args: ["export", "--force", path, unpack_dir])
end
end
end

View File

@ -13,7 +13,7 @@ module Hbc
end
def extract_to_dir(unpack_dir, basename:, verbose:)
@command.run!("tar", args: ["xf", path, "-C", unpack_dir])
system_command!("tar", args: ["xf", path, "-C", unpack_dir])
end
end
end

View File

@ -8,7 +8,7 @@ module Hbc
end
def extract_to_dir(unpack_dir, basename:, verbose:)
@command.run!("xar", args: ["-x", "-f", @path, "-C", unpack_dir])
system_command!("xar", args: ["-x", "-f", @path, "-C", unpack_dir])
end
end
end

View File

@ -8,8 +8,8 @@ module Hbc
end
def extract_to_dir(unpack_dir, basename:, verbose:)
@command.run!("/usr/bin/ditto", args: ["--", path, unpack_dir])
@command.run!("unxz",
system_command!("/usr/bin/ditto", args: ["--", path, unpack_dir])
system_command!("unxz",
args: ["-q", "--", unpack_dir/basename],
env: { "PATH" => PATH.new(Formula["xz"].opt_bin, ENV["PATH"]) })
end

View File

@ -9,7 +9,7 @@ module Hbc
def extract_to_dir(unpack_dir, basename:, verbose:)
Dir.mktmpdir do |tmp_unpack_dir|
@command.run!("/usr/bin/ditto", args: ["-x", "-k", "--", path, tmp_unpack_dir])
system_command!("/usr/bin/ditto", args: ["-x", "-k", "--", path, tmp_unpack_dir])
extract_nested_inside(tmp_unpack_dir, to: unpack_dir)
end

View File

@ -158,7 +158,7 @@ module Hbc
raise CaskError, "Uh oh, could not figure out how to unpack '#{@downloaded_path}'."
end
container.new(@cask, @downloaded_path, @command)
container.new(@cask, @downloaded_path)
end
end

View File

@ -6,6 +6,14 @@ require "extend/io"
require "extend/hash_validator"
using HashValidator
def system_command(*args)
SystemCommand.run(*args)
end
def system_command!(*args)
SystemCommand.run!(*args)
end
class SystemCommand
extend Predicable

View File

@ -1,14 +1,9 @@
describe Hbc::Container::Dmg, :cask do
describe "#mount!" do
describe "#mount" do
let(:transmission) { Hbc::CaskLoader.load(cask_path("local-transmission")) }
subject(:dmg) { described_class.new(transmission, Pathname(transmission.url.path)) }
it "does not store nil mounts for dmgs with extra data" do
transmission = Hbc::CaskLoader.load(cask_path("local-transmission"))
dmg = Hbc::Container::Dmg.new(
transmission,
Pathname(transmission.url.path),
SystemCommand,
)
dmg.mount do |mounts|
begin
expect(mounts).not_to include nil

View File

@ -8,14 +8,11 @@ describe Hbc::Container::Naked, :cask do
path = Pathname("/tmp/downloads/kevin-spacey.pkg")
expected_destination = cask.staged_path.join("kevin spacey.pkg")
container = Hbc::Container::Naked.new(cask, path, FakeSystemCommand)
container = Hbc::Container::Naked.new(cask, path)
FakeSystemCommand.expects_command(
["/usr/bin/ditto", "--", path, expected_destination],
)
expect(container).to receive(:system_command!)
.with("/usr/bin/ditto", args: ["--", path, expected_destination])
expect {
container.extract(to: cask.staged_path, basename: "kevin spacey.pkg")
}.not_to raise_error
end
end