Scala/SBT/Growlで快適TDD環境「sbtのテスト結果をGrowlで通知する」

会社のRuby好きな同僚に

ZenTestいいわぁ。しかもGrowlで通知とかいいわぁ。

なんて自慢されてくやしいので、Scalaにも当然あるよねと思って調べたらありました☆

softprops/sbt-growl-plugin

作者はpicture-showを作ってるsoftpropsさんでした。

このプラグインは、sbtのtestタスクの結果をgrowlnotifyというGrowlコマンドラインインターフェース経由で通知してくれるプラグインです。

さて、ではやってみましょう。

growl とgrowlnotifyをインストールする

ここからダウンロードしてインストールします。

プロジェクトを作る

まずはScalaのサンプルプロジェクトを作りましょう*1

Scalaのプロジェクトはgiter8があるから簡単です。Javaでいうとmaven archetypeみたいな感じでしょうか。ビルドツールに依存してない点でgiter8のほうが良いですよね。

こんな感じで作れます↓

-(~/Documents/scala-sandbox)-
 $ g8 chrislewis/basic-project
version [0.1.0-SNAPSHOT]: 
organization [com.example]: org.everpeace
name [Basic Project]: sbt_growl_plugin_test

Applied chrislewis/basic-project.g8 in sbt_growl_plugin_test

-(~/Documents/scala-sandbox)-
 $ cd sbt_growl_plugin_test/

-(~/Documents/scala-sandbox/sbt_growl_plugin_test)-
 $ sbt test
...途中省略...
[info] Compiling 1 Scala source to /Users/everpeace/Documents/scala-sandbox/sbt_growl_plugin_test/target/scala-2.9.1/classes...
[info] Compiling 1 Scala source to /Users/everpeace/Documents/scala-sandbox/sbt_growl_plugin_test/target/scala-2.9.1/test-classes...
[info] AppSpec
[info] 
[info] The 'Hello world' string should
[info] + contain 11 characters
[info] + start with 'Hello'
[info] + end with 'world'
[info]  
[info]  
[info] Total for specification AppSpec
[info] Finished in 200 ms
[info] 3 examples, 0 failure, 0 error
[info] 
[info] Passed: : Total 3, Failed 0, Errors 0, Passed 3, Skipped 0
[success] Total time: 6 s, completed 2012/05/03 18:14:06

ちゃんとサンプルテストも通ってますね。では、プロジェクトにプラグインを追加してみましょう。

sbt-growl-pluginを追加する

プラグインをプロジェクトに追加しましょう。softprops/sbt-growl-pluginのREADMEにあるようにすれば良いです。僕はプロジェクト単位に追加したかったので、project/plugin.sbtに

resolvers += "less is" at "http://repo.lessis.me"

addSbtPlugin("me.lessis" % "sbt-growl-plugin" % "0.1.3")

と書きました。これだけです。

Growlでテスト結果を通知させてみる

では、テストしてみましょう。結果がGrowlで通知されるはずです。

-(~/Documents/scala-sandbox/sbt_growl_plugin_test)-
 $ sbt test
Detected sbt version 0.11.2
Using /Users/everpeace/.sbt/0.11.2 as sbt dir, -sbt-dir to override.
[info] Loading project definition from /Users/everpeace/Documents/scala-sandbox/sbt_growl_plugin_test/project
[info] Set current project to sbt_growl_plugin_test (in build file:/Users/everpeace/Documents/scala-sandbox/sbt_growl_plugin_test/)
[info] AppSpec
[info] 
[info] The 'Hello world' string should
[info] + contain 11 characters
[info] + start with 'Hello'
[info] + end with 'world'
[info]  
[info]  
[info] Total for specification AppSpec
[info] Finished in 154 ms
[info] 3 examples, 0 failure, 0 error
[info] 
[info] Passed: : Total 3, Failed 0, Errors 0, Passed 3, Skipped 0
[success] Total time: 1 s, completed 2012/05/03 18:54:05

とテスト結果が表示されると同時にこんな風にGrowlで通知が表示されます。

アイコンは自分でカスタマイズもできるみたいですね。
passed,error,failの3種類設定できるみたいです。デフォルトだとScalaのロゴがすべての場合に利用されるようです。

sbtのTriggered Executionでテストをまわす

あとは、sbtのTriggered Executionでソースの変更を監視して、随時テストさせれば、快適なTDD環境の出来上がりです!

sbtコンソールを立ち上げて「~test」を実行します。

-(everpeace@everpeaces-MacBook-Air)-(~/Documents/scala-sandbox/sbt_growl_plugin_test)-
 $ sbt
Detected sbt version 0.11.2
Starting sbt: invoke with -help for other options
Using /Users/everpeace/.sbt/0.11.2 as sbt dir, -sbt-dir to override.
[info] Loading project definition from /Users/everpeace/Documents/scala-sandbox/sbt_growl_plugin_test/project
[info] Set current project to sbt_growl_plugin_test (in build file:/Users/everpeace/Documents/scala-sandbox/sbt_growl_plugin_test/)
> ~test
[info] AppSpec
[info] 
[info] The 'Hello world' string should
[info] + contain 11 characters
[info] + start with 'Hello'
[info] + end with 'world'
[info]  
[info]  
[info] Total for specification AppSpec
[info] Finished in 148 ms
[info] 3 examples, 0 failure, 0 error
[info] 
[info] Passed: : Total 3, Failed 0, Errors 0, Passed 3, Skipped 0
[success] Total time: 1 s, completed 2012/05/03 19:01:01
1. Waiting for source changes... (press enter to interrupt)

この状態になれば、あとはソースが変更されしだいテストが勝手に回ってGrowlが鳴くという具合です。