Rails + watchr + notify-send

Rails と言えばテスト、でも面倒ですね。自動テストを導入しましょう。
どうもググると、autotest が普通らしいですが、初めに導入した自動テストツールが watchr だったので、不都合が起こるまでこれで行きます。

必要なもの

- Gemfile
watchr は当然必要です。

- notify-send
Ubuntu のパッケージで libnotify-bin インストールします。notify-send でgnome
デスクトップ通知にメッセージを送ります。詳細は知らん。

- アイコン
テストが成功した時、失敗した時に文字だけでは寂しいので、それらしいアイコンが
必要です。この辺から拾ってきた。

設定

こんな感じで .watchr ファイルをルートに用意する。
この辺を参考にしてみました。

def notify(title, message, image)
  system "notify-send '#{title}' '#{message}' -i '#{image}' -t 2000"
end

def run_withnotify(*files)
  image_root = File.expand_path("~/.autotest_images")
  puts "Running: #{files.join(' ')}"
  results = `bundle exec rspec -f p -c #{files.join(' ')}`
  output = results.slice(/(\d+)\sexamples?,\s(\d+)\sfailures?/)
  failure = (results.split /\n/)[0].index 'F'
  if failure
    notify "FAIL", "#{output}", "#{image_root}/fail.png"
  else
    notify "Pass", "#{output}", "#{image_root}/pass.png"
  end
end

watch("spec/.*/*_spec\.rb") do |match|
  run_withnotify match[0]
end

watch("app/(.*/.*)\.rb") do |match|
  run_withnotify %{spec/#{match[1]}_spec.rb}
end

watch("app/(.*/.*\.erb)") do |match|
  run_withnotify %{spec/#{match[1]}_spec.rb}
end

watch("config/routes.rb") do |match|
  run_withnotify *Dir["spec/routing/*_spec.rb"]
end

使い方

特に気にせず

 bundle exec watchr .watchr 

とかすれば、ファイルの変更を検知してテストを実行して結果をポップアップしてくれます。

できてないこと

pending については無視しています。

こんな雰囲気