-
-
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
1ed0cfa
commit 33ec01f
Showing
5 changed files
with
77 additions
and
0 deletions.
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,39 @@ | ||
# 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 FileName < Base | ||
include MinitestExplorationHelpers | ||
|
||
MSG = 'Test file path should end with `_test.rb`.' | ||
|
||
def on_class(node) | ||
return unless test_class?(node) | ||
|
||
add_offense(node) unless valid_file_name? | ||
end | ||
|
||
private | ||
|
||
def valid_file_name? | ||
file_name.match?('^test_.*') || file_name.match?('.*_test\.rb$') | ||
end | ||
|
||
def file_name | ||
@file_name ||= File.basename(processed_source.buffer.name) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class FileNameTest < Minitest::Test | ||
def test_registers_offense_for_invalid_path | ||
assert_offense(<<~RUBY, 'foo.rb') | ||
class FooTest < Minitest::Test | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Test file path should end with `_test.rb`. | ||
end | ||
RUBY | ||
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 |