spring 1.1.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,167 @@
1
+ require "tmpdir"
2
+ require "fileutils"
3
+ require "timeout"
4
+ require "active_support/core_ext/numeric/time"
5
+
6
+ module Spring
7
+ module Test
8
+ class WatcherTest < ActiveSupport::TestCase
9
+ runnables.delete self # prevent Minitest running this class
10
+
11
+ LATENCY = 0.001
12
+ TIMEOUT = 1
13
+
14
+ attr_accessor :dir
15
+
16
+ def watcher_class
17
+ raise NotImplementedError
18
+ end
19
+
20
+ def watcher
21
+ @watcher ||= watcher_class.new(dir, LATENCY)
22
+ end
23
+
24
+ def setup
25
+ @dir = File.realpath(Dir.mktmpdir)
26
+ end
27
+
28
+ def teardown
29
+ FileUtils.remove_entry_secure @dir
30
+ watcher.stop
31
+ end
32
+
33
+ def touch(file, mtime = nil)
34
+ options = {}
35
+ options[:mtime] = mtime if mtime
36
+ FileUtils.touch(file, options)
37
+ end
38
+
39
+ def assert_stale
40
+ timeout = Time.now + TIMEOUT
41
+ sleep LATENCY until watcher.stale? || Time.now > timeout
42
+ assert watcher.stale?
43
+ end
44
+
45
+ def assert_not_stale
46
+ sleep LATENCY * 10
47
+ assert !watcher.stale?
48
+ end
49
+
50
+ test "starting with no file" do
51
+ file = "#{@dir}/omg"
52
+ touch file, Time.now - 2.seconds
53
+
54
+ watcher.start
55
+ watcher.add file
56
+
57
+ assert_not_stale
58
+ touch file, Time.now
59
+ assert_stale
60
+ end
61
+
62
+ test "is stale when a watched file is updated" do
63
+ file = "#{@dir}/omg"
64
+ touch file, Time.now - 2.seconds
65
+
66
+ watcher.add file
67
+ watcher.start
68
+
69
+ assert_not_stale
70
+ touch file, Time.now
71
+ assert_stale
72
+ end
73
+
74
+ test "is stale when removing files" do
75
+ file = "#{@dir}/omg"
76
+ touch file, Time.now
77
+
78
+ watcher.add file
79
+ watcher.start
80
+
81
+ assert_not_stale
82
+ FileUtils.rm(file)
83
+ assert_stale
84
+ end
85
+
86
+ test "is stale when files are added to a watched directory" do
87
+ subdir = "#{@dir}/subdir"
88
+ FileUtils.mkdir(subdir)
89
+
90
+ watcher.add subdir
91
+ watcher.start
92
+
93
+ assert_not_stale
94
+ touch "#{subdir}/foo", Time.now - 1.minute
95
+ assert_stale
96
+ end
97
+
98
+ test "is stale when a file is changed in a watched directory" do
99
+ subdir = "#{@dir}/subdir"
100
+ FileUtils.mkdir(subdir)
101
+ touch "#{subdir}/foo", Time.now - 1.minute
102
+
103
+ watcher.add subdir
104
+ watcher.start
105
+
106
+ assert_not_stale
107
+ touch "#{subdir}/foo", Time.now
108
+ assert_stale
109
+ end
110
+
111
+ test "adding doesn't wipe stale state" do
112
+ file = "#{@dir}/omg"
113
+ file2 = "#{@dir}/foo"
114
+ touch file, Time.now - 2.seconds
115
+ touch file2, Time.now - 2.seconds
116
+
117
+ watcher.add file
118
+ watcher.start
119
+
120
+ assert_not_stale
121
+
122
+ touch file, Time.now
123
+ watcher.add file2
124
+
125
+ assert_stale
126
+ end
127
+
128
+ test "on stale" do
129
+ file = "#{@dir}/omg"
130
+ touch file, Time.now - 2.seconds
131
+
132
+ stale = false
133
+ watcher.on_stale { stale = true }
134
+
135
+ watcher.add file
136
+ watcher.start
137
+
138
+ touch file, Time.now
139
+
140
+ Timeout.timeout(1) { sleep 0.01 until stale }
141
+ assert stale
142
+
143
+ # Check that we only get notified once
144
+ stale = false
145
+ sleep LATENCY * 3
146
+ assert !stale
147
+ end
148
+
149
+ test "add relative path" do
150
+ File.write("#{dir}/foo", "foo")
151
+ watcher.add "foo"
152
+ assert_equal ["#{dir}/foo"], watcher.files.to_a
153
+ end
154
+
155
+ test "add dot relative path" do
156
+ File.write("#{dir}/foo", "foo")
157
+ watcher.add "./foo"
158
+ assert_equal ["#{dir}/foo"], watcher.files.to_a
159
+ end
160
+
161
+ test "add non existent file" do
162
+ watcher.add './foobar'
163
+ assert watcher.files.empty?
164
+ end
165
+ end
166
+ end
167
+ end
@@ -0,0 +1,15 @@
1
+ require "active_support/test_case"
2
+
3
+ module Spring
4
+ module Test
5
+ class << self
6
+ attr_accessor :root
7
+ end
8
+
9
+ require "spring/test/application"
10
+ require "spring/test/application_generator"
11
+ require "spring/test/rails_version"
12
+ require "spring/test/watcher_test"
13
+ require "spring/test/acceptance_test"
14
+ end
15
+ end
@@ -1,3 +1,3 @@
1
1
  module Spring
