guard/listener — Issue #2: Defining the listener spec
Author: netzpirat
Created: 2012-01-11 15:17:18+00:00
State: open

I started with the basic spec setup and the initial listener spec. The spec setup is a simple internal DSL to define the internal file setup, make the watched file operations and verify its outcome.

The fixture helpers a defined in [spec/support/fixtures.rb](https://github.com/guard/listener/blob/master/spec/support/fixtures.rb).

All listener specs starts with an empty fixtures playground:

```ruby
fixtures do |path|
...
end
```

After the fixture directory is prepared, you start with the file and directory setup. Node the [FileUtils](http://ruby-doc.org/stdlib-1.9.3/libdoc/fileutils/rdoc/FileUtils.html) is included and you can just use `mkdir`, `touch`, `rm`, `cp`, etc.

```ruby
fixtures do |path|
touch 'plain.txt'
mkdir 'a_directory'
touch 'a_directory/another_file.rb'

...
end
```

After the setup has been done, the listener must be started. The listener takes a path, so we can also test multiple listener in the fixture directory.

```ruby
fixtures do |path|
touch 'move_me.txt'

modified, added, removed = listen(path) do
...
end
```

File operations are only recorded in the `listen` block and can be verified afterwards:

```ruby
fixtures do |path|
touch 'move_me.txt'

modified, added, removed = listen(path) do
mv 'move_me.txt', 'new_name.txt'
end

added.should =~ %w(new_name.txt)
modified.should be_empty
removed.should =~ %w(move_me.txt)
end
```

I hope you agree with the spec setup.