-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Move logic for parsing and linting into new LessFile class - Extend LessFile as LessCachedFile with caching specific implementation - Move non grunt task files to tasks/lib to prevent being read by grunt - Add extra tests for imports invalidation in less-file-spec.coffee
- Loading branch information
Showing
10 changed files
with
388 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
path = require 'path' | ||
grunt = require 'grunt' | ||
{LessFile, LessCachedFile} = require '../tasks/lib/less-file' | ||
|
||
describe 'less-file', -> | ||
describe 'LessFile', -> | ||
filePath = path.join(__dirname, 'fixtures', 'valid.less') | ||
file = null | ||
|
||
beforeEach -> | ||
file = new LessFile(filePath, {}, grunt) | ||
|
||
it 'can load contents', -> | ||
contents = file.getContents() | ||
expect(contents).toBe grunt.file.read(filePath) | ||
|
||
it 'can get a hash', -> | ||
hash = file.getDigest() | ||
expect(hash).toNotBe null | ||
expect(hash.length).toBeGreaterThan 0 | ||
|
||
it 'can lint a file', (done) -> | ||
file.lint (err, result, less, css) -> | ||
expect(err).toBe null | ||
expect(result).toBe undefined | ||
expect(less).toNotBe undefined | ||
expect(less.length).toBeGreaterThan 0 | ||
expect(css).toNotBe undefined | ||
expect(css.length).toBeGreaterThan 0 | ||
|
||
done() | ||
|
||
describe 'LessCachedFile', -> | ||
filePath = path.join(__dirname, 'fixtures', 'valid.less') | ||
file = null | ||
|
||
beforeEach -> | ||
file = new LessCachedFile(filePath, {}, grunt) | ||
|
||
it 'can load contents', -> | ||
contents = file.getContents() | ||
expect(contents).toBe grunt.file.read(filePath) | ||
|
||
it 'can get a hash', -> | ||
hash = file.getDigest() | ||
expect(hash).toNotBe null | ||
expect(hash.length).toBeGreaterThan 0 | ||
|
||
it 'can lint a file', (done) -> | ||
# See if we read the file | ||
spyOn(file, 'getCss').andCallThrough() | ||
# Force no caching | ||
spyOn(file.cache, 'hasCached').andCallFake (hash, cb) -> cb(false) | ||
spyOn(file.cache, 'addCached').andCallFake (hash, cb) -> cb(null) | ||
|
||
file.lint (err, result, less, css) -> | ||
expect(file.getCss).toHaveBeenCalled() | ||
expect(err).toBe null | ||
expect(result).toBe undefined | ||
expect(less).toNotBe undefined | ||
expect(less.length).toBeGreaterThan 0 | ||
expect(css).toNotBe undefined | ||
expect(css.length).toBeGreaterThan 0 | ||
|
||
done() | ||
|
||
it 'does not parse less if previously cached successful run', (done) -> | ||
# See if we read the file | ||
spyOn(file, 'getCss').andCallThrough() | ||
# Force cache hit | ||
spyOn(file.cache, 'hasCached').andCallFake (hash, cb) -> cb(true) | ||
spyOn(file.cache, 'addCached').andCallFake (hash, cb) -> cb(null) | ||
|
||
file.lint (err, result, less, css) -> | ||
expect(file.getCss).not.toHaveBeenCalled() | ||
|
||
done() | ||
|
||
it 'uses imports from config option as a cache key so changes in import files cause re-linting', (done) -> | ||
file.options.imports = ['spec/fixtures/file.less'] | ||
# See if we read the file | ||
spyOn(file, 'getCss').andCallThrough() | ||
|
||
hashKey = null | ||
spyOn(file.cache, 'hasCached').andCallFake (hash, cb) -> | ||
hashKey = hash | ||
cb false | ||
|
||
spyOn(file.cache, 'addCached').andCallFake (hash, cb) -> cb(null) | ||
|
||
# Stub the getContents so we can change it on subsequent runs through | ||
contentsCalls = 0 | ||
spyOn(file, 'getImportsContents').andCallFake -> | ||
contentsCalls += 1 | ||
["body { margin: #{contentsCalls}px; }"] | ||
|
||
file.lint (err, result, less, css) -> | ||
expect(file.getCss).toHaveBeenCalled() | ||
expect(contentsCalls).toBe 1 | ||
expect(err).toBe null | ||
expect(result).toBe undefined | ||
|
||
otherFile = new LessCachedFile(filePath, {}, grunt) | ||
otherFile.options.imports = ['spec/fixtures/file.less'] | ||
|
||
spyOn(otherFile, 'getCss').andCallThrough() | ||
|
||
otherHashKey = null | ||
spyOn(otherFile.cache, 'hasCached').andCallFake (hash, cb) -> | ||
otherHashKey = hash | ||
cb false | ||
|
||
spyOn(otherFile.cache, 'addCached').andCallFake (hash, cb) -> cb(null) | ||
spyOn(otherFile, 'getImportsContents').andCallFake -> | ||
contentsCalls += 1 | ||
["body { margin: #{contentsCalls}px; }"] | ||
|
||
otherFile.lint (err, otherResult, less, css) -> | ||
expect(otherFile.getCss).toHaveBeenCalled() | ||
expect(contentsCalls).toBe 2 | ||
expect(otherHashKey).not.toBe hashKey | ||
|
||
done() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.