Ruby + guardによるテスト自動化を、MacとWindowsでそれぞれ実施した時の手順

Rubyスクリプトを修正した時、いちいちテストコードを実行するのが面倒くさくなったので、guardを導入した。
Windows導入時、しばらくハマってしまったので、備忘録として記述。

# Macの場合
特に問題なく、スムーズに導入完了。
(ruby管理にrbenvを使用)

```bash:mac
$ rbenv exec gem install bundler
$ vi Gemfile
$ rbenv exec gem bundle install --path vendor/bundle
$ rbenv exec guard rake init
$ vi Guardfile
$ guard
```

```ruby:Gemfile
source 'https://rubygems.org'

gem 'guard', '~> 2.14'
gem 'guard-shell', '~> 0.7'
gem 'guard-rake', '~> 1.0'
gem 'test-unit', '~> 3.2'
```

```rub:Guardfile
guard 'rake', :task => 'default' do
  watch(%r{^vendor/bundle/+$})
  watch(%r{^.+\.rb$})
  watch(%r{toos/test/sample/.+\.txt})
  watch(%r{^[^/]+$})
end
```

# Windowsの場合
Macで編集したソースコード + 各種設定ファイルをGitHubからクローン後、
すぐにGuardを起動しようとすると、エンコードエラーになった。

