Module: TestServer

Defined in:
lib/test_server.rb,
lib/test_server/version.rb

Constant Summary collapse

VERSION =
"1.2.0"

Class Method Summary collapse

Class Method Details

.notify(msg) ⇒ Object



87
88
89
90
# File 'lib/test_server.rb', line 87

def notify(msg)
  require 'terminal-notifier'
  TerminalNotifier.notify(msg)
end

.run!(options) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/test_server.rb', line 6

def run!(options)
  case options[:mode]
  when :serve
    serve!(options)
  when :push
    test(options)
  end
end

.serve!(options) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/test_server.rb', line 32

def serve!(options)
  with_server(options) do |server|
    puts "Initializing TestServer with pid #{$$}..."
    eval options[:preload] if options[:preload].length > 0
    puts "Ready!"
    loop do
      conn = server.accept
      files, _ = conn.recvmsg
      pid = fork do
        puts "> Testing files: #{files}"
        eval options[:after_fork] if options[:after_fork].length > 0
        if files == ":all".freeze
          all_glob = ENV['ALL_GLOB'] || 'test/**/*_test.rb'
          Dir.glob(all_glob).each { |f| load f }
        else
          files.split(' ').each { |f| load f }
        end
      end
      _, status = Process.wait2(pid)
      exit_code = status.exitstatus

      if options[:forward_exit_code]
        conn.sendmsg exit_code.to_s
      else
        if exit_code == 0
          notify("Tests passed :)") if options[:notify_on_pass]
        else
          notify("Warning! Tests failed :(") if options[:notify_on_fail]
        end
      end
    end
  end
end

.test(options) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/test_server.rb', line 15

def test(options)
  with_client(options) do |client|
    client.sendmsg options[:files].join(' ')

    if options[:forward_exit_code]
      exit_code, _ = client.recvmsg
      exit_code = exit_code.to_i
      if exit_code == 0
        notify("Tests passed :)") if options[:notify_on_pass]
      else
        notify("Warning! Tests failed :(") if options[:notify_on_fail]
      end
      exit(exit_code)
    end
  end
end

.with_client(options) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/test_server.rb', line 66

def with_client(options)
  if options[:remote]
    yield TCPSocket.new(options[:host], options[:port])
  else
    yield UNIXSocket.new(options[:sock])
  end
end

.with_server(options) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/test_server.rb', line 74

def with_server(options)
  if options[:remote]
    yield TCPServer.new(options[:host], options[:port])
  else
    begin
      `rm #{options[:sock]}` if File.exist?(options[:sock])
      yield UNIXServer.new(options[:sock])
    ensure
      `rm #{options[:sock]}` if File.exist?(options[:sock])
    end
  end
end