spin 0.1.5 → 0.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.
Files changed (2) hide show
  1. data/bin/spin +65 -8
  2. metadata +5 -5
data/bin/spin CHANGED
@@ -1,5 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
- #
3
2
  # Spin will speed up your autotest(ish) workflow for Rails.
4
3
 
5
4
  # Spin preloads your Rails environment for testing, so you don't load the same code over and over and over... Spin works best with an autotest(ish) workflow.
@@ -28,8 +27,25 @@ def socket_file
28
27
  [Dir.tmpdir, key].join('/')
29
28
  end
30
29
 
30
+ def determine_test_framework(force_rspec, force_testunit)
31
+ if force_rspec
32
+ :rspec
33
+ elsif force_testunit
34
+ :testunit
35
+ elsif defined?(RSpec)
36
+ :rspec
37
+ else
38
+ :testunit
39
+ end
40
+ end
41
+
42
+ def disconnect(connection)
43
+ connection.print "\0"
44
+ connection.close
45
+ end
46
+
31
47
  # ## spin serve
32
- def serve(force_rspec = false, time = false)
48
+ def serve(force_rspec, force_testunit, time, push_results)
33
49
  file = socket_file
34
50
  # We delete the tmp file for the Unix socket if it already exists. The file
35
51
  # is scoped to the `pwd`, so if it already exists then it must be from an
@@ -60,6 +76,8 @@ def serve(force_rspec = false, time = false)
60
76
  warn "Could not find config/application.rb. Are you running this from the root of a Rails project?"
61
77
  end
62
78
 
79
+ puts "Pushing test results back to push processes" if push_results
80
+
63
81
  loop do
64
82
  # Since `spin push` reconnects each time it has new files for us we just
65
83
  # need to accept(2) connections from it.
@@ -71,18 +89,34 @@ def serve(force_rspec = false, time = false)
71
89
  # If spin is started with the time flag we will track total execution so
72
90
  # you can easily compare it with time rspec spec for example
73
91
  start = Time.now if time
92
+
93
+ # If we're not sending results back to the push process, we can disconnect
94
+ # it immediately.
95
+ disconnect(conn) unless push_results
74
96
 
75
97
  # We fork(2) before loading the file so that our pristine preloaded
76
98
  # environment is untouched. The child process will load whatever code it
77
99
  # needs to, then it exits and we're back to the baseline preloaded app.
78
100
  fork do
101
+ # To push the test results to the push process instead of having them
102
+ # displayed by the server, we reopen $stdout/$stderr to the open
103
+ # connection.
104
+ if push_results
105
+ $stdout.reopen(conn)
106
+ $stderr.reopen(conn)
107
+ end
108
+
79
109
  puts
80
110
  puts "Loading #{files.inspect}"
81
111
 
112
+ # Determine the test framework to use using the passed-in 'force' options
113
+ # or else default to checking for defined constants.
114
+ test_framework = determine_test_framework(force_rspec, force_testunit)
115
+
82
116
  # Unfortunately rspec's interface isn't as simple as just requiring the
83
117
  # test file that you want to run (suddenly test/unit seems like the less
84
118
  # crazy one!).
85
- if defined?(RSpec) || force_rspec
119
+ if test_framework == :rspec
86
120
  # We pretend the filepath came in as an argument and duplicate the
87
121
  # behaviour of the `rspec` binary.
88
122
  ARGV.push files
@@ -102,6 +136,14 @@ def serve(force_rspec = false, time = false)
102
136
  # If we are tracking time we will output it here after everything has
103
137
  # finished running
104
138
  puts "Total execution time was #{Time.now - start} seconds" if start
139
+
140
+ # Tests have now run. If we were pushing results to a push process, we can
141
+ # now disconnect it.
142
+ begin
143
+ disconnect(conn) if push_results
144
+ rescue Errno::EPIPE
145
+ # Don't abort if the client already disconnected
146
+ end
105
147
  end
106
148
  end
107
149
 
@@ -116,7 +158,7 @@ def push
116
158
  # bit will just be ignored.
117
159
  #
118
160
  # We build a string like `file1.rb:file2.rb` and pass it up to the server.
119
- f = files_to_load.select { |f| File.exist?(f) }.join(File::PATH_SEPARATOR)
161
+ f = files_to_load.select { |f| File.exist?(f) }.uniq.join(File::PATH_SEPARATOR)
120
162
  abort if f.empty?
121
163
  puts "Spinning up #{f}"
122
164
 
@@ -125,16 +167,23 @@ def push
125
167
  socket = UNIXSocket.open(socket_file)
126
168
  # We put the filenames on the socket for the server to read and then load.
127
169
  socket.puts f
170
+
171
+ while line = socket.gets
172
+ break if line == "\0"
173
+ print line
174
+ end
128
175
  rescue Errno::ECONNREFUSED
129
176
  abort "Connection was refused. Have you started up `spin serve` yet?"
130
177
  end
131
178
 
132
179
  force_rspec = false
180
+ force_testunit = false
133
181
  time = false
182
+ push_results = false
134
183
  options = OptionParser.new do |opts|
135
184
  opts.banner = usage
136
185
  opts.separator ""
137
- opts.separator "Options:"
186
+ opts.separator "Server Options:"
138
187
 
139
188
  opts.on("-I", "--load-path=DIR#{File::PATH_SEPARATOR}DIR", "Appends directory to $LOAD_PATH") do |dirs|
140
189
  $LOAD_PATH.concat(dirs.split(File::PATH_SEPARATOR))
@@ -144,12 +193,20 @@ options = OptionParser.new do |opts|
144
193
  force_rspec = v
145
194
  end
146
195
 
147
- opts.on('-e', 'Stub to keep kicker happy')
196
+ opts.on('--test-unit', 'Force the selected test framework to Test::Unit') do |v|
197
+ force_testunit = v
198
+ end
148
199
 
149
- opts.on('-t', '--time') do |v|
200
+ opts.on('-t', '--time', 'See total execution time for each test run') do |v|
150
201
  time = true
151
202
  end
152
203
 
204
+ opts.on('--push-results', 'Push test results to the push process') do |v|
205
+ push_results = v
206
+ end
207
+
208
+ opts.separator "General Options:"
209
+ opts.on('-e', 'Stub to keep kicker happy')
153
210
  opts.on('-h', '--help') do
154
211
  $stderr.puts opts
155
212
  exit 1
@@ -159,7 +216,7 @@ options.parse!
159
216
 
160
217
  subcommand = ARGV.shift
161
218
  case subcommand
162
- when 'serve' then serve(force_rspec, time)
219
+ when 'serve' then serve(force_rspec, force_testunit, time, push_results)
163
220
  when 'push' then push
164
221
  else
165
222
  $stderr.puts options
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spin
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 5
10
- version: 0.1.5
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jesse Storimer
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-11-15 00:00:00 -05:00
18
+ date: 2011-11-19 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies: []
21
21