あかんわ

覚えたことをブログに書くようにすれば多少はやる気が出るかと思ったんです

Mac OSX El CapitanでGuardからテスト結果の通知を受け取ってデスクトップに表示する

Railsアプリケーションの開発環境で、Guardで自動化したテストの結果をデスクトップに通知できるように設定しました。
環境は、Ruby2.3.0とRails4.2.6で、RubyのテストフレームワークRspecを使ってます。

目次

Homebrewでterminal-notifierを入れる

terminal-notifierは、コマンドラインツールからMacの通知を使えるようにできる素晴らしい代物で、Homebrewからインストールできるようです。

$brew search terminal-notifier
terminal-notifier

ちゃちゃっと醸造します。

$brew install terminal-notifier
==> Downloading https://homebrew.bintray.com/bottles/terminal-notifier-1.6.3.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring terminal-notifier-1.6.3.el_capitan.bottle.tar.gz
==> Caveats
.app bundles were installed.
Run `brew linkapps terminal-notifier` to symlink these to /Applications.
==> Summary
🍺  /usr/local/Cellar/terminal-notifier/1.6.3: 12 files, 2.1M

Gemfileにterminal-notifier-guardを追加する

Gemfileのdevelopmentに、terminal-notifier-guardを追加します。

$vi Gemfile
group :development, :test do
  gem 'rspec-rails', '3.4.2'

  gem 'guard', '2.13.0'
  gem 'guard-rspec', '4.6.5'
end

group :development do
  gem 'terminal-notifier-guard', '~> 1.6.1'
end

Railsチュートリアルでは、通知用のgemはtestに追加されていましたが、GuardのWikidevelopmentに追加してあったので、その通りにしました。
terminal-notifier-guardのバージョンは、公式のGemfileへの追加の部分に書いてある通りにしてます。
guardrspecのバージョンも、とりあえず最新*1にしてます。

Guardからの通知を確認する

bundle installとかbundle updateとかした後に、Railsチュートリアルを参考にGuardの設定をして、起動*2させます。

$guard
14:17:05 - INFO - Guard::RSpec is running
[1] guard(main)> 

適当にファイルを変更して、テストが自動で実行されるか確認します。

14:17:23 - INFO - Running: spec/requests/static_pages_spec.rb
The RUBYGEMS_GEMDEPS environment variable is set. This enables RubyGems experimental Gemfile mode, which may conflict with
Bundler and cause unexpected errors. To remove this warning, unset RUBYGEMS_GEMDEPS.
Running via Spring preloader in process 6449
......

Finished in 0.32428 seconds (files took 5.87 seconds to load)
6 examples, 0 failures


[1] guard(main)> 

f:id:b0npu:20160417155017p:plain

ターミナルになにやらずらずらと表示された後、デスクトップの右上に格好良い通知が表示されました(๑•̀ㅂ•́)و✧

参考記事

terminal-notifier-guardについては、こちらの記事を参考にさせていただきました。

Ruby on Railsチュートリアルは、最新*3第3版ではなく第2版を参考にしています。 railstutorial.jp

開発環境

*1:2016年4月時点

*2:今回の環境ではbundle execを省略できる設定をしています

*3:2016年4月時点