Skip to content

Commit

Permalink
[Fix #213] Add new cop Minitest/TestFileName
Browse files Browse the repository at this point in the history
Closes #213
  • Loading branch information
tejasbubane committed Jan 1, 2023
1 parent bb9fd78 commit 21cf39b
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/new_test_file_name_cop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#213](https://github.com/rubocop/rubocop-minitest/issues/213): Add new `Minitest/TestFileName` cop. ([@tejasbubane][])
6 changes: 6 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,12 @@ Minitest/SkipWithoutReason:
Enabled: pending
VersionAdded: '0.24'

Minitest/TestFileName:
Description: 'Checks if test file names start with `test_` or end with `_test.rb`.'
StyleGuide: 'https://minitest.rubystyle.guide/#file-naming'
Enabled: pending
VersionAdded: '<<next>>'

Minitest/TestMethodName:
Description: 'This cop enforces that test method names start with `test_` prefix.'
Enabled: 'pending'
Expand Down
41 changes: 41 additions & 0 deletions lib/rubocop/cop/minitest/test_file_name.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Minitest
# Checks if test file names start with `test_` or end with `_test.rb`.
#
# @example
# # bad
# my_class.rb
#
# # good
# my_class_test.rb
# test_my_class.rb
#
class TestFileName < Base
include MinitestExplorationHelpers

MSG = 'Test file path should start with `test_` or end with `_test.rb`.'

def on_new_investigation
return if processed_source.blank?
return unless test_file?(processed_source.ast)

add_global_offense(MSG) unless valid_file_name?
end

private

def test_file?(node)
test_class?(node) || node.each_child_node.any? { |child_node| test_class?(child_node) }
end

def valid_file_name?
file_name = File.basename(processed_source.buffer.name)
file_name.start_with?('test_') || file_name.end_with?('_test.rb')
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/rubocop/cop/minitest_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
require_relative 'minitest/assert_truthy'
require_relative 'minitest/duplicate_test_run'
require_relative 'minitest/empty_line_before_assertion_methods'
require_relative 'minitest/test_file_name'
require_relative 'minitest/global_expectations'
require_relative 'minitest/literal_as_actual_argument'
require_relative 'minitest/multiple_assertions'
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/mixin/minitest_exploration_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module MinitestExplorationHelpers
private

def test_class?(class_node)
class_node.parent_class && class_node.identifier.source.end_with?('Test')
class_node&.class_type? && class_node&.parent_class && class_node.identifier.source.end_with?('Test')
end

def test_case?(node)
Expand Down
34 changes: 34 additions & 0 deletions test/rubocop/cop/minitest/test_file_name_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

require 'test_helper'

class TestFileNameTest < Minitest::Test
def test_registers_offense_for_invalid_path
offenses = inspect_source(<<~RUBY, @cop, 'foo.rb')
class FooTest < Minitest::Test
end
RUBY
message = 'Test file path should start with `test_` or end with `_test.rb`.'

assert_equal(1, offenses.size)
assert_equal(message, offenses.first.message)
end

def test_does_not_register_offense_for_files_starting_with_test
assert_no_offenses(<<~RUBY, 'test_foo.rb')
class FooTest < Minitest::Test; end
RUBY
end

def test_does_not_register_offense_for_files_ending_with_test
assert_no_offenses(<<~RUBY, 'foo_test.rb')
class FooTest < Minitest::Test; end
RUBY
end

def test_does_not_register_offense_for_non_test_classes
assert_no_offenses(<<~RUBY, 'foo.rb')
class Foo; end
RUBY
end
end

0 comments on commit 21cf39b

Please sign in to comment.