Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new cop Minitest/TestFileName #217

Merged
merged 1 commit into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -265,6 +265,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
45 changes: 45 additions & 0 deletions lib/rubocop/cop/minitest/test_file_name.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Minitest
# Checks if test file names start with `test_` or end with `_test.rb`.
tejasbubane marked this conversation as resolved.
Show resolved Hide resolved
# Files which define classes having names ending with `Test` are checked.
# Not following this convention may result in tests not being run.
#
# @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 unless test_file?(processed_source.ast)

add_global_offense(MSG) unless valid_file_name?
end

private

def test_file?(node)
return true if node.class_type? && test_class?(node)

node.each_descendant(:class).any? { |class_node| test_class?(class_node) }
end

def valid_file_name?
basename = File.basename(processed_source.file_path)

basename.start_with?('test_') || basename.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 @@ -28,6 +28,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
47 changes: 47 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,47 @@
# frozen_string_literal: true

require 'test_helper'

class TestFileNameTest < Minitest::Test
def test_registers_offense_for_invalid_path
offenses = inspect_source(<<~RUBY, @cop, 'lib/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
tejasbubane marked this conversation as resolved.
Show resolved Hide resolved

def test_registers_offense_for_namespaced_invalid_path
offenses = inspect_source(<<~RUBY, @cop, 'lib/foo/bar.rb')
module Foo
class BarTest < Minitest::Test
end
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, 'lib/test_foo.rb')
class FooTest < Minitest::Test; end
RUBY
end
tejasbubane marked this conversation as resolved.
Show resolved Hide resolved

def test_does_not_register_offense_for_files_ending_with_test
assert_no_offenses(<<~RUBY, 'lib/foo_test.rb')
class FooTest < Minitest::Test; end
RUBY
end
tejasbubane marked this conversation as resolved.
Show resolved Hide resolved

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