From ba537460309edcaeeb1548b27f54637c8896bc53 Mon Sep 17 00:00:00 2001 From: Cezary Baginski Date: Fri, 24 Oct 2014 18:32:55 +0200 Subject: [PATCH 01/10] use Listen ~>2.7 --- Gemfile | 2 +- lib/spring/watcher/listen.rb | 8 +++----- test/acceptance/app_test.rb | 2 +- test/unit/watcher_test.rb | 12 ++++++++++-- 4 files changed, 15 insertions(+), 9 deletions(-) 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..33adee43 100644 --- a/lib/spring/watcher/listen.rb +++ b/lib/spring/watcher/listen.rb @@ -1,4 +1,4 @@ -gem "listen", "~> 1.0" +gem "listen", "~> 2.7" require "listen" require "listen/version" @@ -9,9 +9,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 +43,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..c3ce49a5 100644 --- a/test/acceptance/app_test.rb +++ b/test/acceptance/app_test.rb @@ -123,7 +123,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/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 From e57bd0ed6351e8b80028d1cc4c2590c48502f14e Mon Sep 17 00:00:00 2001 From: Cezary Baginski Date: Fri, 24 Oct 2014 18:34:49 +0200 Subject: [PATCH 02/10] reduce lengthy Celluloid shutdown timeout --- lib/spring/watcher/listen.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/spring/watcher/listen.rb b/lib/spring/watcher/listen.rb index 33adee43..2b0fce9a 100644 --- a/lib/spring/watcher/listen.rb +++ b/lib/spring/watcher/listen.rb @@ -1,5 +1,11 @@ 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 From a534069aa714665760a2c0e6501d626c0f3f8b79 Mon Sep 17 00:00:00 2001 From: Cezary Baginski Date: Fri, 24 Oct 2014 18:36:30 +0200 Subject: [PATCH 03/10] abort tests with error when setup() silently fails --- test/acceptance/app_test.rb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/test/acceptance/app_test.rb b/test/acceptance/app_test.rb index c3ce49a5..c7839013 100644 --- a/test/acceptance/app_test.rb +++ b/test/acceptance/app_test.rb @@ -68,9 +68,15 @@ 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) + 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 From 960c36ea0db159e11f60841a605662de3082cd36 Mon Sep 17 00:00:00 2001 From: Cezary Baginski Date: Fri, 24 Oct 2014 18:37:35 +0200 Subject: [PATCH 04/10] stop possibly running spring before each test --- test/acceptance/app_test.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/test/acceptance/app_test.rb b/test/acceptance/app_test.rb index c7839013..5a48c5b4 100644 --- a/test/acceptance/app_test.rb +++ b/test/acceptance/app_test.rb @@ -72,6 +72,7 @@ def self.omg 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" From fd7420e22d2fd1bd169e8eee4671f53aa426d26c Mon Sep 17 00:00:00 2001 From: Cezary Baginski Date: Fri, 24 Oct 2014 18:38:09 +0200 Subject: [PATCH 05/10] allow custom ENV to be passed to run() --- test/acceptance/helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/acceptance/helper.rb b/test/acceptance/helper.rb index 8d79c087..65e490e5 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, From 51a3d8f8ebbda4b12e0ce43606e96361d83d0a28 Mon Sep 17 00:00:00 2001 From: Cezary Baginski Date: Fri, 24 Oct 2014 18:39:01 +0200 Subject: [PATCH 06/10] make Timeout errors more readable --- test/acceptance/helper.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/acceptance/helper.rb b/test/acceptance/helper.rb index 65e490e5..cefeccda 100644 --- a/test/acceptance/helper.rb +++ b/test/acceptance/helper.rb @@ -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 From 0dd21c3f4c4d6a2d741c242336326186457376d9 Mon Sep 17 00:00:00 2001 From: Cezary Baginski Date: Fri, 24 Oct 2014 18:40:21 +0200 Subject: [PATCH 07/10] visually separate commands run during tests --- test/acceptance/helper.rb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/test/acceptance/helper.rb b/test/acceptance/helper.rb index cefeccda..1248999b 100644 --- a/test/acceptance/helper.rb +++ b/test/acceptance/helper.rb @@ -193,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 From 4a5ce89114576b7511dd0e6c33b6eea244e9bd02 Mon Sep 17 00:00:00 2001 From: Cezary Baginski Date: Fri, 24 Oct 2014 18:41:17 +0200 Subject: [PATCH 08/10] reduced total files monitored by skipping docs --- test/acceptance/helper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/acceptance/helper.rb b/test/acceptance/helper.rb index 1248999b..09580480 100644 --- a/test/acceptance/helper.rb +++ b/test/acceptance/helper.rb @@ -244,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 @@ -328,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 From c661e705775d6f1613918b1c982fcfc04002f847 Mon Sep 17 00:00:00 2001 From: Cezary Baginski Date: Fri, 24 Oct 2014 18:41:54 +0200 Subject: [PATCH 09/10] disable spring during setup() --- test/acceptance/helper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/acceptance/helper.rb b/test/acceptance/helper.rb index 09580480..218809fb 100644 --- a/test/acceptance/helper.rb +++ b/test/acceptance/helper.rb @@ -316,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 From 281686fd52691e8bb21c91f56f3d804e7f00ad2d Mon Sep 17 00:00:00 2001 From: Cezary Baginski Date: Fri, 24 Oct 2014 18:46:30 +0200 Subject: [PATCH 10/10] use celluloid/test and show celluloid warnings --- test/helper.rb | 3 +++ 1 file changed, 3 insertions(+) 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__)