From df635c8259dffca891424352765088a3b88a87d2 Mon Sep 17 00:00:00 2001 From: Kaito Udagawa Date: Mon, 21 Nov 2016 04:13:29 +0900 Subject: [PATCH] cask: compact the code --- Library/Homebrew/cask/lib/hbc/artifact/pkg.rb | 23 +++++++++---------- .../cask/test/cask/artifact/pkg_test.rb | 5 ++-- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/artifact/pkg.rb b/Library/Homebrew/cask/lib/hbc/artifact/pkg.rb index fffb10cae7..0569d2a86e 100644 --- a/Library/Homebrew/cask/lib/hbc/artifact/pkg.rb +++ b/Library/Homebrew/cask/lib/hbc/artifact/pkg.rb @@ -54,24 +54,23 @@ module Hbc ] args << "-verboseR" if Hbc.verbose args << "-allowUntrusted" if pkg_install_opts :allow_untrusted - with_choices_file pkg_install_opts(:choices) do |choices_path| + with_choices_file do |choices_path| args << "-applyChoiceChangesXML" << choices_path if choices_path @command.run!("/usr/sbin/installer", sudo: true, args: args, print_stdout: true) end end - def with_choices_file(choices) - unless choices - yield nil - return - end + def with_choices_file + return yield nil unless pkg_install_opts(:choices) - begin - file = Tempfile.new(["choices", ".xml"]) - file.write Plist::Emit.dump(choices) - yield file.path - ensure - file.close(true) + Tempfile.open(["choices", ".xml"]) do |file| + begin + file.write Plist::Emit.dump(pkg_install_opts(:choices)) + file.close + yield file.path + ensure + file.unlink + end end end end diff --git a/Library/Homebrew/cask/test/cask/artifact/pkg_test.rb b/Library/Homebrew/cask/test/cask/artifact/pkg_test.rb index 6e10177d12..3ed427763b 100644 --- a/Library/Homebrew/cask/test/cask/artifact/pkg_test.rb +++ b/Library/Homebrew/cask/test/cask/artifact/pkg_test.rb @@ -60,8 +60,9 @@ describe Hbc::Artifact::Pkg do EOS file.stubs path: Pathname.new("/tmp/choices.xml") - file.expects(:close).with true - Tempfile.expects(:new).returns file + file.expects(:close) + file.expects(:unlink) + Tempfile.expects(:open).yields file Hbc::FakeSystemCommand.expects_command(["/usr/bin/sudo", "-E", "--", "/usr/sbin/installer", "-pkg", @cask.staged_path.join("MyFancyPkg", "Fancy.pkg"), "-target", "/", "-applyChoiceChangesXML", @cask.staged_path.join("/tmp/choices.xml")])