Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexB52 committed Jan 9, 2025
1 parent a0ae9d1 commit 410b8bb
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion lib/retest/watcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,46 @@ def self.watch(dir:, extensions:, polling: false)
# We need the process to still run when trying to stop the current test run
# Maybe there is another way to prevent killing these but for now a new process groups works
# Process group created with: Process.setsid
watch_rd, watch_wr = IO.pipe
pid = fork do
Process.setsid
Listen.to(dir, only: extensions_regex(extensions), relative: true, polling: polling) do |modified, added, removed|
yield modified, added, removed
if modified.any?
watch_wr.write "modify:#{modified.first}"
elsif added.any?
watch_wr.write "create:#{added.first}"
elsif removed.any?
watch_wr.write "remove:#{removed.first}"
end
end.start
sleep
end

at_exit do
Process.kill("TERM", pid) if pid
end

Thread.new do
loop do
ready = IO.select([watch_rd])
readable_connections = ready[0]
readable_connections.each do |conn|
data = conn.readpartial(4096)
change = /^(?<action>create|remove|modify):(?<path>.*)/.match(data.strip)

next unless change

modified, added, removed = result = [[], [], []]
case change[:action]
when 'modify' then modified << change[:path]
when 'create' then added << change[:path]
when 'remove' then removed << change[:path]
end

yield result
end
end
end
end

def self.extensions_regex(extensions)
Expand Down

0 comments on commit 410b8bb

Please sign in to comment.