Module: BareTest::CommandLine

Defined in:
lib/baretest/commandline.rb

Overview

The CommandLine module provides all the functionality the baretest executable uses in a programmatic way. It in fact even is what the baretest executable itself uses.

Class Method Summary collapse

Class Method Details

.commands(arguments, options) ⇒ Object

List the available commands.



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/baretest/commandline.rb', line 148

def commands(arguments, options)
  colors = $stdout.tty?

  description = <<-END_OF_DESCRIPTION.gsub(/^ {8}/, '') #                           |<- 80 cols ends here
    \e[1mCOMMANDS\e[0m

    The following commands are available in baretest:

    \e[1mcommands\e[0m
        List the available commands.

    \e[1menv\e[0m
        Show the baretest environment. This contains all data that influences
        baretests behaviour. That is: ruby version, ruby engine, determined
        test directory, stored data about this suite etc.

    \e[1mformats\e[0m
        Shows all formats available in \e[34mrun\e[0m's -f/--format option.

    \e[1mhelp\e[0m
        Provides help for all commands. Describes options, arguments and env
        variables each command accepts.

    \e[1minit\e[0m
        Create a basic skeleton of directories and files to contain baretests test-
        suite. Non-destructive (existing files won't be overriden or deleted).

    \e[1mreset\e[0m (default command)
        Delete persistent data collected from previous runs.

    \e[1mrun\e[0m (default command)
        Run the tests and display information about them.

    \e[1mselectors\e[0m
        Detailed information about the selectors available to \e[34mrun\e[0m's
        arguments.

    \e[1mversion\e[0m
        Show the baretest executable and library versions.

  END_OF_DESCRIPTION
  #'#                                                                               |<- 80 cols ends here
  description.gsub!(/\e.*?m/, '') unless colors

  puts description
end

.env(arguments, options) ⇒ Object

Show the baretest environment. This contains all data that influences baretests behaviour. That is: ruby version, ruby engine, determined test directory, stored data about this suite etc.



272
273
274
275
276
277
278
# File 'lib/baretest/commandline.rb', line 272

def env(arguments, options)
  puts "Versions:",
       "* executable: #{Version}",
       "* library: #{BareTest::VERSION}",
       "* ruby #{RUBY_VERSION}",
       ""
end

.formats(arguments, options) ⇒ Object

Shows all formats available in run’s -f/–format option.



139
140
141
142
143
144
# File 'lib/baretest/commandline.rb', line 139

def formats(arguments, options)
  puts "Available formats:"
  Dir.glob("{#{$LOAD_PATH.join(',')}}/baretest/run/*.rb") { |path|
    puts "- #{File.basename(path, '.rb')}"
  }
end

.help(arguments, options) ⇒ Object

Provides help for all commands. Describes options, arguments and env variables each command accepts.



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/baretest/commandline.rb', line 252

def help(arguments, options)
  colors = $stdout.tty?

  description = <<-END_OF_DESCRIPTION.gsub(/^ {8}/, '') #                           |<- 80 cols ends here
    \e[1mHELP\e[0m

    See `#{$0} commands` for a list of available commands.
    You can also use `#{$0} COMMAND --help` to get information about
    the command COMMAND.
  END_OF_DESCRIPTION

  description.gsub!(/\e.*?m/, '') unless colors

  puts description
end

.init(arguments, options) ⇒ Object

Create a basic skeleton of directories and files to contain baretests test-suite. Non-destructive (existing files won’t be overriden or deleted).



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
# File 'lib/baretest/commandline.rb', line 64

def init(arguments, options)
  core = %w[
    test
    test/external
    test/helper
    test/helper/suite
    test/suite
  ]
  mirror = {
    'bin'  => %w[test/helper/suite test/suite],
    'lib'  => %w[test/helper/suite test/suite],
    'rake' => %w[test/helper/suite test/suite],
  }
  baretest_version = BareTest::VERSION.to_a.first(3).join('.').inspect
  ruby_version     = RUBY_VERSION.inspect
  files = {
    'test/setup.rb' => <<-END_OF_SETUP.gsub(/^ {10}/, '')
      # Add PROJECT/lib to $LOAD_PATH
      $LOAD_PATH.unshift(File.expand_path("\#{__FILE__}/../../lib"))

      # Ensure baretest is required
      require 'baretest'

      # Some defaults on BareTest (see Kernel#BareTest)
      BareTest do
        require_baretest #{baretest_version} # minimum baretest version to run these tests
        require_ruby     #{ruby_version} # minimum ruby version to run these tests
        use              :support # Use :support in all suites
      end
    END_OF_SETUP
  }

  puts "Creating all directories and files needed in #{File.expand_path('.')}"
  core.each do |dir|
    if File.exist?(dir) then
      puts "Directory #{dir} exists already -- skipping"
    else
      puts "Creating #{dir}"
      Dir.mkdir(dir)
    end
  end
  mirror.each do |path, destinations|
    if File.exist?(path) then
      destinations.each do |destination|
        destination = File.join(destination,path)
        if File.exist?(destination) then
          puts "Mirror #{destination} of #{path} exists already -- skipping"
        else
          puts "Mirroring #{path} in #{destination}"
          Dir.mkdir(destination)
        end
      end
    end
  end
  files.each do |path, data|
    if File.exist?(path) then
      puts "File #{path} exists already -- skipping"
    else
      puts "Writing #{path}"
      File.open(path, 'wb') do |fh|
        fh.write(data)
      end
    end
  end
end

.load_formatter(format) ⇒ Object

Load a formatter (see Run::new)



23
24
25
26
# File 'lib/baretest/commandline.rb', line 23

def load_formatter(format)
  require "baretest/run/#{format}" if String === format
  BareTest.format["baretest/run/#{format}"]
end

.reset(arguments, options) ⇒ Object

Remove all files that store state, cache things etc. from persistence.



132
133
134
135
# File 'lib/baretest/commandline.rb', line 132

def reset(arguments, options)
  options[:persistence] ||= Persistence.new
  options[:persistence].clear
end

.run(arguments, options) ⇒ Object

Run the tests and display information about them.

  • arguments: array of dirs/globs of files to load and run as tests

  • options: a hash with options (all MUST be provided)

:format => String - the formatter :interactive => Boolean - activate interactive mode (drops into irb on failure/error) :verbose => Boolean - provide verbose output



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

def run(arguments, options)
  setup_path              = nil
  selectors               = BareTest.process_selectors(arguments)
  options                 = selectors.merge(options)
  options[:persistence] ||= Persistence.new

  # Load the setup file, all helper files and all test files
  BareTest.load_standard_test_files(
    :verbose    => options[:verbose],
    :setup_path => options[:setup_path],
    :files      => options[:files],
    :chdir      => '.'
  )

  # Run the tests
  puts if options[:verbose]
  ARGV.clear # IRB is being stupid
  runner = BareTest::Run.new(BareTest.toplevel_suite, options)
  runner.run_all

  # Return whether all tests ran successful
  runner.global_status == :success
end

.selectors(arguments, options) ⇒ Object

Detailed information about the selectors available to run’s arguments.



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/baretest/commandline.rb', line 197

def selectors(arguments, options)
  colors = $stdout.tty?

  description = <<-END_OF_DESCRIPTION.gsub(/^ {8}/, '') #                           |<- 80 cols ends here
    \e[1mSELECTORS\e[0m

    \e[1mDescription\e[0m
        Selectors are used to identify what tests to run. Baretest knows 3 kinds of
        selectors: globs, tags and last-run-states. All of these can be preceeded
        with a minus sign (-), to negate the expression.
        Beware that you must use negated expressions only after a -- separator,
        as otherwise baretest will try to interpret them as short options (like -f).
    
    \e[1mExample\e[0m
        `baretest -- test/suite -test/suite/foo :a -:b %failure -%pending`

        This will run all tests that
        * Are in the directory test/suite or any of its subdirectories
        * Are NOT in the directory test/suite/foo, or any of its subdirectories
        * Have the tag 'a'
        * Do NOT have the tag 'b'
        * Terminated with a failure status on the last run
        * Did NOT terminate with a pending status on the last run
    
    \e[1mGlobs\e[0m
        * '**' recursively matches all files and directories
        * '*' wildcard, matches any amount of any character
        * '?' wildcard, matches one character 
        * '{a,b,c}' alternation, matches any pattern in the comma separated list
        * Directories are equivalent to `directory/**/*` patterns

    \e[1mTags\e[0m
        Tags are preceeded with a ':'.
        Examples:
          baretest :focus
          baretest -- -:hocus
          baretest -- :focus :important -:irrelevant -:obsolete

    \e[1mLast-run-status\e[0m
        Last run states are preceeded with a %.
        * %new, %success, %failure, %error, %skipped, %pending
        * %error, %skipped and %pending are a subset of %failure
        * %pending is a subset of %skipped
        * %new matches tests that are run for the very first time

  END_OF_DESCRIPTION

  description.gsub!(/\e.*?m/, '') unless colors

  puts description
end

.version(arguments, options) ⇒ Object

Show the baretest executable and library versions.



282
283
284
285
286
287
# File 'lib/baretest/commandline.rb', line 282

def version(arguments, options)
  puts "baretest executable version #{Version}",
       "library version #{BareTest::VERSION}",
       "ruby version #{RUBY_VERSION}",
       ""
end