Last active
November 21, 2018 20:57
-
-
Save jbodah/b9655514c9fd29b237917ce9973bdf55 to your computer and use it in GitHub Desktop.
test server
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 | |
$: << '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