Class: Test::Unit::AutoRunner
- Inherits:
-
Object
- Object
- Test::Unit::AutoRunner
- Defined in:
- lib/test/unit/autorunner.rb
Defined Under Namespace
Classes: GCStressListener, StopOnFailureListener
Constant Summary collapse
- RUNNERS =
{}
- COLLECTORS =
{}
- ADDITIONAL_OPTIONS =
[]
- PREPARE_HOOKS =
[]
- @@default_runner =
nil- @@need_auto_run =
true
Instance Attribute Summary collapse
-
#base ⇒ Object
Returns the value of attribute base.
-
#collector ⇒ Object
writeonly
Sets the attribute collector.
-
#color_scheme ⇒ Object
Returns the value of attribute color_scheme.
-
#debug_on_failure ⇒ Object
writeonly
Sets the attribute debug_on_failure.
-
#default_test_paths ⇒ Object
Returns the value of attribute default_test_paths.
-
#exclude ⇒ Object
Returns the value of attribute exclude.
-
#filters ⇒ Object
Returns the value of attribute filters.
-
#gc_stress ⇒ Object
writeonly
Sets the attribute gc_stress.
-
#listeners ⇒ Object
Returns the value of attribute listeners.
-
#pattern ⇒ Object
Returns the value of attribute pattern.
-
#runner ⇒ Object
writeonly
Sets the attribute runner.
-
#runner_options ⇒ Object
readonly
Returns the value of attribute runner_options.
-
#stop_on_failure ⇒ Object
writeonly
Sets the attribute stop_on_failure.
-
#suite ⇒ Object
readonly
Returns the value of attribute suite.
-
#to_run ⇒ Object
Returns the value of attribute to_run.
-
#workdir ⇒ Object
Returns the value of attribute workdir.
Class Method Summary collapse
- .collector(id) ⇒ Object
- .default_runner ⇒ Object
- .default_runner=(id) ⇒ Object
- .need_auto_run=(need) ⇒ Object
- .need_auto_run? ⇒ Boolean
- .prepare(hook = nil, &block) ⇒ Object
- .register_collector(id, collector_builder = nil, &block) ⇒ Object
- .register_color_scheme(id, scheme) ⇒ Object
- .register_runner(id, runner_builder = nil, &block) ⇒ Object
- .run(force_standalone = false, default_dir = nil, argv = ARGV, &block) ⇒ Object
- .runner(id) ⇒ Object
- .setup_option(option_builder = nil, &block) ⇒ Object
- .standalone? ⇒ Boolean
Instance Method Summary collapse
- #debug_on_failure? ⇒ Boolean
-
#initialize(standalone) {|_self| ... } ⇒ AutoRunner
constructor
A new instance of AutoRunner.
- #keyword_display(keywords) ⇒ Object
- #load_config(file) ⇒ Object
- #load_plain_text_config(file) ⇒ Object
- #options ⇒ Object
- #prepare ⇒ Object
- #process_args(args = ARGV) ⇒ Object
- #run ⇒ Object
- #stop_on_failure? ⇒ Boolean
Constructor Details
#initialize(standalone) {|_self| ... } ⇒ AutoRunner
Returns a new instance of AutoRunner.
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 179 180 181 182 |
# File 'lib/test/unit/autorunner.rb', line 154 def initialize(standalone) @standalone = standalone @runner = default_runner @collector = default_collector @filters = [] @to_run = [] @default_test_paths = [] @color_scheme = ColorScheme.default = {} @default_arguments = [] @workdir = nil @listeners = [] @stop_on_failure = false @debug_on_failure = false @gc_stress = false @test_suite_runner_class = TestSuiteRunner @load_paths = [] config_file = "test-unit.yml" if File.exist?(config_file) load_config(config_file) else load_global_config end plain_text_config_file = ".test-unit" if File.exist?(plain_text_config_file) load_plain_text_config(plain_text_config_file) end yield(self) if block_given? end |
Instance Attribute Details
#base ⇒ Object
Returns the value of attribute base.
147 148 149 |
# File 'lib/test/unit/autorunner.rb', line 147 def base @base end |
#collector=(value) ⇒ Object (writeonly)
Sets the attribute collector
152 153 154 |
# File 'lib/test/unit/autorunner.rb', line 152 def collector=(value) @collector = value end |
#color_scheme ⇒ Object
Returns the value of attribute color_scheme.
148 149 150 |
# File 'lib/test/unit/autorunner.rb', line 148 def color_scheme @color_scheme end |
#debug_on_failure=(value) ⇒ Object (writeonly)
Sets the attribute debug_on_failure
150 151 152 |
# File 'lib/test/unit/autorunner.rb', line 150 def debug_on_failure=(value) @debug_on_failure = value end |
#default_test_paths ⇒ Object
Returns the value of attribute default_test_paths.
146 147 148 |
# File 'lib/test/unit/autorunner.rb', line 146 def default_test_paths @default_test_paths end |
#exclude ⇒ Object
Returns the value of attribute exclude.
147 148 149 |
# File 'lib/test/unit/autorunner.rb', line 147 def exclude @exclude end |
#filters ⇒ Object
Returns the value of attribute filters.
145 146 147 |
# File 'lib/test/unit/autorunner.rb', line 145 def filters @filters end |
#gc_stress=(value) ⇒ Object (writeonly)
Sets the attribute gc_stress
151 152 153 |
# File 'lib/test/unit/autorunner.rb', line 151 def gc_stress=(value) @gc_stress = value end |
#listeners ⇒ Object
Returns the value of attribute listeners.
148 149 150 |
# File 'lib/test/unit/autorunner.rb', line 148 def listeners @listeners end |
#pattern ⇒ Object
Returns the value of attribute pattern.
147 148 149 |
# File 'lib/test/unit/autorunner.rb', line 147 def pattern @pattern end |
#runner=(value) ⇒ Object (writeonly)
Sets the attribute runner
152 153 154 |
# File 'lib/test/unit/autorunner.rb', line 152 def runner=(value) @runner = value end |
#runner_options ⇒ Object (readonly)
Returns the value of attribute runner_options.
144 145 146 |
# File 'lib/test/unit/autorunner.rb', line 144 def end |
#stop_on_failure=(value) ⇒ Object (writeonly)
Sets the attribute stop_on_failure
149 150 151 |
# File 'lib/test/unit/autorunner.rb', line 149 def stop_on_failure=(value) @stop_on_failure = value end |
#suite ⇒ Object (readonly)
Returns the value of attribute suite.
144 145 146 |
# File 'lib/test/unit/autorunner.rb', line 144 def suite @suite end |
#to_run ⇒ Object
Returns the value of attribute to_run.
145 146 147 |
# File 'lib/test/unit/autorunner.rb', line 145 def to_run @to_run end |
#workdir ⇒ Object
Returns the value of attribute workdir.
147 148 149 |
# File 'lib/test/unit/autorunner.rb', line 147 def workdir @workdir end |
Class Method Details
.collector(id) ⇒ Object
46 47 48 |
# File 'lib/test/unit/autorunner.rb', line 46 def collector(id) COLLECTORS[id.to_s] end |
.default_runner ⇒ Object
32 33 34 |
# File 'lib/test/unit/autorunner.rb', line 32 def default_runner runner(@@default_runner) end |
.default_runner=(id) ⇒ Object
36 37 38 |
# File 'lib/test/unit/autorunner.rb', line 36 def default_runner=(id) @@default_runner = id end |
.need_auto_run=(need) ⇒ Object
85 86 87 |
# File 'lib/test/unit/autorunner.rb', line 85 def need_auto_run=(need) @@need_auto_run = need end |
.need_auto_run? ⇒ Boolean
81 82 83 |
# File 'lib/test/unit/autorunner.rb', line 81 def need_auto_run? @@need_auto_run end |
.prepare(hook = nil, &block) ⇒ Object
59 60 61 62 |
# File 'lib/test/unit/autorunner.rb', line 59 def prepare(hook=nil, &block) hook ||= Proc.new(&block) PREPARE_HOOKS << hook end |
.register_collector(id, collector_builder = nil, &block) ⇒ Object
40 41 42 43 44 |
# File 'lib/test/unit/autorunner.rb', line 40 def register_collector(id, collector_builder=nil, &block) collector_builder ||= Proc.new(&block) COLLECTORS[id] = collector_builder COLLECTORS[id.to_s] = collector_builder end |
.register_color_scheme(id, scheme) ⇒ Object
50 51 52 |
# File 'lib/test/unit/autorunner.rb', line 50 def register_color_scheme(id, scheme) ColorScheme[id] = scheme end |
.register_runner(id, runner_builder = nil, &block) ⇒ Object
21 22 23 24 25 |
# File 'lib/test/unit/autorunner.rb', line 21 def register_runner(id, runner_builder=nil, &block) runner_builder ||= Proc.new(&block) RUNNERS[id] = runner_builder RUNNERS[id.to_s] = runner_builder end |
.run(force_standalone = false, default_dir = nil, argv = ARGV, &block) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/test/unit/autorunner.rb', line 64 def run(force_standalone=false, default_dir=nil, argv=ARGV, &block) r = new(force_standalone || standalone?, &block) r.base = default_dir r.prepare r.process_args(argv) r.run end |
.runner(id) ⇒ Object
27 28 29 |
# File 'lib/test/unit/autorunner.rb', line 27 def runner(id) RUNNERS[id.to_s] end |
.setup_option(option_builder = nil, &block) ⇒ Object
54 55 56 57 |
# File 'lib/test/unit/autorunner.rb', line 54 def setup_option(option_builder=nil, &block) option_builder ||= Proc.new(&block) ADDITIONAL_OPTIONS << option_builder end |
.standalone? ⇒ Boolean
72 73 74 75 76 77 78 |
# File 'lib/test/unit/autorunner.rb', line 72 def standalone? return false unless("-e" == $0) ObjectSpace.each_object(Class) do |klass| return false if(klass < TestCase) end true end |
Instance Method Details
#debug_on_failure? ⇒ Boolean
188 189 190 |
# File 'lib/test/unit/autorunner.rb', line 188 def debug_on_failure? @debug_on_failure end |
#keyword_display(keywords) ⇒ Object
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 |
# File 'lib/test/unit/autorunner.rb', line 470 def keyword_display(keywords) keywords = keywords.collect do |keyword, _| keyword.to_s end.uniq.sort i = 0 keywords.collect do |keyword| if (i > 0 and keyword[0] == keywords[i - 1][0]) or ((i < keywords.size - 1) and (keyword[0] == keywords[i + 1][0])) n = 2 else n = 1 end i += 1 keyword.sub(/^(.{#{n}})([A-Za-z-]+)(?=\w*$)/, '\\1[\\2]') end.join(", ") end |
#load_config(file) ⇒ Object
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 |
# File 'lib/test/unit/autorunner.rb', line 513 def load_config(file) require "yaml" config = YAML.load(File.read(file)) runner_name = config["runner"] @runner = self.class.runner(runner_name) || @runner @collector = self.class.collector(config["collector"]) || @collector (config["color_schemes"] || {}).each do |name, | ColorScheme[name] = end = {} (config["#{runner_name}_options"] || {}).each do |key, value| key = key.to_sym value = ColorScheme[value] if key == :color_scheme if key == :arguments @default_arguments.concat(value.split) else [key] = value end end = .merge() end |
#load_plain_text_config(file) ⇒ Object
535 536 537 538 539 540 541 542 |
# File 'lib/test/unit/autorunner.rb', line 535 def load_plain_text_config(file) require "shellwords" File.readlines(file, chomp: true).each do |line| next if line.empty? args = Shellwords.shellsplit(line) @default_arguments.concat(args) end end |
#options ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 221 222 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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 |
# File 'lib/test/unit/autorunner.rb', line 210 def ||= OptionParser.new do |o| o.version = VERSION o. = "Test::Unit automatic runner." o. += "\nUsage: #{$0} [options] [-- untouched arguments]" o.on("-r", "--runner=RUNNER", RUNNERS, "Use the given RUNNER.", "(" + keyword_display(RUNNERS) + ")") do |r| @runner = r end o.on("--collector=COLLECTOR", COLLECTORS, "Use the given COLLECTOR.", "(" + keyword_display(COLLECTORS) + ")") do |collector| @collector = collector end if (@standalone) o.on("-b", "--basedir=DIR", "Base directory of test suites.") do |b| @base = b end o.on("-w", "--workdir=DIR", "Working directory to run tests.") do |w| @workdir = w end o.on("--default-test-path=PATH", "Add PATH to the default test paths.", "The PATH is used when user doesn't specify any test path.", "You can specify this option multiple times.") do |path| @default_test_paths << path end o.on("-a", "--add=TORUN", Array, "Add TORUN to the list of things to run;", "can be a file or a directory.") do |paths| paths.each do |path| add_test_path(path) end end @pattern = [] o.on("-p", "--pattern=PATTERN", Regexp, "Match files to collect against PATTERN.") do |e| @pattern << e end @exclude = [] o.on("-x", "--exclude=PATTERN", Regexp, "Ignore files to collect against PATTERN.") do |e| @exclude << e end end o.on("-n", "--name=NAME", String, "Runs tests matching NAME.", "Use '/PATTERN/' for NAME to use regular expression.", "Regular expression accepts options.", "Example: '/taRget/i' matches 'target' and 'TARGET'") do |name| name = prepare_name(name) @filters << lambda do |test| match_test_name(test, name) end end o.on("--ignore-name=NAME", String, "Ignores tests matching NAME.", "Use '/PATTERN/' for NAME to use regular expression.", "Regular expression accepts options.", "Example: '/taRget/i' matches 'target' and 'TARGET'") do |name| name = prepare_name(name) @filters << lambda do |test| not match_test_name(test, name) end end o.on("-t", "--testcase=TESTCASE", String, "Runs tests in TestCases matching TESTCASE.", "Use '/PATTERN/' for TESTCASE to use regular expression.", "Regular expression accepts options.", "Example: '/taRget/i' matches 'target' and 'TARGET'") do |name| name = prepare_name(name) @filters << lambda do |test| match_test_case_name(test, name) end end o.on("--ignore-testcase=TESTCASE", String, "Ignores tests in TestCases matching TESTCASE.", "Use '/PATTERN/' for TESTCASE to use regular expression.", "Regular expression accepts options.", "Example: '/taRget/i' matches 'target' and 'TARGET'") do |name| name = prepare_name(name) @filters << lambda do |test| not match_test_case_name(test, name) end end o.on("--location=LOCATION", String, "Runs tests that defined in LOCATION.", "LOCATION is one of PATH:LINE, PATH or LINE.") do |location| case location when /\A(\d+)\z/ path = nil line = $1.to_i when /:(\d+)\z/ path = $PREMATCH line = $1.to_i else path = location line = nil end add_location_filter(path, line) end o.on("--attribute=EXPRESSION", String, "Runs tests that matches EXPRESSION.", "EXPRESSION is evaluated as Ruby's expression.", "Test attribute name can be used with no receiver in EXPRESSION.", "EXPRESSION examples:", " !slow", " tag == 'important' and !slow") do |expression| @filters << lambda do |test| matcher = AttributeMatcher.new(test) matcher.match?(expression) end end priority_filter = Proc.new do |test| if @filters == [priority_filter] Priority::Checker.new(test).need_to_run? else nil end end o.on("--[no-]priority-mode", "Runs some tests based on their priority.") do |priority_mode| if priority_mode Priority.enable @filters |= [priority_filter] else Priority.disable @filters -= [priority_filter] end end o.on("--default-priority=PRIORITY", Priority.available_values, "Uses PRIORITY as default priority", "(#{keyword_display(Priority.available_values)})") do |priority| Priority.default = priority end o.on("-I", "--load-path=DIR[#{File::PATH_SEPARATOR}DIR...]", "Appends directory list to $LOAD_PATH.") do |dirs| load_paths = dirs.split(File::PATH_SEPARATOR) $LOAD_PATH.concat(load_paths) @load_paths.concat(load_paths) end color_schemes = ColorScheme.all o.on("--color-scheme=SCHEME", color_schemes, "Use SCHEME as color scheme.", "(#{keyword_display(color_schemes)})") do |scheme| @color_scheme = scheme end o.on("--config=FILE", "Use YAML format FILE content as configuration file.") do |file| load_config(file) end o.on("--order=ORDER", TestCase::AVAILABLE_ORDERS, "Run tests in a test case in ORDER order.", "(#{keyword_display(TestCase::AVAILABLE_ORDERS)})") do |order| TestCase.test_order = order end = Test::Unit::Assertions::AssertionMessage o.on("--max-diff-target-string-size=SIZE", Integer, "Shows diff if both expected result string size and " + "actual result string size are " + "less than or equal SIZE in bytes.", "(#{assertion_message_class.max_diff_target_string_size})") do |size| .max_diff_target_string_size = size end o.on("--[no-]stop-on-failure", "Stops immediately on the first non success test", "(#{@stop_on_failure})") do |boolean| @stop_on_failure = boolean end o.on("--[no-]debug-on-failure", "Run debugger if available on failure", "(#{AssertionFailedError.debug_on_failure?})") do |boolean| AssertionFailedError.debug_on_failure = boolean end o.on("--[no-]gc-stress", "Enable GC.stress only while each test is running", "(#{@gc_stress})") do |boolean| @gc_stress = boolean end = [ :thread, :process, ] o.on("--[no-]parallel=[thread]", , "Runs tests in parallel", "(#{parallel_options.first})") do |parallel| case parallel when nil, :thread @test_suite_runner_class = TestSuiteThreadRunner when :process @test_suite_runner_class = TestSuiteProcessRunner else @test_suite_runner_class = TestSuiteRunner end end o.on("--n-workers=N", Integer, "The number of parallelism", "(#{TestSuiteRunner.n_workers})") do |n| TestSuiteRunner.n_workers = n end ADDITIONAL_OPTIONS.each do |option_builder| option_builder.call(self, o) end o.on("--", "Stop processing options so that the", "remaining options will be passed to the", "test."){o.terminate} o.on("-h", "--help", "Display this help."){puts o; exit} o.on_tail o.on_tail("Deprecated options:") o.on_tail("--console", "Console runner (use --runner).") do warn("Deprecated option (--console).") @runner = self.class.runner(:console) end if RUNNERS[:fox] o.on_tail("--fox", "Fox runner (use --runner).") do warn("Deprecated option (--fox).") @runner = self.class.runner(:fox) end end o.on_tail end end |
#prepare ⇒ Object
192 193 194 195 196 |
# File 'lib/test/unit/autorunner.rb', line 192 def prepare PREPARE_HOOKS.each do |handler| handler.call(self) end end |
#process_args(args = ARGV) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/test/unit/autorunner.rb', line 198 def process_args(args=ARGV) begin args.unshift(*@default_arguments) .order!(args) {|arg| add_test_path(arg)} rescue OptionParser::ParseError => e puts e puts exit(false) end not @to_run.empty? end |
#run ⇒ Object
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 |
# File 'lib/test/unit/autorunner.rb', line 488 def run self.class.need_auto_run = false suite = @collector[self] return false if suite.nil? return true if suite.empty? runner = @runner[self] return false if runner.nil? [:color_scheme] ||= @color_scheme [:listeners] ||= [] [:listeners].concat(@listeners) if @stop_on_failure [:listeners] << StopOnFailureListener.new end if @gc_stress [:listeners] << GCStressListener.new end [:test_suite_runner_class] = @test_suite_runner_class [:load_paths] = @load_paths [:base_directory] = @base [:test_paths] = @to_run change_work_directory do runner.run(suite, ).passed? end end |
#stop_on_failure? ⇒ Boolean
184 185 186 |
# File 'lib/test/unit/autorunner.rb', line 184 def stop_on_failure? @stop_on_failure end |