Growlでautospecの結果を表示

Growlの準備

gem類の準備

バージョンも併記した。

まずはgemの準備。

$sudo gem install ZenTest # 4.1.1
$sudo gem install redgreen # 1.2.2, autospecの結果をコンソールで赤と緑で塗り分けてくれる。Growlだけなら不要
$sudo gem install ruby-growl # 1.0.1

RSpecとRSpec on Railsのインストール。

ruby script/plugin install git://github.com/dchelimsky/rspec.git -r 'refs/tags/1.2.7' # 1.2.7
ruby script/plugin install git://github.com/dchelimsky/rspec-rails.git -r 'refs/tags/1.2.7.1' # 1.2.7.1
ruby script/generate rspec

画像を拝借

cd ~
curl http://blog.internautdesign.com/files/rails_fail.png > .rails_fail.png
curl http://blog.internautdesign.com/files/rails_ok.png > .rails_ok.png

moroさんが勉強会で使ってたチェックとバツマークのが欲しいんだけど、どこにあるんだろう?

~/.autotestの編集

ZenTestをインストールしたときについてくる設定ファイルのひな形をコピー。 x.x.xはインストールしたZenTestのバージョンで、ここでは4.1.1。

$ cp /Library/Ruby/Gems/1.8/gems/ZenTest-x.x.x/example_dot_autotest.rb ~/.autotest
$ chmod 644 ~/.autotest

以下のように設定する


# -*- ruby -*-

# require 'autotest/autoupdate'
# require 'autotest/once'
# require 'autotest/rcov'
# require 'autotest/restart'
# require 'autotest/timestamp'

# Autotest::AutoUpdate.sleep_time = o
# Autotest::AutoUpdate.update_cmd = o
# Autotest::RCov.command = o
# Autotest::RCov.pattern = o

ok_img = "~/.rails_ok.png"
ng_img = "~/.rails_fail.png"

module Autotest::Growl
  def self.growl title, msg, img=ok_image, pri=0, sticky=""
    msg += " at #{Time.now.strftime('%Y-%m-%d %H:%M:%S')}"
    # autotestは使わないので、-nで指定するアプリケーション名はautospecで良いと思う
    # -Hで通知先のGrowlのあるホスト名を指定する
    system "growlnotify -n autospec -H localhost --image #{img} -p #{pri} -m #{msg.inspect} #{title} #{sticky}"
  end

  Autotest.add_hook :ran_command do |at|
    results = at.results.last
    examples = results[/(\d+)\s+examples?/].to_i  # テストの総数
    failures = results[/(\d+)\s+failures?/].to_i  # 失敗の数
    errors = results[/(\d+)\s+errors?/].to_i # エラーの数
    if examples >= 0
      if failures > 0 || errors > 0
        growl "Tests Failed", "#{examples} examples, #{failures} failures, and #{errors} errors", ng_img, 2
      else
        growl "Tests Passed", "#{examples} examples, #{failures} failures, and #{errors} errors", ok_img, -2
      end
    else
      growl "Tests Errored", "errors", ng_img, 2
    end
  end
end

実行

$ cd RAILS_ROOT
$ autospec

備考

いくつかサイトを巡って試行錯誤を繰り返したけど、とりあえず以上の作業のみで動作している。結構古い情報もあるみたいだし、autotestとautospecでは~/.autotestでテストの結果を拾う処理に差異があるのでご注意を。

ところでgrowlnotifyでは-Hオプションで通知先のGrowlのホスト名を指定しているけど、うまくやれば,別サーバ上で作業しているときのテスト結果をローカルのGrowlに通知なんてこともできるのかな?ちょっと余裕があるときにでも試してみたい。

参考サイト

変更履歴

2009-08-19

2010-02-13