From 2dd40720f075bf93551a27fe0be64c96312632fd Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Thu, 17 Dec 2020 18:09:33 +0100 Subject: [PATCH] Add test for `SystemCommand` with `SIGINT` handler. --- Library/Homebrew/test/spec_helper.rb | 49 +++++++++++--------- Library/Homebrew/test/system_command_spec.rb | 23 +++++++++ 2 files changed, 49 insertions(+), 23 deletions(-) diff --git a/Library/Homebrew/test/spec_helper.rb b/Library/Homebrew/test/spec_helper.rb index 47a9d38ab3..0652949937 100644 --- a/Library/Homebrew/test/spec_helper.rb +++ b/Library/Homebrew/test/spec_helper.rb @@ -165,34 +165,37 @@ RSpec.configure do |config| config.around do |example| def find_files + return [] unless File.exist?(TEST_TMPDIR) + Find.find(TEST_TMPDIR) .reject { |f| File.basename(f) == ".DS_Store" } + .reject { |f| TEST_DIRECTORIES.include?(Pathname(f)) } .map { |f| f.sub(TEST_TMPDIR, "") } end + Homebrew.raise_deprecation_exceptions = true + + Formulary.clear_cache + Tap.clear_cache + DependencyCollector.clear_cache + Formula.clear_cache + Keg.clear_cache + Tab.clear_cache + FormulaInstaller.clear_attempted + FormulaInstaller.clear_installed + + TEST_DIRECTORIES.each(&:mkpath) + + @__homebrew_failed = Homebrew.failed? + + @__files_before_test = find_files + + @__env = ENV.to_hash # dup doesn't work on ENV + + @__stdout = $stdout.clone + @__stderr = $stderr.clone + begin - Homebrew.raise_deprecation_exceptions = true - - Formulary.clear_cache - Tap.clear_cache - DependencyCollector.clear_cache - Formula.clear_cache - Keg.clear_cache - Tab.clear_cache - FormulaInstaller.clear_attempted - FormulaInstaller.clear_installed - - TEST_DIRECTORIES.each(&:mkpath) - - @__homebrew_failed = Homebrew.failed? - - @__files_before_test = find_files - - @__env = ENV.to_hash # dup doesn't work on ENV - - @__stdout = $stdout.clone - @__stderr = $stderr.clone - if (example.metadata.keys & [:focus, :byebug]).empty? && !ENV.key?("VERBOSE_TESTS") $stdout.reopen(File::NULL) $stderr.reopen(File::NULL) @@ -224,7 +227,7 @@ RSpec.configure do |config| Tab.clear_cache FileUtils.rm_rf [ - TEST_DIRECTORIES.map(&:children), + *TEST_DIRECTORIES, *Keg::MUST_EXIST_SUBDIRECTORIES, HOMEBREW_LINKED_KEGS, HOMEBREW_PINNED_KEGS, diff --git a/Library/Homebrew/test/system_command_spec.rb b/Library/Homebrew/test/system_command_spec.rb index 9124c58d4e..0ade6a87c6 100644 --- a/Library/Homebrew/test/system_command_spec.rb +++ b/Library/Homebrew/test/system_command_spec.rb @@ -281,5 +281,28 @@ describe SystemCommand do }.to raise_error.with_message(redacted_msg).and output(redacted_msg).to_stderr end end + + context "when a `SIGINT` handler is set in the parent process" do + it "is not interrupted" do + start_time = Time.now + + pid = fork do + trap("INT") do + # Ignore SIGINT. + end + + described_class.run! "sleep", args: [5] + + exit! + end + + sleep 1 + Process.kill("INT", pid) + + Process.waitpid(pid) + + expect(Time.now - start_time).to be >= 5 + end + end end end