| MoreImportantStatus | = | {} | A lookup table to test which of two states is more important (MoreImportantStatus[[a,b]] # => a or b) | |
| StatusOrder | = | :error, :failure, :pending, :manually_skipped, :dependency_missing, :library_missing, :component_missing, :ignored, :success | All states in the order of relevance, more relevant states first | |
| Setup | = | Struct.new(:component, :substitute, :value, :block) do def inspect | Encapsulates a single setup block and associated information. Relevant for setup variants. |
| components | [R] | A hash of components - available via BareTest::use(name) and Suite#suite :use => name |
| extender | [R] | For mock integration and others, append modules that should extend the Test::Run instance. |
| format | [R] | A hash of formatters (require-string => module) to be used with Test::Run. |
| toplevel_suite | [R] | The toplevel suite. That‘s the one run_if_mainfile and define add suites and assertions to. |
# File lib/baretest.rb, line 213
213: def self.component(name)
214: component = @components[name]
215: begin
216: require "baretest/use/#{name}"
217: rescue LoadError
218: else
219: component = @components[name]
220: end
221: component
222: end
Loads all files in a test directory in order to load the suites and assertions. Used by the ‘baretest’ executable and the standard rake task.
Options:
| :verbose: | Will print information about the load process (default: false) |
| :setup_path: | The path to the setup file, the first loaded file (default: ‘test/setup.rb’) |
| :chdir: | The directory this routine chdirs before loading, will jump back to the original directory after loading (default: ’.’) |
# File lib/baretest.rb, line 108
108: def self.load_standard_test_files(opts={})
109: verbose = opts.delete(:verbose)
110: setup_path = opts.delete(:setup_path) || 'test/setup.rb'
111: lib_path = opts.delete(:lib_path) || 'test/lib'
112: chdir = opts.delete(:chdir) || '.'
113: files = opts.delete(:files)
114: files = [DefaultInitialPositiveGlob] if (files.nil? || files.empty?)
115: Dir.chdir(chdir) do
116: $LOAD_PATH.unshift(File.expand_path(lib_path)) if File.exist?(lib_path)
117: load(setup_path) if File.exist?(setup_path)
118: files.each do |glob|
119: glob = "#{glob}/**/*.rb" if File.directory?(glob)
120: Dir.glob(glob) { |path|
121: helper_path = path.sub(%r{((?:^|/)test)/(suite|unit|integration|system)/}, '\1/helper/\2/')
122: exists = (helper_path != path && File.exist?(helper_path))
123: if verbose then
124: if helper_path == path then
125: puts "Could not resolve helper path for path #{path}"
126: elsif exists
127: puts "Loading helper file #{helper_path}"
128: else
129: puts "No helper file #{helper_path} to load"
130: end
131: end
132: load(helper_path) if exists
133: puts "Loading test file #{path}" if verbose
134: load(path)
135: }
136: end
137: end
138: end
Determine which of the named states is the most important one (see StatusOrder)
# File lib/baretest.rb, line 142
142: def self.most_important_status(states)
143: (StatusOrder & states).first # requires Array#& to be stable (keep order of first operand)
144: end
Create a new component for Suite‘s :use option (see BareTest::Suite::new)
# File lib/baretest.rb, line 238
238: def self.new_component(name, &block)
239: name = name.to_sym
240: raise ArgumentError, "Component named #{name.inspect} already exists" if @components.has_key?(name)
241: @components[name] = block
242: end
Convert an array of selectors into a hash with those selectors preprocessed as far as possible. Example:
BareTest.process_selectors %w[-some/**/glob/*.rb %failure :tag1 -:tag2]
# => {
# :files => ...an array with paths...,
# :include_tags => [:tag1],
# :exclude_tags => [:tag2],
# :include_states => [:failure]
# :exclude_states => nil,
# }
# File lib/baretest.rb, line 157
157: def self.process_selectors(selectors, base_directory=".", default_initial_positive_glob=nil)
158: files = []
159: include_tags = []
160: exclude_tags = []
161: include_states = []
162: exclude_states = []
163:
164: default_initial_positive_glob ||= DefaultInitialPositiveGlob
165: Dir.chdir(base_directory) do
166: selectors.each do |selector|
167: case selector
168: when /\A-%(.*)/ then exclude_states << $1.to_sym
169: when /\A-:(.*)/ then exclude_tags << $1.to_sym
170: when /\A\+?%(.*)/ then include_states << $1.to_sym
171: when /\A\+?:(.*)/ then include_tags << $1.to_sym
172: when /\A-(.*)/ then
173: files = Dir[default_initial_positive_glob] if files.empty? && default_initial_positive_glob
174: glob = File.directory?($1) ? "#{$1}/**/*.rb" : $1
175: files -= Dir[glob]
176: when /\A\+?(.*)/ then
177: glob = File.directory?(selector) ? "#{selector}/**/*.rb" : selector
178: files |= Dir[glob]
179: else
180: raise "Should never reach else - selector: #{selector.inspect}"
181: end
182: end
183: files = Dir[default_initial_positive_glob] if files.empty? && default_initial_positive_glob
184: files.map! do |path| File.expand_path(path) end
185: end
186:
187: invalid_states = (include_states|exclude_states)-ValidStateSelectors
188: raise InvalidSelectors.new(invalid_states) unless invalid_states.empty?
189:
190: return {
191: :files => files,
192: :include_tags => include_tags,
193: :exclude_tags => exclude_tags,
194: :include_states => include_states.empty? ? nil : include_states,
195: :exclude_states => exclude_states.empty? ? nil : exclude_states
196: }
197: end
Tries to require a file, if it fails, it will require rubygems and retries
# File lib/baretest.rb, line 250
250: def self.require(*paths)
251: paths.each do |path|
252: begin
253: Kernel.require path
254: rescue LoadError
255: begin
256: Kernel.require 'rubygems'
257: rescue LoadError
258: end
259: Kernel.instance_method(:require).bind(self).call path # ugly, but at least until rubygems 1.3.5, Kernel.require isn't overriden
260: end
261: end
262: end
Enure that the suite is run wiht a minimal version of baretest
# File lib/baretest.rb, line 86
86: def self.require_baretest(version)
87: if (version.split(".").map { |s| s.to_i } <=> BareTest::VERSION.to_a) > 0 then
88: abort "Requires baretest version #{version}, you have #{BareTest::VERSION}"
89: end
90: end
Ensure that the suite is run with a minimal version of ruby
# File lib/baretest.rb, line 93
93: def self.require_ruby(version)
94: if (version.split(".").map { |s| s.to_i } <=> RUBY_VERSION.split(".").map { |s| s.to_i }) > 0 then
95: abort "Requires ruby version #{version}, you have #{RUBY_VERSION}"
96: end
97: end
If no description was given, it adds the contained assertions and suites to the toplevel suite, if a description was given, a suite with the given description is created, added to the toplevel suite, and all the contained assertions and suites are added to the created suite.
# File lib/baretest.rb, line 227
227: def self.suite(description=nil, opts={}, &block)
228: if description then
229: @toplevel_suite.suite(description, opts, &block)
230: elsif opts && !opts.empty?
231: raise ArgumentError, "Suites with options must have names"
232: else
233: @toplevel_suite.instance_eval(&block)
234: end
235: end
Returns the absolute path to the test directory
# File lib/baretest.rb, line 274
274: def self.test_directory
275: File.expand_path(path, 'test')
276: end