2
- VERSION = "1.1.1"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -1,3 +1,5 @@
1
+ require "spring/watcher"
2
+
1
3
  module Spring
2
4
  module Watcher
3
5
  class Polling < Abstract
@@ -9,15 +9,11 @@ module Spring
9
9
  end
10
10
 
11
11
  def self.watch_method=(method)
12
- case method
13
- when :polling
14
- require_relative "watcher/polling"
15
- @watch_method = Watcher::Polling
16
- when :listen
17
- require_relative "watcher/listen"
18
- @watch_method = Watcher::Listen
19
- else
12
+ if method.is_a?(Class)
20
13
  @watch_method = method
14
+ else
15
+ require "spring/watcher/#{method}"
16
+ @watch_method = Watcher.const_get(method.to_s.gsub(/(^.|_.)/) { $1[-1].upcase })
21
17
  end
22
18
  end
23
19
 
@@ -0,0 +1,4 @@
1
+ require "helper"
2
+
3
+ class AcceptanceTest < Spring::Test::AcceptanceTest
4
+ end
data/test/helper.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
2
2
 
3
3
  require "bundler/setup"
4
- require "active_support/test_case"
5
4
  require "minitest/autorun"
6
5
 
7
- TEST_ROOT = File.expand_path('..', __FILE__)
6
+ require "spring/test"
7
+ Spring::Test.root = File.expand_path('..', __FILE__)
@@ -7,7 +7,17 @@ class CommandsTest < ActiveSupport::TestCase
7
7
  assert_equal 'test', command.env(['test'])
8
8
  end
9
9
 
10
- test 'console command ignores first argument if it is a flag' do
10
+ test 'console command sets rails environment from -e option' do
11
+ command = Spring::Commands::RailsConsole.new
12
+ assert_equal 'test', command.env(['-e', 'test'])
13
+ end
14
+
15
+ test 'console command sets rails environment from --environment option' do
16
+ command = Spring::Commands::RailsConsole.new
17
+ assert_equal 'test', command.env(['--environment=test'])
18
+ end
19
+
20
+ test 'console command ignores first argument if it is a flag except -e and --environment' do
11
21
  command = Spring::Commands::RailsConsole.new
12
22
  assert_nil command.env(['--sandbox'])
13
23
  end
@@ -27,6 +37,18 @@ class CommandsTest < ActiveSupport::TestCase
27
37
  assert_nil command.env(['puts 1+1'])
28
38
  end
29
39
 
40
+ test 'RailsRunner#extract_environment removes -e <env>' do
41
+ command = Spring::Commands::RailsRunner.new
42
+ args = ['-b', '-a', '-e', 'test', '-r']
43
+ assert_equal [['-b', '-a', '-r'], 'test'], command.extract_environment(args)
44
+ end
45
+
46
+ test 'RailsRunner#extract_environment removes --environment=<env>' do
47
+ command = Spring::Commands::RailsRunner.new
48
+ args = ['-b', '--environment=test', '-a', '-r']
49
+ assert_equal [['-b', '-a', '-r'], 'test'], command.extract_environment(args)
50
+ end
51
+
30
52
  test "rake command has configurable environments" do
31
53
  command = Spring::Commands::Rake.new
32
54
  assert_nil command.env(["foo"])
@@ -1,194 +1,8 @@
1
1
  require "helper"
2
- require "tmpdir"
3
- require "fileutils"
4
- require "timeout"
5
- require "active_support/core_ext/numeric/time"
6
- require "spring/watcher"
2
+ require "spring/test/watcher_test"
7
3
  require "spring/watcher/polling"
