Module: Retest::Watcher::Watchexec

Defined in:
lib/retest/watcher.rb

Class Method Summary collapse

Class Method Details

.installed?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/retest/watcher.rb', line 76

def self.installed?
  system "watchexec --version > /dev/null 2>&1"
end

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/retest/watcher.rb', line 80

def self.watch(dir:, extensions:, polling: false)
  command = "watchexec --exts #{extensions.join(',')} -w #{dir} --emit-events-to stdio --no-meta --only-emit-events"

  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
    files = VersionControl.files(extensions: extensions).zip([]).to_h

    loop do
      ready = IO.select([watch_rd])
      readable_connections = ready[0]
      readable_connections.each do |conn|
        data = conn.readpartial(4096)
        # Watchexec is not great at figuring out whether a file has been deleted and comes as an update.
        # This is why we're not looking at the action like we do with Listen.
        change = /^(?:create|remove|rename|modify):(?<path>.*)/.match(data.strip)

        next unless change

        path = Pathname(change[:path]).relative_path_from(Dir.pwd).to_s
        file_exist = File.exist?(path)
        file_cached = files.key?(path)

        modified, added, removed = result = [[], [], []]
        if file_exist && file_cached
          modified << path
        elsif file_exist && !file_cached
          added << path
          files[path] = nil
        elsif !file_exist && file_cached
          removed << path
          files.delete(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