Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 46 lines (37 sloc) 0.897 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
require 'tmpdir'

include FileUtils

# Prepares the temporary fixture directory and
# cleans it afterwards.
#
# @yield [path] an empty fixture directory
# @yieldparam [String] path the path to the fixture directory
#
def fixtures
  path = File.expand_path(File.join(Dir.tmpdir, 'listener'))
  FileUtils.mkdir_p(path)

  pwd = FileUtils.pwd
  FileUtils.cd(path)

  yield(path)

ensure
  FileUtils.cd pwd
  FileUtils.rm_rf(path) if File.exists?(path)
end

# Start the listener
#
# @param [String] path the path to watch
# @param [Hash] options the listener options
# @yield The block to listen for file changes
# @return [Array, Array, Array] the file changes
#
def listen(path, options={})
  modified = []
  added = []
  removed = []

  watcher = Listener.listen(path, options) do |m, a, r|
    modified += m
    added += a
    removed += r
  end

  yield

  [modified, added, removed]
end
Something went wrong with that request. Please try again.