Permalink
Browse files

Borrow some support files for the specs from Listen and customize the…

…m for WDM
  • Loading branch information...
1 parent 3369087 commit 7af224eb97d65c885e940c60124ff3f1972da273 @Maher4Ever committed Jul 11, 2012
Showing with 55 additions and 1 deletion.
  1. +1 −1 Guardfile
  2. +6 −0 spec/spec_helper.rb
  3. +48 −0 spec/support/fixture_helper.rb
View
2 Guardfile
@@ -13,4 +13,4 @@ group :test do
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
end
-end
+end
View
6 spec/spec_helper.rb
@@ -1,8 +1,14 @@
require 'wdm'
+Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
+
RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
config.run_all_when_everything_filtered = true
config.color_enabled = true
config.filter_run :focus
+
+ config.before(:all) { `rake compile` }
+
+ config.include WDM::SpecHelpers
end
View
48 spec/support/fixture_helper.rb
@@ -0,0 +1,48 @@
+require 'tmpdir'
+
+module WDM
+ module SpecHelpers
+ 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 fixture
+ pwd = FileUtils.pwd
+ path = File.expand_path(File.join(pwd, "spec/.fixtures/#{rand(99999)}"))
+
+ FileUtils.mkdir_p(path)
+ FileUtils.cd(path)
+
+ yield(path)
+
+ ensure
+ FileUtils.cd pwd
+ FileUtils.rm_rf(path) if File.exists?(path)
+ end
+
+ # Prepares the monitor for the test with a fixture directory and the given
+ # callback, then it yields back to the user. It also gives time for
+ # the callbacks to be called. After the tests run, it stops the monitor.
+ #
+ # @yield
+ #
+ def watch_fixture_with(monitor, callback)
+ fixture do |f|
+ monitor.watch(f, &callback)
+
+ thread = Thread.new(monitor) { |m| m.run! }
+ sleep(0.1) # give time for the monitor to bootup
+
+ yield
+
+ sleep(0.1) # allow the monitor to run the callbacks
+ subject.stop
+ thread.join
+ end
+ end
+ end
+end

0 comments on commit 7af224e

Please sign in to comment.