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
# File 'lib/rubytest/cli.rb', line 24

def initialize
  require 'optparse'

  @config = {}
  @config_file = nil
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



176
177
178
# File 'lib/rubytest/cli.rb', line 176

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

#autopath?Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/rubytest/cli.rb', line 172

def autopath?
  @config[:autopath]
end

#chdirObject



180
181
182
# File 'lib/rubytest/cli.rb', line 180

def chdir
  @config[:chdir]
end

#chdir=(dir) ⇒ Object



184
185
186
# File 'lib/rubytest/cli.rb', line 184

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

#configHash

Test run configuration.

Returns:

  • (Hash)


120
121
122
# File 'lib/rubytest/cli.rb', line 120

def config
  @config
end

#config_fileString

Find traditional configuration file.

Returns:

  • (String)

    Config file path.



140
141
142
# File 'lib/rubytest/cli.rb', line 140

def config_file
  @config_file
end

#formatObject



152
153
154
# File 'lib/rubytest/cli.rb', line 152

def format
  @config[:format]
end

#format=(format) ⇒ Object



156
157
158
# File 'lib/rubytest/cli.rb', line 156

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

#load_configObject

Load configuration file.



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/rubytest/cli.rb', line 125

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



188
189
190
# File 'lib/rubytest/cli.rb', line 188

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

#matchObject



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

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

#optionsOptionParser

Setup OptionsParser instance.

Returns:

  • (OptionParser)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
# File 'lib/rubytest/cli.rb', line 52

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.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

    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|
      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 '-V' , '--verbose', 'provide extra detail in reports' do
      conf.verbose = true
    end
    opt.on("--[no-]ansi" , 'turn on/off ANSI colors'){ |v| $ansi = v }
    opt.on("--debug" , 'turn on debugging mode'){ $DEBUG = true }

    opt.on('--version' , 'display rubytest version') do
      puts "rubytest v#{Test::VERSION}"
      exit
    end
    opt.on('-h', '--help', 'display this help message'){
      puts opt
      exit
    }
  end
end

#profileObject



144
145
146
# File 'lib/rubytest/cli.rb', line 144

def profile
  @profile
end

#profile=(name) ⇒ Object



148
149
150
# File 'lib/rubytest/cli.rb', line 148

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

#requiresObject



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

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

#run(argv = nil) ⇒ Object

Run tests.

Returns:

  • nothing



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rubytest/cli.rb', line 34

def run(argv=nil)
  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)
end

#tagsObject



160
161
162
# File 'lib/rubytest/cli.rb', line 160

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

#unitsObject



164
165
166
# File 'lib/rubytest/cli.rb', line 164

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

#verbose=(bool) ⇒ Object



200
201
202
# File 'lib/rubytest/cli.rb', line 200

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

#verbose?Boolean

Returns:

  • (Boolean)


196
197
198
# File 'lib/rubytest/cli.rb', line 196

def verbose?
  @config[:verbose]
end