From 6d58ce3e548b7073681e04d5ae8f3ce2a3a00273 Mon Sep 17 00:00:00 2001 From: Matt Brictson Date: Wed, 21 Feb 2024 16:41:08 -0800 Subject: [PATCH] Minor refactor and cleanup (#8) * Minor code cleanup and additional test coverage * Rename method for clarity/consistency --- lib/mighty_test/file_system.rb | 2 +- lib/mighty_test/watcher.rb | 6 +- .../rails_project/app/lib/not_a_test.rb | 0 .../rails_project/test/test_helper.rb | 0 test/mighty_test/file_system_test.rb | 74 ++++++++++++++----- 5 files changed, 61 insertions(+), 21 deletions(-) create mode 100644 test/fixtures/rails_project/app/lib/not_a_test.rb create mode 100644 test/fixtures/rails_project/test/test_helper.rb diff --git a/lib/mighty_test/file_system.rb b/lib/mighty_test/file_system.rb index 0c49794..38fda07 100644 --- a/lib/mighty_test/file_system.rb +++ b/lib/mighty_test/file_system.rb @@ -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$}) diff --git a/lib/mighty_test/watcher.rb b/lib/mighty_test/watcher.rb index 43ee41d..adb1dbf 100644 --- a/lib/mighty_test/watcher.rb +++ b/lib/mighty_test/watcher.rb @@ -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 @@ -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 @@ -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) diff --git a/test/fixtures/rails_project/app/lib/not_a_test.rb b/test/fixtures/rails_project/app/lib/not_a_test.rb new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/rails_project/test/test_helper.rb b/test/fixtures/rails_project/test/test_helper.rb new file mode 100644 index 0000000..e69de29 diff --git a/test/mighty_test/file_system_test.rb b/test/mighty_test/file_system_test.rb index 6d7b6da..a2c03bd 100644 --- a/test/mighty_test/file_system_test.rb +++ b/test/mighty_test/file_system_test.rb @@ -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