diff --git a/changelog/fix_an_error_for_minitest_test_file_name.md b/changelog/fix_an_error_for_minitest_test_file_name.md new file mode 100644 index 00000000..78512c29 --- /dev/null +++ b/changelog/fix_an_error_for_minitest_test_file_name.md @@ -0,0 +1 @@ +* [#225](https://github.com/rubocop/rubocop-minitest/issues/225): Fix an error for `Minitest/TestFileName` when using empty file. ([@koic][]) diff --git a/lib/rubocop/cop/minitest/test_file_name.rb b/lib/rubocop/cop/minitest/test_file_name.rb index 483e7d3f..e6f6711d 100644 --- a/lib/rubocop/cop/minitest/test_file_name.rb +++ b/lib/rubocop/cop/minitest/test_file_name.rb @@ -21,7 +21,8 @@ class TestFileName < Base MSG = 'Test file path should start with `test_` or end with `_test.rb`.' def on_new_investigation - return unless test_file?(processed_source.ast) + return unless (ast = processed_source.ast) + return unless test_file?(ast) add_global_offense(MSG) unless valid_file_name? end diff --git a/test/rubocop/cop/minitest/test_file_name_test.rb b/test/rubocop/cop/minitest/test_file_name_test.rb index a7fa2ba7..11b7d093 100644 --- a/test/rubocop/cop/minitest/test_file_name_test.rb +++ b/test/rubocop/cop/minitest/test_file_name_test.rb @@ -44,4 +44,8 @@ def test_does_not_register_offense_for_non_test_classes class Foo; end RUBY end + + def test_does_not_register_offense_for_empty_file + assert_no_offenses('', 'lib/foo.rb') + end end