Skip to content

Instantly share code, notes, and snippets.

@jbodah
Last active November 21, 2018 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbodah/b9655514c9fd29b237917ce9973bdf55 to your computer and use it in GitHub Desktop.
Save jbodah/b9655514c9fd29b237917ce9973bdf55 to your computer and use it in GitHub Desktop.
test server
#! /usr/bin/env ruby
$: << 'test'
require 'bundler/setup'
require 'socket'
require 'terminal-notifier' if ENV['NOTIFY']
module TestServer
class << self
SOCK = "test.sock".freeze
def client
@client ||= UNIXSocket.new(SOCK)
end
def server
@server ||= UNIXServer.new(SOCK)
end
def running?
File.exist?(SOCK)
end
def test(files)
client.sendmsg files.join(' ')
end
def start!
`rm #{SOCK}` if running?
puts "Initializing TestServer with pid #{$$}..."
require 'test_helper'
puts "Ready!"
loop do
conn = server.accept
files, _ = conn.recvmsg
pid = fork do
puts "> Testing files: #{files}"
if files == ":all".freeze
all_glob = ENV['ALL_GLOB'] || 'test/unit/*_test.rb'
Dir.glob(all_glob).each { |f| load f }
else
files.split(' ').each { |f| load f }
end
end
_, status = Process.wait2(pid)
if ENV['NOTIFY']
msg = (status.exitstatus == 0) ? "Tests passed :)" : "Warning! Tests failed :("
TerminalNotifier.notify(msg)
end
end
ensure
`rm #{SOCK}`
end
end
end
queue = ARGV
until queue.empty?
case queue.shift
when "--serve"
TestServer.start!
when "--test"
TestServer.test(queue)
queue = []
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment