diff --git a/changelog/new_accept_it_test_block_as_testcase.md b/changelog/new_accept_it_test_block_as_testcase.md new file mode 100644 index 00000000..50c8959e --- /dev/null +++ b/changelog/new_accept_it_test_block_as_testcase.md @@ -0,0 +1 @@ +* [#238](/~https://github.com/rubocop/rubocop-minitest/pull/238): Support `it`/`test` testing blocks as test cases. ([@fatkodima][]) diff --git a/lib/rubocop/cop/minitest/assert_output.rb b/lib/rubocop/cop/minitest/assert_output.rb index e21fadf5..14a18de0 100644 --- a/lib/rubocop/cop/minitest/assert_output.rb +++ b/lib/rubocop/cop/minitest/assert_output.rb @@ -36,8 +36,7 @@ def on_gvasgn(node) private def find_test_case(node) - def_ancestor = node.each_ancestor(:def).first - def_ancestor if test_case?(def_ancestor) + node.each_ancestor.find { |ancestor| test_case?(ancestor) } end def references_gvar?(assertion, gvar_name) diff --git a/lib/rubocop/cop/mixin/minitest_exploration_helpers.rb b/lib/rubocop/cop/mixin/minitest_exploration_helpers.rb index 9656ad9d..5cb0aefa 100644 --- a/lib/rubocop/cop/mixin/minitest_exploration_helpers.rb +++ b/lib/rubocop/cop/mixin/minitest_exploration_helpers.rb @@ -30,22 +30,23 @@ def test_class?(class_node) end def test_case?(node) - return false unless node&.def_type? && test_method?(node) + return false unless (node&.def_type? && test_method?(node)) || + (node&.block_type? && test_block?(node)) class_ancestor = node.each_ancestor(:class).first test_class?(class_ancestor) end def test_cases(class_node, visibility_check: true) - test_cases = class_def_nodes(class_node).select do |def_node| + test_methods = class_def_nodes(class_node).select do |def_node| test_method?(def_node, visibility_check: visibility_check) end # Support Active Support's `test 'example' { ... }` method. # https://api.rubyonrails.org/classes/ActiveSupport/Testing/Declarative.html - test_blocks = class_node.each_descendant(:block).select { |block| block.method?(:test) || block.method?(:it) } + test_blocks = class_node.each_descendant(:block).select { |block_node| test_block?(block_node) } - test_cases + test_blocks + test_methods + test_blocks end def test_method?(def_node, visibility_check: true) @@ -54,6 +55,10 @@ def test_method?(def_node, visibility_check: true) test_case_name?(def_node.method_name) && !def_node.arguments? end + def test_block?(block_node) + block_node.method?(:test) || block_node.method?(:it) + end + def lifecycle_hooks(class_node) class_def_nodes(class_node) .select { |def_node| lifecycle_hook_method?(def_node) } diff --git a/test/rubocop/cop/minitest/assert_output_test.rb b/test/rubocop/cop/minitest/assert_output_test.rb index 5e3c0982..4484427a 100644 --- a/test/rubocop/cop/minitest/assert_output_test.rb +++ b/test/rubocop/cop/minitest/assert_output_test.rb @@ -17,6 +17,22 @@ def test_do_something RUBY end + def test_registers_offense_when_mutating_output_global_variable_in_it_block_form + assert_offense(<<~RUBY) + class FooTest < Minitest::Test + describe Foo do + it "does something" do + $stdout = StringIO.new + puts object.method + $stdout.rewind + assert_match expected, $stdout.read + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `assert_output` instead of mutating $stdout. + end + end + end + RUBY + end + def test_does_not_register_offense_when_not_inside_test_case assert_no_offenses(<<~RUBY) class FooTest < Minitest::Test