Skip to content

Commit

Permalink
Show watcher help menu when "h" key is pressed (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbrictson committed Feb 27, 2024
1 parent 78824c5 commit 2ba2a19
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@ Watch mode also offers a menu of interactive commands:

```
> Press Enter to run all tests.
> Press a to run all tests, including slow tests.
> Press d to run tests for files diffed or added since the last git commit.
> Press h to show this help menu.
> Press q to quit.
> Press "a" to run all tests, including slow tests.
> Press "d" to run tests for files diffed or added since the last git commit.
> Press "h" to show this help menu.
> Press "q" to quit.
```

## 🔬 Focus Mode
Expand Down
26 changes: 23 additions & 3 deletions lib/mighty_test/watcher.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module MightyTest
class Watcher
WATCHING_FOR_CHANGES = 'Watching for changes to source and test files. Press "q" to quit.'.freeze
class Watcher # rubocop:disable Metrics/ClassLength
WATCHING_FOR_CHANGES = 'Watching for changes to source and test files. Press "h" for help or "q" to quit.'.freeze

def initialize(console: Console.new, extra_args: [], file_system: FileSystem.new, system_proc: method(:system))
@queue = Thread::Queue.new
Expand All @@ -10,7 +10,7 @@ def initialize(console: Console.new, extra_args: [], file_system: FileSystem.new
@system_proc = system_proc
end

def run(iterations: :indefinitely) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength
def run(iterations: :indefinitely) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
start_file_system_listener
start_keypress_listener
puts WATCHING_FOR_CHANGES
Expand All @@ -25,6 +25,8 @@ def run(iterations: :indefinitely) # rubocop:disable Metrics/CyclomaticComplexit
run_all_tests(flags: ["--all"])
in [:keypress, "d"]
run_matching_test_files_from_git_diff
in [:keypress, "h"]
show_help
in [:keypress, "q"]
break
else
Expand All @@ -41,6 +43,24 @@ def run(iterations: :indefinitely) # rubocop:disable Metrics/CyclomaticComplexit

attr_reader :console, :extra_args, :file_system, :file_system_listener, :keypress_listener, :system_proc

def show_help
console.clear
puts <<~MENU
`mt --watch` is watching file system activity and will automatically run
test files when they are added or modified. If you modify a source file,
mt will find and run the corresponding tests.
You can also trigger test runs with the following interactive commands.
> Press Enter to run all tests.
> Press "a" to run all tests, including slow tests.
> Press "d" to run tests for files diffed or added since the last git commit.
> Press "h" to show this help menu.
> Press "q" to quit.
MENU
end

def run_all_tests(flags: [])
console.clear
puts flags.any? ? "Running tests with #{flags.join(' ')}..." : "Running tests..."
Expand Down
18 changes: 15 additions & 3 deletions test/mighty_test/watcher_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def test_watcher_prints_a_status_message_and_plays_a_sound_after_successful_test
[SYSTEM] mt -- test/example_test.rb
[SOUND] :pass
Watching for changes to source and test files. Press "q" to quit.
Watching for changes to source and test files. Press "h" for help or "q" to quit.
EXPECTED
end

Expand All @@ -87,7 +87,7 @@ def test_watcher_prints_a_status_message_and_plays_a_sound_after_failed_test_run
[SYSTEM] mt -- test/example_test.rb
[SOUND] :fail
Watching for changes to source and test files. Press "q" to quit.
Watching for changes to source and test files. Press "h" for help or "q" to quit.
EXPECTED
end

Expand Down Expand Up @@ -172,7 +172,19 @@ def test_watcher_shows_a_message_if_d_key_is_pressed_and_there_are_no_changes
assert_includes(stdout, <<~EXPECTED)
[CLEAR]
No affected test files detected since the last git commit.
Watching for changes to source and test files. Press "q" to quit.
Watching for changes to source and test files. Press "h" for help or "q" to quit.
EXPECTED
end

def test_watcher_shows_help_menu_when_h_key_is_pressed
stdout, = run_watcher(stdin: "hq", in: fixtures_path.join("example_project"))

assert_includes(stdout, <<~EXPECTED)
> Press Enter to run all tests.
> Press "a" to run all tests, including slow tests.
> Press "d" to run tests for files diffed or added since the last git commit.
> Press "h" to show this help menu.
> Press "q" to quit.
EXPECTED
end

Expand Down

0 comments on commit 2ba2a19

Please sign in to comment.