Class: TConsole::Runner

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv = []) ⇒ Runner

Public: Sets up the new runner’s config.



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

def initialize(argv = [])
  # try to load the default configs
  Config.load_config(File.join(Dir.home, ".tconsole"))
  Config.load_config(File.join(Dir.pwd, ".tconsole"))
  self.config = Config.configure(argv)
  self.reporter = Reporter.new(config)
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



4
5
6
# File 'lib/tconsole/runner.rb', line 4

def config
  @config
end

#consoleObject

Returns the value of attribute console.



4
5
6
# File 'lib/tconsole/runner.rb', line 4

def console
  @console
end

#reporterObject

Returns the value of attribute reporter.



4
5
6
# File 'lib/tconsole/runner.rb', line 4

def reporter
  @reporter
end

#stty_saveObject

Returns the value of attribute stty_save.



4
5
6
# File 'lib/tconsole/runner.rb', line 4

def stty_save
  @stty_save
end

Instance Method Details

#cleanup_processObject

Internal: Cleans up the process at the end of execution.



44
45
46
47
# File 'lib/tconsole/runner.rb', line 44

def cleanup_process
  reporter.exit_message
  system("stty", self.stty_save);
end

#console_run_loop(console) ⇒ Object

Internal: Environment reload run loop.

This run loop handles spawning a new tconsole environment - it’s basically just there to handle reloads. Also calls out to the input loop for the console.

Returns false if tconsole needs to stop, true otherwise.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/tconsole/runner.rb', line 70

def console_run_loop(console)
  pipe_server = ChattyProc::PipeServer.new

  reporter.trace("Forking test server.")
  server_pid = fork do
    server_run_loop(pipe_server)
  end

  pipe_server.caller!
  unless load_environment(pipe_server)
    pipe_server.write({:action => "exit"})
    return false
  end

  continue = console.read_and_execute(pipe_server)
  reporter.trace("Console read loop returned - continue: #{continue}")

  Process.waitall

  continue
end

#load_environment(pipe_server) ⇒ Object

Internal: Asks the server to load the environment.

Returns true if the environment was loaded, or false otherwise.



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/tconsole/runner.rb', line 95

def load_environment(pipe_server)
  reporter.trace("Attempting to load environment.")
  pipe_server.write({:action => "load_environment"})

  if pipe_server.read
    reporter.trace("Environment loaded successfully.")
    true
  else
    reporter.error("Couldn't load the test environment. Exiting.")
    false
  end
end

#prepare_processObject

Internal: Set up the process and console.



36
37
38
39
40
41
# File 'lib/tconsole/runner.rb', line 36

def prepare_process
  self.stty_save = `stty -g`.chomp

  trap("SIGINT", "IGNORE")
  trap("SIGTSTP", "IGNORE")
end

Internal: Prints config errors, if there are any.

Returns true if there were errors, false otherwise.



52
53
54
55
56
57
58
59
60
61
# File 'lib/tconsole/runner.rb', line 52

def print_config_errors
  config_errors = @config.validation_errors
  if config_errors.length > 0
    reporter.error
    reporter.error(config_errors.first)
    true
  else
    false
  end
end

#runObject

Spawns a new environment. Looks at the results of the environment to determine whether to stop or keep running



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/tconsole/runner.rb', line 17

def run
  prepare_process
  reporter.welcome_message
  exit(1) if print_config_errors

  # Set up our console input handling and history
  console = Console.new(config, reporter)

  # Start the server
  while console_run_loop(console)
    # just need to run the loop
  end

  console.store_history

  cleanup_process
end

#server_run_loop(pipe_server) ⇒ Object

Internal: Run loop for the server.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/tconsole/runner.rb', line 109

def server_run_loop(pipe_server)
  pipe_server.callee!
  server = Server.new(config, reporter)

  while message = pipe_server.read
    reporter.trace("Server Received Message: #{message[:action]}")
    begin
      result = server.handle(message)
      pipe_server.write(result)
    rescue => e
      reporter.error
      reporter.error("An error occured: #{e.message}")
      reporter.trace_backtrace(e)
      pipe_server.write(nil)
    end
  end
end