-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #213
- Loading branch information
1 parent
bb9fd78
commit 9ab5429
Showing
6 changed files
with
84 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/FileName` cop. ([@tejasbubane][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |