diff --git a/Gemfile b/Gemfile index 23989523..6e03ed09 100644 --- a/Gemfile +++ b/Gemfile @@ -3,4 +3,4 @@ source 'https://rubygems.org' # Specify your gem's dependencies in spring.gemspec gemspec -gem 'listen', "~> 1.0", :require => false +gem 'listen', "~> 2.7", :require => false diff --git a/lib/spring/watcher/listen.rb b/lib/spring/watcher/listen.rb index 6bc7c939..2b0fce9a 100644 --- a/lib/spring/watcher/listen.rb +++ b/lib/spring/watcher/listen.rb @@ -1,5 +1,11 @@ -gem "listen", "~> 1.0" +gem "listen", "~> 2.7" require "listen" + +# fork() doesn't preserve threads, so a clean +# Celluloid shutdown isn't possible, but we can +# reduce the 10 second timeout +Celluloid.shutdown_timeout = 2 + require "listen/version" module Spring @@ -9,9 +15,7 @@ class Listen < Abstract def start unless @listener - @listener = ::Listen.to(*base_directories, relative_paths: false) - @listener.latency(latency) - @listener.change(&method(:changed)) + @listener = ::Listen.to(*base_directories, latency: latency, &method(:changed)) @listener.start end end @@ -45,7 +49,7 @@ def base_directories ([root] + files.reject { |f| f.start_with? "#{root}/" }.map { |f| File.expand_path("#{f}/..") } + directories.reject { |d| d.start_with? "#{root}/" } - ).uniq + ).uniq.map { |path| Pathname.new(path) } end end end diff --git a/test/acceptance/app_test.rb b/test/acceptance/app_test.rb index 90896b06..5a48c5b4 100644 --- a/test/acceptance/app_test.rb +++ b/test/acceptance/app_test.rb @@ -68,9 +68,16 @@ def self.omg end setup do - generator.generate_if_missing - generator.install_spring - generator.copy_to(app.root) + begin + generator.generate_if_missing + generator.install_spring + generator.copy_to(app.root) + app.stop_spring + rescue Exception => e + STDERR.puts "FATAL: setup() caused an exception, so running tests makes no sense." + STDERR.puts "The exception was: #{e.inspect}, backtrace:\n\t#{e.backtrace.join("\n\t")}\n" + abort + end end teardown do @@ -123,7 +130,7 @@ def self.omg end test "app gets reloaded when preloaded files change (listen watcher)" do - File.write(app.gemfile, "#{app.gemfile.read}gem 'listen', '~> 1.0'") + File.write(app.gemfile, "#{app.gemfile.read}\ngem 'listen', '~> 2.7'") File.write(app.spring_config, "Spring.watch_method = :listen") app.bundle diff --git a/test/acceptance/helper.rb b/test/acceptance/helper.rb index 8d79c087..218809fb 100644 --- a/test/acceptance/helper.rb +++ b/test/acceptance/helper.rb @@ -130,7 +130,7 @@ def run(command, opts = {}) Bundler.with_clean_env do Process.spawn( - env, + env.merge(opts.fetch(:env, {})), command.to_s, out: stdout.last, err: stderr.last, @@ -139,7 +139,8 @@ def run(command, opts = {}) ) end - _, status = Timeout.timeout(opts.fetch(:timeout, DEFAULT_TIMEOUT)) { Process.wait2 } + max_time = opts.fetch(:timeout, DEFAULT_TIMEOUT) + _, status = Timeout.timeout(max_time) { Process.wait2 } if pid = spring_env.pid @server_pid = pid @@ -154,7 +155,7 @@ def run(command, opts = {}) output.merge(status: status, command: command) rescue Timeout::Error => e - raise e, "Output:\n\n#{dump_streams(command, read_streams)}" + raise "#{e.to_s}: Output:\n\n#{dump_streams(command, read_streams)} \n(command took more than #{max_time} seconds)" end def with_timing @@ -192,13 +193,19 @@ def read_stream(stream) output end + def prefix(line) + " >#{line}" + end + def dump_streams(command, streams) - output = "$ #{command}\n" + output = prefix("$ #{command}\n") streams.each do |name, stream| unless stream.chomp.empty? - output << "--- #{name} ---\n" - output << "#{stream.chomp}\n" + output << prefix("--- #{name} ---\n") + stream.lines.each do |line| + output << prefix("#{line.chomp}\n") + end end end @@ -237,7 +244,7 @@ def run!(command, options = {}) end def bundle - run! "(gem list bundler | grep bundler) || gem install bundler", timeout: nil, retry: 2 + run! "(gem list bundler | grep bundler) || gem install --no-ri --no-rdoc bundler", timeout: nil, retry: 2 run! "bundle check || bundle update --retry=2", timeout: nil end @@ -309,8 +316,8 @@ def generate install_spring - application.run! "bundle exec rails g scaffold post title:string" - application.run! "bundle exec rake db:migrate db:test:clone" + application.run! "bundle exec rails g scaffold post title:string", env: {'DISABLE_SPRING' => '1'} + application.run! "bundle exec rake db:migrate db:test:clone", env: {'DISABLE_SPRING' => '1'} end def generate_if_missing @@ -321,7 +328,7 @@ def install_spring return if @installed system("gem build spring.gemspec 2>&1") - application.run! "gem install ../../../spring-#{Spring::VERSION}.gem", timeout: nil + application.run! "gem install --no-ri --no-rdoc ../../../spring-#{Spring::VERSION}.gem", timeout: nil application.bundle diff --git a/test/helper.rb b/test/helper.rb index d6e10c8f..ac0310d6 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -4,4 +4,7 @@ require "active_support/test_case" require "minitest/autorun" +require "celluloid/test" +Celluloid.logger.level = Logger::WARN + TEST_ROOT = File.expand_path('..', __FILE__) diff --git a/test/unit/watcher_test.rb b/test/unit/watcher_test.rb index 448041cd..1c4e9a1e 100644 --- a/test/unit/watcher_test.rb +++ b/test/unit/watcher_test.rb @@ -163,6 +163,12 @@ def test_add_non_existant_file class ListenWatcherTest < ActiveSupport::TestCase include WatcherTests + setup { + Celluloid.boot + } + + teardown { Listen.stop } + def watcher_class Spring::Watcher::Listen end @@ -178,7 +184,8 @@ def watcher_class watcher.add other_dir_2 watcher.add "#{dir}/foo" - assert_equal [dir, other_dir_1, other_dir_2].sort, watcher.base_directories.sort + dirs = [dir, other_dir_1, other_dir_2].sort.map { |path| Pathname.new(path) } + assert_equal dirs, watcher.base_directories.sort ensure FileUtils.rmdir other_dir_1 FileUtils.rmdir other_dir_2 @@ -199,7 +206,8 @@ def watcher_class watcher.add "#{other_dir_1}/foo" watcher.add other_dir_2 - assert_equal [dir, other_dir_1, other_dir_2].sort, watcher.base_directories.sort + dirs = [dir, other_dir_1, other_dir_2].sort.map { |path| Pathname.new(path) } + assert_equal dirs, watcher.base_directories.sort ensure FileUtils.rmdir other_dir_1 FileUtils.rmdir other_dir_2