From 1993177be0006c2ba943a2ce996b9fccb48b7a39 Mon Sep 17 00:00:00 2001 From: Alexandre Barret Date: Mon, 2 Dec 2024 09:44:37 +1300 Subject: [PATCH] Default to watchexec when installed --- exe/retest | 9 +++++++-- lib/retest/options.rb | 2 +- lib/retest/watcher.rb | 9 +++++++-- test/retest/options_test.rb | 4 ++-- test/retest/watcher_test.rb | 3 ++- 5 files changed, 19 insertions(+), 8 deletions(-) diff --git a/exe/retest b/exe/retest index 844bfc8..e956d1b 100755 --- a/exe/retest +++ b/exe/retest @@ -12,7 +12,6 @@ Signal.trap(:INT) do end options = Retest::Options.new(ARGV) -puts "Watcher: [#{options.watcher.upcase}]" if options.help? $stdout.puts options.help @@ -28,8 +27,14 @@ prompt = Retest::Prompt.new repository = Retest::Repository.new(files: Retest::VersionControl.files, prompt: prompt) command = Retest::Command.for_options(options) runner = Retest::Runner.new(command) -watcher = Retest::Watcher.for(options.watcher) sounds = Retest::Sounds.for(options) +watcher = Retest::Watcher.for(options.watcher).tap do |tool| + if tool == Retest::Watcher::Watchexec + puts "Watcher: [WATCHEXEC]" + else Retest::Watcher::Default + puts "Watcher: [LISTEN]" + end +end sounds.play(:start) runner.add_observer(sounds) diff --git a/lib/retest/options.rb b/lib/retest/options.rb index a258a69..553e91c 100644 --- a/lib/retest/options.rb +++ b/lib/retest/options.rb @@ -169,7 +169,7 @@ def extensions end def watcher - params[:watcher] || :listen + params[:watcher] || :installed end def merge(options = []) diff --git a/lib/retest/watcher.rb b/lib/retest/watcher.rb index ec5070d..924772d 100644 --- a/lib/retest/watcher.rb +++ b/lib/retest/watcher.rb @@ -2,8 +2,9 @@ module Retest module Watcher def self.for(watcher) tool = case watcher.to_s - when 'listen' then Default - when 'watchexec' then Watchexec + when 'listen' then Default + when 'watchexec' then Watchexec + when '', 'installed' then installed else raise ArgumentError, "Unknown #{watcher}" end @@ -14,6 +15,10 @@ def self.for(watcher) tool end + def self.installed + [Watchexec, Default].find(&:installed?) + end + module Default def self.installed? true diff --git a/test/retest/options_test.rb b/test/retest/options_test.rb index 0956a4a..76181bb 100644 --- a/test/retest/options_test.rb +++ b/test/retest/options_test.rb @@ -82,10 +82,10 @@ def test_listener assert_equal :listen, @subject.watcher @subject.args = %w[-w hello] - assert_equal :listen, @subject.watcher + assert_equal :installed, @subject.watcher @subject.args = %w[] # default when no listeners are install by default - assert_equal :listen, @subject.watcher + assert_equal :installed, @subject.watcher end end end \ No newline at end of file diff --git a/test/retest/watcher_test.rb b/test/retest/watcher_test.rb index 57303cd..1895224 100644 --- a/test/retest/watcher_test.rb +++ b/test/retest/watcher_test.rb @@ -5,8 +5,9 @@ class TestWatcher < Minitest::Test def test_for assert_equal Watcher::Default, Watcher.for('listen') assert_equal Watcher::Watchexec, Watcher.for('watchexec') + assert_equal Watcher::Watchexec, Watcher.for(nil) - assert_raises(ArgumentError) { Watcher.for(nil) } + assert_raises(ArgumentError) { Watcher.for('fswatch') } end end