From 50a352c0c0cd151f12aa35790bdcb2d52c5d5249 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Thu, 23 Nov 2023 16:11:41 +0900 Subject: [PATCH] Fix an error for `Minitst/AssertOperator` and `Minitest/RefuteOperator` This PR fixes the following error for `Minitst/AssertOperator` and `Minitest/RefuteOperator` when running with Ruby 2.7: ```console $ ruby -v ruby 2.7.8p225 (2023-03-30 revision 1f4d455848) [x86_64-darwin19] $ bundle exec rake Finished in 2.522134s, 15.0666 runs/s, 18.6350 assertions/s. 1) Error: RefuteOperatorTest#test_registers_offense_when_using_refute_with_operator_method_and_message: FrozenError: can't modify frozen String: "expected, :<, actual" /Users/koic/src/github.com/rubocop/rubocop-minitest/lib/rubocop/cop/minitest/refute_operator.rb:46: in `build_new_arguments' /Users/koic/src/github.com/rubocop/rubocop-minitest/lib/rubocop/cop/minitest/refute_operator.rb:30: in `on_send' ``` And it introduces CI for the oldest RuboCop version still supported, implementing a workflow for regression testing that is similar to the one found a https://github.com/rubocop/rubocop-rails/commit/c9acb7a. --- .github/workflows/test.yml | 32 +++++++++++++++++++ ...est_assert_operator_and_refute_operator.md | 1 + lib/rubocop/cop/minitest/assert_operator.rb | 2 +- lib/rubocop/cop/minitest/refute_operator.rb | 2 +- 4 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/test.yml create mode 100644 changelog/fix_error_for_minitest_assert_operator_and_refute_operator.md diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 00000000..af69493d --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,32 @@ +name: CI + +on: + push: + branches: + - master + pull_request: + workflow_dispatch: + +concurrency: + group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} + cancel-in-progress: true + +jobs: + oldest_supported_rubocop: + runs-on: ubuntu-latest + name: The oldest supported RuboCop version + steps: + - uses: actions/checkout@v4 + - name: Use the oldest supported RuboCop + run: | + sed -e "/gem 'rubocop', github: 'rubocop\/rubocop'/d" -i Gemfile + cat << EOF > Gemfile.local + gem 'rubocop', '1.39.0' # Specify the oldest supported RuboCop version + EOF + - name: set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: 2.7 + bundler-cache: true + - name: test + run: bundle exec rake diff --git a/changelog/fix_error_for_minitest_assert_operator_and_refute_operator.md b/changelog/fix_error_for_minitest_assert_operator_and_refute_operator.md new file mode 100644 index 00000000..a890e1df --- /dev/null +++ b/changelog/fix_error_for_minitest_assert_operator_and_refute_operator.md @@ -0,0 +1 @@ +* [#275](https://github.com/rubocop/rubocop-minitest/pull/275): Make `Minitest/AssertMatch` aware of `assert_operator` when running with Ruby 2.7. ([@koic][]) diff --git a/lib/rubocop/cop/minitest/assert_operator.rb b/lib/rubocop/cop/minitest/assert_operator.rb index 241adca2..15b2935a 100644 --- a/lib/rubocop/cop/minitest/assert_operator.rb +++ b/lib/rubocop/cop/minitest/assert_operator.rb @@ -40,7 +40,7 @@ def on_send(node) def build_new_arguments(node) lhs, op, rhs = *node.first_argument - new_arguments = "#{lhs.source}, :#{op}, #{rhs.source}" + new_arguments = +"#{lhs.source}, :#{op}, #{rhs.source}" if node.arguments.count == 2 new_arguments << ", #{node.last_argument.source}" diff --git a/lib/rubocop/cop/minitest/refute_operator.rb b/lib/rubocop/cop/minitest/refute_operator.rb index 213ebeeb..be58bd8c 100644 --- a/lib/rubocop/cop/minitest/refute_operator.rb +++ b/lib/rubocop/cop/minitest/refute_operator.rb @@ -40,7 +40,7 @@ def on_send(node) def build_new_arguments(node) lhs, op, rhs = *node.first_argument - new_arguments = "#{lhs.source}, :#{op}, #{rhs.source}" + new_arguments = +"#{lhs.source}, :#{op}, #{rhs.source}" if node.arguments.count == 2 new_arguments << ", #{node.last_argument.source}"