Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

more extract method and make spin push notification pasteable

  • Loading branch information...
commit 59f8265a24a071fb088cef056d8ee6474312db39 1 parent f80f152
@grosser authored
Showing with 60 additions and 49 deletions.
  1. +51 −40 lib/spin.rb
  2. +9 −9 spec/integration_spec.rb
View
91 lib/spin.rb
@@ -38,8 +38,42 @@ def serve(options)
end
def push(argv, options)
- # The filenames that we will spin up to `spin serve` are passed in as
- # arguments.
+ files_to_load = convert_push_arguments_to_files(argv)
+
+ if root_path = rails_root(options[:preload])
+ make_files_relative(files_to_load, root_path)
+ Dir.chdir root_path
+ end
+
+ files_to_load << "tty?" if $stdout.tty?
+
+ abort if files_to_load.empty?
+
+ puts "Spinning up #{files_to_load.join(" ")}"
+ send_files_to_serve(files_to_load)
+ end
+
+ private
+
+ def send_files_to_serve(files_to_load)
+ # This is the other end of the socket that `spin serve` opens. At this point
+ # `spin serve` will accept(2) our connection.
+ socket = UNIXSocket.open(socket_file)
+
+ # We put the filenames on the socket for the server to read and then load.
+ socket.puts files_to_load.join(PUSH_FILE_SEPARATOR)
+
+ while line = socket.readpartial(100)
+ break if line[-1,1] == "\0"
+ print line
+ end
+ rescue Errno::ECONNREFUSED, Errno::ENOENT
+ abort "Connection was refused. Have you started up `spin serve` yet?"
+ end
+
+ # The filenames that we will spin up to `spin serve` are passed in as
+ # arguments.
+ def convert_push_arguments_to_files(argv)
files_to_load = argv
# We reject anything in ARGV that isn't a file that exists. This takes
@@ -69,49 +103,17 @@ def push(argv, options)
else
file_name
end
- end.compact.uniq
-
- if root_path = rails_root(options[:preload])
- files_to_load.map! do |file|
- Pathname.new(file).expand_path.relative_path_from(root_path).to_s
- end
- Dir.chdir root_path
- end
-
- files_to_load << "tty?" if $stdout.tty?
- f = files_to_load.join(PUSH_FILE_SEPARATOR)
-
- abort if f.empty?
- puts "Spinning up #{f}"
-
- # This is the other end of the socket that `spin serve` opens. At this point
- # `spin serve` will accept(2) our connection.
- socket = UNIXSocket.open(socket_file)
- # We put the filenames on the socket for the server to read and then load.
- socket.puts f
+ end.reject(&:empty?).uniq
+ end
- while line = socket.readpartial(100)
- break if line[-1,1] == "\0"
- print line
+ def make_files_relative(files_to_load, root_path)
+ files_to_load.map! do |file|
+ Pathname.new(file).expand_path.relative_path_from(root_path).to_s
end
- rescue Errno::ECONNREFUSED, Errno::ENOENT
- abort "Connection was refused. Have you started up `spin serve` yet?"
end
- private
-
-
- # If we're not going to push the results,
- # Trap SIGQUIT (Ctrl+\) and re-run the last files that were
- # pushed.
def run_pushed_tests(socket, options)
- unless options[:push_results]
- trap('QUIT') do
- fork_and_run(@last_files_ran, nil, options)
- # See WAIT below
- Process.wait
- end
- end
+ rerun_last_tests_on_quit(options) unless options[:push_results]
# Since `spin push` reconnects each time it has new files for us we just
# need to accept(2) connections from it.
@@ -149,6 +151,15 @@ def run_pushed_tests(socket, options)
end
end
+ # Trap SIGQUIT (Ctrl+\) and re-run the last files that were pushed
+ # TODO test this
+ def rerun_last_tests_on_quit(options)
+ trap('QUIT') do
+ fork_and_run(@last_files_ran, nil, options)
+ Process.wait
+ end
+ end
+
def preload(options)
duration = Benchmark.realtime do
# We require config/application because that file (typically) loads Rails
View
18 spec/integration_spec.rb
@@ -78,14 +78,14 @@ def serve_and_push(serve_command, push_commands)
it "can serve and push" do
served, pushed = serve_and_push("", "test/foo_test.rb")
- served.should include "Preloaded Rails env in 0"
+ served.should include "Preloaded Rails env in "
served.should include "2468"
pushed.first.should == @default_pushed
end
it "can run files without .rb extension" do
served, pushed = serve_and_push("", "test/foo_test")
- served.should include "Preloaded Rails env in 0"
+ served.should include "Preloaded Rails env in "
served.should include "2468"
pushed.first.should == @default_pushed
end
@@ -93,7 +93,7 @@ def serve_and_push(serve_command, push_commands)
it "can run multiple times" do
write "test/foo_test.rb", "puts $xxx *= 2"
served, pushed = serve_and_push("", ["test/foo_test.rb", "test/foo_test.rb", "test/foo_test.rb"])
- served.should include "Preloaded Rails env in 0"
+ served.should include "Preloaded Rails env in "
served.scan("2468").size.should == 3
pushed.size.should == 3
pushed.each{|x| x.should == @default_pushed }
@@ -102,17 +102,17 @@ def serve_and_push(serve_command, push_commands)
it "can run multiple files at once" do
write "test/bar_test.rb", "puts $xxx / 2"
served, pushed = serve_and_push("", "test/foo_test.rb test/bar_test.rb")
- served.should include "Preloaded Rails env in 0"
+ served.should include "Preloaded Rails env in "
served.should include "2468"
served.should include "617"
- pushed.first.should == "Spinning up test/foo_test.rb|test/bar_test.rb\n"
+ pushed.first.should == "Spinning up test/foo_test.rb test/bar_test.rb\n"
end
it "complains when the preloaded file cannot be found" do
delete "config/application.rb"
write "test/foo_test.rb", "puts 2468"
served, pushed = serve_and_push("", "test/foo_test.rb")
- served.should_not include "Preloaded Rails env in 0"
+ served.should_not include "Preloaded Rails env in "
served.should include "Could not find config/application.rb. Are you running"
served.should include "2468"
pushed.first.should == @default_pushed
@@ -155,7 +155,7 @@ def serve_and_push(serve_command, push_commands)
it "can --push-results" do
served, pushed = serve_and_push("--push-results", "test/foo_test.rb")
- served.should include "Preloaded Rails env in 0"
+ served.should include "Preloaded Rails env in "
served.should_not include "2468"
pushed.first.should include "2468"
end
@@ -164,7 +164,7 @@ def serve_and_push(serve_command, push_commands)
write "config/application.rb", "raise"
write "config/environment.rb", "$xxx = 1234"
served, pushed = serve_and_push("--preload config/environment.rb", "test/foo_test.rb")
- served.should include "Preloaded Rails env in 0"
+ served.should include "Preloaded Rails env in "
served.should include "2468"
pushed.first.should == @default_pushed
end
@@ -179,7 +179,7 @@ def serve_and_push(serve_command, push_commands)
it "ignores -e" do
served, pushed = serve_and_push("-e", "test/foo_test.rb -e")
- served.should include "Preloaded Rails env in 0"
+ served.should include "Preloaded Rails env in "
served.should include "2468"
pushed.first.should == @default_pushed
end
Please sign in to comment.
Something went wrong with that request. Please try again.