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 bd380af commit 978a7ea
Show file tree
Hide file tree
Showing 5 changed files with 84 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][])
6 changes: 6 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ Minitest/EmptyLineBeforeAssertionMethods:
Enabled: pending
VersionAdded: '0.23'

Minitest/FileName:
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/GlobalExpectations:
Description: 'This cop checks for deprecated global expectations.'
StyleGuide: 'https://minitest.rubystyle.guide#global-expectations'
Expand Down
42 changes: 42 additions & 0 deletions lib/rubocop/cop/minitest/file_name.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# 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 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| child_node&.class_type? && 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/file_name'
require_relative 'minitest/global_expectations'
require_relative 'minitest/literal_as_actual_argument'
require_relative 'minitest/multiple_assertions'
Expand Down
34 changes: 34 additions & 0 deletions test/rubocop/cop/minitest/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 FileNameTest < 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(offenses.size, 1)
assert_equal(offenses.first.message, 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 978a7ea

Please sign in to comment.