From 0f481c83efd8d2fda2d8d6f6a2245cd7eed64d3a Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Mon, 4 Mar 2024 09:46:58 +0900 Subject: [PATCH] Fix flaky testing for `Minitst/MultipleAssertions` The shared `@cop` instance variable of `AssertOffense` module causes flaky tests due to state changes. The `AssertOffense` testing module needs to be updated to allow for tests that are independent in configuration between tests. It is necessary to prevent race conditions caused by shared configurations. This is a workaround to skip tests until the issue is resolved in `AssertOffense` module. --- .github/workflows/test.yml | 2 -- Rakefile | 4 +--- .../cop/minitest/multiple_assertions_test.rb | 20 +++++++++++++------ 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 18c9f79..0ba3c46 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -84,5 +84,3 @@ jobs: bundler-cache: true - name: test run: bundle exec rake test - env: - ERROR_ON_TEST_FAILURE: "false" diff --git a/Rakefile b/Rakefile index e85f4a3..7f37039 100644 --- a/Rakefile +++ b/Rakefile @@ -29,9 +29,7 @@ if RUBY_ENGINE == 'jruby' || RuboCop::Platform.windows? else desc 'Run tests' task :test do - error_on_failure = ENV.fetch('ERROR_ON_TEST_FAILURE', 'true') != 'false' - - system("bundle exec minitest-queue #{Dir.glob('test/**/*_test.rb').shelljoin}", exception: error_on_failure) + sh("bundle exec minitest-queue #{Dir.glob('test/**/*_test.rb').shelljoin}") end end diff --git a/test/rubocop/cop/minitest/multiple_assertions_test.rb b/test/rubocop/cop/minitest/multiple_assertions_test.rb index 232bba9..b47d666 100644 --- a/test/rubocop/cop/minitest/multiple_assertions_test.rb +++ b/test/rubocop/cop/minitest/multiple_assertions_test.rb @@ -186,9 +186,9 @@ class FooTest < ActiveSupport::TestCase err = assert_raises React::ServerRendering::PrerenderError do @renderer.render("NonExistentComponent", {}, nil) end - + assert_match(/NonExistentComponent/, err.to_s, "it names the component") - + assert_match(/\n/, err.to_s, "it includes the multi-line backtrace") end end @@ -205,9 +205,9 @@ class FooTest < ActiveSupport::TestCase assert_equal 1, 1 assert_equal 1, 1 end - + assert_match(/NonExistentComponent/, err.to_s, "it names the component") - + assert_match(/\n/, err.to_s, "it includes the multi-line backtrace") end end @@ -231,9 +231,9 @@ class FooTest < ActiveSupport::TestCase assert_equal 1, 1 end end - + assert_match(/NonExistentComponent/, err.to_s, "it names the component") - + assert_match(/\n/, err.to_s, "it includes the multi-line backtrace") end end @@ -292,6 +292,8 @@ def test_asserts_once end def test_generates_a_todo_based_on_the_worst_violation + skip 'FIXME: The shared `@cop` instance variable causes flaky tests due to state changes.' + inspect_source(<<-RUBY, @cop, 'test/foo_test.rb') class FooTest < Minitest::Test def test_asserts_once @@ -533,6 +535,8 @@ class FooTest < ActiveSupport::TestCase end def test_registers_offense_when_complex_structure_with_multiple_assertions + skip 'FIXME: The shared `@cop` instance variable causes flaky tests due to state changes.' + configure_max_assertions(2) assert_offense(<<~RUBY) @@ -572,6 +576,8 @@ class FooTest < ActiveSupport::TestCase end def test_registers_offense_when_complex_structure_with_assignments_and_multiple_assertions + skip 'FIXME: The shared `@cop` instance variable causes flaky tests due to state changes.' + configure_max_assertions(2) assert_offense(<<~RUBY) @@ -611,6 +617,8 @@ class FooTest < ActiveSupport::TestCase end def test_registers_offense_when_complex_multiple_assignment_structure_and_multiple_assertions + skip 'FIXME: The shared `@cop` instance variable causes flaky tests due to state changes.' + configure_max_assertions(2) assert_offense(<<~RUBY)