Class: Cucumber::CLI

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

Constant Summary collapse

FORMATS =
%w{pretty profile progress html autotest}
DEFAULT_FORMAT =
'pretty'

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(out_stream = STDOUT, error_stream = STDERR) ⇒ CLI

Returns a new instance of CLI.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cucumber/cli.rb', line 31

def initialize(out_stream = STDOUT, error_stream = STDERR)
  @out_stream = out_stream
  @error_stream = error_stream
  @paths = []
  @options = {
    :require => nil,
    :lang    => 'en',
    :dry_run => false,
    :source  => true,
    :snippets => true,
    :formats => {},
    :excludes => [],
    :scenario_names => nil
  }
  @active_format = DEFAULT_FORMAT
end

Class Attribute Details

.executor=(value) ⇒ Object (writeonly)

Sets the attribute executor

Parameters:

  • value

    the value to set the attribute executor to.



9
10
11
# File 'lib/cucumber/cli.rb', line 9

def executor=(value)
  @executor = value
end

.features=(value) ⇒ Object (writeonly)

Sets the attribute features

Parameters:

  • value

    the value to set the attribute features to.



9
10
11
# File 'lib/cucumber/cli.rb', line 9

def features=(value)
  @features = value
end

.step_mother=(value) ⇒ Object (writeonly)

Sets the attribute step_mother

Parameters:

  • value

    the value to set the attribute step_mother to.



9
10
11
# File 'lib/cucumber/cli.rb', line 9

def step_mother=(value)
  @step_mother = value
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



27
28
29
# File 'lib/cucumber/cli.rb', line 27

def options
  @options
end

#pathsObject (readonly)

Returns the value of attribute paths.



27
28
29
# File 'lib/cucumber/cli.rb', line 27

def paths
  @paths
end

Class Method Details

.executeObject



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

def execute
  @execute_called = true
  parse(ARGV).execute!(@step_mother, @executor, @features)
end

.execute_called?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/cucumber/cli.rb', line 16

def execute_called?
  @execute_called
end

.parse(args) ⇒ Object



20
21
22
23
24
# File 'lib/cucumber/cli.rb', line 20

def parse(args)
  cli = new
  cli.parse_options!(args)
  cli
end

Instance Method Details

#execute!(step_mother, executor, features) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
# File 'lib/cucumber/cli.rb', line 145

def execute!(step_mother, executor, features)
  Term::ANSIColor.coloring = @options[:color] unless @options[:color].nil?
  Cucumber.load_language(@options[:lang])
  require_files
  executor.formatters = build_formatter_broadcaster(step_mother)
  load_plain_text_features(features)
  executor.lines_for_features = @options[:lines_for_features]
  executor.scenario_names = @options[:scenario_names] if @options[:scenario_names]
  executor.visit_features(features)
  exit 1 if executor.failed
end

#parse_options!(args) ⇒ Object



48
49
50
51
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
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
# File 'lib/cucumber/cli.rb', line 48

def parse_options!(args)
  return parse_args_from_profile('default') if args.empty?
  args.extend(OptionParser::Arguable)

  args.options do |opts|
    opts.banner = ["Usage: cucumber [options] [[FILE[:LINE[:LINE]*]] | [FILES|DIRS]]", "",
      "Examples:",
      "cucumber examples/i18n/en/features",
      "cucumber --language it examples/i18n/it/features/somma.feature:6:98:113", "", ""
    ].join("\n")
    opts.on("-r LIBRARY|DIR", "--require LIBRARY|DIR", "Require files before executing the features.",
      "If this option is not specified, all *.rb files that",
      "are siblings or below the features will be autorequired",
      "This option can be specified multiple times.") do |v|
      @options[:require] ||= []
      @options[:require] << v
    end
    opts.on("-s SCENARIO", "--scenario SCENARIO", "Only execute the scenario with the given name.",
                                                  "If this option is given more than once, run all",
                                                  "the specified scenarios.") do |v|
      @options[:scenario_names] ||= []
      @options[:scenario_names] << v
    end
    opts.on("-l LANG", "--language LANG", "Specify language for features (Default: #{@options[:lang]})",
      "Available languages: #{Cucumber.languages.join(", ")}",
      "Look at #{Cucumber::LANGUAGE_FILE} for keywords") do |v|
      @options[:lang] = v
    end
    opts.on("-f FORMAT", "--format FORMAT", "How to format features (Default: #{DEFAULT_FORMAT})",
      "Available formats: #{FORMATS.join(", ")}",
      "You can also provide your own formatter classes as long as they have been",
      "previously required using --require or if they are in the folder",
      "structure such that cucumber will require them automatically.",
      "This option can be specified multiple times.") do |v|
  
      @options[:formats][v] ||= []
      @options[:formats][v] << @out_stream
      @active_format = v
    end
    opts.on("-o", "--out FILE", "Write output to a file instead of @out_stream.",
      "This option can be specified multiple times, and applies to the previously",
      "specified --format.") do |v|
      @options[:formats][@active_format] ||= []
      if @options[:formats][@active_format].last == @out_stream
        @options[:formats][@active_format][-1] = File.open(v, 'w')
      else
        @options[:formats][@active_format] << File.open(v, 'w')
      end
    end
    opts.on("-c", "--[no-]color", "Use ANSI color in the output, if formatters use it.  If",
                                  "these options are given multiple times, the last one is",
                                  "used.  If neither --color or --no-color is given cucumber",
                                  "decides based on your platform and the output destination") do |v|
      @options[:color] = v
    end
    opts.on("-e", "--exclude PATTERN", "Don't run features matching a pattern") do |v|
      @options[:excludes] << v
    end
    opts.on("-p", "--profile PROFILE", "Pull commandline arguments from cucumber.yml.") do |v|
      parse_args_from_profile(v)
    end
    opts.on("-d", "--dry-run", "Invokes formatters without executing the steps.") do
      @options[:dry_run] = true
    end
    opts.on("-n", "--no-source", "Don't show the file and line of the step definition with the steps.") do
      @options[:source] = false
    end
    opts.on("-i", "--no-snippets", "Don't show the snippets for pending steps") do
      @options[:snippets] = false
    end
    opts.on("-q", "--quiet", "Don't show any development aid information") do
      @options[:snippets] = false
      @options[:source] = false
    end
    opts.on("-v", "--verbose", "Show the files and features loaded") do
      @options[:verbose] = true
    end
    opts.on_tail("--version", "Show version") do
      @out_stream.puts VERSION::STRING
      Kernel.exit
    end
    opts.on_tail("--help", "You're looking at it") do
      @out_stream.puts opts.help
      Kernel.exit
    end
  end.parse!

  if @options[:formats].empty?
    @options[:formats][DEFAULT_FORMAT] = [@out_stream]
  end
  
  # Whatever is left after option parsing is the FILE arguments
  args = extract_and_store_line_numbers(args)
  @paths += args
end