Class: Test::CLI

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

Overview

Command line interface to test runner.

TODO: Use cli based library instead of option parser?

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObject

Initialize CLI instance.



19
20
21
22
23
# File 'lib/rubytest/cli.rb', line 19

def initialize
  require 'optparse'

  @config = Test.configuration(true)
end

Class Method Details

.run(*argv) ⇒ Object

Convenience method for invoking the CLI.

Returns:

  • nothing



12
13
14
# File 'lib/rubytest/cli.rb', line 12

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

Instance Method Details

#configConfig

Test run configuration.

Returns:



28
29
30
# File 'lib/rubytest/cli.rb', line 28

def config
  @config
end

#makelist(list) ⇒ Array<String>

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:



153
154
155
156
157
158
159
160
161
# File 'lib/rubytest/cli.rb', line 153

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

#optionsOptionParser

Setup OptionsParser instance.

Returns:

  • (OptionParser)


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

def options
  this = self

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

    #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|
      config.format = name
    end
    opt.on '-y', '--tapy', 'shortcut for -f tapy' do
      config.format = 'tapy'
    end
    opt.on '-j', '--tapj', 'shortcut for -f tapj' do
      config.format = 'tapj'
    end

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

    opt.on '-A', '--autopath', 'automatically add paths to $LOAD_PATH' do |paths|
      config.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
      config.loadpath.concat makelist(paths)
    end
    opt.on '-C', '--chdir DIR', 'change directory before running tests' do |dir|
      config.chdir = dir
    end
    opt.on '-R', '--chroot', 'change to project root directory before running tests' do
      config.chdir = Config.root
    end
    opt.on '-r', '--require FILE', 'require file' do |file|
      #require file
      config.requires.concat makelist(file)
    end
    opt.on '-c', '--config FILE', "require local config file (immediately)" do |file|
      config.load_config(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
      config.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

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

def run(argv=nil)
  argv = (argv || ARGV.dup)

  options.parse!(argv)

  config.files.replace(argv) unless argv.empty?

  config.apply_environment_overrides

  #Test.run(config)
  runner = Runner.new(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