Skip to content

Commit

Permalink
Add --out flag to redirect output to file
Browse files Browse the repository at this point in the history
Change-Id: I61012c538b569766a4306a76b0a6fbb526cc4c29
Reviewed-on: http://gerrit.causes.com/44157
Tested-by: jenkins <[email protected]>
Reviewed-by: Shane da Silva <[email protected]>
  • Loading branch information
Adam Eberlin authored and sds committed Nov 3, 2014
1 parent 3952a9d commit 81ab90f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Command Line Flag | Description
`-c`/`--config` | Specify a configuration file to use
`-e`/`--exclude` | Exclude one or more files from being linted
`-f`/`--format` | Output format (see [Formatters](#formatters))
`-o`/`--out` | Write output to a file instead of STDOUT
`-i`/`--include-linter` | Specify which linters you specifically want to run
`-x`/`--exclude-linter` | Specify which linters you _don't_ want to run
`-h`/`--help` | Show command line flag documentation
Expand Down
29 changes: 23 additions & 6 deletions lib/scss_lint/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ class CLI
config: 78, # Configuration error
}

DEFAULT_REPORTER = [SCSSLint::Reporter::DefaultReporter, :stdout]

# @param args [Array]
def initialize(args = [])
@args = args
@options = {}
@options = { reporters: [DEFAULT_REPORTER] }
@config = Config.default
end

Expand Down Expand Up @@ -72,6 +74,10 @@ def options_parser # rubocop:disable MethodLength
define_output_format(format)
end

opts.on('-o', '--out path', 'Write output to a file instead of STDOUT', String) do |path|
define_output_path(path)
end

opts.on_tail('--show-formatters', 'Shows available formatters') do
print_formatters
end
Expand Down Expand Up @@ -198,20 +204,31 @@ def scssish_file?(file)
# @param lints [Array<Lint>]
def report_lints(lints)
sorted_lints = lints.sort_by { |l| [l.filename, l.location] }
reporter = @options.fetch(:reporter, SCSSLint::Reporter::DefaultReporter)
.new(sorted_lints)
output = reporter.report_lints
print output if output
@options.fetch(:reporters).each do |reporter, output|
results = reporter.new(sorted_lints).report_lints
io = (output == :stdout ? $stdout : File.new(output, 'w+'))
io.print results if results
end
end

# @param format [String]
def define_output_format(format)
@options[:reporter] = SCSSLint::Reporter.const_get(format + 'Reporter')
unless @options[:reporters] == [DEFAULT_REPORTER] && format == 'Default'
@options[:reporters].reject! { |i| i == DEFAULT_REPORTER }
reporter = SCSSLint::Reporter.const_get(format + 'Reporter')
@options[:reporters] << [reporter, :stdout]
end
rescue NameError
puts "Invalid output format specified: #{format}"
halt :config
end

# @param path [String]
def define_output_path(path)
last_reporter, _output = @options[:reporters].pop
@options[:reporters] << [last_reporter, path]
end

def print_formatters
puts 'Installed formatters:'

Expand Down
38 changes: 36 additions & 2 deletions spec/scss_lint/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,30 @@ def safe_parse

it 'includes all linters except the excluded one' do
safe_parse
subject.config.enabled_linters.should == [SCSSLint::Linter::FakeTestLinter2]
subject.config.enabled_linters.should == \
[SCSSLint::Linter::FakeTestLinter2]
end
end

context 'when neither format nor out flag is set' do
let(:flags) { %w[] }

it 'sets the default reporter to output to stdout' do
safe_parse
subject.options[:reporters].should \
include([SCSSLint::Reporter::DefaultReporter, :stdout])
end
end

context 'when the out flag is set' do
context 'and the path is valid' do
let(:flags) { %w[--out foo.txt] }

it 'sets the default :output to the given path' do
safe_parse
subject.options[:reporters].should \
include([SCSSLint::Reporter::DefaultReporter, 'foo.txt'])
end
end
end

Expand All @@ -103,7 +126,18 @@ def safe_parse

it 'sets the :reporter option to the correct reporter' do
safe_parse
subject.options[:reporter].should == SCSSLint::Reporter::XMLReporter
subject.options[:reporters].should \
include([SCSSLint::Reporter::XMLReporter, :stdout])
end
end

context 'and an out path is specified' do
let(:flags) { %w[--format XML --out foo.txt] }

it 'sets the specified reporter :output to the given path' do
safe_parse
subject.options[:reporters].should \
include([SCSSLint::Reporter::XMLReporter, 'foo.txt'])
end
end

Expand Down

0 comments on commit 81ab90f

Please sign in to comment.