-
-
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.
Merge pull request #83 from fatkodima/path-exists-cops
Add new `Minitest/AssertPathExists` and `Minitest/RefutePathExists` cops
- Loading branch information
Showing
9 changed files
with
321 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
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
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,67 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Minitest | ||
# This cop enforces the test to use `assert_path_exists` | ||
# instead of using `assert(File.exist?(path))`. | ||
# | ||
# @example | ||
# # bad | ||
# assert(File.exist?(path)) | ||
# assert(File.exist?(path), 'message') | ||
# | ||
# # good | ||
# assert_path_exists(path) | ||
# assert_path_exists(path, 'message') | ||
# | ||
class AssertPathExists < Cop | ||
MSG = 'Prefer using `%<good_method>s` over `%<bad_method>s`.' | ||
|
||
def_node_matcher :assert_file_exists, <<~PATTERN | ||
(send nil? :assert | ||
(send | ||
(const _ :File) ${:exist? :exists?} $_) | ||
$...) | ||
PATTERN | ||
|
||
def on_send(node) | ||
assert_file_exists(node) do |method_name, path, message| | ||
message = message.first | ||
|
||
add_offense(node, | ||
message: format(MSG, good_method: build_good_method(path, message), | ||
bad_method: build_bad_method(method_name, path, message))) | ||
end | ||
end | ||
|
||
def autocorrect(node) | ||
assert_file_exists(node) do |_method_name, path, message| | ||
message = message.first | ||
|
||
lambda do |corrector| | ||
replacement = build_good_method(path, message) | ||
corrector.replace(node, replacement) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def build_good_method(path, message) | ||
args = [path.source, message&.source].compact.join(', ') | ||
"assert_path_exists(#{args})" | ||
end | ||
|
||
def build_bad_method(method_name, path, message) | ||
path_arg = "File.#{method_name}(#{path.source})" | ||
if message | ||
"assert(#{path_arg}, #{message.source})" | ||
else | ||
"assert(#{path_arg})" | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Minitest | ||
# This cop enforces the test to use `refute_path_exists` | ||
# instead of using `refute(File.exist?(path))`. | ||
# | ||
# @example | ||
# # bad | ||
# refute(File.exist?(path)) | ||
# refute(File.exist?(path), 'message') | ||
# | ||
# # good | ||
# refute_path_exists(path) | ||
# refute_path_exists(path, 'message') | ||
# | ||
class RefutePathExists < Cop | ||
MSG = 'Prefer using `%<good_method>s` over `%<bad_method>s`.' | ||
|
||
def_node_matcher :refute_file_exists, <<~PATTERN | ||
(send nil? :refute | ||
(send | ||
(const _ :File) ${:exist? :exists?} $_) | ||
$...) | ||
PATTERN | ||
|
||
def on_send(node) | ||
refute_file_exists(node) do |method_name, path, message| | ||
message = message.first | ||
|
||
add_offense(node, | ||
message: format(MSG, good_method: build_good_method(path, message), | ||
bad_method: build_bad_method(method_name, path, message))) | ||
end | ||
end | ||
|
||
def autocorrect(node) | ||
refute_file_exists(node) do |_method_name, path, message| | ||
message = message.first | ||
|
||
lambda do |corrector| | ||
replacement = build_good_method(path, message) | ||
corrector.replace(node, replacement) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def build_good_method(path, message) | ||
args = [path.source, message&.source].compact.join(', ') | ||
"refute_path_exists(#{args})" | ||
end | ||
|
||
def build_bad_method(method_name, path, message) | ||
path_arg = "File.#{method_name}(#{path.source})" | ||
if message | ||
"refute(#{path_arg}, #{message.source})" | ||
else | ||
"refute(#{path_arg})" | ||
end | ||
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,53 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class AssertPathExistsTest < Minitest::Test | ||
def test_registers_offense_when_using_assert_file_exist | ||
assert_offense(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert(File.exist?(path)) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_path_exists(path)` over `assert(File.exist?(path))`. | ||
end | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert_path_exists(path) | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_registers_offense_when_using_assert_file_exist_and_message | ||
assert_offense(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert(File.exist?(path), 'message') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_path_exists(path, 'message')` over `assert(File.exist?(path), 'message')`. | ||
end | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert_path_exists(path, 'message') | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_does_not_register_offense_when_using_assert_path_exists_method | ||
assert_no_offenses(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert_path_exists(path) | ||
end | ||
end | ||
RUBY | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class RefutePathExistsTest < Minitest::Test | ||
def test_registers_offense_when_using_refute_file_exist | ||
assert_offense(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
refute(File.exist?(path)) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_path_exists(path)` over `refute(File.exist?(path))`. | ||
end | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
refute_path_exists(path) | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_registers_offense_when_using_refute_file_exist_and_message | ||
assert_offense(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
refute(File.exist?(path), 'message') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_path_exists(path, 'message')` over `refute(File.exist?(path), 'message')`. | ||
end | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
refute_path_exists(path, 'message') | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_does_not_register_offense_when_using_refute_path_exists_method | ||
assert_no_offenses(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
refute_path_exists(path) | ||
end | ||
end | ||
RUBY | ||
end | ||
end |