Class: Test::Unit::AutoRunner
- Inherits:
-
Object
- Object
- Test::Unit::AutoRunner
- Defined in:
- lib/test/unit/autorunner.rb
Constant Summary collapse
- RUNNERS =
{}
- COLLECTORS =
{}
- ADDITIONAL_OPTIONS =
[]
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.
-
#exclude ⇒ Object
Returns the value of attribute exclude.
-
#filters ⇒ Object
Returns the value of attribute filters.
-
#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.
-
#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
- .register_collector(id, collector_builder = Proc.new) ⇒ Object
- .register_color_scheme(id, scheme) ⇒ Object
- .register_runner(id, runner_builder = Proc.new) ⇒ Object
- .run(force_standalone = false, default_dir = nil, argv = ARGV, &block) ⇒ Object
- .runner(id) ⇒ Object
- .setup_option(option_builder = Proc.new) ⇒ Object
- .standalone? ⇒ Boolean
Instance Method Summary collapse
-
#initialize(standalone) {|_self| ... } ⇒ AutoRunner
constructor
A new instance of AutoRunner.
- #keyword_display(keywords) ⇒ Object
- #load_config(file) ⇒ Object
- #options ⇒ Object
- #process_args(args = ARGV) ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize(standalone) {|_self| ... } ⇒ AutoRunner
Returns a new instance of AutoRunner.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/test/unit/autorunner.rb', line 97 def initialize(standalone) Unit.run = true @standalone = standalone @runner = default_runner @collector = default_collector @filters = [] @to_run = [] @color_scheme = ColorScheme.default @runner_options = {} @workdir = nil default_config_file = "test-unit.yml" load_config(default_config_file) if File.exist?(default_config_file) yield(self) if block_given? end |
Instance Attribute Details
#base ⇒ Object
Returns the value of attribute base.
93 94 95 |
# File 'lib/test/unit/autorunner.rb', line 93 def base @base end |
#collector=(value) ⇒ Object (writeonly)
Sets the attribute collector
95 96 97 |
# File 'lib/test/unit/autorunner.rb', line 95 def collector=(value) @collector = value end |
#color_scheme ⇒ Object
Returns the value of attribute color_scheme.
94 95 96 |
# File 'lib/test/unit/autorunner.rb', line 94 def color_scheme @color_scheme end |
#exclude ⇒ Object
Returns the value of attribute exclude.
93 94 95 |
# File 'lib/test/unit/autorunner.rb', line 93 def exclude @exclude end |
#filters ⇒ Object
Returns the value of attribute filters.
93 94 95 |
# File 'lib/test/unit/autorunner.rb', line 93 def filters @filters end |
#pattern ⇒ Object
Returns the value of attribute pattern.
93 94 95 |
# File 'lib/test/unit/autorunner.rb', line 93 def pattern @pattern end |
#runner=(value) ⇒ Object (writeonly)
Sets the attribute runner
95 96 97 |
# File 'lib/test/unit/autorunner.rb', line 95 def runner=(value) @runner = value end |
#runner_options ⇒ Object (readonly)
Returns the value of attribute runner_options.
92 93 94 |
# File 'lib/test/unit/autorunner.rb', line 92 def @runner_options end |
#suite ⇒ Object (readonly)
Returns the value of attribute suite.
92 93 94 |
# File 'lib/test/unit/autorunner.rb', line 92 def suite @suite end |
#to_run ⇒ Object
Returns the value of attribute to_run.
93 94 95 |
# File 'lib/test/unit/autorunner.rb', line 93 def to_run @to_run end |
#workdir ⇒ Object
Returns the value of attribute workdir.
93 94 95 |
# File 'lib/test/unit/autorunner.rb', line 93 def workdir @workdir end |
Class Method Details
.collector(id) ⇒ Object
27 28 29 |
# File 'lib/test/unit/autorunner.rb', line 27 def collector(id) COLLECTORS[id.to_s] end |
.register_collector(id, collector_builder = Proc.new) ⇒ Object
22 23 24 25 |
# File 'lib/test/unit/autorunner.rb', line 22 def register_collector(id, collector_builder=Proc.new) COLLECTORS[id] = collector_builder COLLECTORS[id.to_s] = collector_builder end |
.register_color_scheme(id, scheme) ⇒ Object
31 32 33 |
# File 'lib/test/unit/autorunner.rb', line 31 def register_color_scheme(id, scheme) ColorScheme[id] = scheme end |
.register_runner(id, runner_builder = Proc.new) ⇒ Object
13 14 15 16 |
# File 'lib/test/unit/autorunner.rb', line 13 def register_runner(id, runner_builder=Proc.new) RUNNERS[id] = runner_builder RUNNERS[id.to_s] = runner_builder end |
.run(force_standalone = false, default_dir = nil, argv = ARGV, &block) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/test/unit/autorunner.rb', line 40 def self.run(force_standalone=false, default_dir=nil, argv=ARGV, &block) r = new(force_standalone || standalone?, &block) r.base = default_dir r.process_args(argv) r.run end |
.runner(id) ⇒ Object
18 19 20 |
# File 'lib/test/unit/autorunner.rb', line 18 def runner(id) RUNNERS[id.to_s] end |
.setup_option(option_builder = Proc.new) ⇒ Object
35 36 37 |
# File 'lib/test/unit/autorunner.rb', line 35 def setup_option(option_builder=Proc.new) ADDITIONAL_OPTIONS << option_builder end |
.standalone? ⇒ Boolean
47 48 49 50 51 52 53 |
# File 'lib/test/unit/autorunner.rb', line 47 def self.standalone? return false unless("-e" == $0) ObjectSpace.each_object(Class) do |klass| return false if(klass < TestCase) end true end |
Instance Method Details
#keyword_display(keywords) ⇒ Object
254 255 256 257 258 259 260 |
# File 'lib/test/unit/autorunner.rb', line 254 def keyword_display(keywords) keywords.collect do |keyword, _| keyword.to_s end.uniq.sort.collect do |keyword| keyword.sub(/^(.)([A-Za-z]+)(?=\w*$)/, '\\1[\\2]') end.join(", ") end |
#load_config(file) ⇒ Object
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/test/unit/autorunner.rb', line 272 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 [key.to_sym] = value end @runner_options = @runner_options.merge() end |
#options ⇒ Object
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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 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 |
# File 'lib/test/unit/autorunner.rb', line 125 def @options ||= OptionParser.new do |o| o. = "Test::Unit automatic runner." o. << "\nUsage: #{$0} [options] [-- untouched arguments]" o.on o.on('-r', '--runner=RUNNER', RUNNERS, "Use the given RUNNER.", "(" + keyword_display(RUNNERS) + ")") do |r| @runner = r 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('-a', '--add=TORUN', Array, "Add TORUN to the list of things to run;", "can be a file or a directory.") do |a| @to_run.concat(a) 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.", "(patterns may be used).") do |n| n = (%r{\A/(.*)/\Z} =~ n ? Regexp.new($1) : n) case n when Regexp @filters << proc{|t| n =~ t.method_name ? true : nil} else @filters << proc{|t| n == t.method_name ? true : nil} end end o.on('-t', '--testcase=TESTCASE', String, "Runs tests in TestCases matching TESTCASE.", "(patterns may be used).") do |n| n = (%r{\A/(.*)/\Z} =~ n ? Regexp.new($1) : n) case n when Regexp @filters << proc{|t| n =~ t.class.name ? true : nil} else @filters << proc{|t| n == t.class.name ? true : nil} end end priority_filter = Proc.new do |test| if @filters.size > 2 nil else Priority::Checker.new(test).need_to_run? or 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('-I', "--load-path=DIR[#{File::PATH_SEPARATOR}DIR...]", "Appends directory list to $LOAD_PATH.") do |dirs| $LOAD_PATH.concat(dirs.split(File::PATH_SEPARATOR)) 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 fomat FILE content as configuration file.") do |file| load_config(file) 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 |
#process_args(args = ARGV) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/test/unit/autorunner.rb', line 112 def process_args(args = ARGV) begin .order!(args) {|arg| @to_run << arg} rescue OptionParser::ParseError => e puts e puts exit(false) else @filters << proc{false} unless(@filters.empty?) end not @to_run.empty? end |
#run ⇒ Object
262 263 264 265 266 267 268 269 270 |
# File 'lib/test/unit/autorunner.rb', line 262 def run suite = @collector[self] return false if suite.nil? runner = @runner[self] return false if runner.nil? @runner_options[:color_scheme] ||= @color_scheme Dir.chdir(@workdir) if @workdir runner.run(suite, @runner_options).passed? end |