8
- require "spring/watcher/listen"
9
-
10
- module WatcherTests
11
- LATENCY = 0.001
12
- TIMEOUT = 1
13
-
14
- attr_accessor :dir
15
-
16
- def watcher
17
- @watcher ||= watcher_class.new(dir, LATENCY)
18
- end
19
-
20
- def setup
21
- @dir = File.realpath(Dir.mktmpdir)
22
- end
23
-
24
- def teardown
25
- FileUtils.remove_entry_secure @dir
26
- watcher.stop
27
- end
28
-
29
- def touch(file, mtime = nil)
30
- options = {}
31
- options[:mtime] = mtime if mtime
32
- FileUtils.touch(file, options)
33
- end
34
-
35
- def assert_stale
36
- timeout = Time.now + TIMEOUT
37
- sleep LATENCY until watcher.stale? || Time.now > timeout
38
- assert watcher.stale?
39
- end
40
-
41
- def assert_not_stale
42
- sleep LATENCY * 10
43
- assert !watcher.stale?
44
- end
45
-
46
- def test_starting_with_no_file
47
- file = "#{@dir}/omg"
48
- touch file, Time.now - 2.seconds
49
-
50
- watcher.start
51
- watcher.add file
52
-
53
- assert_not_stale
54
- touch file, Time.now
55
- assert_stale
56
- end
57
-
58
- def test_is_stale_when_a_watched_file_is_updated
59
- file = "#{@dir}/omg"
60
- touch file, Time.now - 2.seconds
61
-
62
- watcher.add file
63
- watcher.start
64
-
65
- assert_not_stale
66
- touch file, Time.now
67
- assert_stale
68
- end
69
-
70
- def test_is_stale_when_removing_files
71
- file = "#{@dir}/omg"
72
- touch file, Time.now
73
-
74
- watcher.add file
75
- watcher.start
76
-
77
- assert_not_stale
78
- FileUtils.rm(file)
79
- assert_stale
80
- end
81
-
82
- def test_is_stale_when_files_are_added_to_a_watched_directory
83
- subdir = "#{@dir}/subdir"
84
- FileUtils.mkdir(subdir)
85
-
86
- watcher.add subdir
87
- watcher.start
88
-
89
- assert_not_stale
90
- touch "#{subdir}/foo", Time.now - 1.minute
91
- assert_stale
92
- end
93
-
94
- def test_is_stale_when_a_file_is_changed_in_a_watched_directory
95
- subdir = "#{@dir}/subdir"
96
- FileUtils.mkdir(subdir)
97
- touch "#{subdir}/foo", Time.now - 1.minute
98
-
99
- watcher.add subdir
100
- watcher.start
101
-
102
- assert_not_stale
103
- touch "#{subdir}/foo", Time.now
104
- assert_stale
105
- end
106
-
107
- def test_adding_doesnt_wipe_stale_state
108
- file = "#{@dir}/omg"
109
- file2 = "#{@dir}/foo"
110
- touch file, Time.now - 2.seconds
111
- touch file2, Time.now - 2.seconds
112
-
113
- watcher.add file
114
- watcher.start
115
-
116
- assert_not_stale
117
-
118
- touch file, Time.now
119
- watcher.add file2
120
-
121
- assert_stale
122
- end
123
-
124
- def test_on_stale
125
- file = "#{@dir}/omg"
126
- touch file, Time.now - 2.seconds
127
-
128
- watcher.add file
129
- watcher.start
130
-
131
- stale = false
132
- watcher.on_stale { stale = true }
133
-
134
- touch file, Time.now
135
-
136
- Timeout.timeout(1) { sleep 0.01 until stale }
137
- assert stale
138
-
139
- # Check that we only get notified once
140
- stale = false
141
- sleep LATENCY * 3
142
- assert !stale
143
- end
144
-
145
- def test_add_relative_path
146
- File.write("#{dir}/foo", "foo")
147
- watcher.add "foo"
148
- assert_equal ["#{dir}/foo"], watcher.files.to_a
149
- end
150
-
151
- def test_add_dot_relative_path
152
- File.write("#{dir}/foo", "foo")
153
- watcher.add "./foo"
154
- assert_equal ["#{dir}/foo"], watcher.files.to_a
155
- end
156
-
157
- def test_add_non_existant_file
158
- watcher.add './foobar'
159
- assert watcher.files.empty?
160
- end
161
- end
162
-
163
- class ListenWatcherTest < ActiveSupport::TestCase
164
- include WatcherTests
165
-
166
- def watcher_class
167
- Spring::Watcher::Listen
168
- end
169
-
170
- test "root directories" do
171
- begin
172
- other_dir_1 = File.realpath(Dir.mktmpdir)
173
- other_dir_2 = File.realpath(Dir.mktmpdir)
174
- File.write("#{other_dir_1}/foo", "foo")
175
- File.write("#{dir}/foo", "foo")
176
-
177
- watcher.add "#{other_dir_1}/foo"
178
- watcher.add other_dir_2
179
- watcher.add "#{dir}/foo"
180
-
181
- assert_equal [dir, other_dir_1, other_dir_2].sort, watcher.base_directories.sort
182
- ensure
183
- FileUtils.rmdir other_dir_1
184
- FileUtils.rmdir other_dir_2
185
- end
186
- end
187
- end
188
-
189
- class PollingWatcherTest < ActiveSupport::TestCase
190
- include WatcherTests
191
4
 
