Skip to content

Commit

Permalink
Minor refactor and cleanup (#8)
Browse files Browse the repository at this point in the history
* Minor code cleanup and additional test coverage

* Rename method for clarity/consistency
  • Loading branch information
mattbrictson authored Feb 22, 2024
1 parent fc76461 commit 6d58ce3
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/mighty_test/file_system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def listen(&)
Listen.to(*%w[app lib test].select { |p| Dir.exist?(p) }, relative: true, &).tap(&:start)
end

def find_matching_test_file(path)
def find_matching_test_path(path)
return nil unless path && File.exist?(path) && !Dir.exist?(path)
return path if path.match?(%r{^test/.*_test.rb$})

Expand Down
6 changes: 4 additions & 2 deletions lib/mighty_test/watcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def run(iterations: :indefinitely) # rubocop:disable Metrics/MethodLength
case await_next_event
in [:file_system_changed, [_, *] => paths]
console.clear
puts [*paths.join("\n"), ""]
puts paths.join("\n")
puts
mt(*paths)
in [:keypress, "\r" | "\n"]
console.clear
Expand Down Expand Up @@ -50,6 +51,7 @@ def mt(*test_paths)

console.play_sound(success ? :pass : :fail)
puts "\n#{WATCHING_FOR_CHANGES}"
$stdout.flush
rescue Interrupt
# Pressing ctrl-c kills the fs_event background process, so we have to manually restart it.
restart_file_system_listener
Expand All @@ -63,7 +65,7 @@ def start_file_system_listener
listener.pause unless listener.stopped?

test_paths = [*modified, *added].filter_map do |path|
file_system.find_matching_test_file(path)
file_system.find_matching_test_path(path)
end

post_event(:file_system_changed, test_paths.uniq)
Expand Down
Empty file.
Empty file.
74 changes: 56 additions & 18 deletions test/mighty_test/file_system_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,84 @@ module MightyTest
class FileSystemTest < Minitest::Test
include FixturesPath

def test_find_matching_test_file_returns_nil_for_nil_path
assert_nil find_matching_test_file(nil)
def test_find_matching_test_path_returns_nil_for_nil_path
assert_nil find_matching_test_path(nil)
end

def test_find_matching_test_file_returns_nil_for_non_existent_path
assert_nil find_matching_test_file("path/to/nowhere.rb")
def test_find_matching_test_path_returns_nil_for_non_existent_path
assert_nil find_matching_test_path("path/to/nowhere.rb")
end

def test_find_matching_test_file_returns_nil_for_directory_path
assert_nil find_matching_test_file("lib/example", in: fixtures_path.join("example_project"))
def test_find_matching_test_path_returns_nil_for_directory_path
assert_nil find_matching_test_path("lib/example", in: fixtures_path.join("example_project"))
end

def test_find_matching_test_file_returns_nil_for_path_with_no_corresponding_test
assert_nil find_matching_test_file("lib/example/version.rb", in: fixtures_path.join("example_project"))
def test_find_matching_test_path_returns_nil_for_path_with_no_corresponding_test
assert_nil find_matching_test_path("lib/example/version.rb", in: fixtures_path.join("example_project"))
end

def test_find_matching_test_file_returns_nil_for_a_test_support_file
assert_nil find_matching_test_file("test/test_helper.rb", in: fixtures_path.join("example_project"))
def test_find_matching_test_path_returns_nil_for_a_test_support_file
assert_nil find_matching_test_path("test/test_helper.rb", in: fixtures_path.join("example_project"))
end

def test_find_matching_test_file_returns_argument_if_it_is_already_a_test
test_path = find_matching_test_file("test/example_test.rb", in: fixtures_path.join("example_project"))
def test_find_matching_test_path_returns_argument_if_it_is_already_a_test
test_path = find_matching_test_path("test/example_test.rb", in: fixtures_path.join("example_project"))
assert_equal("test/example_test.rb", test_path)
end

def test_find_matching_test_file_returns_matching_test_given_an_implementation_path_in_a_gem_project
test_path = find_matching_test_file("lib/example.rb", in: fixtures_path.join("example_project"))
def test_find_matching_test_path_returns_matching_test_given_an_implementation_path_in_a_gem_project
test_path = find_matching_test_path("lib/example.rb", in: fixtures_path.join("example_project"))
assert_equal("test/example_test.rb", test_path)
end

def test_find_matching_test_file_returns_matching_test_given_a_model_path_in_a_rails_project
test_path = find_matching_test_file("app/models/user.rb", in: fixtures_path.join("rails_project"))
def test_find_matching_test_path_returns_matching_test_given_a_model_path_in_a_rails_project
test_path = find_matching_test_path("app/models/user.rb", in: fixtures_path.join("rails_project"))
assert_equal("test/models/user_test.rb", test_path)
end

def test_find_test_paths_looks_in_test_directory_by_default
test_paths = find_test_paths(in: fixtures_path.join("rails_project"))

assert_equal(
%w[
test/helpers/users_helper_test.rb
test/models/account_test.rb
test/models/user_test.rb
test/system/users_system_test.rb
],
test_paths.sort
)
end

def test_find_test_paths_returns_empty_array_if_given_non_existent_path
test_paths = find_test_paths("path/to/nowhere", in: fixtures_path.join("rails_project"))

assert_empty(test_paths)
end

def test_find_test_paths_returns_test_files_in_specific_directory
test_paths = find_test_paths("test/models", in: fixtures_path.join("rails_project"))

assert_equal(
%w[
test/models/account_test.rb
test/models/user_test.rb
],
test_paths.sort
)
end

private

def find_matching_test_file(path, in: ".")
def find_matching_test_path(path, in: ".")
Dir.chdir(binding.local_variable_get(:in)) do
FileSystem.new.find_matching_test_path(path)
end
end

def find_test_paths(*path, in: ".")
Dir.chdir(binding.local_variable_get(:in)) do
FileSystem.new.find_matching_test_file(path)
FileSystem.new.find_test_paths(*path)
end
end
end
Expand Down

0 comments on commit 6d58ce3

Please sign in to comment.