Module: Retest::Watcher::Default

Defined in:
lib/retest/watcher.rb

Class Method Summary collapse

Class Method Details

.extensions_regex(extensions) ⇒ Object



29
30
31
# File 'lib/retest/watcher.rb', line 29

def self.extensions_regex(extensions)
  Regexp.new("\\.(?:#{extensions.join("|")})$")
end

.installed?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/retest/watcher.rb', line 25

def self.installed?
  true
end

.watch(dir:, extensions:, polling: false) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/retest/watcher.rb', line 33

def self.watch(dir:, extensions:, polling: false)
  executable = File.expand_path("../../scripts/listen", __FILE__)
  command = "#{executable} --exts #{extensions.join(',')} -w #{dir} --polling #{polling}"

  watch_rd, watch_wr = IO.pipe
  # Process needs its own process group otherwise the process gets killed on INT signal
  # 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: pgroup: true
  pid = Process.spawn(command, out: watch_wr, pgroup: true)

  thread = 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

  at_exit do
    thread.exit
    Process.kill("TERM", pid) if pid
    watch_rd.close
    watch_wr.close
  end
end