Skip to content

Commit

Permalink
Merge 7b572e9 into 13c621d
Browse files Browse the repository at this point in the history
  • Loading branch information
brasmusson committed Apr 13, 2015
2 parents 13c621d + 7b572e9 commit 87d6371
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 5 deletions.
23 changes: 22 additions & 1 deletion features/docs/cli/specifying_multiple_formatters.feature
Expand Up @@ -3,8 +3,9 @@ Feature: Running multiple formatters

When running cucumber, you are able to using multiple different
formatters and redirect the output to text files.
Two formatters cannot both print to the same file (or to STDOUT)

Scenario: Multiple formatters and outputs
Background:
Given a file named "features/test.feature" with:
"""
Feature: Lots of undefined
Expand All @@ -16,6 +17,8 @@ Feature: Running multiple formatters
When I stop procrastinating
And there is world peace
"""

Scenario: Multiple formatters and outputs
When I run `cucumber --no-color --format progress --out progress.txt --format pretty --out pretty.txt --no-source --dry-run --no-snippets features/test.feature`
Then the stderr should not contain anything
Then the file "progress.txt" should contain:
Expand All @@ -42,3 +45,21 @@ Feature: Running multiple formatters
"""

Scenario: Two formatters to stdout
When I run `cucumber -f progress -f pretty features/test.feature`
Then it should fail with:
"""
All but one formatter must use --out, only one can print to each stream (or STDOUT) (RuntimeError)
"""

Scenario: Two formatters to stdout when using a profile
Given the following profiles are defined:
"""
default: -q
"""
When I run `cucumber -f progress -f pretty features/test.feature`
Then it should fail with:
"""
All but one formatter must use --out, only one can print to each stream (or STDOUT) (RuntimeError)
"""

5 changes: 1 addition & 4 deletions lib/cucumber/cli/configuration.rb
Expand Up @@ -201,10 +201,7 @@ def arrange_formats
@options[:formats] << ['pretty', @out_stream] if @options[:formats].empty?
@options[:formats] = @options[:formats].sort_by{|f| f[1] == @out_stream ? -1 : 1}
@options[:formats].uniq!
streams = @options[:formats].map { |(_, stream)| stream }
if streams != streams.uniq
raise "All but one formatter must use --out, only one can print to each stream (or STDOUT)"
end
@options.check_formatter_stream_conflicts()
end

def remove_excluded_files_from(files)
Expand Down
9 changes: 9 additions & 0 deletions lib/cucumber/cli/options.rb
Expand Up @@ -263,6 +263,8 @@ def parse!(args)
extract_environment_variables
@options[:paths] = @args.dup #whatver is left over

check_formatter_stream_conflicts()

merge_profiles

self
Expand All @@ -276,6 +278,13 @@ def filters
@options[:filters] ||= []
end

def check_formatter_stream_conflicts()
streams = @options[:formats].uniq.map { |(_, stream)| stream }
if streams != streams.uniq
raise "All but one formatter must use --out, only one can print to each stream (or STDOUT)"
end
end

protected

attr_reader :options, :profiles, :expanded_args
Expand Down
10 changes: 10 additions & 0 deletions spec/cucumber/cli/configuration_spec.rb
Expand Up @@ -300,6 +300,11 @@ def reset_config
expect(-> { config.parse!(%w{--format pretty --format progress}) }).to raise_error("All but one formatter must use --out, only one can print to each stream (or STDOUT)")
end

it "does not accept multiple --format options when both use implicit STDOUT (using profile with no formatters)" do
given_cucumber_yml_defined_as({'default' => ['-q']})
expect(-> { config.parse!(%w{--format pretty --format progress}) }).to raise_error("All but one formatter must use --out, only one can print to each stream (or STDOUT)")
end

it "accepts same --format options with implicit STDOUT, and keep only one" do
config.parse!(%w{--format pretty --format pretty})

Expand All @@ -310,6 +315,11 @@ def reset_config
expect(-> { config.parse!(%w{--format pretty --out file1 --format progress --out file1}) }).to raise_error("All but one formatter must use --out, only one can print to each stream (or STDOUT)")
end

it "does not accept multiple --out streams pointing to the same place (one from profile, one from command line)" do
given_cucumber_yml_defined_as({'default' => ['-f','progress', '--out', 'file1']})
expect(-> { config.parse!(%w{--format pretty --out file1}) }).to raise_error("All but one formatter must use --out, only one can print to each stream (or STDOUT)")
end

it "associates --out to previous --format" do
config.parse!(%w{--format progress --out file1 --format profile --out file2})

Expand Down
29 changes: 29 additions & 0 deletions spec/cucumber/cli/options_spec.rb
Expand Up @@ -88,6 +88,35 @@ def after_parsing(args)
end
end

context 'handling multiple formatters' do
it "catches multiple command line formatters using the same stream" do
expect{ options.parse!(prepare_args('-f pretty -f progress')) }.to raise_error("All but one formatter must use --out, only one can print to each stream (or STDOUT)")
end

it "catches multiple profile formatters using the same stream" do
given_cucumber_yml_defined_as({'default' => '-f progress -f pretty'})
options = Options.new(output_stream, error_stream, :default_profile => 'default')

expect{ options.parse!(%w{}) }.to raise_error("All but one formatter must use --out, only one can print to each stream (or STDOUT)")
end

it "profiles does not affect the catching of multiple command line formatters using the same stream" do
given_cucumber_yml_defined_as({'default' => '-q'})
options = Options.new(output_stream, error_stream, :default_profile => 'default')

expect{ options.parse!(%w{-f progress -f pretty}) }.to raise_error("All but one formatter must use --out, only one can print to each stream (or STDOUT)")
end

it "merges profile formatters and command line formatters" do
given_cucumber_yml_defined_as({'default' => '-f junit -o result.xml'})
options = Options.new(output_stream, error_stream, :default_profile => 'default')

options.parse!(%w{-f pretty})

expect(options[:formats]).to eq [['pretty', output_stream], ["junit", "result.xml"]]
end
end

context '-t TAGS --tags TAGS' do
it "designates tags prefixed with ~ as tags to be excluded" do
after_parsing('--tags ~@foo,@bar') { expect(options[:tag_expressions]).to eq ['~@foo,@bar'] }
Expand Down

0 comments on commit 87d6371

Please sign in to comment.