Skip to content

Commit 4ed3ab8

Browse files
committed
Add mode to Config for special cases.
1 parent 1bd8ea3 commit 4ed3ab8

1 file changed

Lines changed: 196 additions & 21 deletions

File tree

lib/rubytest/config.rb

Lines changed: 196 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -81,26 +81,28 @@ def self.root
8181
)
8282
end
8383

84-
# Load and cache a project's `.index` file.
84+
# Load and cache a project's `.index` file, if it has one.
8585
#
86-
# @return [Hash] Project's loaded `.index` file, if it has one.
86+
# @return [Hash] YAML loaded `.index` file, or empty hash.
8787
def self.dotindex
8888
@dotindex ||= (
8989
file = File.join(root, '.index')
9090
if File.exist?(file)
91+
require 'yaml'
9192
YAML.load_file(file) rescue {}
9293
else
9394
{}
9495
end
9596
)
9697
end
9798

98-
# Require a configuration file.
99-
#
100-
# TODO: Should config files be require relative to project root
99+
# TODO: Should config files be required relative to project root
101100
# or relatvie to current working directory?
102-
#
101+
103102
# TODO: Use load instead of require?
103+
104+
# Require a configuration file. Configuration files are required
105+
# relative to the project's root directory.
104106
#
105107
# @return nothing.
106108
def self.require_config(file)
@@ -132,79 +134,181 @@ def self.load_path_setup
132134

133135
# Initialize new Config instance.
134136
def initialize(&block)
137+
@files = env(:testfiles, [])
138+
@match = env(:match, [])
139+
@tags = env(:tags, [])
140+
@units = env(:units, [])
141+
@format = env(:format, DEFAULT_FORMAT)
142+
@requires = env(:requires, [])
143+
@loadpath = env(:loadpath, [])
144+
135145
self.class.load_config
146+
136147
apply(&block)
137148
end
138149

139150
# Evaluate configuration block.
151+
#
152+
# @return nothing
140153
def apply(&block)
141154
block.call(self) if block
142155
end
143156

144157
# Default test suite ($TEST_SUITE).
158+
#
159+
# @return [Array]
145160
def suite
146161
$TEST_SUITE
147162
end
148163

149164
# Default list of test files to load.
165+
#
166+
# @return [Array<String>]
150167
def files
151-
@files ||= []
168+
@files
169+
end
170+
alias test_files files
171+
172+
# Set the list of test files to run. Entries can be file glob patterns.
173+
# This can also be set via the `RUBYTEST_FILES` environment variable.
174+
#
175+
# @return [Array<String>]
176+
def files=(list)
177+
@files = pathlist(list)
178+
end
179+
alias test_files= files=
180+
181+
# Paths to add to $LOAD_PATH.
182+
#
183+
# @return [Array<String>]
184+
attr :loadpath
185+
186+
# Set paths to add to $LOAD_PATH. This can also be set via the
187+
# `RUBYTEST_LOADPATH` environment variable.
188+
#
189+
# @return [Array<String>]
190+
def loadpath=(list)
191+
@loadpath = pathlist(list)
192+
end
193+
194+
# Scripts to require prior to tests.
195+
#
196+
# @return [Array<String>]
197+
attr :requires
198+
199+
# Set the features that need to be required before the
200+
# test files. This can also be set via the `RUBYTEST_REQUIRES`
201+
# environment variable.
202+
#
203+
# @return [Array<String>]
204+
def requires=(list)
205+
@requires = pathlist(list)
152206
end
153207

154-
# Format by default is `dotprogress`. The format can also be set
155-
# via the `RUBYTEST_FORMAT` environment variable.
208+
# Name of test report format, by default it is `dotprogress`.
156209
#
157210
# @return [String] format
158211
def format
159-
@format || ENV['RUBYTEST_FORMAT'] || DEFAULT_FORMAT
212+
@format
160213
end
161214

162-
# Set test report format.
215+
# Set test report format. The format can also be set via the
216+
# `RUBYTEST_FORMAT` environment variable.
217+
#
218+
# @return [String] format
163219
def format=(format)
164220
@format = format
165221
end
166222

167223
# Provide extra details in reports?
168-
def verbose
224+
#
225+
# @return [Boolean]
226+
def verbose?
169227
@verbose
170228
end
171229

172-
# Set verbose mode.
230+
# Set verbose mode. The verbosity can also be set via the
231+
# `RUBYTEST_VERBOSE` environment variable.
232+
#
233+
# @return [Boolean]
173234
def verbose=(boolean)
174235
@verbose = !!boolean
175236
end
176237

177-
# Default description match for filtering tests.
238+
# Description match for filtering tests.
239+
#
240+
# @return [Array<String>]
178241
def match
179-
@match ||= []
242+
@match
243+
end
244+
245+
# Set the description matches for filtering tests. The list of matches
246+
# can also be set via the `RUBYTEST_MATCH` environment variable.
247+
# Separate them like paths, with `:` or `;` marks.
248+
#
249+
# @return [Array<String>]
250+
def match=(list)
251+
@match = pathlist(list)
180252
end
181253

182-
# Default selection of tags for filtering tests.
254+
# Selection of tags for filtering tests. The list of tags can also
255+
# be set via the `RUBYTEST_TAGS` environment variable. Separate
256+
# each tag with `:` or `;` marks.
257+
#
258+
# @return [Array<String>]
183259
def tags
184-
@tags ||= []
260+
@tags
185261
end
186262

187-
# Default selection of units for filtering tests.
263+
# Set the list of tags for filtering tests. The list of tags can also
264+
# be set via the `RUBYTEST_TAGS` environment variable. Separate
265+
# each tag with `:` or `;` marks.
266+
#
267+
# @return [Array<String>]
268+
def tags=(list)
269+
@tags = pathlist(list)
270+
end
271+
272+
# List of units with which to filter tests. It is an array of strings
273+
# which are matched against module, class and method names.
274+
#
275+
# @return [Array<String>]
188276
def units
189-
@unit ||= []
277+
@units
278+
end
279+
280+
# Set the list of units with which to filter tests. It is an array of
281+
# strings which are matched against module, class and method names.
282+
#
283+
# @return [Array<String>]
284+
def units=(list)
285+
@units = pathlist(list)
190286
end
191287

192288
# Hard is a synonym for assertionless.
193-
def hard
289+
#
290+
# @return [Boolean]
291+
def hard?
194292
@hard || self.class.assertionless
195293
end
196294

197295
# Hard is a synonym for assertionless.
296+
#
297+
# @return [Boolean]
198298
def hard=(boolean)
199299
@hard = !!boolean
200300
end
201301

202302
# Automatically modify the `$LOAD_PATH`?
203-
def autopath
303+
#
304+
# @return [Boolean]
305+
def autopath?
204306
@autopath
205307
end
206308

207309
# Automatically modify the `$LOAD_PATH`?
310+
#
311+
# @return [Boolean]
208312
def autopath=(boolean)
209313
@autopath = !!boolean
210314
end
@@ -223,6 +327,77 @@ def autopath=(boolean)
223327
# @chroot = dir.to_s
224328
#end
225329

330+
# The mode is only useful for specialied purposes, such how
331+
# to run tests via the Rake task. It has no general purpose
332+
# use and can be ignored in most cases.
333+
#
334+
# @return [Symbol]
335+
def mode
336+
@mode
337+
end
338+
339+
# The mode is only useful for specialied purposes, such how
340+
# to run tests via the Rake task. It has no general purpose
341+
# use and can be ignored in most cases.
342+
#
343+
# @return [String]
344+
def mode=(type)
345+
@mode = type.to_s
346+
end
347+
348+
# Convert configuration to shell options, compatible with the
349+
# rubytest command line.
350+
#
351+
# @return [Array<String>]
352+
def to_shellwords
353+
argv = []
354+
argv << %[--autoload] if autoload?
355+
argv << %[--verbose] if verbose?
356+
argv << %[--format="#{format}"] if format
357+
argv << %[--match="#{match.join(';')}"] unless match.empty?
358+
argv << %[--units="#{units.join(';')}"] unless units.empty?
359+
argv << %[--tags="#{tags.join(';')}"] unless tags.empty?
360+
argv << %[--loadpath="#{loadpath.join(';')}"] unless loadpath.empty?
361+
argv << %[--requires="#{requires.join(';')}"] unless requires.empty?
362+
argv << files.join(' ') unless files.empty?
363+
argv
364+
end
365+
366+
private
367+
368+
# Lookup environment variable with name `RUBYTEST_{NAME}`,
369+
# and transform in according to the type of the given
370+
# default. If the environment variable is not set then
371+
# returns the default.
372+
#
373+
# @return [Object]
374+
def env(name, default=nil)
375+
value = ENV["RUBYTEST_#{name.capitalize}"]
376+
377+
case default
378+
when Array
379+
return value.split(/[:;]/) if value
380+
else
381+
return value if value
382+
end
383+
default
384+
end
385+
386+
# If given a String then split up at `:` and `;` markers.
387+
# Otherwise ensure the list is an Array and the entries are
388+
# all strings and not empty.
389+
#
390+
# @return [Array<String>]
391+
def pathlist(list)
392+
case list
393+
when String
394+
list = list.split(/[:;]/)
395+
else
396+
list = Array(list).map{ |path| path.to_s }
397+
end
398+
list.reject{ |path| path.strip.empty? }
399+
end
400+
226401
end
227402

228403
end

0 commit comments

Comments
 (0)