brew/Library/Homebrew/test/rubocops/text/option_declarations_spec.rb
Issy Long da734a30c2
Say yes to RuboCop's DisplayCopNames; fix test expectations
- Fixing the test expected output was unbelievably tedious.
- There's been debate about this setting being `false` but in
  https://github.com/Homebrew/brew/pull/15136#issuecomment-1500063225
  we decided that it was worth using the default since RuboCop behaviour changed
  so we'd have had to do some horrible things to keep it as `false` -
  https://github.com/Homebrew/brew/pull/15136#issuecomment-1500037278 -
  and multiple maintainers specify the `--display-cop-names` option to
  `brew style` themselves since it's clearer what's gone wrong.
2023-04-07 19:14:07 +01:00

160 lines
5.3 KiB
Ruby

# typed: false
# frozen_string_literal: true
require "rubocops/lines"
describe RuboCop::Cop::FormulaAudit::OptionDeclarations do
subject(:cop) { described_class.new }
context "when auditing options" do
it "reports an offense when `build.without?` is used in homebrew/core" do
expect_offense(<<~RUBY, "/homebrew-core/")
class Foo < Formula
desc "foo"
url 'https://brew.sh/foo-1.0.tgz'
def install
build.without? "bar"
^^^^^^^^^^^^^^^^^^^^ FormulaAudit/OptionDeclarations: Formulae in homebrew/core should not use `build.without?`.
end
end
RUBY
end
it "reports an offense when `build.with?` is used in homebrew/core" do
expect_offense(<<~RUBY, "/homebrew-core/")
class Foo < Formula
desc "foo"
url 'https://brew.sh/foo-1.0.tgz'
def install
build.with? "bar"
^^^^^^^^^^^^^^^^^ FormulaAudit/OptionDeclarations: Formulae in homebrew/core should not use `build.with?`.
end
end
RUBY
end
it "reports an offense when `build.without?` is used for a conditional dependency" do
expect_offense(<<~RUBY)
class Foo < Formula
depends_on "bar" if build.without?("baz")
^^^^^^^^^^^^^^^^^^^^^ FormulaAudit/OptionDeclarations: Use `:optional` or `:recommended` instead of `if build.without?("baz")`
end
RUBY
end
it "reports an offense when `build.with?` is used for a conditional dependency" do
expect_offense(<<~RUBY)
class Foo < Formula
depends_on "bar" if build.with?("baz")
^^^^^^^^^^^^^^^^^^ FormulaAudit/OptionDeclarations: Use `:optional` or `:recommended` instead of `if build.with?("baz")`
end
RUBY
end
it "reports an offense when `build.without?` is used with `unless`" do
expect_offense(<<~RUBY)
class Foo < Formula
desc "foo"
url 'https://brew.sh/foo-1.0.tgz'
def post_install
return unless build.without? "bar"
^^^^^^^^^^^^^^^^^^^^ FormulaAudit/OptionDeclarations: Use if build.with? "bar" instead of unless build.without? "bar"
end
end
RUBY
end
it "reports an offense when `build.with?` is used with `unless`" do
expect_offense(<<~RUBY)
class Foo < Formula
desc "foo"
url 'https://brew.sh/foo-1.0.tgz'
def post_install
return unless build.with? "bar"
^^^^^^^^^^^^^^^^^ FormulaAudit/OptionDeclarations: Use if build.without? "bar" instead of unless build.with? "bar"
end
end
RUBY
end
it "reports an offense when `build.with?` is negated" do
expect_offense(<<~RUBY)
class Foo < Formula
desc "foo"
url 'https://brew.sh/foo-1.0.tgz'
def post_install
return if !build.with? "bar"
^^^^^^^^^^^^^^^^^^ FormulaAudit/OptionDeclarations: Don't negate 'build.with?': use 'build.without?'
end
end
RUBY
end
it "reports an offense when `build.without?` is negated" do
expect_offense(<<~RUBY)
class Foo < Formula
desc "foo"
url 'https://brew.sh/foo-1.0.tgz'
def post_install
return if !build.without? "bar"
^^^^^^^^^^^^^^^^^^^^^ FormulaAudit/OptionDeclarations: Don't negate 'build.without?': use 'build.with?'
end
end
RUBY
end
it "reports an offense when a `build.without?` conditional is unnecessary" do
expect_offense(<<~RUBY)
class Foo < Formula
desc "foo"
url 'https://brew.sh/foo-1.0.tgz'
def post_install
return if build.without? "--without-bar"
^^^^^^^^^^^^^^^ FormulaAudit/OptionDeclarations: Don't duplicate 'without': Use `build.without? "bar"` to check for "--without-bar"
end
end
RUBY
end
it "reports an offense when a `build.with?` conditional is unnecessary" do
expect_offense(<<~RUBY)
class Foo < Formula
desc "foo"
url 'https://brew.sh/foo-1.0.tgz'
def post_install
return if build.with? "--with-bar"
^^^^^^^^^^^^ FormulaAudit/OptionDeclarations: Don't duplicate 'with': Use `build.with? "bar"` to check for "--with-bar"
end
end
RUBY
end
it "reports an offense when `build.include?` is used" do
expect_offense(<<~RUBY)
class Foo < Formula
desc "foo"
url 'https://brew.sh/foo-1.0.tgz'
def post_install
return if build.include? "foo"
^^^^^^^^^^^^^^^^^^^^ FormulaAudit/OptionDeclarations: `build.include?` is deprecated
end
end
RUBY
end
it "reports an offense when `def option` is used" do
expect_offense(<<~RUBY)
class Foo < Formula
desc "foo"
url 'https://brew.sh/foo-1.0.tgz'
def options
^^^^^^^^^^^ FormulaAudit/OptionDeclarations: Use new-style option definitions
[["--bar", "desc"]]
end
end
RUBY
end
end
end