Menu

Published by
Categories: Ruby, RSpec, Linux

Andreas Wolff recently released RSpactor, a (up to now) command line tool similar to autotest. Nevertheless it differs from autotest in two points. First it’s focused on RSpec and secondly it’s using Mac OS’ FSEvents to monitor file changes. According to this it only runs on Mac OS. To get it running on Linux you’ll have to change RSpactor’s Listener class to use Linux’ equivalent to FSEvents called inotify. Luckily there’s a gem called RInotify which introduces a simple class to access the inotify events within ruby. I rewrote the Listeners class yesterday to get it running on my Linux notebook:

# inotify_listener.rb
class Listener
  def initialize(&block)
    require 'rinotify'
    begin
      @spec_run_time = Time.now
      @watching      = {}

      notify = RInotify.new
      Dir.glob(File.join(Dir.pwd, '**')).each do |dir|
        watch_desc = notify.add_watch(dir, RInotify::MODIFY | RInotify::CREATE | RInotify::DELETE)
        @watching[watch_desc] = dir
      end

      while true do
        changed_files = []
        notify.each_event do |event|
          changed_files << build_path_from_event(event)
        end
        changed_files.uniq!
        unless changed_files.empty?
          @spec_run_time = Time.now
          yield changed_files
        end
        sleep(5)
      end
    rescue Interrupt
      @watching.each_key { |key| notify.rm_watch(key) }
    end
  end

  def build_path_from_event(event)
    File.join(@watching[event.watch_descriptor], event.name || '')
  end
end

To get it running you simply have to install the RInotify gem and change one line in bin/rspactor:

# from
require File.join(File.dirname(__FILE__), '..', 'lib', 'listener')
# to
require File.join(File.dirname(__FILE__), '..', 'lib', 'inotify_listener')

That’s it! RSpactor should be running on Linux now and consuming much less CPU than autotest.

(You might also want to change the system-call in lib/resulting.rb as it’s currently using Growl to notify you about the test results.)