generated from mattbrictson/gem
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve console feedback and add sound effects to --watch mode (#5)
* Improve console feedback and add sound effects to --watch mode This commit makes the following improvements to `mt --watch`: - The console is cleared just before running tests. This makes it easier to notice when a change has been detected and tests are starting. It also makes the output from the latest test run easy to distinguish, since it is no longer mixed in with previous runs. - On macOS, two different sounds are played at the conclusion of the test run, depending on whether the tests passed or failed. This makes it easy to tell the result of a test while doing TDD without having to even glance at the console. The clear screen and sound playback functions have been extracted into a `Console` class. They are only enabled when running `mt` with a TTY. Sound playback is only enabled on macOS. * Mock .tty? to ensure proper test setup
- Loading branch information
1 parent
369b200
commit 5d80649
Showing
5 changed files
with
154 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require "io/console" | ||
|
||
module MightyTest | ||
class Console | ||
def initialize(sound_player: "/usr/bin/afplay", sound_paths: SOUNDS) | ||
@sound_player = sound_player | ||
@sound_paths = sound_paths | ||
end | ||
|
||
def clear | ||
return false unless tty? | ||
|
||
$stdout.clear_screen | ||
true | ||
end | ||
|
||
def play_sound(name, wait: false) | ||
return false unless tty? | ||
|
||
paths = sound_paths.fetch(name) { raise ArgumentError, "Unknown sound name #{name}" } | ||
path = paths.find { |p| File.exist?(p) } | ||
return false unless path && File.executable?(sound_player) | ||
|
||
thread = Thread.new { system(sound_player, path) } | ||
thread.join if wait | ||
true | ||
end | ||
|
||
private | ||
|
||
# rubocop:disable Layout/LineLength | ||
SOUNDS = { | ||
pass: %w[ | ||
/System/Library/PrivateFrameworks/ToneLibrary.framework/Versions/A/Resources/AlertTones/EncoreInfinitum/Milestone-EncoreInfinitum.caf | ||
/System/Library/Sounds/Glass.aiff | ||
], | ||
fail: %w[ | ||
/System/Library/PrivateFrameworks/ToneLibrary.framework/Versions/A/Resources/AlertTones/EncoreInfinitum/Rebound-EncoreInfinitum.caf | ||
/System/Library/Sounds/Bottle.aiff | ||
] | ||
}.freeze | ||
private_constant :SOUNDS | ||
# rubocop:enable Layout/LineLength | ||
|
||
attr_reader :sound_player, :sound_paths | ||
|
||
def tty? | ||
$stdout.respond_to?(:tty?) && $stdout.tty? | ||
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,67 @@ | ||
require "test_helper" | ||
|
||
module MightyTest | ||
class ConsoleTest < Minitest::Test | ||
def test_clear_returns_false_if_not_tty | ||
result = nil | ||
capture_io { result = Console.new.clear } | ||
refute result | ||
end | ||
|
||
def test_clear_clears_the_screen_and_returns_true_and_if_tty | ||
result = nil | ||
stdout, = capture_io do | ||
$stdout.define_singleton_method(:tty?) { true } | ||
$stdout.define_singleton_method(:clear_screen) { print "clear!" } | ||
result = Console.new.clear | ||
end | ||
|
||
assert result | ||
assert_equal "clear!", stdout | ||
end | ||
|
||
def test_play_sound_returns_false_if_not_tty | ||
result = nil | ||
capture_io { result = Console.new.play_sound(:pass) } | ||
refute result | ||
end | ||
|
||
def test_play_sound_returns_false_if_player_is_not_executable | ||
result = nil | ||
capture_io do | ||
$stdout.define_singleton_method(:tty?) { true } | ||
console = Console.new(sound_player: "/path/to/nothing") | ||
result = console.play_sound(:pass) | ||
end | ||
refute result | ||
end | ||
|
||
def test_play_sound_returns_false_if_sound_files_are_missing | ||
result = nil | ||
capture_io do | ||
$stdout.define_singleton_method(:tty?) { true } | ||
console = Console.new(sound_player: "/bin/echo", sound_paths: { pass: ["/path/to/nothing"] }) | ||
result = console.play_sound(:pass) | ||
end | ||
refute result | ||
end | ||
|
||
def test_play_sound_raises_argument_error_if_invalid_sound_name_is_specified | ||
capture_io do | ||
$stdout.define_singleton_method(:tty?) { true } | ||
assert_raises(ArgumentError) { Console.new.play_sound(:whatever) } | ||
end | ||
end | ||
|
||
def test_play_sound_calls_sound_player_with_matching_sound_path | ||
result = nil | ||
stdout, = capture_subprocess_io do | ||
$stdout.define_singleton_method(:tty?) { true } | ||
console = Console.new(sound_player: "/bin/echo", sound_paths: { pass: [__FILE__] }) | ||
result = console.play_sound(:pass, wait: true) | ||
end | ||
assert result | ||
assert_equal __FILE__, stdout.chomp | ||
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