From c1a4a806e1b67eb6c8e8932eccd39401c5bdfec6 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Sat, 30 Mar 2024 18:44:39 -0700 Subject: [PATCH 1/4] rm unused OutputAsTTY helper --- Library/Homebrew/test/spec_helper.rb | 4 - .../test/support/helper/output_as_tty.rb | 88 ------------------- Library/Homebrew/test/system_command_spec.rb | 2 + 3 files changed, 2 insertions(+), 92 deletions(-) delete mode 100644 Library/Homebrew/test/support/helper/output_as_tty.rb diff --git a/Library/Homebrew/test/spec_helper.rb b/Library/Homebrew/test/spec_helper.rb index 70fa46df38..f6ce098f5d 100644 --- a/Library/Homebrew/test/spec_helper.rb +++ b/Library/Homebrew/test/spec_helper.rb @@ -44,7 +44,6 @@ require "test/support/helper/files" require "test/support/helper/fixtures" require "test/support/helper/formula" require "test/support/helper/mktmpdir" -require "test/support/helper/output_as_tty" require "test/support/helper/spec/shared_context/homebrew_cask" if OS.mac? require "test/support/helper/spec/shared_context/integration_test" @@ -131,15 +130,12 @@ RSpec.configure do |config| config.include(FileUtils) - config.include(Context) - config.include(RuboCop::RSpec::ExpectOffense) config.include(Test::Helper::Cask) config.include(Test::Helper::Fixtures) config.include(Test::Helper::Formula) config.include(Test::Helper::MkTmpDir) - config.include(Test::Helper::OutputAsTTY) config.before(:each, :needs_linux) do skip "Not running on Linux." unless OS.linux? diff --git a/Library/Homebrew/test/support/helper/output_as_tty.rb b/Library/Homebrew/test/support/helper/output_as_tty.rb deleted file mode 100644 index 7fd4b97c7a..0000000000 --- a/Library/Homebrew/test/support/helper/output_as_tty.rb +++ /dev/null @@ -1,88 +0,0 @@ -# frozen_string_literal: true - -require "delegate" - -module Test - module Helper - module OutputAsTTY - # This is a custom wrapper for the `output` matcher, - # used for testing output to a TTY: - # - # expect { - # print "test" if $stdout.tty? - # }.to output("test").to_stdout.as_tty - # - # expect { - # # command - # }.to output(...).to_stderr.as_tty.with_color - # - class Output < SimpleDelegator - def matches?(block) - return super(block) unless @tty - - colored_tty_block = lambda do - instance_eval("$#{@output} # $stdout", __FILE__, __LINE__).extend(Module.new do - def tty? - true - end - - alias_method :isatty, :tty? - end) - block.call - end - - return super(colored_tty_block) if @colors - - uncolored_tty_block = lambda do - instance_eval <<-EOS, __FILE__, __LINE__ + 1 - begin # begin - captured_stream = StringIO.new # captured_stream = StringIO.new - - original_stream = $#{@output} # original_stream = $stdout - $#{@output} = captured_stream # $stdout = captured_stream - - colored_tty_block.call # colored_tty_block.call - ensure # ensure - $#{@output} = original_stream # $stdout = original_stream - $#{@output}.print Tty.strip_ansi(captured_stream.string) # $stdout.print Tty.strip_ansi(captured_stream.string) - end # end - EOS - end - - super(uncolored_tty_block) - end - - def to_stdout - @output = :stdout - super - self - end - - def to_stderr - @output = :stderr - super - self - end - - def as_tty - @tty = true - return self if [:stdout, :stderr].include?(@output) - - raise "`as_tty` can only be chained to `stdout` or `stderr`." - end - - def with_color - @colors = true - return self if @tty - - raise "`with_color` can only be chained to `as_tty`." - end - end - - def output(*args) - core_matcher = super(*args) - Output.new(core_matcher) - end - end - end -end diff --git a/Library/Homebrew/test/system_command_spec.rb b/Library/Homebrew/test/system_command_spec.rb index 679851a930..d09612a1fb 100644 --- a/Library/Homebrew/test/system_command_spec.rb +++ b/Library/Homebrew/test/system_command_spec.rb @@ -194,6 +194,8 @@ RSpec.describe SystemCommand do end context "when `debug?` is true" do + include Context + let(:options) do { args: [ "-c", From fde7d380f77ce6cfb0d0dc656e432151ea58170d Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Sat, 30 Mar 2024 19:10:56 -0700 Subject: [PATCH 2/4] Don't include FileUtils everywhere --- Library/Homebrew/test/api/internal_tap_json/formula_spec.rb | 2 ++ Library/Homebrew/test/cmd/deps_spec.rb | 2 ++ Library/Homebrew/test/cmd/uses_spec.rb | 2 ++ Library/Homebrew/test/dev-cmd/pr-pull_spec.rb | 2 ++ Library/Homebrew/test/formula_auditor_spec.rb | 2 ++ Library/Homebrew/test/keg_spec.rb | 2 ++ Library/Homebrew/test/spec_helper.rb | 2 -- Library/Homebrew/test/tap_spec.rb | 2 ++ Library/Homebrew/test/utils/gzip_spec.rb | 2 ++ 9 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/test/api/internal_tap_json/formula_spec.rb b/Library/Homebrew/test/api/internal_tap_json/formula_spec.rb index 1426677483..cc03354e23 100644 --- a/Library/Homebrew/test/api/internal_tap_json/formula_spec.rb +++ b/Library/Homebrew/test/api/internal_tap_json/formula_spec.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true RSpec.describe "Internal Tap JSON -- Formula" do + include FileUtils + let(:internal_tap_json) { File.read(TEST_FIXTURE_DIR/"internal_tap_json/homebrew-core.json").chomp } let(:tap_git_head) { "9977471165641744a829d3e494fa563407503297" } diff --git a/Library/Homebrew/test/cmd/deps_spec.rb b/Library/Homebrew/test/cmd/deps_spec.rb index aa6277196b..e7e19faa01 100644 --- a/Library/Homebrew/test/cmd/deps_spec.rb +++ b/Library/Homebrew/test/cmd/deps_spec.rb @@ -3,6 +3,8 @@ require "cmd/shared_examples/args_parse" RSpec.describe "brew deps" do + include FileUtils + it_behaves_like "parseable arguments" it "outputs all of a Formula's dependencies and their dependencies on separate lines", :integration_test do diff --git a/Library/Homebrew/test/cmd/uses_spec.rb b/Library/Homebrew/test/cmd/uses_spec.rb index 3495adca0a..6c1b864235 100644 --- a/Library/Homebrew/test/cmd/uses_spec.rb +++ b/Library/Homebrew/test/cmd/uses_spec.rb @@ -3,6 +3,8 @@ require "cmd/shared_examples/args_parse" RSpec.describe "brew uses" do + include FileUtils + it_behaves_like "parseable arguments" it "prints the Formulae a given Formula is used by", :integration_test do diff --git a/Library/Homebrew/test/dev-cmd/pr-pull_spec.rb b/Library/Homebrew/test/dev-cmd/pr-pull_spec.rb index 422c9ea485..8850c1cb98 100644 --- a/Library/Homebrew/test/dev-cmd/pr-pull_spec.rb +++ b/Library/Homebrew/test/dev-cmd/pr-pull_spec.rb @@ -6,6 +6,8 @@ require "tap" require "cmd/shared_examples/args_parse" RSpec.describe Homebrew::DevCmd::PrPull do + include FileUtils + let(:pr_pull) { described_class.new(["foo"]) } let(:formula_rebuild) do <<~EOS diff --git a/Library/Homebrew/test/formula_auditor_spec.rb b/Library/Homebrew/test/formula_auditor_spec.rb index 1270a266c2..f25a1920e2 100644 --- a/Library/Homebrew/test/formula_auditor_spec.rb +++ b/Library/Homebrew/test/formula_auditor_spec.rb @@ -3,6 +3,8 @@ require "formula_auditor" RSpec.describe Homebrew::FormulaAuditor do + include FileUtils + let(:dir) { mktmpdir } let(:foo_version) do @count ||= 0 diff --git a/Library/Homebrew/test/keg_spec.rb b/Library/Homebrew/test/keg_spec.rb index 5231fe619f..d7f6d804b6 100644 --- a/Library/Homebrew/test/keg_spec.rb +++ b/Library/Homebrew/test/keg_spec.rb @@ -4,6 +4,8 @@ require "keg" require "stringio" RSpec.describe Keg do + include FileUtils + def setup_test_keg(name, version) path = HOMEBREW_CELLAR/name/version (path/"bin").mkpath diff --git a/Library/Homebrew/test/spec_helper.rb b/Library/Homebrew/test/spec_helper.rb index f6ce098f5d..141bee02f7 100644 --- a/Library/Homebrew/test/spec_helper.rb +++ b/Library/Homebrew/test/spec_helper.rb @@ -128,8 +128,6 @@ RSpec.configure do |config| # Never truncate output objects. RSpec::Support::ObjectFormatter.default_instance.max_formatted_output_length = nil - config.include(FileUtils) - config.include(RuboCop::RSpec::ExpectOffense) config.include(Test::Helper::Cask) diff --git a/Library/Homebrew/test/tap_spec.rb b/Library/Homebrew/test/tap_spec.rb index ecfa2b8996..2a87f189df 100644 --- a/Library/Homebrew/test/tap_spec.rb +++ b/Library/Homebrew/test/tap_spec.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true RSpec.describe Tap do + include FileUtils + alias_matcher :have_formula_file, :be_formula_file alias_matcher :have_custom_remote, :be_custom_remote diff --git a/Library/Homebrew/test/utils/gzip_spec.rb b/Library/Homebrew/test/utils/gzip_spec.rb index 59540a1675..4679a15e1e 100644 --- a/Library/Homebrew/test/utils/gzip_spec.rb +++ b/Library/Homebrew/test/utils/gzip_spec.rb @@ -3,6 +3,8 @@ require "utils/gzip" RSpec.describe Utils::Gzip do + include FileUtils + describe "compress_with_options" do it "uses the explicitly specified mtime, orig_name, and output path when passed" do mktmpdir do |path| From 914e53e9d51095b855b9460351d1c1deef8093f8 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Sat, 30 Mar 2024 20:06:26 -0700 Subject: [PATCH 3/4] Move isolated matchers into the specs that uses them --- Library/Homebrew/test/cmd/info_spec.rb | 9 +++++++++ Library/Homebrew/test/spec_helper.rb | 10 ---------- Library/Homebrew/test/system_command_result_spec.rb | 2 ++ 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/Library/Homebrew/test/cmd/info_spec.rb b/Library/Homebrew/test/cmd/info_spec.rb index de636a6e4e..d302dca998 100644 --- a/Library/Homebrew/test/cmd/info_spec.rb +++ b/Library/Homebrew/test/cmd/info_spec.rb @@ -5,6 +5,15 @@ require "cmd/info" require "cmd/shared_examples/args_parse" RSpec.describe "brew info" do + RSpec::Matchers.define :a_json_string do + match do |actual| + JSON.parse(actual) + true + rescue JSON::ParserError + false + end + end + it_behaves_like "parseable arguments" it "prints as json with the --json=v1 flag", :integration_test do diff --git a/Library/Homebrew/test/spec_helper.rb b/Library/Homebrew/test/spec_helper.rb index 141bee02f7..1cf4a428ea 100644 --- a/Library/Homebrew/test/spec_helper.rb +++ b/Library/Homebrew/test/spec_helper.rb @@ -304,16 +304,6 @@ end RSpec::Matchers.define_negated_matcher :not_to_output, :output RSpec::Matchers.alias_matcher :have_failed, :be_failed -RSpec::Matchers.alias_matcher :a_string_containing, :include - -RSpec::Matchers.define :a_json_string do - match do |actual| - JSON.parse(actual) - true - rescue JSON::ParserError - false - end -end # Match consecutive elements in an array. RSpec::Matchers.define :array_including_cons do |*cons| diff --git a/Library/Homebrew/test/system_command_result_spec.rb b/Library/Homebrew/test/system_command_result_spec.rb index a985fc9010..d06df27fd8 100644 --- a/Library/Homebrew/test/system_command_result_spec.rb +++ b/Library/Homebrew/test/system_command_result_spec.rb @@ -3,6 +3,8 @@ require "system_command" RSpec.describe SystemCommand::Result do + RSpec::Matchers.alias_matcher :a_string_containing, :include + subject(:result) do described_class.new([], output_array, instance_double(Process::Status, exitstatus: 0, success?: true), secrets: []) From 471e94d1e8dc19b637e2abf0c4d7e111f670dd63 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Sun, 31 Mar 2024 13:14:56 -0700 Subject: [PATCH 4/4] Add missing require --- Library/Homebrew/formula_auditor.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/Library/Homebrew/formula_auditor.rb b/Library/Homebrew/formula_auditor.rb index 42ae0d277b..587a83fb2c 100644 --- a/Library/Homebrew/formula_auditor.rb +++ b/Library/Homebrew/formula_auditor.rb @@ -5,6 +5,7 @@ require "deprecate_disable" require "formula_text_auditor" require "formula_versions" require "resource_auditor" +require "utils/shared_audits" module Homebrew # Auditor for checking common violations in {Formula}e.