Class: ClucumberSubprocess

Inherits:
Object
  • Object
show all
Defined in:
lib/clucumber.rb

Defined Under Namespace

Classes: LaunchFailed

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir, options = {}) ⇒ ClucumberSubprocess

Returns a new instance of ClucumberSubprocess.



19
20
21
22
23
24
# File 'lib/clucumber.rb', line 19

def initialize(dir, options={})
  @dir = dir
  @lisp = options[:lisp] || ENV['LISP'] || 'sbcl --disable-debugger'
  @port = options[:port]
  @output = ""
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



8
9
10
# File 'lib/clucumber.rb', line 8

def output
  @output
end

Class Method Details

.launch(dir, options = {}) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/clucumber.rb', line 10

def self.launch(dir, options={})
  proc = ClucumberSubprocess.new(dir, options)
  at_exit do
    proc.kill
  end
  proc.run
  proc
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


93
94
95
96
97
# File 'lib/clucumber.rb', line 93

def alive?
  if !@pid.nil?
    (Process.kill("CONT", @pid) && true) rescue false
  end
end

#connectable?Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
68
69
# File 'lib/clucumber.rb', line 62

def connectable?
  if socket = TCPSocket.new("127.0.0.1", @port) rescue nil
    socket.close
    true
  else
    false
  end
end

#killObject



82
83
84
85
86
87
88
89
90
91
# File 'lib/clucumber.rb', line 82

def kill
  if @pid
    @reader.terminate!
    Process.kill("TERM", @pid)
    Process.waitpid(@pid)
    @pid = nil
  end
rescue PTY::ChildExited
  @pid = nil
end

#listen(additional_forms = "") ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/clucumber.rb', line 53

def listen(additional_forms="")
  start_clucumber_server(additional_forms) unless connectable?
  until connectable?
    raise LaunchFailed, "Couldn't start clucumber:\n#{@output}" unless alive?
    sleep 0.5
  end
  sleep 1
end

#record_outputObject



71
72
73
74
75
76
77
78
79
80
# File 'lib/clucumber.rb', line 71

def record_output
  begin
    while line = @out.readline
      @output << line
    end
  rescue PTY::ChildExited
    STDOUT.puts "child exited, stopping."
    nil
  end
end

#runObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/clucumber.rb', line 26

def run
  set_port
  
  Dir.chdir(@dir) do
    @out, @in, @pid = PTY.spawn(@lisp)
  end
  @reader = Thread.start {
    record_output
  }
  cluke_dir = File.expand_path("clucumber/", File.dirname(__FILE__))
  Dir[cluke_dir + '/**/*.fasl'].each do |fasl|
    FileUtils.rm(fasl)
  end
  @in.puts(<<-LISP)
    (load #p"#{File.expand_path("clucumber/clucumber-bootstrap.lisp", File.dirname(__FILE__))}")
  LISP
end

#start_clucumber_server(additional_forms) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/clucumber.rb', line 44

def start_clucumber_server(additional_forms)
  @in.puts <<-LISP
    #{additional_forms}
    (asdf:oos 'asdf:load-op :clucumber)
    (clucumber-external:start #p"./" "localhost" #{@port})
  LISP
  sleep 1
end