```bash:windows
> guard
C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/guard-2.14.0/lib/guard/guardfile/evaluator.rb:87:in `evaluate': invalid byte sequence in Windows-31J (ArgumentError)
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/guard-2.14.0/lib/guard.rb:134:in `_evaluate'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/guard-2.14.0/lib/guard.rb:49:in `setup'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/guard-2.14.0/lib/guard/commander.rb:32:in `start'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/guard-2.14.0/lib/guard/cli/environments/valid.rb:16:in `start_guard'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/guard-2.14.0/lib/guard/cli.rb:122:in `start'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/thor-0.19.1/lib/thor/command.rb:27:in `run'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/thor-0.19.1/lib/thor/invocation.rb:126:in `invoke_command'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/thor-0.19.1/lib/thor.rb:359:in `dispatch'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/thor-0.19.1/lib/thor/base.rb:440:in `start'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/guard-2.14.0/lib/guard/aruba_adapter.rb:32:in `execute'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/guard-2.14.0/lib/guard/aruba_adapter.rb:19:in `execute!'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/guard-2.14.0/bin/_guard-core:11:in `<main>'
```

bundlerでグローバルにgemをインストールしてみると、エラーメッセージが変わった。

```bash:windows
> bundle install
> guard
Warning: you have a Gemfile, but you're not using bundler or RUBYGEMS_GEMDEPS
07:47:59 - INFO - Guard here! It looks like your project has a Gemfile, yet you are running
> [#] `guard` outside of Bundler. If this is your intent, feel free to ignore this
> [#] message. Otherwise, consider using `bundle exec guard` to ensure your
> [#] dependencies are loaded correctly.
> [#] (You can run `guard` with --no-bundler-warning to get rid of this message.)
07:47:59 - INFO - Run 'gem install win32console' to use color on Windows
07:47:59 - ERROR - Could not load 'guard/rake' or find class Guard::Rake
07:47:59 - ERROR - Error is: cannot load such file -- guard/rake
07:47:59 - ERROR - C:/Ruby23-x64/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
> [#] C:/Ruby23-x64/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
> [#] C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/guard-2.14.0/lib/guard/plugin_util.rb:105:in `rescue in plugin_class
> [#] C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/guard-2.14.0/lib/guard/plugin_util.rb:97:in `plugin_class'
> [#] C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/guard-2.14.0/lib/guard/plugin_util.rb:56:in `initialize_plugin'
> [#] C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/guard-2.14.0/lib/guard/internals/plugins.rb:26:in `add'
> [#] C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/guard-2.14.0/lib/guard/dsl.rb:185:in `block in guard'
> [#] C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/guard-2.14.0/lib/guard/dsl.rb:182:in `each'
> [#] C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/guard-2.14.0/lib/guard/dsl.rb:182:in `guard'
> [#] tool_root/Guardfile:18:in `evaluate'
> [#] C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/guard-2.14.0/lib/guard/dsl.rb:377:in `instance_eval'
> [#] C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/guard-2.14.0/lib/guard/dsl.rb:377:in `evaluate'
> [#] C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/guard-2.14.0/lib/guard/guardfile/evaluator.rb:89:in `evaluate'
> [#] C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/guard-2.14.0/lib/guard.rb:134:in `_evaluate'
> [#] C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/guard-2.14.0/lib/guard.rb:49:in `setup'
> [#] C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/guard-2.14.0/lib/guard/commander.rb:32:in `start'
> [#] C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/guard-2.14.0/lib/guard/cli/environments/valid.rb:16:in `start_guard'
> [#] C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/guard-2.14.0/lib/guard/cli.rb:122:in `start'
> [#] C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/thor-0.19.1/lib/thor/command.rb:27:in `run'
> [#] C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/thor-0.19.1/lib/thor/invocation.rb:126:in `invoke_command'
> [#] C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/thor-0.19.1/lib/thor.rb:359:in `dispatch'
> [#] C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/thor-0.19.1/lib/thor/base.rb:440:in `start'
> [#] C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/guard-2.14.0/lib/guard/aruba_adapter.rb:32:in `execute'
> [#] C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/guard-2.14.0/lib/guard/aruba_adapter.rb:19:in `execute!'
> [#] C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/guard-2.14.0/bin/_guard-core:11:in `<main>'
07:47:59 - ERROR - Invalid Guardfile, original error is:
> [#]
> [#] Could not load class: "Rake",
> [#] backtrace:
> [#]   (dsl)> C
> [#]   (dsl)> C
> [#]   (dsl)> C
> [#]   (dsl)> C
> [#]   (dsl)> C
> [#]   (dsl)> G
> [#]   (dsl)> C
> [#]   (dsl)> C
> [#]   (dsl)> C
> [#]   (dsl)> C
> [#]   (dsl)> C
> [#]   (dsl)> C
> [#]   (dsl)> C
> [#]   (dsl)> C
> [#]   (dsl)> C
> [#]   (dsl)> C
> [#]   (dsl)> C
> [#]   (dsl)> C
> [#]   (dsl)> C
> [#]   (dsl)> C
> [#]   (dsl)> C
```

gemを見つけられない的なエラーメッセージになった + 「Gemfile有るのにbundle使ってねーぞ」的なメッセージが表示されているので、
bundle経由で実行し直してみる。

```bash:windows
> bundle exec guard
Ignoring ffi-1.9.14 because its extensions are not built.  Try: gem pristine ffi --version 1.9.14
  Please add the following to your Gemfile to avoid polling for changes:
    gem 'wdm', '>= 0.1.0' if Gem.win_platform?
07:48:17 - INFO - Run 'gem install win32console' to use color on Windows
07:48:17 - INFO - Starting guard-rake default
07:48:17 - INFO - running default
Ignoring ffi-1.9.14 because its extensions are not built.  Try: gem pristine ffi --version 1.9.14
C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/rubygems_integration.rb:468: warning: method redefined; discarding old find_spec_for_exe
C:/Ruby23-x64/lib/ruby/site_ruby/2.3.0/rubygems.rb:241: warning: previous definition of find_spec_for_exe was here
Loaded suite tool_root/vendor/bundle/ruby/2.3.0/gems/rake-11.2.2/lib/rake/rake_test_loader
Started
```

動き出した。ソースを更新すると、自動で再テストされるようになった。
後は、ffiをビルドしてないから無視、っていうメッセージが出ているのでちゃんとビルドしておくのと、windows用のgemが足りていないっぽいので、それを追加する予定。
ちなみに、chcpコマンドで文字コードをutf-8に変更してからguardを実行すると、何やらリソースエラーっぽいメッセージが出て、やっぱりguardが起動しない。
(エラーメッセージ保存しておくの忘れてた)