5
+ class PollingWatcherTest < Spring::Test::WatcherTest
192
6
  def watcher_class
193
7
  Spring::Watcher::Polling
194
8
  end
metadata CHANGED
@@ -1,41 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spring
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Leighton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-09 00:00:00.000000000 Z
11
+ date: 2014-11-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  description: Rails application preloader
@@ -46,8 +46,8 @@ executables:
46
46
  extensions: []
47
47
  extra_rdoc_files: []
48
48
  files:
49
- - .gitignore
50
- - .travis.yml
49
+ - ".gitignore"
50
+ - ".travis.yml"
51
51
  - CHANGELOG.md
52
52
  - CONTRIBUTING.md
53
53
  - Gemfile
@@ -80,14 +80,18 @@ files:
80
80
  - lib/spring/process_title_updater.rb
81
81
  - lib/spring/server.rb
82
82
  - lib/spring/sid.rb
83
+ - lib/spring/test.rb
84
+ - lib/spring/test/acceptance_test.rb
85
+ - lib/spring/test/application.rb
86
+ - lib/spring/test/application_generator.rb
87
+ - lib/spring/test/rails_version.rb
88
+ - lib/spring/test/watcher_test.rb
83
89
  - lib/spring/version.rb
84
90
  - lib/spring/watcher.rb
85
91
  - lib/spring/watcher/abstract.rb
86
- - lib/spring/watcher/listen.rb
87
92
  - lib/spring/watcher/polling.rb
88
93
  - spring.gemspec
89
- - test/acceptance/app_test.rb
90
- - test/acceptance/helper.rb
94
+ - test/acceptance_test.rb
91
95
  - test/apps/.gitignore
92
96
  - test/helper.rb
93
97
  - test/unit/client/help_test.rb
@@ -105,23 +109,22 @@ require_paths:
105
109
  - lib
106
110
  required_ruby_version: !ruby/object:Gem::Requirement
107
111
  requirements:
108
- - - '>='
112
+ - - ">="
109
113
  - !ruby/object:Gem::Version
110
114
  version: '0'
111
115
  required_rubygems_version: !ruby/object:Gem::Requirement
112
116
  requirements:
113
- - - '>='
117
+ - - ">="
114
118
  - !ruby/object:Gem::Version
115
119
  version: '0'
116
120
  requirements: []
117
121
  rubyforge_project:
118
- rubygems_version: 2.1.9
122
+ rubygems_version: 2.2.2
119
123
  signing_key:
120
124
  specification_version: 4
121
125
  summary: Rails application preloader
122
126
  test_files:
123
- - test/acceptance/app_test.rb
124
- - test/acceptance/helper.rb
127
+ - test/acceptance_test.rb
125
128
  - test/apps/.gitignore
126
129
  - test/helper.rb
127
130
  - test/unit/client/help_test.rb
@@ -129,4 +132,3 @@ test_files:
129
132
  - test/unit/commands_test.rb
130
133
  - test/unit/process_title_updater_test.rb
131
134
  - test/unit/watcher_test.rb
132
- has_rdoc:
@@ -1,52 +0,0 @@
1
- gem "listen", "~> 1.0"
2
- require "listen"
3
- require "listen/version"
4
-
5
- module Spring
6
- module Watcher
7
- class Listen < Abstract
8
- attr_reader :listener
9
-
10
- def start
11
- unless @listener
12
- @listener = ::Listen.to(*base_directories, relative_paths: false)
13
- @listener.latency(latency)
14
- @listener.change(&method(:changed))
15
- @listener.start
16
- end
17
- end
18
-
19
- def stop
20
- if @listener
21
- @listener.stop
22
- @listener = nil
23
- end
24
- end
25
-
26
- def subjects_changed
27
- if @listener && @listener.directories.sort != base_directories.sort
28
- restart
29
- end
30
- end
31
-
32
- def watching?(file)
33
- files.include?(file) || file.start_with?(*directories)
34
- end
35
-
36
- def changed(modified, added, removed)
37
- synchronize do
38
- if (modified + added + removed).any? { |f| watching? f }
39
- mark_stale
40
- end
41
- end
42
- end
43
-
44
- def base_directories
45
- ([root] +
46
- files.reject { |f| f.start_with? root }.map { |f| File.expand_path("#{f}/..") } +
47
- directories.reject { |d| d.start_with? root }
48
- ).uniq
49
- end
50
- end
51
- end
52
- end