Class: TConsole::Server
- Inherits:
-
Object
- Object
- TConsole::Server
- Defined in:
- lib/tconsole/server.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#last_result ⇒ Object
Returns the value of attribute last_result.
Instance Method Summary collapse
-
#autocomplete(text) ⇒ Object
Returns an array of possible completions based on the available element data.
-
#handle(message) ⇒ Object
Processes the message sent from the console.
-
#initialize(config) ⇒ Server
constructor
A new instance of Server.
- #load_environment ⇒ Object
-
#preload_test_ids ⇒ Object
Preloads our autocomplete cache.
-
#run_all_tests(match_patterns = nil) ⇒ Object
Runs all tests against the match patterns given.
- #run_failed ⇒ Object
-
#run_file_set(set) ⇒ Object
Runs a file set out of the config.
-
#run_in_fork(&block) ⇒ Object
Runs the given code in a block and returns the result of the code in the block.
- #run_info ⇒ Object
-
#run_tests(globs, match_patterns, message = "Running tests...") ⇒ Object
Loads the files that match globs and then executes tests against them.
- #set(key, value) ⇒ Object
-
#shell(command) ⇒ Object
Internal: Runs the given command in the shell.
- #show_performance(limit = nil) ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(config) ⇒ Server
Returns a new instance of Server.
5 6 7 8 |
# File 'lib/tconsole/server.rb', line 5 def initialize(config) self.config = config self.last_result = TConsole::TestResult.new end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
3 4 5 |
# File 'lib/tconsole/server.rb', line 3 def config @config end |
#last_result ⇒ Object
Returns the value of attribute last_result.
3 4 5 |
# File 'lib/tconsole/server.rb', line 3 def last_result @last_result end |
Instance Method Details
#autocomplete(text) ⇒ Object
Returns an array of possible completions based on the available element data
65 66 67 |
# File 'lib/tconsole/server.rb', line 65 def autocomplete(text) config.cached_elements.keys.grep(/^#{Regexp.escape(text)}/) end |
#handle(message) ⇒ Object
Processes the message sent from the console
11 12 13 14 15 16 |
# File 'lib/tconsole/server.rb', line 11 def handle() action = [:action] args = [:args] send(action, *args) end |
#load_environment ⇒ Object
22 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 60 61 62 |
# File 'lib/tconsole/server.rb', line 22 def load_environment result = false time = Benchmark.realtime do puts puts "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 puts "Error - Loading your environment failed: #{e.}" if config.trace? puts puts " #{e.backtrace.join("\n ")}" end return false end preload_test_ids end puts "Environment loaded in #{"%0.6f" % time}s." puts result end |
#preload_test_ids ⇒ Object
Preloads our autocomplete cache
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/tconsole/server.rb', line 181 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.(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
198 199 200 |
# File 'lib/tconsole/server.rb', line 198 def run_all_tests(match_patterns = nil) run_tests(config.file_sets["all"], match_patterns) end |
#run_failed ⇒ Object
207 208 209 210 211 212 213 214 |
# File 'lib/tconsole/server.rb', line 207 def run_failed if last_result.failures.empty? puts "No tests failed in your last run, or you haven't run any tests in this session yet." puts 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
203 204 205 |
# File 'lib/tconsole/server.rb', line 203 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.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/tconsole/server.rb', line 71 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 config.trace("Reading result from fork.") Marshal.load(response.unpack("m")[0]) rescue => e config.trace("Problem reading result from fork. Returning nil.") config.trace(e.) nil end end |
#run_info ⇒ Object
216 217 218 219 220 221 |
# File 'lib/tconsole/server.rb', line 216 def run_info puts "Defined Constants:" puts Module.constants.sort.join("\n") puts puts 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.
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 176 177 178 |
# File 'lib/tconsole/server.rb', line 100 def run_tests(globs, match_patterns, = "Running tests...") time = Benchmark.realtime do puts puts paths = [] globs.each do |glob| paths.concat(Dir.glob(glob)) end if paths.length == 0 puts ::Term::ANSIColor.yellow("No test files match your requested test set: #{globs.join(",")}.") puts ::Term::ANSIColor.yellow("Skipping execution.") return nil end self.last_result = run_in_fork do paths.each do |path| config.trace("Requested path `#{path}` doesn't exist.") unless File.exist?(path) require File.(path) end config.trace("Running before_test_run callback") config.before_test_run! config.trace("Completed before_test_run callback") result = nil if defined?(::MiniTest) config.trace("Detected minitest.") require File.join(File.dirname(__FILE__), "minitest_handler") config.trace("Running tests.") runner = MiniTestHandler.setup(match_patterns, config) # Handle trapping interrupts trap("SIGINT") do puts puts "Trapped interrupt. Halting tests." runner.interrupted = true end runner.run result = runner.results # Make sure minitest doesn't run automatically MiniTestHandler.patch_minitest config.trace("Finished running tests.") if runner.interrupted puts ::Term::ANSIColor.red("Test run was interrupted.") end elsif defined?(::Test::Unit) puts "Sorry, but tconsole doesn't support Test::Unit yet" elsif defined?(::RSpec) puts "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 puts puts "Tests ran in #{"%0.6f" % time}s. Finished at #{Time.now.strftime('%Y-%m-%d %l:%M:%S %p')}." puts end |
#set(key, value) ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/tconsole/server.rb', line 252 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 puts ::Term::ANSIColor.green + "Fail Fast is now #{config.fail_fast ? "on" : "off"}" + ::Term::ANSIColor.reset puts else puts ::Term::ANSIColor.green + "Fail fast is currently #{config.fail_fast ? "on" : "off"}" + ::Term::ANSIColor.reset puts end else puts ::Term::ANSIColor.yellow + "I don't know how to set `#{key}`." + ::Term::ANSIColor.reset + " Usage: set {key} {value}" puts end end |
#shell(command) ⇒ Object
Internal: Runs the given command in the shell.
command - the command to execute
Examples
shell("ls -la")
281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/tconsole/server.rb', line 281 def shell(command) system(command) result = $? puts if result.exitstatus == 0 puts ::Term::ANSIColor.green + "Command exited with status code: 0" + ::Term::ANSIColor.reset else puts ::Term::ANSIColor.red + "Command exited with status code: #{result.exitstatus}" + ::Term::ANSIColor.reset end end |
#show_performance(limit = nil) ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/tconsole/server.rb', line 223 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] } puts puts "Timings from last run:" puts if sorted_timings.length == 0 puts ::Term::ANSIColor.red + "No timing data available. Be sure you've run some tests." + ::Term::ANSIColor.reset else sorted_timings.reverse[0, limit].each do |timing| output = "#{"%0.6f" % timing[:time]}s #{timing[:name]}" if timing[:time] > 1 print ::Term::ANSIColor.red, output, ::Term::ANSIColor.reset else print ::Term::ANSIColor.green, output, ::Term::ANSIColor.reset end print ::Term::ANSIColor.magenta, " #{last_result.elements[timing[:name]]}", ::Term::ANSIColor.reset, "\n" end end puts end |
#stop ⇒ Object
18 19 20 |
# File 'lib/tconsole/server.rb', line 18 def stop Kernel.exit(0) end |