Class: TConsole::Server

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, reporter) ⇒ Server

Returns a new instance of Server.



5
6
7
8
9
# File 'lib/tconsole/server.rb', line 5

def initialize(config, reporter)
  self.config = config
  self.reporter = reporter
  self.last_result = TConsole::TestResult.new
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



3
4
5
# File 'lib/tconsole/server.rb', line 3

def config
  @config
end

#last_resultObject

Returns the value of attribute last_result.



3
4
5
# File 'lib/tconsole/server.rb', line 3

def last_result
  @last_result
end

#reporterObject

Returns the value of attribute reporter.



3
4
5
# File 'lib/tconsole/server.rb', line 3

def reporter
  @reporter
end

Instance Method Details

#autocomplete(text) ⇒ Object

Returns an array of possible completions based on the available element data



62
63
64
# File 'lib/tconsole/server.rb', line 62

def autocomplete(text)
  config.cached_elements.keys.grep(/^#{Regexp.escape(text)}/)
end

#handle(message) ⇒ Object

Processes the message sent from the console



12
13
14
15
16
17
# File 'lib/tconsole/server.rb', line 12

def handle(message)
  action = message[:action]
  args = message[:args]

  send(action, *args)
end

#load_environmentObject



23
24
25
26
27
28
29
30
31
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
# File 'lib/tconsole/server.rb', line 23

def load_environment
  result = false

  time = Benchmark.realtime do
    reporter.info
    reporter.info("Loading environment...")

    begin
      # Append our include paths
      config.include_paths.each do |include_path|
        $:.unshift(include_path)
      end

      config.before_load!

      # Load our preload files
      config.preload_paths.each do |preload_path|
        require preload_path
      end

      config.after_load!

      result = true
    rescue Exception => e
      reporter.error("Error - Loading your environment failed: #{e.message}")
      reporter.trace_backtrace(e)
      return false
    end

    preload_test_ids
  end

  reporter.info("Environment loaded in #{"%0.6f" % time}s.")
  reporter.info

  result
end

#preload_test_idsObject

Preloads our autocomplete cache



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/tconsole/server.rb', line 178

def preload_test_ids
  result = run_in_fork do
    paths = []
    config.file_sets["all"].each do |glob|
      paths.concat(Dir.glob(glob))
    end

    paths.each { |path| require File.expand_path(path) }

    require File.join(File.dirname(__FILE__), "minitest_handler")
    MiniTestHandler.preload_elements
  end

  config.cache_test_ids(result) unless result.nil?
end

#run_all_tests(match_patterns = nil) ⇒ Object

Runs all tests against the match patterns given



195
196
197
# File 'lib/tconsole/server.rb', line 195

def run_all_tests(match_patterns = nil)
  run_tests(config.file_sets["all"], match_patterns)
end

#run_failedObject



204
205
206
207
208
209
210
211
# File 'lib/tconsole/server.rb', line 204

def run_failed
  if last_result.failures.empty?
    reporter.info("No tests failed in your last run, or you haven't run any tests in this session yet.")
    reporter.info
  else
    run_tests(config.file_sets["all"], last_result.failures)
  end
end

#run_file_set(set) ⇒ Object

Runs a file set out of the config



200
201
202
# File 'lib/tconsole/server.rb', line 200

def run_file_set(set)
  run_tests(config.file_sets[set], nil)
end

#run_in_fork(&block) ⇒ Object

Runs the given code in a block and returns the result of the code in the block. The block’s result needs to be marshallable. Otherwise, nil is returned.



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

def run_in_fork(&block)
 # Pipe for communicating with child so we can get its results back
  read, write = IO.pipe

  pid = fork do
    read.close

    result = block.call

    write.puts([Marshal.dump(result)].pack("m0"))
  end

  write.close
  response = read.read
  read.close
  Process.wait(pid)

  begin
    reporter.trace("Reading result from fork.")
    Marshal.load(response.unpack("m")[0])
  rescue => e
    reporter.trace("Problem reading result from fork. Returning nil.")
    reporter.trace(e.message)
    nil
  end
end

#run_infoObject



213
214
215
216
217
218
# File 'lib/tconsole/server.rb', line 213

def run_info
  reporter.info("Defined Constants:")
  reporter.info(Module.constants.sort.join("\n"))
  reporter.info
  reporter.info
end

#run_tests(globs, match_patterns, message = "Running tests...") ⇒ Object

Loads the files that match globs and then executes tests against them. Limit tests with class names, method names, and test ids using match_patterns.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/tconsole/server.rb', line 97

def run_tests(globs, match_patterns, message = "Running tests...")
  time = Benchmark.realtime do
    reporter.info(message)
    reporter.info

    paths = []
    globs.each do |glob|
      paths.concat(Dir.glob(glob))
    end

    if paths.length == 0
      reporter.warn("No test files match your requested test set: #{globs.join(",")}.")
      reporter.warn("Skipping execution.")
      return nil
    end

    self.last_result = run_in_fork do

      paths.each do |path|
        reporter.trace("Requested path `#{path}` doesn't exist.") unless File.exist?(path)
        require File.expand_path(path)
      end

      reporter.trace("Running before_test_run callback")
      config.before_test_run!
      reporter.trace("Completed before_test_run callback")

      result = nil
      if defined?(::MiniTest)
        reporter.trace("Detected minitest.")
        require File.join(File.dirname(__FILE__), "minitest_handler")

        reporter.trace("Running tests.")
        runner = MiniTestHandler.setup(match_patterns, config)

        # Handle trapping interrupts
        trap("SIGINT") do
          reporter.warn
          reporter.warn("Trapped interrupt. Halting tests.")

          runner.interrupted = true
        end

        runner.run

        result = runner.results

        # Make sure minitest doesn't run automatically
        MiniTestHandler.patch_minitest

        reporter.trace("Finished running tests.")

        if runner.interrupted
          reporter.error("Test run was interrupted.")
        end

      elsif defined?(::Test::Unit)
        reporter.error("Sorry, but tconsole doesn't support Test::Unit yet")
      elsif defined?(::RSpec)
        reporter.error("Sorry, but tconsole doesn't support RSpec yet")
      end

      result
    end

    if self.last_result == nil
      # Just in case anything crazy goes down with marshalling
      self.last_result = TConsole::TestResult.new
    end

    config.cache_test_ids(self.last_result)

    true
  end

  reporter.info
  reporter.info("Tests ran in #{"%0.6f" % time}s. Finished at #{Time.now.strftime('%Y-%m-%d %l:%M:%S %p')}.")
  reporter.info
end

#set(key, value) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/tconsole/server.rb', line 242

def set(key, value)
  if key == "fast"
    if !value.nil?
      value.downcase!
      if ["on", "true", "yes"].include?(value)
        config.fail_fast = true
      else
        config.fail_fast = false
      end

      reporter.exclaim("Fail Fast is now #{config.fail_fast ? "on" : "off"}")
      reporter.exclaim
    else
      reporter.exclaim("Fail fast is currently #{config.fail_fast ? "on" : "off"}")
      reporter.exclaim
    end
  else
    reporter.warn("I don't know how to set `#{key}`.")
    reporter.info("Usage: set {key} {value}")
    reporter.warn
  end
end

#show_performance(limit = nil) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/tconsole/server.rb', line 220

def show_performance(limit = nil)

  limit = limit.to_i
  limit = last_result.timings.length if limit == 0

  sorted_timings = last_result.timings.sort_by { |timing| timing[:time] }

  reporter.info
  reporter.info("Timings from last run:")
  reporter.info

  if sorted_timings.length == 0
    reporter.error("No timing data available. Be sure you've run some tests.")
  else
    sorted_timings.reverse[0, limit].each do |timing|
      reporter.timing(timing, last_result.elements[timing[:name]])
    end
  end

  reporter.info
end

#stopObject



19
20
21
# File 'lib/tconsole/server.rb', line 19

def stop
  Kernel.exit(0)
end