Class | Spec::Runner::OptionParser |
In: |
lib/spec/runner/option_parser.rb
|
Parent: | Object |
BUILT_IN_FORMATTERS | = | { 'specdoc' => Formatter::SpecdocFormatter, 's' => Formatter::SpecdocFormatter, 'html' => Formatter::HtmlFormatter, 'h' => Formatter::HtmlFormatter, 'rdoc' => Formatter::RdocFormatter, 'r' => Formatter::RdocFormatter, 'progress' => Formatter::ProgressBarFormatter, 'p' => Formatter::ProgressBarFormatter, 'failing_examples' => Formatter::FailingExamplesFormatter, 'e' => Formatter::FailingExamplesFormatter, 'failing_behaviours' => Formatter::FailingBehavioursFormatter, 'b' => Formatter::FailingBehavioursFormatter |
COMMAND_LINE | = | { :diff => ["-D", "--diff [FORMAT]", "Show diff of objects that are expected to be equal when they are not", "Builtin formats: unified|u|context|c", "You can also specify a custom differ class", "(in which case you should also specify --require)"], :colour => ["-c", "--colour", "--color", "Show coloured (red/green) output"], :example => ["-e", "--example [NAME|FILE_NAME]", "Execute example(s) with matching name(s). If the argument is", "the path to an existing file (typically generated by a previous", "run using --format failing_examples:file.txt), then the examples", "on each line of thatfile will be executed. If the file is empty,", "all examples will be run (as if --example was not specified).", " ", "If the argument is not an existing file, then it is treated as", "an example name directly, causing RSpec to run just the example", "matching that name"], :specification => ["-s", "--specification [NAME]", "DEPRECATED - use -e instead", "(This will be removed when autotest works with -e)"], :line => ["-l", "--line LINE_NUMBER", Integer, "Execute behaviout or specification at given line.", "(does not work for dynamically generated specs)"], :format => ["-f", "--format FORMAT[:WHERE]", "Specifies what format to use for output. Specify WHERE to tell", "the formatter where to write the output. All built-in formats", "expect WHERE to be a file name, and will write to STDOUT if it's", "not specified. The --format option may be specified several times", "if you want several outputs", " ", "Builtin formats: ", "progress|p : Text progress", "specdoc|s : Behaviour doc as text", "rdoc|r : Behaviour doc as RDoc", "html|h : A nice HTML report", "failing_examples|e : Write all failing examples - input for --example", "failing_behaviours|b : Write all failing behaviours - input for --example", " ", "FORMAT can also be the name of a custom formatter class", "(in which case you should also specify --require to load it)"], :require => ["-r", "--require FILE", "Require FILE before running specs", "Useful for loading custom formatters or other extensions.", "If this option is used it must come before the others"], :backtrace => ["-b", "--backtrace", "Output full backtrace"], :loadby => ["-L", "--loadby STRATEGY", "Specify the strategy by which spec files should be loaded.", "STRATEGY can currently only be 'mtime' (File modification time)", "By default, spec files are loaded in alphabetical order if --loadby", "is not specified."], :reverse => ["-R", "--reverse", "Run examples in reverse order"], :timeout => ["-t", "--timeout FLOAT", "Interrupt and fail each example that doesn't complete in the", "specified time"], :heckle => ["-H", "--heckle CODE", "If all examples pass, this will mutate the classes and methods", "identified by CODE little by little and run all the examples again", "for each mutation. The intent is that for each mutation, at least", "one example *should* fail, and RSpec will tell you if this is not the", "case. CODE should be either Some::Module, Some::Class or", "Some::Fabulous#method}"], :dry_run => ["-d", "--dry-run", "Invokes formatters without executing the examples."], :options_file => ["-O", "--options PATH", "Read options from a file"], :generate_options => ["-G", "--generate-options PATH", "Generate an options file for --options"], :runner => ["-U", "--runner RUNNER", "Use a custom BehaviourRunner."], :drb => ["-X", "--drb", "Run examples via DRb. (For example against script/spec_server)"], :version => ["-v", "--version", "Show version"], :help => ["-h", "--help", "You're looking at it"] |
# File lib/spec/runner/option_parser.rb, line 82 82: def initialize 83: @spec_parser = SpecParser.new 84: @file_factory = File 85: end
# File lib/spec/runner/option_parser.rb, line 87 87: def create_behaviour_runner(args, err, out, warn_if_no_files) 88: options = parse(args, err, out, warn_if_no_files) 89: # Some exit points in parse (--generate-options, --drb) don't return the options, 90: # but hand over control. In that case we don't want to continue. 91: return nil unless options.is_a?(Options) 92: options.create_behaviour_runner 93: end
# File lib/spec/runner/option_parser.rb, line 95 95: def parse(args, err, out, warn_if_no_files) 96: options_file = nil 97: args_copy = args.dup 98: options = Options.new 99: 100: opts = ::OptionParser.new do |opts| 101: opts.banner = "Usage: spec (FILE|DIRECTORY|GLOB)+ [options]" 102: opts.separator "" 103: 104: def opts.rspec_on(name, &block) 105: on(*COMMAND_LINE[name], &block) 106: end 107: 108: opts.rspec_on(:diff) {|diff| options.parse_diff(diff, out, err)} 109: 110: opts.rspec_on(:colour) {options.colour = true} 111: 112: opts.rspec_on(:example) {|example| options.parse_example(example)} 113: 114: opts.rspec_on(:specification) {|example| options.parse_example(example)} 115: 116: opts.rspec_on(:line) {|line_number| options.line_number = line_number.to_i} 117: 118: opts.rspec_on(:format) {|format| options.parse_format(format, out, err)} 119: 120: opts.rspec_on(:require) {|req| options.parse_require(req)} 121: 122: opts.rspec_on(:backtrace) {options.backtrace_tweaker = NoisyBacktraceTweaker.new} 123: 124: opts.rspec_on(:loadby) {|loadby| options.loadby = loadby} 125: 126: opts.rspec_on(:reverse) {options.reverse = true} 127: 128: opts.rspec_on(:timeout) {|timeout| options.timeout = timeout.to_f} 129: 130: opts.rspec_on(:heckle) {|heckle| options.parse_heckle(heckle)} 131: 132: opts.rspec_on(:dry_run) {options.dry_run = true} 133: 134: opts.rspec_on(:options_file) do |options_file| 135: return parse_options_file(options_file, out, err, args_copy, warn_if_no_files) 136: end 137: 138: opts.rspec_on(:generate_options) do |options_file| 139: options.parse_generate_options(options_file, args_copy, out) 140: end 141: 142: opts.rspec_on(:runner) do |runner| 143: options.parse_runner(runner, out, err) 144: end 145: 146: opts.rspec_on(:drb) do 147: return parse_drb(args_copy, out, err, warn_if_no_files) 148: end 149: 150: opts.rspec_on(:version) {parse_version(out)} 151: 152: opts.on_tail(*COMMAND_LINE[:help]) {parse_help(opts, out)} 153: end 154: opts.parse!(args) 155: 156: if args.empty? && warn_if_no_files 157: err.puts "No files specified." 158: err.puts opts 159: exit(6) if err == $stderr 160: end 161: 162: if options.line_number 163: set_spec_from_line_number(options, args, err) 164: end 165: 166: if options.formatters.empty? 167: options.formatters << Formatter::ProgressBarFormatter.new(out) 168: end 169: 170: options 171: end
# File lib/spec/runner/option_parser.rb, line 183 183: def parse_drb(args_copy, out_stream, error_stream, warn_if_no_files) 184: # Remove the --drb option 185: index = args_copy.index("-X") || args_copy.index("--drb") 186: args_copy.delete_at(index) 187: 188: return DrbCommandLine.run(args_copy, error_stream, out_stream, true, warn_if_no_files) 189: end
# File lib/spec/runner/option_parser.rb, line 196 196: def parse_help(opts, out_stream) 197: out_stream.puts opts 198: exit if out_stream == $stdout 199: end
# File lib/spec/runner/option_parser.rb, line 173 173: def parse_options_fileparse_options_file(options_file, out_stream, error_stream, args_copy, warn_if_no_files) 174: # Remove the --options option and the argument before writing to file 175: index = args_copy.index("-O") || args_copy.index("--options") 176: args_copy.delete_at(index) 177: args_copy.delete_at(index) 178: 179: new_args = args_copy + IO.readlines(options_file).map {|l| l.chomp.split " "}.flatten 180: return CommandLine.run(new_args, error_stream, out_stream, true, warn_if_no_files) 181: end
# File lib/spec/runner/option_parser.rb, line 191 191: def parse_version(out_stream) 192: out_stream.puts ::Spec::VERSION::DESCRIPTION 193: exit if out_stream == $stdout 194: end
# File lib/spec/runner/option_parser.rb, line 201 201: def set_spec_from_line_number(options, args, err) 202: if options.examples.empty? 203: if args.length == 1 204: if @file_factory.file?(args[0]) 205: source = @file_factory.open(args[0]) 206: example = @spec_parser.spec_name_for(source, options.line_number) 207: options.parse_example(example) 208: elsif @file_factory.directory?(args[0]) 209: err.puts "You must specify one file, not a directory when using the --line option" 210: exit(1) if err == $stderr 211: else 212: err.puts "#{args[0]} does not exist" 213: exit(2) if err == $stderr 214: end 215: else 216: err.puts "Only one file can be specified when using the --line option: #{args.inspect}" 217: exit(3) if err == $stderr 218: end 219: else 220: err.puts "You cannot use both --line and --example" 221: exit(4) if err == $stderr 222: end 223: end