2021-01-31 14:50:29 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "rubocops/lines"
|
|
|
|
|
2024-02-18 15:11:11 -08:00
|
|
|
RSpec.describe RuboCop::Cop::FormulaAudit::ShellVariables do
|
2021-01-31 14:50:29 -05:00
|
|
|
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"
|
2023-04-07 17:16:48 +01:00
|
|
|
^^^^^^^^^^^^^^^^ FormulaAudit/ShellVariables: Use `Utils.popen({ "SHELL" => "bash" }, "foo")` instead of `Utils.popen "SHELL=bash foo"`
|
2021-01-31 14:50:29 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
expect_correction(<<~RUBY)
|
|
|
|
class Foo < Formula
|
|
|
|
def install
|
2024-10-17 23:46:40 -07:00
|
|
|
Utils.popen({ "SHELL" => "bash" }, "foo")
|
2021-01-31 14:50:29 -05:00
|
|
|
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"
|
2023-04-07 17:16:48 +01:00
|
|
|
^^^^^^^^^^^^^^^^ FormulaAudit/ShellVariables: Use `Utils.safe_popen_read({ "SHELL" => "bash" }, "foo")` instead of `Utils.safe_popen_read "SHELL=bash foo"`
|
2021-01-31 14:50:29 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
expect_correction(<<~RUBY)
|
|
|
|
class Foo < Formula
|
|
|
|
def install
|
2024-10-17 23:46:40 -07:00
|
|
|
Utils.safe_popen_read({ "SHELL" => "bash" }, "foo")
|
2021-01-31 14:50:29 -05:00
|
|
|
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"
|
2023-04-07 17:16:48 +01:00
|
|
|
^^^^^^^^^^^^^^^^ FormulaAudit/ShellVariables: Use `Utils.safe_popen_write({ "SHELL" => "bash" }, "foo")` instead of `Utils.safe_popen_write "SHELL=bash foo"`
|
2021-01-31 14:50:29 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
expect_correction(<<~RUBY)
|
|
|
|
class Foo < Formula
|
|
|
|
def install
|
2024-10-17 23:46:40 -07:00
|
|
|
Utils.safe_popen_write({ "SHELL" => "bash" }, "foo")
|
2021-01-31 14:50:29 -05:00
|
|
|
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"
|
2023-04-07 17:16:48 +01:00
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^ FormulaAudit/ShellVariables: Use `Utils.popen({ "SHELL" => "bash" }, "\#{bin}/foo")` instead of `Utils.popen "SHELL=bash \#{bin}/foo"`
|
2021-01-31 14:50:29 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
expect_correction(<<~RUBY)
|
|
|
|
class Foo < Formula
|
|
|
|
def install
|
2024-10-17 23:46:40 -07:00
|
|
|
Utils.popen({ "SHELL" => "bash" }, "\#{bin}/foo")
|
2021-01-31 14:50:29 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|