Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Listen 2 support #194

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 9 additions & 5 deletions lib/spring/watcher/listen.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
15 changes: 11 additions & 4 deletions test/acceptance/app_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
27 changes: 17 additions & 10 deletions test/acceptance/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
3 changes: 3 additions & 0 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
12 changes: 10 additions & 2 deletions test/unit/watcher_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down