From 2c3926ae1ed61610daf7f994ae6ba6fe44a29263 Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Sat, 16 Jul 2022 01:59:59 +0200 Subject: [PATCH] Check nesting for blocks with one child --- Library/Homebrew/rubocops/components_order.rb | 9 ++ .../test/rubocops/components_order_spec.rb | 124 ++++++++++++++++++ 2 files changed, 133 insertions(+) diff --git a/Library/Homebrew/rubocops/components_order.rb b/Library/Homebrew/rubocops/components_order.rb index 6a5a789325..002ce1f9cf 100644 --- a/Library/Homebrew/rubocops/components_order.rb +++ b/Library/Homebrew/rubocops/components_order.rb @@ -116,6 +116,15 @@ module RuboCop end def check_on_system_block_content(component_precedence_list, on_system_block) + if on_system_block.body.block_type? && !on_system_methods.include?(on_system_block.body.method_name) + offending_node(on_system_block) + problem "Nest `#{on_system_block.method_name}` blocks inside `#{on_system_block.body.method_name}` " \ + "blocks when there is only one inner block." do |corrector| + original_source = on_system_block.source.split("\n") + new_source = [original_source.second, original_source.first, *original_source.drop(2)] + corrector.replace(on_system_block.source_range, new_source.join("\n")) + end + end on_system_allowed_methods = %w[ depends_on patch diff --git a/Library/Homebrew/test/rubocops/components_order_spec.rb b/Library/Homebrew/test/rubocops/components_order_spec.rb index 7b8e2a813a..20c6736a55 100644 --- a/Library/Homebrew/test/rubocops/components_order_spec.rb +++ b/Library/Homebrew/test/rubocops/components_order_spec.rb @@ -679,6 +679,130 @@ describe RuboCop::Cop::FormulaAudit::ComponentsOrder do RUBY end + it "reports an offense when a single `patch` block is inside the `on_arm` block" do + expect_offense(<<~RUBY) + class Foo < Formula + url "https://brew.sh/foo-1.0.tgz" + on_arm do + ^^^^^^^^^ Nest `on_arm` blocks inside `patch` blocks when there is only one inner block. + patch do + url "https://brew.sh/patch1.tar.gz" + sha256 "2c39089f64d9d4c3e632f120894b36b68dcc8ae8c6f5130c0c2e6f5bb7aebf2f" + end + end + end + RUBY + + expect_correction(<<~RUBY) + class Foo < Formula + url "https://brew.sh/foo-1.0.tgz" + patch do + on_arm do + url "https://brew.sh/patch1.tar.gz" + sha256 "2c39089f64d9d4c3e632f120894b36b68dcc8ae8c6f5130c0c2e6f5bb7aebf2f" + end + end + end + RUBY + end + + it "reports an offense when a single `resource` block is inside the `on_linux` block" do + expect_offense(<<~RUBY) + class Foo < Formula + url "https://brew.sh/foo-1.0.tgz" + on_linux do + ^^^^^^^^^^^ Nest `on_linux` blocks inside `resource` blocks when there is only one inner block. + resource do + url "https://brew.sh/resource1.tar.gz" + sha256 "586372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35" + end + end + end + RUBY + + expect_correction(<<~RUBY) + class Foo < Formula + url "https://brew.sh/foo-1.0.tgz" + resource do + on_linux do + url "https://brew.sh/resource1.tar.gz" + sha256 "586372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35" + end + end + end + RUBY + end + + it "reports an offense when a single `patch` block is inside the `on_monterey :or_newer` block" do + expect_offense(<<~RUBY) + class Foo < Formula + url "https://brew.sh/foo-1.0.tgz" + on_monterey :or_newer do + ^^^^^^^^^^^^^^^^^^^^^^^^ Nest `on_monterey` blocks inside `patch` blocks when there is only one inner block. + patch do + url "https://brew.sh/patch1.tar.gz" + sha256 "2c39089f64d9d4c3e632f120894b36b68dcc8ae8c6f5130c0c2e6f5bb7aebf2f" + end + end + end + RUBY + + expect_correction(<<~RUBY) + class Foo < Formula + url "https://brew.sh/foo-1.0.tgz" + patch do + on_monterey :or_newer do + url "https://brew.sh/patch1.tar.gz" + sha256 "2c39089f64d9d4c3e632f120894b36b68dcc8ae8c6f5130c0c2e6f5bb7aebf2f" + end + end + end + RUBY + end + + it "reports an offense when a single `resource` block is inside the `on_system` block" do + expect_offense(<<~RUBY) + class Foo < Formula + url "https://brew.sh/foo-1.0.tgz" + on_system :linux, macos: :monterey_or_older do + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Nest `on_system` blocks inside `resource` blocks when there is only one inner block. + resource do + url "https://brew.sh/resource1.tar.gz" + sha256 "586372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35" + end + end + end + RUBY + + expect_correction(<<~RUBY) + class Foo < Formula + url "https://brew.sh/foo-1.0.tgz" + resource do + on_system :linux, macos: :monterey_or_older do + url "https://brew.sh/resource1.tar.gz" + sha256 "586372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35" + end + end + end + RUBY + end + + it "reports no offenses when a single `on_arm` block is inside the `on_macos` block" do + expect_no_offenses(<<~RUBY) + class Foo < Formula + url "https://brew.sh/foo-1.0.tgz" + on_macos do + on_arm do + resource do + url "https://brew.sh/resource1.tar.gz" + sha256 "586372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35" + end + end + end + end + RUBY + end + context "when in a resource block" do it "reports no offenses for a valid `on_macos` and `on_linux` block" do expect_no_offenses(<<~RUBY)