Skip to content

Commit

Permalink
[Fix #213] Add new cop Minitest/FileName
Browse files Browse the repository at this point in the history
Closes #213
  • Loading branch information
tejasbubane committed Dec 30, 2022
1 parent 1ed0cfa commit 33ec01f
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/new_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/FileName` cop. ([@tejasbubane][])
5 changes: 5 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ Minitest/EmptyLineBeforeAssertionMethods:
Enabled: pending
VersionAdded: '0.23'

Minitest/FileName:
Description: 'Checks if test file names start with `test_` or end with `_test.rb`.'
Enabled: pending
VersionAdded: '<<next>>'

Minitest/GlobalExpectations:
Description: 'This cop checks for deprecated global expectations.'
StyleGuide: 'https://minitest.rubystyle.guide#global-expectations'
Expand Down
39 changes: 39 additions & 0 deletions lib/rubocop/cop/minitest/file_name.rb
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
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/file_name'
require_relative 'minitest/global_expectations'
require_relative 'minitest/literal_as_actual_argument'
require_relative 'minitest/multiple_assertions'
Expand Down
31 changes: 31 additions & 0 deletions test/rubocop/cop/minitest/file_name_test.rb
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

0 comments on commit 33ec01f

Please sign in to comment.