lib/teaspoon/command_line.rb in teaspoon-0.7.9 vs lib/teaspoon/command_line.rb in teaspoon-0.8.0

- old
+ new

@@ -1,157 +1,139 @@ require "optparse" require "teaspoon/version" +require "teaspoon/exceptions" module Teaspoon class CommandLine def initialize @options = {} - @files = opt_parser.parse! + @options[:files] = opt_parser.parse! - begin - require_console - abort if Teaspoon::Console.new(@options, @files).execute - rescue Teaspoon::EnvironmentNotFound => e - STDOUT.print "Unable to load Teaspoon environment in {#{Teaspoon::Environment.standard_environments.join(', ')}}.\n" - STDOUT.print "Consider using -r path/to/teaspoon_env\n" - abort - end + require_console + abort if Teaspoon::Console.new(@options).failures? + rescue Teaspoon::EnvironmentNotFound => e + abort("#{e.message}\nConsider using -r path/to/teaspoon_env\n") end def opt_parser OptionParser.new do |parser| - parser.banner = "Usage: teaspoon [options] [files]\n\n" + @parser = parser + @parser.banner = "Usage: teaspoon [options] [files]\n\n" - parser.on("-r", "--require FILE", "Require Teaspoon environment file.") do |file| - @options[:environment] = file - end + opts_for_general + opts_for_filtering + opts_for_output + opts_for_coverage + opts_for_utility + end + end - #parser.on("-O", "--options PATH", "Specify the path to a custom options file.") do |path| - # @options[:custom_options_file] = path - #end + protected - parser.on("-d", "--driver DRIVER", "Specify driver:", - " phantomjs (default)", - " selenium") do |driver| - @options[:driver] = driver - end + def opts_for_general + opt :environment, + "-r", "--require FILE", + "Require Teaspoon environment file." - parser.on("-o", "--driver-cli-options OPTIONS", "Specify driver-specific options string to pass into the driver, e.g.", - " '--ssl-protocol=any --ssl-certificates-path=/path/to/certs' could be used for phantomjs", - " Currently driver CLI options are only supported for phantomjs. It will be ignored if using the selenium driver.") do |driver_cli_options| - @options[:driver_cli_options] = driver_cli_options - end + #opt :custom_options_file, + # "-O", "--options PATH", + # "Specify the path to a custom options file." - parser.on("--server SERVER", "Sets server to use with Rack.") do |server| - @options[:server] = server - end + opt :driver, "-d", "--driver DRIVER", + "Specify driver:", + " phantomjs (default)", + " selenium" - parser.on("--server-timeout SECONDS", "Sets the timeout for the server to start.") do |seconds| - @options[:server_timeout] = seconds - end + opt :driver_options, "--driver-options OPTIONS", + "Specify driver-specific options to pass into the driver.", + " e.g. \"--ssl-protocol=any --ssl-certificates-path=/path/to/certs\".", + " Driver options are only supported with phantomjs." - parser.on("--server-port PORT", "Sets the server to use a specific port.") do |port| - @options[:server_port] = port - end + opt :driver_timeout, "--driver-timeout SECONDS", + "Sets the timeout for the driver to wait before exiting." - parser.on("--[no-]fail-fast", "Abort after the first failing suite.") do |bool| - @options[:fail_fast] = bool - end + opt :server, "--server SERVER", + "Sets server to use with Rack.", + " e.g. webrick, thin" - parser.separator("\n **** Filtering ****\n\n") + opt :server_port, "--server-port PORT", + "Sets the server to use a specific port." - parser.on("-s", "--suite SUITE", "Focus to a specific suite.") do |suite| - @options[:suite] = suite - end + opt :server_timeout, "--server-timeout SECONDS", + "Sets the timeout that the server must start within." - parser.on("-g", "--filter FILTER", "Filter tests matching a specific filter.") do |filter| - @options[:filter] = filter - end + opt :fail_fast, "-F", "--[no-]fail-fast", + "Abort after the first failing suite." + end - parser.separator("\n **** Output ****\n\n") + def opts_for_filtering + separator("Filtering") - parser.on("-f", "--format FORMATTERS", "Specify formatters (comma separated)", - " dot (default) - dots", - " tap_y - format used by tapout", - " swayze_or_oprah - random quote from Patrick Swayze or Oprah Winfrey") do |formatters| - @options[:formatters] = formatters - end + opt :suite, "-s", "--suite SUITE", + "Focus to a specific suite." - parser.on("-q", "--[no-]suppress-log", "Suppress logs coming from console[log/debug/error].") do |bool| - @options[:suppress_log] = bool - end + opt :filter, "-g", "--filter FILTER", + "Filter tests matching a specific filter." + end - parser.on("-c", "--[no-]colour", "Enable/Disable color output.") do |bool| - @options[:color] = bool - end + def opts_for_output + separator("Output") - parser.separator("\n **** Coverage ****\n\n") + opt :suppress_log, "-q", "--[no-]suppress-log", + "Suppress logs coming from console[log/debug/error]." - parser.on("-C", "--coverage", "Generate coverage report (requires Istanbul).") do |bool| - @options[:coverage] = bool - end + opt :color, "-c", "--[no-]color", + "Enable/Disable color output." - parser.on("-R", "--coverage-reports FORMATS", "Specify which coverage reports to generate (comma separated)", - " text-summary (default) - compact text summary in results", - " text - text table with coverage for all files in results", - " html - HTML files with annotated source code", - " lcov - html + lcov files", - " lcovonly - an lcov.info file", - " cobertura - cobertura-coverage.xml used by Hudson") do |reports| - @options[:coverage] = true - @options[:coverage_reports] = reports - end + opt :export, "-e", "--export [OUTPUT_PATH]", + "Exports the test suite as the full HTML (requires wget)." - parser.on("-O", "--coverage-output-dir DIR", "Specify directory where coverage reports should be generated.") do |dir| - @options[:coverage_output_dir] = dir - end + opt :formatters, "-f", "--format FORMATTERS", + "Specify formatters (comma separated)", + " dot (default) - dots", + " documentation - descriptive documentation", + " clean - like dots but doesn't log re-run commands", + " json - json formatter (raw teaspoon)", + " junit - junit compatible formatter", + " pride - yay rainbows!", + " snowday - makes you feel warm inside", + " swayze_or_oprah - quote from either Patrick Swayze or Oprah Winfrey", + " tap - test anything protocol formatter", + " tap_y - tap_yaml, format used by tapout", + " teamcity - teamcity compatible formatter" + end - parser.separator("\n **** Coverage Thresholds ****\n\n") + def opts_for_coverage + separator("Coverage") - parser.on("-S", "--statements-coverage-threshold THRESHOLD", "Specify the statements coverage threshold.", - " If this is a positive number, it is the minimum percentage required for coverage to not fail.", - " If it is a negative number, it is the maximum number of uncovered statements allowed to not fail.") do |threshold| - @options[:statements_coverage_threshold] = threshold - end + opt :use_coverage, "-C", "--coverage CONFIG_NAME", + "Generate coverage reports using a pre-defined coverage configuration." + end - parser.on("-F", "--functions-coverage-threshold THRESHOLD", "Specify the functions coverage threshold.", - " If this is a positive number, it is the minimum percentage required for coverage to not fail.", - " If it is a negative number, it is the maximum number of uncovered functions allowed to not fail.") do |threshold| - @options[:functions_coverage_threshold] = threshold - end + def opts_for_utility + separator("Utility") - parser.on("-B", "--branches-coverage-threshold THRESHOLD", "Specify the branches coverage threshold.", - " If this is a positive number, it is the minimum percentage required for coverage to not fail.", - " If it is a negative number, it is the maximum number of uncovered branches allowed to not fail.") do |threshold| - @options[:branches_coverage_threshold] = threshold - end + @parser.on "-v", "--version", "Display the version.", proc{ STDOUT.print("#{Teaspoon::VERSION}\n"); exit } + @parser.on "-h", "--help", "You're looking at it.", proc { STDOUT.print("#{@parser}\n"); exit } + end - parser.on("-L", "--lines-coverage-threshold THRESHOLD", "Specify the lines coverage threshold.", - " If this is a positive number, it is the minimum percentage required for coverage to not fail.", - " If it is a negative number, it is the maximum number of uncovered lines allowed to not fail.") do |threshold| - @options[:lines_coverage_threshold] = threshold - end + private - parser.separator("\n **** Utility ****\n\n") + def separator(message) + @parser.separator("\n **** #{message} ****\n\n") + end - parser.on("-v", "--version", "Display the version.") do - puts Teaspoon::VERSION - exit - end - - parser.on("-h", "--help", "You're looking at it.") do - puts parser - exit - end - end + def opt(config, *args) + @parser.on(*args, proc{ |value| @options[config] = value }) end def require_console require "teaspoon/console" end - def abort + def abort(message = nil) + STDOUT.print(message) if message exit(1) end end end