Created
December 12, 2012 17:02
-
-
Save pbrisbin/4269567 to your computer and use it in GitHub Desktop.
Remote test runner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# Vagrant users are constantly switching between a local editor and a | |
# terminal into their VM to work on code then run the tests. | |
# | |
# This script (when used as a server) listens on a TCP port inside the | |
# VM for test commands to execute. The script (when used as a client) | |
# also handles sending the appropriate "run this test" command to that | |
# same port on the VM. | |
# | |
# Assumes VM is at 33.33.33.10 and you're using it for rails tests. | |
# Binds to port 5544. | |
# | |
# [vm]$ remote-test --server | |
# | |
# [host]$ remote-test <file> [options, ...] | |
# | |
# vim: noremap ,t :!remote-test %<CR> | |
# | |
### | |
require 'socket' | |
Server = Struct.new(:port) do | |
def run(&block) | |
server = TCPServer.new(port) | |
loop do | |
puts "[*] listening..." | |
line = server.accept.gets | |
yield(line.strip) | |
puts | |
end | |
end | |
end | |
Client = Struct.new(:host, :port) do | |
def send(text) | |
s = TCPSocket.new(host, port) | |
s.puts(text) | |
ensure | |
s.close | |
end | |
end | |
class Main | |
HOST = '33.33.33.10' | |
PORT = 5544 | |
class << self | |
def run(argv) | |
if argv.first == '--server' | |
Server.new(PORT).run do |cmd| | |
if test_command?(cmd) | |
puts "[*] running command: #{cmd}" | |
system(cmd) | |
end | |
end | |
else | |
file = argv.shift or raise "file required" | |
cmd = test_command(file, argv) | |
c = Client.new(HOST, PORT) | |
c.send(cmd) | |
end | |
rescue Exception => ex | |
$stderr.puts "#{ex}" | |
exit 1 | |
end | |
private | |
def test_command(file, options) | |
options = options.any? && options.join(' ') | |
cmd = case file | |
when /^test\/unit/ then 'rake test:units' | |
when /^test\/functional/ then 'rake test:functionals' | |
when /^test\/integration/ then 'rake test:integration' | |
end | |
cmd += " TEST='#{file}'" | |
cmd += " TESTOPTS='#{options}'" if options | |
cmd | |
end | |
def test_command?(cmd) | |
cmd =~ /^rake test:\S+ TEST='[^']+'( TESTOPTS='[^']+')?$/ | |
end | |
end | |
end | |
Main.run(ARGV) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment