Class: Test::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/rubytest-cli.rb

Overview

Command line interface to test runner.

Constant Summary collapse

GLOB_CONFIG =

Test configuration file can be in ‘etc/test.rb` or `config/test.rb`, or `Testfile` or ’.test` with optional ‘.rb` extension, in that order of precedence. To use a different file there is the -c/–config option.

'{etc/test.rb,config/test.rb,testfile.rb,testfile,.test.rb,.test}'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObject

Initialize CLI instance.



24
25
26
27
28
29
30
# File 'lib/rubytest-cli.rb', line 24

def initialize
  require 'optparse'

  @config = {}
  @config_file = nil
  #@config = Test.configuration(true)
end

Class Method Details

.run(*argv) ⇒ Object

Convenience method for invoking the CLI.

Returns:

  • nothing



17
18
19
# File 'lib/rubytest-cli.rb', line 17

def self.run(*argv)
  new.run(*argv)
end

Instance Method Details

#autopath=(bool) ⇒ Object



233
234
235
# File 'lib/rubytest-cli.rb', line 233

def autopath=(bool)
  @config[:autopath] = !! bool
end

#autopath?Boolean

Returns:

  • (Boolean)


229
230
231
# File 'lib/rubytest-cli.rb', line 229

def autopath?
  @config[:autopath]
end

#chdirObject



237
238
239
# File 'lib/rubytest-cli.rb', line 237

def chdir
  @config[:chdir]
end

#chdir=(dir) ⇒ Object



241
242
243
# File 'lib/rubytest-cli.rb', line 241

def chdir=(dir)
  @config[:chdir] = dir.to_str
end

#configHash

Test run configuration.

Returns:

  • (Hash)


167
168
169
# File 'lib/rubytest-cli.rb', line 167

def config
  @config
end

#config_fileString

Find traditional configuration file.

Returns:

  • (String)

    Config file path.



193
194
195
# File 'lib/rubytest-cli.rb', line 193

def config_file
  @config_file
end

#formatObject



208
209
210
# File 'lib/rubytest-cli.rb', line 208

def format
  @config[:format]
end

#format=(format) ⇒ Object



213
214
215
# File 'lib/rubytest-cli.rb', line 213

def format=(format)
  @config[:format] = format.to_s
end

#load_configObject

Deprecated.

Planned for deprecation in April 2013.

Load configuration file. An example file might look like:

Test.configure do |run|
  run.files << 'test/case_*.rb'
end


178
179
180
181
182
183
184
185
186
187
188
# File 'lib/rubytest-cli.rb', line 178

def load_config
  file = config_file
  unless file
    if chdir
      file = Dir.glob(File.join(chdir, GLOB_CONFIG)).first
    else
      file = Dir.glob(GLOB_CONFIG).first
    end
  end
  load file if file
end

#loadpathObject



245
246
247
# File 'lib/rubytest-cli.rb', line 245

def loadpath
  @config[:loadpath] ||= []
end

#makelist(list) ⇒ Array<String> (private)

If given a String then split up at ‘:` and `;` markers. Otherwise ensure the list is an Array and the entries are all strings and not empty.

Returns:

  • (Array<String>)


268
269
270
271
272
273
274
275
276
# File 'lib/rubytest-cli.rb', line 268

def makelist(list)
  case list
  when String
    list = list.split(/[:;]/)
  else
    list = Array(list).map{ |path| path.to_s }
  end
  list.reject{ |path| path.strip.empty? }
end

#matchObject



225
226
227
# File 'lib/rubytest-cli.rb', line 225

def match
  @config[:match] ||= []
end

#optionsOptionParser

Setup OptionsParser instance.

Returns:

  • (OptionParser)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
159
160
161
162
# File 'lib/rubytest-cli.rb', line 72

def options
  conf = self

  OptionParser.new do |opt|
    opt.banner = "Usage: #{File.basename($0)} [options] [files ...]"

    opt.on '-p', '--profile NAME', 'use configuration profile' do |name|
      conf.profile = name
    end

    #opt.separator "PRESET OPTIONS:"
    #pnames = profile_names
    #unless pnames.empty?
    #  pnames.each do |pname|
    #    opt.separator((" " * 40) + "* #{pname}")
    #  end
    #end
    #opt.separator "CONFIG OPTIONS:"

    opt.on '-f', '--format NAME', 'report format' do |name|
      conf.format = name
    end
    opt.on '-y', '--tapy', 'shortcut for -f tapy' do
      conf.format = 'tapy'
    end
    opt.on '-j', '--tapj', 'shortcut for -f tapj' do
      conf.format = 'tapj'
    end

    # tempted to change -T
    opt.on '-t', '--tag TAG', 'select tests by tag' do |tag|
      conf.tags.concat makelist(tag)
    end
    opt.on '-u', '--unit TAG', 'select tests by software unit' do |unit|
      conf.units.concat makelist(unit)
    end
    opt.on '-m', '--match TEXT', 'select tests by description' do |text|
      conf.match.concat makelist(text)
    end

    opt.on '-A', '--autopath', 'automatically add paths to $LOAD_PATH' do |paths|
      conf.autopath = true
    end
    opt.on '-I', '--loadpath PATH', 'add given path to $LOAD_PATH' do |paths|
      #makelist(paths).reverse_each do |path|
      #  $LOAD_PATH.unshift path
      #end
      conf.loadpath.concat makelist(paths)
    end
    opt.on '-C', '--chdir DIR', 'change directory before running tests' do |dir|
      conf.chdir = dir
    end
    opt.on '-R', '--chroot', 'change to project root directory before running tests' do
      conf.chdir = Config.root
    end
    opt.on '-r', '--require FILE', 'require file' do |file|
      conf.requires.concat makelist(file)
    end
    opt.on '-c', '--config FILE', "use alternate config file" do |file|
      conf.config_files << file
    end
    #opt.on '-T', '--tests GLOB', "tests to run (if none given as arguments)" do |glob|
    #  config.files << glob
    #end
    opt.on '-V' , '--verbose', 'provide extra detail in reports' do
      conf.verbose = true
    end
    #opt.on('--log PATH', 'log test output to path'){ |path|
    #  config.log = path
    #}
    opt.on("--[no-]ansi" , 'turn on/off ANSI colors'){ |v| $ansi = v }
    opt.on("--debug" , 'turn on debugging mode'){ $DEBUG = true }

    #opt.separator "COMMAND OPTIONS:"
    opt.on('--about' , 'display information about rubytest') do
      puts "Ruby Test v%s" % [Test.index['version']]
      Test.index['copyrights'].each do |c|
        puts "(c) %s %s (%s)" % c.values_at('year', 'holder', 'license')
      end
      exit
    end
    opt.on('--version' , 'display rubytest version') do
      puts Test::VERSION
      exit
    end
    opt.on('-h', '--help', 'display this help message'){
      puts opt
      exit
    }
  end
end

#profileObject



198
199
200
# File 'lib/rubytest-cli.rb', line 198

def profile
  @profile
end

#profile=(name) ⇒ Object



203
204
205
# File 'lib/rubytest-cli.rb', line 203

def profile=(name)
  @profile = name.to_s
end

#require_dotoptsObject

TODO: Not sure if this should be used or not.



62
63
64
65
66
67
# File 'lib/rubytest-cli.rb', line 62

def require_dotopts
  begin
    require 'dotopts'
  rescue LoadError
  end
end

#requiresObject



249
250
251
# File 'lib/rubytest-cli.rb', line 249

def requires
  @config[:requires] ||= []
end

#run(argv = nil) ⇒ Object

Run tests.

Returns:

  • nothing



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rubytest-cli.rb', line 35

def run(argv=nil)
  #require_dotopts
  argv = (argv || ARGV.dup)
  options.parse!(argv)

  @config[:files] = argv unless argv.empty?

  load_config

  test_config = Test.configuration(profile)
  test_config.apply_environment_defaults
  test_config.apply(@config)

  Test.run!(test_config)

  #runner = Runner.new(test_config)
  #begin
  #  success = runner.run
  #  exit -1 unless success
  #rescue => error
  #  raise error if $DEBUG
  #  $stderr.puts('ERROR: ' + error.to_s)
  #  exit -1
  #end
end

#tagsObject



217
218
219
# File 'lib/rubytest-cli.rb', line 217

def tags
  @config[:tags] ||= []
end

#unitsObject



221
222
223
# File 'lib/rubytest-cli.rb', line 221

def units
  @config[:units] ||= []
end

#verbose=(bool) ⇒ Object



257
258
259
# File 'lib/rubytest-cli.rb', line 257

def verbose=(bool)
  @config[:verbose] = !! bool
end

#verbose?Boolean

Returns:

  • (Boolean)


253
254
255
# File 'lib/rubytest-cli.rb', line 253

def verbose?
  @config[:verbose]
end