Skip to content

Commit

Permalink
Support any command line flags in the rake task
Browse files Browse the repository at this point in the history
  • Loading branch information
srawlins authored and sds committed Aug 31, 2015
1 parent 1759e74 commit e2a96cb
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 13 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[![Dependency Status](https://gemnasium.com/brigade/scss-lint.svg)](https://gemnasium.com/brigade/scss-lint)

`scss-lint` is a tool to help keep your [SCSS](http://sass-lang.com) files
clean and readable. You can run it manually from the command-line, or integrate
clean and readable. You can run it manually from the command line, or integrate
it into your [SCM hooks](https://github.com/brigade/overcommit).

* [Requirements](#requirements)
Expand Down Expand Up @@ -55,7 +55,7 @@ context in which you are linting, nowhere else.

## Usage

Run `scss-lint` from the command-line by passing in a directory (or multiple
Run `scss-lint` from the command line by passing in a directory (or multiple
directories) to recursively scan:

```bash
Expand Down Expand Up @@ -463,10 +463,15 @@ require 'scss_lint/rake_task'
SCSSLint::RakeTask.new do |t|
t.config = 'custom/config.yml'
t.args = ['--formatter', 'JSON', '--out', 'results.txt']
t.files = ['app/assets', 'custom/*.scss']
end
```

You can specify any command line arguments in the `args` attribute that are
allowed by the `scss-lint` Ruby binary script. Each argument must be passed as
an Array element, rather than one String with spaces.

You can also use this custom configuration with a set of files specified via
the command line:

Expand Down
7 changes: 6 additions & 1 deletion lib/scss_lint/rake_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ class RakeTask < Rake::TaskLib
# @return [String]
attr_accessor :config

# Command-line args to use.
# @return [Array<String>]
attr_accessor :args

# List of files to lint (can contain shell globs).
#
# Note that this will be ignored if you explicitly pass a list of files as
Expand Down Expand Up @@ -67,7 +71,7 @@ def define
def run_cli(task_args)
cli_args = ['--config', config] if config

result = SCSSLint::CLI.new.run(Array(cli_args) + files_to_lint(task_args))
result = SCSSLint::CLI.new.run(Array(cli_args) + Array(args) + files_to_lint(task_args))

message =
case result
Expand Down Expand Up @@ -98,6 +102,7 @@ def files_to_lint(task_args)
def default_description
description = 'Run `scss-lint'
description += " --config #{config}" if config
description += " #{args}" if args
description += " #{files.join(' ')}" if files.any?
description += ' [files...]`'
description
Expand Down
69 changes: 59 additions & 10 deletions spec/scss_lint/rake_task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
require 'tempfile'

describe SCSSLint::RakeTask do
before(:all) do
SCSSLint::RakeTask.new
end

before do
STDOUT.stub(:write) # Silence console output
end

after(:each) do
Rake::Task['scss_lint'].clear if Rake::Task.task_defined?('scss_lint')
end

let(:file) do
Tempfile.new(%w[scss-file .scss]).tap do |f|
f.write(scss)
Expand All @@ -25,19 +25,68 @@ def run_task
end
end

context 'when SCSS document is valid with no lints' do
context 'basic RakeTask' do
before(:each) do
SCSSLint::RakeTask.new
end

context 'when SCSS document is valid with no lints' do
let(:scss) { '' }

it 'does not call Kernel.exit' do
expect { run_task }.not_to raise_error
end
end

context 'when SCSS document is invalid' do
let(:scss) { '.class {' }

it 'calls Kernel.exit with the status code' do
expect { run_task }.to raise_error SystemExit
end
end
end

context 'configured RakeTask with a config file' do
let(:scss) { '' }

it 'does not call Kernel.exit' do
let(:config_file) do
config = Tempfile.new(%w[foo .yml])
config.write('')
config.close
config.path
end

it 'passes config files to the CLI' do
SCSSLint::RakeTask.new.tap do |t|
t.config = config_file
end

cli = double(SCSSLint::CLI)
SCSSLint::CLI.should_receive(:new) { cli }
args = ['--config', config_file, file.path]
cli.should_receive(:run).with(args) { 0 }

expect { run_task }.not_to raise_error
end
end

context 'when SCSS document is invalid' do
let(:scss) { '.class {' }
context 'configured RakeTask with args' do
let(:scss) { '' }

it 'passes args to the CLI' do
formatter_args = ['--formatter', 'JSON']

SCSSLint::RakeTask.new.tap do |t|
t.args = formatter_args
end

it 'calls Kernel.exit with the status code' do
expect { run_task }.to raise_error SystemExit
cli = double(SCSSLint::CLI)
SCSSLint::CLI.should_receive(:new) { cli }
args = formatter_args + [file.path]
cli.should_receive(:run).with(args) { 0 }

expect { run_task }.not_to raise_error
end
end
end

0 comments on commit e2a96cb

Please sign in to comment.