From 3db65e7ee53039c63e6b570d9881896ee51c5569 Mon Sep 17 00:00:00 2001 From: Max Eisner <4730112+max-ae@users.noreply.github.com> Date: Thu, 11 Aug 2022 17:59:53 +0200 Subject: [PATCH] move tests to own specs file --- .../lines/generate_completions_spec.rb | 78 ++++++++++ Library/Homebrew/test/rubocops/lines_spec.rb | 146 +++++------------- 2 files changed, 114 insertions(+), 110 deletions(-) create mode 100644 Library/Homebrew/test/rubocops/lines/generate_completions_spec.rb diff --git a/Library/Homebrew/test/rubocops/lines/generate_completions_spec.rb b/Library/Homebrew/test/rubocops/lines/generate_completions_spec.rb new file mode 100644 index 0000000000..f927b9cf08 --- /dev/null +++ b/Library/Homebrew/test/rubocops/lines/generate_completions_spec.rb @@ -0,0 +1,78 @@ +# typed: false +# frozen_string_literal: true + +require "rubocops/lines" + +describe RuboCop::Cop::FormulaAudit do + describe RuboCop::Cop::FormulaAudit::GenerateCompletionsDSL do + subject(:cop) { described_class.new } + + it "reports an offense when writing to a shell completions file directly" do + expect_offense(<<~RUBY, "/homebrew-core/Formula/foo.rb") + class Foo < Formula + name "foo" + + def install + (bash_completion/"foo").write Utils.safe_popen_read(bin/"foo", "completions", "bash") + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `generate_completions_from_executable(bin/"foo", "completions", shells: [:bash])` instead of `(bash_completion/"foo").write Utils.safe_popen_read(bin/"foo", "completions", "bash")`. + end + end + RUBY + + expect_correction(<<~RUBY) + class Foo < Formula + name "foo" + + def install + generate_completions_from_executable(bin/"foo", "completions", shells: [:bash]) + end + end + RUBY + end + + it "reports an offense when writing to a completions file indirectly" do + expect_offense(<<~RUBY) + class Foo < Formula + name "foo" + + def install + output = Utils.safe_popen_read(bin/"foo", "completions", "bash") + (bash_completion/"foo").write output + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `generate_completions_from_executable` DSL instead of `(bash_completion/"foo").write output`. + end + end + RUBY + end + end + + describe RuboCop::Cop::FormulaAudit::SingleGenerateCompletionsDSLCall do + subject(:cop) { described_class.new } + + it "reports an offense when using multiple #generate_completions_from_executable calls for different shells" do + expect_offense(<<~RUBY) + class Foo < Formula + name "foo" + + def install + generate_completions_from_executable(bin/"foo", "completions", shells: [:bash]) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use a single `generate_completions_from_executable` call combining all specified shells. + generate_completions_from_executable(bin/"foo", "completions", shells: [:zsh]) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use a single `generate_completions_from_executable` call combining all specified shells. + generate_completions_from_executable(bin/"foo", "completions", shells: [:fish]) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `generate_completions_from_executable(bin/"foo", "completions")` instead of `generate_completions_from_executable(bin/"foo", "completions", shells: [:fish])`. + end + end + RUBY + + expect_correction(<<~RUBY) + class Foo < Formula + name "foo" + + def install + generate_completions_from_executable(bin/"foo", "completions") + end + end + RUBY + end + end +end diff --git a/Library/Homebrew/test/rubocops/lines_spec.rb b/Library/Homebrew/test/rubocops/lines_spec.rb index ebfc4baf8f..10c3c776ea 100644 --- a/Library/Homebrew/test/rubocops/lines_spec.rb +++ b/Library/Homebrew/test/rubocops/lines_spec.rb @@ -3,130 +3,56 @@ require "rubocops/lines" -describe RuboCop::Cop::FormulaAudit do - describe RuboCop::Cop::FormulaAudit::Lines do - subject(:cop) { described_class.new } +describe RuboCop::Cop::FormulaAudit::Lines do + subject(:cop) { described_class.new } - context "when auditing deprecated special dependencies" do - it "reports an offense when using depends_on :automake" do - expect_offense(<<~RUBY) - class Foo < Formula - url 'https://brew.sh/foo-1.0.tgz' - depends_on :automake - ^^^^^^^^^^^^^^^^^^^^ :automake is deprecated. Usage should be \"automake\". - end - RUBY - end - - it "reports an offense when using depends_on :autoconf" do - expect_offense(<<~RUBY) - class Foo < Formula - url 'https://brew.sh/foo-1.0.tgz' - depends_on :autoconf - ^^^^^^^^^^^^^^^^^^^^ :autoconf is deprecated. Usage should be \"autoconf\". - end - RUBY - end - - it "reports an offense when using depends_on :libtool" do - expect_offense(<<~RUBY) - class Foo < Formula - url 'https://brew.sh/foo-1.0.tgz' - depends_on :libtool - ^^^^^^^^^^^^^^^^^^^ :libtool is deprecated. Usage should be \"libtool\". - end - RUBY - end - - it "reports an offense when using depends_on :apr" do - expect_offense(<<~RUBY) - class Foo < Formula - url 'https://brew.sh/foo-1.0.tgz' - depends_on :apr - ^^^^^^^^^^^^^^^ :apr is deprecated. Usage should be \"apr-util\". - end - RUBY - end - - it "reports an offense when using depends_on :tex" do - expect_offense(<<~RUBY) - class Foo < Formula - url 'https://brew.sh/foo-1.0.tgz' - depends_on :tex - ^^^^^^^^^^^^^^^ :tex is deprecated. - end - RUBY - end - end - end - - describe RuboCop::Cop::FormulaAudit::GenerateCompletionsDSL do - subject(:cop) { described_class.new } - - it "reports an offense when writing to a shell completions file directly" do - expect_offense(<<~RUBY, "/homebrew-core/Formula/foo.rb") - class Foo < Formula - name "foo" - - def install - (bash_completion/"foo").write Utils.safe_popen_read(bin/"foo", "completions", "bash") - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `generate_completions_from_executable(bin/"foo", "completions", shells: [:bash])` instead of `(bash_completion/"foo").write Utils.safe_popen_read(bin/"foo", "completions", "bash")`. - end - end - RUBY - - expect_correction(<<~RUBY) - class Foo < Formula - name "foo" - - def install - generate_completions_from_executable(bin/"foo", "completions", shells: [:bash]) - end - end - RUBY - end - - it "reports an offense when writing to a completions file indirectly" do + context "when auditing deprecated special dependencies" do + it "reports an offense when using depends_on :automake" do expect_offense(<<~RUBY) class Foo < Formula - name "foo" - - def install - output = Utils.safe_popen_read(bin/"foo", "completions", "bash") - (bash_completion/"foo").write output - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `generate_completions_from_executable` DSL instead of `(bash_completion/"foo").write output`. - end + url 'https://brew.sh/foo-1.0.tgz' + depends_on :automake + ^^^^^^^^^^^^^^^^^^^^ :automake is deprecated. Usage should be \"automake\". end RUBY end - end - describe RuboCop::Cop::FormulaAudit::SingleGenerateCompletionsDSLCall do - subject(:cop) { described_class.new } - - it "reports an offense when using multiple #generate_completions_from_executable calls for different shells" do + it "reports an offense when using depends_on :autoconf" do expect_offense(<<~RUBY) class Foo < Formula - name "foo" - - def install - generate_completions_from_executable(bin/"foo", "completions", shells: [:bash]) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use a single `generate_completions_from_executable` call combining all specified shells. - generate_completions_from_executable(bin/"foo", "completions", shells: [:zsh]) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use a single `generate_completions_from_executable` call combining all specified shells. - generate_completions_from_executable(bin/"foo", "completions", shells: [:fish]) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `generate_completions_from_executable(bin/"foo", "completions")` instead of `generate_completions_from_executable(bin/"foo", "completions", shells: [:fish])`. - end + url 'https://brew.sh/foo-1.0.tgz' + depends_on :autoconf + ^^^^^^^^^^^^^^^^^^^^ :autoconf is deprecated. Usage should be \"autoconf\". end RUBY + end - expect_correction(<<~RUBY) + it "reports an offense when using depends_on :libtool" do + expect_offense(<<~RUBY) class Foo < Formula - name "foo" + url 'https://brew.sh/foo-1.0.tgz' + depends_on :libtool + ^^^^^^^^^^^^^^^^^^^ :libtool is deprecated. Usage should be \"libtool\". + end + RUBY + end - def install - generate_completions_from_executable(bin/"foo", "completions") - end + it "reports an offense when using depends_on :apr" do + expect_offense(<<~RUBY) + class Foo < Formula + url 'https://brew.sh/foo-1.0.tgz' + depends_on :apr + ^^^^^^^^^^^^^^^ :apr is deprecated. Usage should be \"apr-util\". + end + RUBY + end + + it "reports an offense when using depends_on :tex" do + expect_offense(<<~RUBY) + class Foo < Formula + url 'https://brew.sh/foo-1.0.tgz' + depends_on :tex + ^^^^^^^^^^^^^^^ :tex is deprecated. end RUBY end