Skip to content

Commit

Permalink
Add support for running specs against :sass syntax AST
Browse files Browse the repository at this point in the history
Only test against :sass AST if a :sass example is provided in the spec.
Convert bang_format_spec examples to all have :sass syntax counterparts.
  • Loading branch information
mdiebolt committed Feb 23, 2015
1 parent c0957c8 commit 0710e44
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 19 deletions.
15 changes: 8 additions & 7 deletions lib/scss_lint/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ class Engine
# or file.
#
# @param scss_or_filename [String]
def initialize(scss_or_filename)
if File.exist?(scss_or_filename)
build_from_file(scss_or_filename)
def initialize(str_or_filename, syntax=:scss)
if File.exist?(str_or_filename)
build_from_file(str_or_filename)
else
build_from_string(scss_or_filename)
build_from_string(str_or_filename, syntax)
end

# Need to force encoding to avoid Windows-related bugs.
Expand All @@ -39,16 +39,17 @@ def initialize(scss_or_filename)
private

# @param path [String]
# TODO: parse file extension switch sass syntax option
def build_from_file(path)
@filename = path
@engine = Sass::Engine.for_file(path, ENGINE_OPTIONS)
@contents = File.open(path, 'r').read
end

# @param scss [String]
def build_from_string(scss)
@engine = Sass::Engine.new(scss, ENGINE_OPTIONS)
@contents = scss
def build_from_string(str, syntax)
@engine = Sass::Engine.new(str, ENGINE_OPTIONS.merge(syntax: syntax))
@contents = str
end
end
end
56 changes: 56 additions & 0 deletions spec/scss_lint/linter/bang_format_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
}
SCSS

let(:sass) { <<-SASS }
p
color: #000
SASS

it { should_not report_lint }
end

Expand All @@ -18,6 +23,11 @@
}
SCSS

let(:sass) { <<-SASS }
p
color: #000 !important
SASS

it { should_not report_lint }
end

Expand All @@ -28,6 +38,11 @@
}
SCSS

let(:sass) { <<-SASS }
p
color: #000!important
SASS

it { should report_lint line: 2 }
end

Expand All @@ -38,6 +53,11 @@
}
SCSS

let(:sass) { <<-SASS }
p
color: #000 ! important
SASS

it { should report_lint line: 2 }
end

Expand All @@ -50,6 +70,11 @@
}
SCSS

let(:sass) { <<-SASS }
p
color: #000 ! important
SASS

it { should_not report_lint }
end

Expand All @@ -62,6 +87,11 @@
}
SCSS

let(:sass) { <<-SASS }
p
color: #000 ! important
SASS

it { should report_lint line: 2 }
end

Expand All @@ -74,6 +104,11 @@
}
SCSS

let(:sass) { <<-SASS }
p
color: #000!important
SASS

it { should_not report_lint }
end

Expand All @@ -94,6 +129,27 @@
$foo: "b!ar";
SCSS

let(:sass) { <<-SASS }
p:before
content: "!important"
p:before
content: "imp!ortant"
p:after
content: '!'
div:before
content: 'important!'
div:after
content: ' ! '
p[attr="foo!bar"]
p[attr='foo!bar']
p[attr="!foobar"]
p[attr='foobar!']
$foo: 'bar!'
$foo: "bar!"
$foo: "!bar"
$foo: "b!ar"
SASS

it { should_not report_lint }
end

Expand Down
29 changes: 17 additions & 12 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@

Dir[File.dirname(__FILE__) + '/support/**/*.rb'].each { |f| require f }

def lint(style, syntax)
initial_indent = style[/\A(\s*)/, 1]
normalized = style.gsub(/^#{initial_indent}/, '')

local_config = if respond_to?(:linter_config)
linter_config
else
SCSSLint::Config.default.linter_options(subject)
end

if style
subject.run(SCSSLint::Engine.new(normalized, syntax), local_config)
end
end

RSpec.configure do |config|
config.expect_with :rspec do |c|
c.syntax = [:expect, :should]
Expand All @@ -17,18 +32,8 @@
# for each example. This significantly DRYs up our linter specs to contain
# only tests, since all the setup code is now centralized here.
if described_class <= SCSSLint::Linter
initial_indent = scss[/\A(\s*)/, 1]
normalized_css = scss.gsub(/^#{initial_indent}/, '')

# Use the configuration settings defined by default unless a specific
# configuration has been provided for the test.
local_config = if respond_to?(:linter_config)
linter_config
else
SCSSLint::Config.default.linter_options(subject)
end

subject.run(SCSSLint::Engine.new(normalized_css), local_config)
lint(scss, :scss)
lint(sass, :sass) if defined? sass
end
end
end

0 comments on commit 0710e44

Please sign in to comment.