inotifywait(1) と GNU screen とテスト駆動開発と夏と花火と私の死体
テスト駆動開発,Test Driven Development, TDD.健忘症の薬.
例えば以下のような,まだほとんど中身の無い書きかけのスクリプトを考える.
#! /usr/bin/ruby require 'pathname' require 'test/unit' def canonPath(path) dirComps = path.split(%r[/]) p dirComps clean = Pathname(path).cleanpath str = clean.to_s if clean.exist? and clean.directory? str + '/' else str end end class TC01 < Test::Unit::TestCase def test01() res = canonPath("/home/nodakai/mips/lib/gcc/mips/4.5.2/../../../../mips/bin/") assert_equal("/home/nodakai/mips/mips/bin/", res) end end
処理の目的は今はどうでもいい.要するにスクリプトがあって,実行すればテストが走ることまで準備するってこと.
ここでscreenで画面2分割し,上でvimを実行.下で以下のように inotifywait(1)
でディレクトリの監視を始める:
$ inotifywait -m -e modify --format %f .|while read line; do echo $line; ruby $line; done Setting up watches. Watches established. (このタイミングでエディタ上で canonPath.rb をセーブ) Loaded suite canonPath Started ["", "home", "nodakai", "mips", "lib", "gcc", "mips", "4.5.2", "..", "..", "..", "..", "mips", "bin"] . Finished in 0.002274 seconds. 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips Test run options: --seed 24043
セーブのイベントハンドラとして ruby canonPath.rb が勝手に実行された.(うーん,伝わるかなぁ...)
元々 OMake の omake -P
(persistent mode) や Emacs のscratch bufferを参考にして考えたプラクティスで,今は書き捨ての(だがワンライナーほど短くはない)スクリプトを書くときは大抵こうやってる.ユニットテスト・フレームワークを使うかどうかはともかくとして,ファイルを保存したら即座にテストまたはサンプルケースが走るというのは小気味よい.やってることは単純なのでシェルの履歴から呼び出せばすぐに使えるからね.makeを呼んでもいいし(そうするともっと OMake に近づく).