Skip to content

Commit

Permalink
Manage stdin based on the process running (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexB52 authored Oct 20, 2024
1 parent 9b86f2b commit 3f1557e
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ gemspec

gem "rake", "~> 13.0"
gem "minitest", "~> 5.0"
gem "byebug"
gem "byebug", require: false
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
retest (2.0.0.pre)
retest (2.0.0.pre1)
listen (~> 3.9)
observer (~> 0.1)
string-similarity (~> 2.1)
Expand All @@ -11,7 +11,7 @@ GEM
remote: https://rubygems.org/
specs:
byebug (11.1.3)
ffi (1.17.0)
ffi (1.16.3)
listen (3.9.0)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
Expand Down
41 changes: 32 additions & 9 deletions exe/retest
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ require 'retest'

$stdout.sync = true
prompt_rd, prompt_wr = IO.pipe
runner_rd, runner_wr = IO.pipe

Signal.trap(:INT) do
$stdout.puts "Goodbye"
Expand All @@ -27,9 +28,14 @@ end
prompt = Retest::Prompt.new(input: prompt_rd)
repository = Retest::Repository.new(files: Retest::VersionControl.files, prompt: prompt)
command = Retest::Command.for_options(options)
runner = Retest::Runners.runner_for(command.to_s)
runner = Retest::Runners.runner_for(command.to_s, command_stdin: runner_rd)
sounds = Retest::Sounds.for(options)

# All test runner
all_test_command = Retest::Command.for_options(options.merge(%w[--all]), stdout: nil)
all_test_runner = Retest::Runners.runner_for(all_test_command.to_s)
all_test_runner.add_observer(sounds)

sounds.play(:start)
runner.add_observer(sounds)
prompt.add_observer(sounds)
Expand All @@ -41,6 +47,8 @@ program = Retest::Program.new(
)

if options.params[:diff]
prompt.input = $stdin
runner.command_stdin = $stdin
program.diff(options.params[:diff])
return
end
Expand Down Expand Up @@ -74,12 +82,26 @@ end
loop do
print_interactive_message

puts "waiting for input"
input = $stdin.gets.to_s.chomp

puts "input is: #{input.inspect}"

if prompt.question_asked?
prompt_wr.puts input
next
end

if all_test_runner.running?
$stdin.puts input
next
end

if runner.running?
runner_wr.puts input
next
end

case input
when 'p', 'pause'
program.pause
Expand All @@ -90,16 +112,17 @@ loop do
when 'e', 'exit'
Process.kill("INT", 0)
when ''
puts "Running last command\n"
runner.run(nil, repository: repository)
begin
puts "Running last command\n"
old_stdin = runner.command_stdin
runner.command_stdin = $stdin
runner.run(nil, repository: repository)
ensure
runner.command_stdin = old_stdin
end
when 'ra', 'run all'
puts "Running all tests\n"
tmp_opts = options.dup.tap { |opts| opts.params[:all] = true }
tmp_cmd = Retest::Command.for_options(tmp_opts)
tmp_runner = Retest::Runners.runner_for(tmp_cmd.to_s)
tmp_runner.add_observer(sounds)
tmp_runner.run
tmp_runner.delete_observers
all_test_runner.run
when /^di?f?f?\s(.*)$/
program.diff($1)
when 'h', 'help'
Expand Down
14 changes: 7 additions & 7 deletions lib/retest/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ module Retest
class Command
extend Forwardable

def self.for_options(options)
new(options: options).command
end

def self.for_setup(setup)
new(setup: setup).command
def self.for_options(options, stdout: $stdout)
new(options: options, stdout: stdout).command
end

def_delegator :setup, :type
Expand Down Expand Up @@ -51,12 +47,16 @@ def setup_command
end

def default_command
@stdout.puts "Setup identified: [#{type.upcase}]. Using command: '#{setup_command}'"
log "Setup identified: [#{type.upcase}]. Using command: '#{setup_command}'"
setup_command
end

private

def log(message)
@stdout&.puts(message)
end

def rspec_command
Rspec.new(all: full_suite?)
end
Expand Down
4 changes: 4 additions & 0 deletions lib/retest/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,9 @@ def force_polling?
def extension
Regexp.new(params[:ext])
end

def merge(options = [])
self.class.new(@args.dup.concat(options))
end
end
end
4 changes: 2 additions & 2 deletions lib/retest/runners.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ class NotSupportedError < StandardError; end

module_function

def runner_for(command)
def runner_for(command, **opts)
for_test = command.include?('<test>')
for_change = command.include?('<changed>')

if for_test && for_change then VariableRunner
elsif for_test then TestRunner
elsif for_change then ChangeRunner
else Runner
end.new command
end.new command, **opts
end
end
end
13 changes: 10 additions & 3 deletions lib/retest/runners/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ module Runners
class Runner
include Observable

attr_accessor :command, :stdout
def initialize(command, stdout: $stdout)
attr_accessor :command, :stdout, :command_stdin
def initialize(command, stdout: $stdout, command_stdin: $stdin)
@stdout = stdout
@command = command
@command_stdin = command_stdin
end

def ==(obj)
Expand All @@ -24,12 +25,18 @@ def run_all_tests(tests_string)
def sync(added:, removed:)
end

def running?
@running
end

private

def system_run(command)
result = system(command) ? :tests_pass : :tests_fail
@running = true
result = system(command, in: @command_stdin) ? :tests_pass : :tests_fail
changed
notify_observers(result)
@running = false
end

def log(message)
Expand Down
2 changes: 1 addition & 1 deletion lib/retest/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Retest
VERSION = "2.0.0.pre"
VERSION = "2.0.0.pre1"
end
9 changes: 9 additions & 0 deletions test/retest/options_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,14 @@ def test_notify?
@subject.args = ["--notify"]
assert @subject.notify?
end

def test_all_version_copy
@subject.args = %w[--notify --rake]

copy = @subject.merge(%w[--all])

assert_equal %w[--notify --rake --all], copy.args
refute_equal copy.object_id, @subject.object_id
end
end
end
2 changes: 1 addition & 1 deletion test/retest/repository_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def test_find_test_similar_files_but_no_exact_match
Which file do you want to use?
Enter the file number now:
>
>\s
EXPECTED
end

Expand Down

0 comments on commit 3f1557e

Please sign in to comment.