brew/Library/Homebrew/test/rubocops/text/shell_variables_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

87 lines
2.7 KiB
Ruby

# typed: false
# frozen_string_literal: true
require "rubocops/lines"
describe RuboCop::Cop::FormulaAudit::ShellVariables do
subject(:cop) { described_class.new }
context "when auditing shell variables" do
it "reports and corrects unexpanded shell variables in `Utils.popen`" do
expect_offense(<<~RUBY)
class Foo < Formula
def install
Utils.popen "SHELL=bash foo"
^^^^^^^^^^^^^^^^ FormulaAudit/ShellVariables: Use `Utils.popen({ "SHELL" => "bash" }, "foo")` instead of `Utils.popen "SHELL=bash foo"`
end
end
RUBY
expect_correction(<<~RUBY)
class Foo < Formula
def install
Utils.popen { "SHELL" => "bash" }, "foo"
end
end
RUBY
end
it "reports and corrects unexpanded shell variables in `Utils.safe_popen_read`" do
expect_offense(<<~RUBY)
class Foo < Formula
def install
Utils.safe_popen_read "SHELL=bash foo"
^^^^^^^^^^^^^^^^ FormulaAudit/ShellVariables: Use `Utils.safe_popen_read({ "SHELL" => "bash" }, "foo")` instead of `Utils.safe_popen_read "SHELL=bash foo"`
end
end
RUBY
expect_correction(<<~RUBY)
class Foo < Formula
def install
Utils.safe_popen_read { "SHELL" => "bash" }, "foo"
end
end
RUBY
end
it "reports and corrects unexpanded shell variables in `Utils.safe_popen_write`" do
expect_offense(<<~RUBY)
class Foo < Formula
def install
Utils.safe_popen_write "SHELL=bash foo"
^^^^^^^^^^^^^^^^ FormulaAudit/ShellVariables: Use `Utils.safe_popen_write({ "SHELL" => "bash" }, "foo")` instead of `Utils.safe_popen_write "SHELL=bash foo"`
end
end
RUBY
expect_correction(<<~RUBY)
class Foo < Formula
def install
Utils.safe_popen_write { "SHELL" => "bash" }, "foo"
end
end
RUBY
end
it "reports and corrects unexpanded shell variables while preserving string interpolation" do
expect_offense(<<~RUBY)
class Foo < Formula
def install
Utils.popen "SHELL=bash \#{bin}/foo"
^^^^^^^^^^^^^^^^^^^^^^^ FormulaAudit/ShellVariables: Use `Utils.popen({ "SHELL" => "bash" }, "\#{bin}/foo")` instead of `Utils.popen "SHELL=bash \#{bin}/foo"`
end
end
RUBY
expect_correction(<<~RUBY)
class Foo < Formula
def install
Utils.popen { "SHELL" => "bash" }, "\#{bin}/foo"
end
end
RUBY
end
end
end