Class: Autotest
- Inherits:
-
Object
- Object
- Autotest
- Defined in:
- lib/autotest.rb,
lib/autotest/notify.rb
Overview
Autotest continuously scans the files in your project for changes and runs the appropriate tests. Test failures are run until they have all passed. Then the full test suite is run to ensure that nothing else was inadvertantly broken.
If you want Autotest to start over from the top, hit ^C once. If you want Autotest to quit, hit ^C twice.
Rails:
The autotest command will automatically discover a Rails directory by looking for config/environment.rb. When Rails is discovered, autotest uses RailsAutotest to perform file mappings and other work. See RailsAutotest for details.
Plugins:
Plugins are available by creating a .autotest file either in your project root or in your home directory. You can then write event handlers in the form of:
Autotest.add_hook hook_name { |autotest| ... }
The available hooks are listed in ALL_HOOKS
.
See example_dot_autotest.rb for more details.
If a hook returns a true value, it signals to autotest that the hook was handled and should not continue executing hooks.
Naming:
Autotest uses a simple naming scheme to figure out how to map implementation files to test files following the Test::Unit naming scheme.
-
Test files must be stored in test/
-
Test files names must start with test_
-
Test class names must start with Test
-
Implementation files must be stored in lib/
-
Implementation files must match up with a test file named test_.*implementation.rb
Strategy:
-
Find all files and associate them from impl <-> test.
-
Run all tests.
-
Scan for failures.
-
Detect changes in ANY (ruby?. file, rerun all failures + changed files.
-
Until 0 defects, goto 3.
-
When 0 defects, goto 2.
Defined Under Namespace
Modules: AutoUpdate, Bundler, Once, RCov, Restart, Timestamp Classes: Notify
Constant Summary collapse
- VERSION =
File.read( File.join(File.dirname(__FILE__),'..','VERSION') ).strip
- RUBY19 =
defined? Encoding
- T0 =
Time.at 0
- ALL_HOOKS =
[ :all_good, :died, :green, :initialize, :interrupt, :quit, :ran_command, :red, :reset, :run_command, :updated, :waiting ]
- HOOKS =
unfound keys are []
Hash.new { |h,k| h[k] = [] }
- WINDOZE =
- SEP =
WINDOZE ? '&' : ';'
- @@discoveries =
[]
Instance Attribute Summary collapse
-
#completed_re ⇒ Object
Returns the value of attribute completed_re.
-
#extra_class_map ⇒ Object
Returns the value of attribute extra_class_map.
-
#extra_files ⇒ Object
Returns the value of attribute extra_files.
-
#failed_results_re ⇒ Object
Returns the value of attribute failed_results_re.
-
#files_to_test ⇒ Object
Returns the value of attribute files_to_test.
-
#find_directories ⇒ Object
Returns the value of attribute find_directories.
-
#find_order ⇒ Object
Returns the value of attribute find_order.
-
#interrupted ⇒ Object
Returns the value of attribute interrupted.
-
#known_files ⇒ Object
Lazy accessor for the known_files hash.
-
#last_mtime ⇒ Object
Returns the value of attribute last_mtime.
-
#latest_results ⇒ Object
Returns the value of attribute latest_results.
-
#libs ⇒ Object
Returns the value of attribute libs.
-
#order ⇒ Object
Returns the value of attribute order.
-
#output ⇒ Object
Returns the value of attribute output.
-
#prefix ⇒ Object
Returns the value of attribute prefix.
-
#results ⇒ Object
Returns the value of attribute results.
-
#sleep ⇒ Object
Returns the value of attribute sleep.
-
#tainted ⇒ Object
(also: #tainted?)
Returns the value of attribute tainted.
-
#testlib ⇒ Object
Returns the value of attribute testlib.
-
#unit_diff ⇒ Object
Returns the value of attribute unit_diff.
-
#wants_to_quit ⇒ Object
Returns the value of attribute wants_to_quit.
Class Method Summary collapse
-
.add_discovery(&proc) ⇒ Object
Add a proc to the collection of discovery procs.
-
.add_hook(name, &block) ⇒ Object
Add the supplied block to the available hooks, with the given name.
-
.autodiscover ⇒ Object
Automatically find all potential autotest runner styles by searching your loadpath, vendor/plugins, and rubygems for “autotest/discover.rb”.
- .options ⇒ Object
- .parse_options(args = ARGV) ⇒ Object
-
.run ⇒ Object
Initialize and run the system.
-
.runner ⇒ Object
Calculates the autotest runner to use to run the tests.
Instance Method Summary collapse
-
#add_exception(regexp) ⇒ Object
Adds
regexp
to the list of exceptions for find_file. -
#add_mapping(regexp, prepend = false, &proc) ⇒ Object
Adds a file mapping, optionally prepending the mapping to the front of the list if
prepend
is true. -
#add_sigint_handler ⇒ Object
Installs a sigint handler.
- #add_test_unit_mappings ⇒ Object
-
#all_good ⇒ Object
If there are no files left to test (because they’ve all passed), then all is good.
-
#clear_exceptions ⇒ Object
Clears the list of exceptions for find_file.
-
#clear_mappings ⇒ Object
Clears all file mappings.
-
#consolidate_failures(failed) ⇒ Object
Returns a hash mapping a file name to the known failures for that file.
- #escape_filenames(classes) ⇒ Object
-
#exceptions ⇒ Object
Return a compiled regexp of exceptions for find_files or nil if no filtering should take place.
-
#files_matching(regexp) ⇒ Object
Returns all known files in the codebase matching
regexp
. -
#find_files ⇒ Object
Find the files to process, ignoring temporary files, source configuration management files, etc., and return a Hash mapping filename to modification time.
-
#find_files_to_test(files = find_files) ⇒ Object
Find the files which have been modified, update the recorded timestamps, and use this to update the files to test.
-
#get_to_green ⇒ Object
Keep running the tests after a change, until all pass.
-
#handle_results(results) ⇒ Object
Check results for failures, set the “bar” to red or green, and if there are failures record this.
-
#hook(name, *args) ⇒ Object
Hooks: Call the event hook named
name
, passing in optional args depending on the hook itself. -
#initialize ⇒ Autotest
constructor
Initialize the instance and then load the user’s .autotest file, if any.
- #load_custom_extensions ⇒ Object
-
#make_test_cmd(files_to_test) ⇒ Object
Generate the commands to test the supplied files.
- #new_hash_of_arrays ⇒ Object
- #options ⇒ Object
-
#path_to_classname(s) ⇒ Object
Convert a path in a string, s, into a class name, changing underscores to CamelCase, etc.
-
#remove_exception(regexp) ⇒ Object
Removes
regexp
to the list of exceptions for find_file. -
#remove_mapping(regexp) ⇒ Object
Removed a file mapping matching
regexp
. - #reorder(files_to_test) ⇒ Object
-
#rerun_all_tests ⇒ Object
Rerun the tests from cold (reset state).
-
#reset ⇒ Object
Clear all state information about test failures and whether interrupts will kill autotest.
-
#ruby ⇒ Object
Determine and return the path of the ruby executable.
- #ruby_cmd ⇒ Object
-
#run ⇒ Object
Repeatedly run failed tests, then all tests, then wait for changes and carry on until killed.
-
#run_tests ⇒ Object
Look for files to test then run the tests and handle the results.
-
#test_files_for(filename) ⇒ Object
Return the name of the file with the tests for filename by finding a
test_mapping
that matches the file and executing the mapping’s proc. -
#wait_for_changes ⇒ Object
Sleep then look for files to test, until there are some.
Constructor Details
#initialize ⇒ Autotest
Initialize the instance and then load the user’s .autotest file, if any.
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 |
# File 'lib/autotest.rb', line 266 def initialize # these two are set directly because they're wrapped with # add/remove/clear accessor methods @exception_list = [] @test_mappings = [] self.completed_re = /\d+ tests, \d+ assertions, \d+ failures, \d+ errors(, \d+ skips)?/ self.extra_class_map = {} self.extra_files = [] self.failed_results_re = /^\s+\d+\) (?:Failure|Error):\n(.*?)\((.*?)\)/ self.files_to_test = new_hash_of_arrays self.find_order = [] self.known_files = nil self.libs = %w[. lib test].join(File::PATH_SEPARATOR) self.order = :random self.output = $stderr self.prefix = nil self.sleep = 1 self.testlib = "test/unit" self.find_directories = ['.'] self.unit_diff = "ruby #{File.("#{File.dirname(__FILE__)}/../bin/unit_diff")} -u" # add ruby to also work for windows self.latest_results = nil add_test_unit_mappings load_custom_extensions end |
Instance Attribute Details
#completed_re ⇒ Object
Returns the value of attribute completed_re.
240 241 242 |
# File 'lib/autotest.rb', line 240 def completed_re @completed_re end |
#extra_class_map ⇒ Object
Returns the value of attribute extra_class_map.
240 241 242 |
# File 'lib/autotest.rb', line 240 def extra_class_map @extra_class_map end |
#extra_files ⇒ Object
Returns the value of attribute extra_files.
240 241 242 |
# File 'lib/autotest.rb', line 240 def extra_files @extra_files end |
#failed_results_re ⇒ Object
Returns the value of attribute failed_results_re.
240 241 242 |
# File 'lib/autotest.rb', line 240 def failed_results_re @failed_results_re end |
#files_to_test ⇒ Object
Returns the value of attribute files_to_test.
240 241 242 |
# File 'lib/autotest.rb', line 240 def files_to_test @files_to_test end |
#find_directories ⇒ Object
Returns the value of attribute find_directories.
240 241 242 |
# File 'lib/autotest.rb', line 240 def find_directories @find_directories end |
#find_order ⇒ Object
Returns the value of attribute find_order.
240 241 242 |
# File 'lib/autotest.rb', line 240 def find_order @find_order end |
#interrupted ⇒ Object
Returns the value of attribute interrupted.
240 241 242 |
# File 'lib/autotest.rb', line 240 def interrupted @interrupted end |
#known_files ⇒ Object
Lazy accessor for the known_files hash.
548 549 550 551 552 553 |
# File 'lib/autotest.rb', line 548 def known_files unless @known_files then @known_files = Hash[*find_order.map { |f| [f, true] }.flatten] end @known_files end |
#last_mtime ⇒ Object
Returns the value of attribute last_mtime.
240 241 242 |
# File 'lib/autotest.rb', line 240 def last_mtime @last_mtime end |
#latest_results ⇒ Object
Returns the value of attribute latest_results.
240 241 242 |
# File 'lib/autotest.rb', line 240 def latest_results @latest_results end |
#libs ⇒ Object
Returns the value of attribute libs.
240 241 242 |
# File 'lib/autotest.rb', line 240 def libs @libs end |
#order ⇒ Object
Returns the value of attribute order.
240 241 242 |
# File 'lib/autotest.rb', line 240 def order @order end |
#output ⇒ Object
Returns the value of attribute output.
240 241 242 |
# File 'lib/autotest.rb', line 240 def output @output end |
#prefix ⇒ Object
Returns the value of attribute prefix.
240 241 242 |
# File 'lib/autotest.rb', line 240 def prefix @prefix end |
#results ⇒ Object
Returns the value of attribute results.
240 241 242 |
# File 'lib/autotest.rb', line 240 def results @results end |
#sleep ⇒ Object
Returns the value of attribute sleep.
240 241 242 |
# File 'lib/autotest.rb', line 240 def sleep @sleep end |
#tainted ⇒ Object Also known as: tainted?
Returns the value of attribute tainted.
240 241 242 |
# File 'lib/autotest.rb', line 240 def tainted @tainted end |
#testlib ⇒ Object
Returns the value of attribute testlib.
240 241 242 |
# File 'lib/autotest.rb', line 240 def testlib @testlib end |
#unit_diff ⇒ Object
Returns the value of attribute unit_diff.
240 241 242 |
# File 'lib/autotest.rb', line 240 def unit_diff @unit_diff end |
#wants_to_quit ⇒ Object
Returns the value of attribute wants_to_quit.
240 241 242 |
# File 'lib/autotest.rb', line 240 def wants_to_quit @wants_to_quit end |
Class Method Details
.add_discovery(&proc) ⇒ Object
Add a proc to the collection of discovery procs. See autodiscover
.
186 187 188 |
# File 'lib/autotest.rb', line 186 def self.add_discovery &proc @@discoveries << proc end |
.add_hook(name, &block) ⇒ Object
Add the supplied block to the available hooks, with the given name.
804 805 806 |
# File 'lib/autotest.rb', line 804 def self.add_hook(name, &block) HOOKS[name] << block end |
.autodiscover ⇒ Object
Automatically find all potential autotest runner styles by searching your loadpath, vendor/plugins, and rubygems for “autotest/discover.rb”. If found, that file is loaded and it should register discovery procs with autotest using add_discovery
. That proc should return one or more strings describing the user’s current environment. Those styles are then combined to dynamically invoke an autotest plugin to suite your environment. That plugin should define a subclass of Autotest with a corresponding name.
Process:
-
All autotest/discover.rb files loaded.
-
Those procs determine your styles (eg [“rails”, “rspec”]).
-
Require file by sorting styles and joining (eg ‘autotest/rails_rspec’).
-
Invoke run method on appropriate class (eg Autotest::RailsRspec.run).
Example autotest/discover.rb:
Autotest.add_discovery do
"rails" if File.exist? 'config/environment.rb'
end
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/autotest.rb', line 214 def self.autodiscover require 'rubygems' begin require 'win32console' if WINDOZE rescue LoadError end with_current_path_in_load_path do # search load paths for autotest/discover.rb and load em all Gem.find_files("autotest/discover").each do |f| load f end end #call all discover procs an determine style @@discoveries.map{ |proc| proc.call }.flatten.compact.sort.uniq end |
.options ⇒ Object
71 72 73 |
# File 'lib/autotest.rb', line 71 def self. @@options ||= {} end |
.parse_options(args = ARGV) ⇒ Object
87 88 89 90 91 92 93 94 95 96 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 |
# File 'lib/autotest.rb', line 87 def self.(args = ARGV) require 'optparse' = {} OptionParser.new do |opts| opts. = <<-BANNER.gsub(/^ /, '') Continuous testing for your ruby app. Autotest automatically tests code that has changed. It assumes the code is in lib, and tests are in tests. Autotest uses plugins to control what happens. You configure plugins with require statements in the .autotest file in your project base directory, and a default configuration for all your projects in the .autotest file in your home directory. Usage: autotest [options] BANNER opts.on "-f", "--fast-start", "Do not run full tests at start" do [:no_full_after_start] = true end opts.on("-c", "--no-full-after-failed", "Do not run all tests on red->green") do [:no_full_after_failed] = true end opts.on "-v", "--verbose", "Be annoyingly verbose (debugs .autotest)." do [:verbose] = true end opts.on "-q", "--quiet", "Be quiet." do [:quiet] = true end opts.on("-r", "--rc CONF", String, "Override path to config file") do |o| [:rc] = Array(o) end opts.on("-s", "--style STYLE", String, "Manually specify test style. (default: autodiscover)") do |style| [:style] = Array(style) end opts.on("-p", "--parallel","Run tests (Test::Unit only) in parallel -- gem install parallel_tests") do [:parallel] = true require 'parallel_tests' end opts.on("-b", "--bundle-exec", "Use bundle exec to run tests") do require 'autotest/bundler' end opts.on("-n", "--notify", "Notify about success and failure via popups") do require 'autotest/notify' end opts.on "-h", "--help", "Show this." do puts opts exit 1 end opts.on "-x", "--extras OPTIONS", Array, "Send command line options to the test framework." do |extra| [:extras] ||= [] [:extras] << extra end end.parse args Autotest..merge! end |
.run ⇒ Object
Initialize and run the system.
235 236 237 |
# File 'lib/autotest.rb', line 235 def self.run new.run end |
.runner ⇒ Object
Calculates the autotest runner to use to run the tests.
Can be overridden with –style, otherwise uses ::autodiscover.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/autotest.rb', line 164 def self.runner style = [:style] || Autotest.autodiscover target = Autotest unless style.empty? then mod = "autotest/#{style.join "_"}" puts "loading #{mod}" begin require mod rescue LoadError => e abort "Error loading Autotest style #{mod} (#{e.to_s}). Aborting." end target = Autotest.const_get(style.map {|s| s.capitalize}.join) end target end |
Instance Method Details
#add_exception(regexp) ⇒ Object
Adds regexp
to the list of exceptions for find_file. This must be called before the exceptions are compiled.
733 734 735 736 737 738 |
# File 'lib/autotest.rb', line 733 def add_exception regexp raise "exceptions already compiled" if defined? @exceptions @exception_list << regexp nil end |
#add_mapping(regexp, prepend = false, &proc) ⇒ Object
Adds a file mapping, optionally prepending the mapping to the front of the list if prepend
is true. regexp
should match a file path in the codebase. proc
is passed a matched filename and Regexp.last_match. proc
should return an array of tests to run.
For example, if test_helper.rb is modified, rerun all tests:
at.add_mapping(/test_helper.rb/) do |f, _|
at.files_matching(/^test.*rb$/)
end
697 698 699 700 701 702 703 704 |
# File 'lib/autotest.rb', line 697 def add_mapping(regexp, prepend = false, &proc) if prepend then @test_mappings.unshift [regexp, proc] else @test_mappings.push [regexp, proc] end nil end |
#add_sigint_handler ⇒ Object
Installs a sigint handler.
406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
# File 'lib/autotest.rb', line 406 def add_sigint_handler trap 'INT' do if self.interrupted then self.wants_to_quit = true else unless hook :interrupt then puts "Interrupt a second time to quit" self.interrupted = true Kernel.sleep 1.5 end raise Interrupt, nil # let the run loop catch it end end end |
#add_test_unit_mappings ⇒ Object
294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/autotest.rb', line 294 def add_test_unit_mappings #file in /lib -> run test in /test self.add_mapping(/^lib\/.*\.rb$/) do |filename, _| possible = File.basename(filename).gsub '_', '_?' # ' stupid emacs files_matching %r%^test/.*#{possible}$% end #file in /test -> run it self.add_mapping(/^test.*\/test_.*rb$/) do |filename, _| filename end end |
#all_good ⇒ Object
If there are no files left to test (because they’ve all passed), then all is good.
425 426 427 |
# File 'lib/autotest.rb', line 425 def all_good files_to_test.empty? end |
#clear_exceptions ⇒ Object
Clears the list of exceptions for find_file. This must be called before the exceptions are compiled.
754 755 756 757 758 |
# File 'lib/autotest.rb', line 754 def clear_exceptions raise "exceptions already compiled" if defined? @exceptions @exception_list.clear nil end |
#clear_mappings ⇒ Object
Clears all file mappings. This is DANGEROUS as it entirely disables autotest. You must add at least one file mapping that does a good job of rerunning appropriate tests.
721 722 723 724 |
# File 'lib/autotest.rb', line 721 def clear_mappings @test_mappings.clear nil end |
#consolidate_failures(failed) ⇒ Object
Returns a hash mapping a file name to the known failures for that file.
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 |
# File 'lib/autotest.rb', line 446 def consolidate_failures(failed) filters = new_hash_of_arrays class_map = Hash[*self.find_order.grep(/^test/).map { |f| # TODO: ugly [path_to_classname(f), f] }.flatten] class_map.merge!(self.extra_class_map) failed.each do |method, klass| if class_map.has_key? klass then filters[class_map[klass]] << method else output.puts "Unable to map class #{klass} to a file" end end filters end |
#escape_filenames(classes) ⇒ Object
585 586 587 |
# File 'lib/autotest.rb', line 585 def escape_filenames(classes) classes.map{|klass| "'#{klass}'"} end |
#exceptions ⇒ Object
Return a compiled regexp of exceptions for find_files or nil if no filtering should take place. This regexp is generated from exception_list
.
765 766 767 768 769 770 771 772 773 774 775 |
# File 'lib/autotest.rb', line 765 def exceptions unless defined? @exceptions then @exceptions = if @exception_list.empty? then nil else Regexp.union(*@exception_list) end end @exceptions end |
#files_matching(regexp) ⇒ Object
Returns all known files in the codebase matching regexp
.
681 682 683 |
# File 'lib/autotest.rb', line 681 def files_matching regexp self.find_order.select { |k| k =~ regexp } end |
#find_files ⇒ Object
Find the files to process, ignoring temporary files, source configuration management files, etc., and return a Hash mapping filename to modification time.
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 |
# File 'lib/autotest.rb', line 470 def find_files result = {} targets = self.find_directories + self.extra_files self.find_order.clear targets.each do |target| order = [] Find.find(target) do |f| Find.prune if f =~ self.exceptions next if test ?d, f next if f =~ /(swp|~|rej|orig)$/ # temporary/patch files next if f =~ /\/\.?#/ # Emacs autosave/cvs merge files filename = f.sub(/^\.\//, '') result[filename] = File.stat(filename).mtime rescue next order << filename end self.find_order.push(*order.sort) end result end |
#find_files_to_test(files = find_files) ⇒ Object
Find the files which have been modified, update the recorded timestamps, and use this to update the files to test. Returns the latest mtime of the files modified or nil when nothing was modified.
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 |
# File 'lib/autotest.rb', line 500 def find_files_to_test(files=find_files) updated = files.select { |filename, mtime| self.last_mtime < mtime } # ensure we're dealing with a nested array in Ruby 1.9 updated = updated.to_a # nothing to update or initially run unless updated.empty? || self.last_mtime.to_i == 0 then p updated if [:verbose] hook :updated, updated end updated.map { |f,m| test_files_for(f) }.flatten.uniq.each do |filename| self.files_to_test[filename] # creates key with default value end if updated.empty? then nil else files.values.max end end |
#get_to_green ⇒ Object
Keep running the tests after a change, until all pass.
349 350 351 352 353 354 |
# File 'lib/autotest.rb', line 349 def get_to_green begin run_tests wait_for_changes unless all_good end until all_good end |
#handle_results(results) ⇒ Object
Check results for failures, set the “bar” to red or green, and if there are failures record this.
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 |
# File 'lib/autotest.rb', line 526 def handle_results(results) failed = results.scan(self.failed_results_re) completed = results[self.completed_re] if completed then completed = completed.scan(/(\d+) (\w+)/).map { |v, k| [k, v.to_i] } self.latest_results = Hash[*completed.flatten] self.files_to_test = consolidate_failures failed color = self.files_to_test.empty? ? :green : :red hook color unless $TESTING else self.latest_results = nil end self.tainted = true unless self.files_to_test.empty? end |
#hook(name, *args) ⇒ Object
Hooks: Call the event hook named name
, passing in optional args depending on the hook itself.
Returns false if no hook handled the event.
Hook Writers!
This executes all registered hooks until one returns truthy. Pay attention to the return value of your block!
788 789 790 791 792 793 794 795 796 797 798 |
# File 'lib/autotest.rb', line 788 def hook(name, *args) deprecated = { # none currently } if deprecated[name] and not HOOKS[name].empty? then warn "hook #{name} has been deprecated, use #{deprecated[name]}" end HOOKS[name].any? { |plugin| plugin[self, *args] } end |
#load_custom_extensions ⇒ Object
307 308 309 310 311 312 313 314 |
# File 'lib/autotest.rb', line 307 def load_custom_extensions default_configs = [File.('~/.autotest'), './.autotest'] configs = [:rc] || default_configs configs.each do |f| load f if File.exist? f end end |
#make_test_cmd(files_to_test) ⇒ Object
Generate the commands to test the supplied files
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 |
# File 'lib/autotest.rb', line 558 def make_test_cmd files_to_test cmds = [] full, partial = reorder(files_to_test).partition { |k,v| v.empty? } unless full.empty? then files = full.map {|k,v| k}.flatten.uniq if [:parallel] and files.size > 1 files = files.map{|file| File.(file) } if RUBY19 cmds << "#{prefix}parallel_test #{escape_filenames(files).join(' ')}" else files.unshift testlib cmds << "#{ruby_cmd} -e \"[#{escape_filenames(files).join(', ')}].each { |f| require f }\" | #{unit_diff}" end end partial.each do |klass, methods| regexp = Regexp.union(*methods).source cmds << "#{ruby_cmd} #{klass} -n \"/^(#{regexp})$/\" | #{unit_diff}" end cmds.join("#{SEP} ") end |
#new_hash_of_arrays ⇒ Object
589 590 591 |
# File 'lib/autotest.rb', line 589 def new_hash_of_arrays Hash.new { |h,k| h[k] = [] } end |
#options ⇒ Object
75 76 77 |
# File 'lib/autotest.rb', line 75 def self.class. end |
#path_to_classname(s) ⇒ Object
Convert a path in a string, s, into a class name, changing underscores to CamelCase, etc.
433 434 435 436 437 438 439 440 |
# File 'lib/autotest.rb', line 433 def path_to_classname(s) sep = File::SEPARATOR f = s.sub(/^test#{sep}/, '').sub(/\.rb$/, '').split(sep) f = f.map { |path| path.split(/_|(\d+)/).map { |seg| seg.capitalize }.join } f = f.map { |path| path =~ /^Test/ ? path : "Test#{path}" } f.join('::') end |
#remove_exception(regexp) ⇒ Object
Removes regexp
to the list of exceptions for find_file. This must be called before the exceptions are compiled.
744 745 746 747 748 |
# File 'lib/autotest.rb', line 744 def remove_exception regexp raise "exceptions already compiled" if defined? @exceptions @exception_list.delete regexp nil end |
#remove_mapping(regexp) ⇒ Object
Removed a file mapping matching regexp
.
709 710 711 712 713 714 |
# File 'lib/autotest.rb', line 709 def remove_mapping regexp @test_mappings.delete_if do |k,v| k == regexp end nil end |
#reorder(files_to_test) ⇒ Object
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 |
# File 'lib/autotest.rb', line 593 def reorder files_to_test case self.order when :alpha then files_to_test.sort_by { |k,v| k } when :reverse then files_to_test.sort_by { |k,v| k }.reverse when :random then max = files_to_test.size files_to_test.sort_by { |k,v| rand(max) } when :natural then (self.find_order & files_to_test.keys).map { |f| [f, files_to_test[f]] } else raise "unknown order type: #{self.order.inspect}" end end |
#rerun_all_tests ⇒ Object
Rerun the tests from cold (reset state)
612 613 614 615 616 617 |
# File 'lib/autotest.rb', line 612 def rerun_all_tests reset run_tests hook :all_good if all_good end |
#reset ⇒ Object
Clear all state information about test failures and whether interrupts will kill autotest.
623 624 625 626 627 628 629 630 631 632 633 634 |
# File 'lib/autotest.rb', line 623 def reset self.files_to_test.clear self.find_order.clear self.interrupted = false self.known_files = nil self.last_mtime = T0 self.tainted = false self.wants_to_quit = false hook :reset end |
#ruby ⇒ Object
Determine and return the path of the ruby executable.
639 640 641 642 643 644 645 646 647 |
# File 'lib/autotest.rb', line 639 def ruby ruby = ENV['RUBY'] ruby ||= File.join(RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name']) ruby.gsub! File::SEPARATOR, File::ALT_SEPARATOR if File::ALT_SEPARATOR return ruby end |
#ruby_cmd ⇒ Object
581 582 583 |
# File 'lib/autotest.rb', line 581 def ruby_cmd "#{prefix}#{ruby} -I#{libs} -rubygems" end |
#run ⇒ Object
Repeatedly run failed tests, then all tests, then wait for changes and carry on until killed.
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 |
# File 'lib/autotest.rb', line 320 def run hook :initialize reset add_sigint_handler self.last_mtime = Time.now if [:no_full_after_start] loop do begin # ^c handler get_to_green if tainted? and not [:no_full_after_failed] then rerun_all_tests else hook :all_good end wait_for_changes rescue Interrupt break if wants_to_quit reset end end hook :quit rescue Exception => err hook :died, err end |
#run_tests ⇒ Object
Look for files to test then run the tests and handle the results.
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 |
# File 'lib/autotest.rb', line 359 def run_tests hook :run_command new_mtime = self.find_files_to_test return unless new_mtime self.last_mtime = new_mtime cmd = self.make_test_cmd self.files_to_test return if cmd.empty? puts cmd unless [:quiet] old_sync = $stdout.sync $stdout.sync = true self.results = [] line = [] begin open("| #{cmd}", "r") do |f| until f.eof? do c = f.getc or break putc (c.is_a?(Fixnum) ? c.chr : c) # print breaks coloring on windows -> putc line << c if c == ?\n then self.results << if RUBY19 then line.join else line.pack "c*" end line.clear end end end ensure $stdout.sync = old_sync end hook :ran_command self.results = self.results.join handle_results(self.results) end |
#test_files_for(filename) ⇒ Object
Return the name of the file with the tests for filename by finding a test_mapping
that matches the file and executing the mapping’s proc.
654 655 656 657 658 659 660 661 662 663 664 665 |
# File 'lib/autotest.rb', line 654 def test_files_for(filename) result = @test_mappings.find { |file_re, ignored| filename =~ file_re } p :test_file_for => [filename, result.first] if result and $DEBUG result = result.nil? ? [] : [result.last.call(filename, $~)].flatten output.puts "No tests matched #{filename}" if ([:verbose] or $TESTING) and result.empty? result.sort.uniq.select { |f| known_files[f] } end |
#wait_for_changes ⇒ Object
Sleep then look for files to test, until there are some.
670 671 672 673 |
# File 'lib/autotest.rb', line 670 def wait_for_changes hook :waiting Kernel.sleep self.sleep until find_files_to_test end |