-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
412 additions
and
85 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
const expect = require('chai').expect; | ||
const { | ||
unlinkSync, | ||
existsSync, | ||
} = require('fs'); | ||
const { | ||
createLessFile, | ||
createStream | ||
} = require('../test-functions'); | ||
const through = require('through2').obj; | ||
const RequiredParamException = require('../../lib/exceptions/RequiredParamException'); | ||
const FileManager = require('../../lib/compiler/FileManager'); | ||
const path = require('path'); | ||
|
||
describe('Test FileManager', () => { | ||
let manager; | ||
let file; | ||
let inputFile; | ||
let outputFile = '../build/output.css'; | ||
let cwd; | ||
|
||
beforeEach(() => { | ||
file = createLessFile(); | ||
inputFile = file.path; | ||
cwd = process.cwd(); | ||
manager = new FileManager(inputFile, outputFile, cwd) | ||
}); | ||
|
||
it('Should create a throw Error', () => { | ||
expect(() => { | ||
manager = new FileManager(); | ||
}).to.throw(RequiredParamException); | ||
}); | ||
|
||
it('Should create a correct instance', () => { | ||
expect(manager).to.be.an.instanceOf(FileManager); | ||
expect(manager).to.be.an('object').to.have.own.property('inputFile', inputFile).that.is.a('string'); | ||
expect(manager).to.be.an('object').to.have.own.property('outputFile', outputFile).that.is.a('string'); | ||
expect(manager).to.be.an('object').to.have.own.property('cwd', cwd).that.is.a('string'); | ||
expect(manager).to.be.an('object').to.have.own.property('rootFile').that.is.a('null'); | ||
}); | ||
|
||
it('Should validate main file', () => { | ||
expect(manager.isMainFile(inputFile)).to.be.true; | ||
expect(manager.isMainFile(outputFile)).to.be.false; | ||
}); | ||
|
||
it('Should return dirname from output file', () => { | ||
expect(manager.getOutputDir()).to.equal(path.dirname(outputFile)); | ||
}); | ||
|
||
describe('Test streams', () => { | ||
let stream; | ||
|
||
beforeEach(() => { | ||
stream = createStream(manager.getInputFile()); | ||
}); | ||
|
||
it('Should set correct rootFile', (ended) => { | ||
stream | ||
.pipe(through(function (file, enc, done) { | ||
expect(manager.isRootFileEmpty()).to.be.true; | ||
manager.setRootFile(file); | ||
done(); | ||
})) | ||
.on('finish', () => { | ||
expect(manager.isRootFileEmpty()).to.be.false; | ||
ended(); | ||
}); | ||
}); | ||
|
||
it('Should get content as a string from rootFile', (ended) => { | ||
let currentFile = null; | ||
stream | ||
.pipe(through(function (file, enc, done) { | ||
manager.setRootFile(file); | ||
currentFile = file; | ||
done(); | ||
})) | ||
.on('finish', () => { | ||
expect(manager.getRootFileContentString()).to.have.string(currentFile.contents.toString()); | ||
ended(); | ||
}); | ||
}); | ||
|
||
it('Should get content as a buffer object from rootFile', (ended) => { | ||
let currentFile = null; | ||
|
||
stream | ||
.pipe(through(function (file, enc, done) { | ||
currentFile = file; | ||
manager.setRootFile(file); | ||
done(); | ||
})) | ||
.on('finish', () => { | ||
expect(manager.getBufferFromRootFile()).to.eql(currentFile.contents); | ||
ended(); | ||
}); | ||
}); | ||
|
||
it('Should override content inside rootFile', (ended) => { | ||
let newContent = 'tested'; | ||
let contentBuffer = new Buffer(newContent); | ||
|
||
stream | ||
.pipe(through(function (file, enc, done) { | ||
manager.setRootFile(file); | ||
manager.overrideRootBufferContent(newContent); | ||
done(); | ||
})) | ||
.on('finish', () => { | ||
expect(manager.getBufferFromRootFile()).to.eql(contentBuffer); | ||
expect(manager.getRootFileContentString()).to.have.string(newContent); | ||
ended(); | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
if (existsSync(manager.getInputFile())) { | ||
unlinkSync(manager.getInputFile()); | ||
} | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
if (existsSync(manager.getInputFile())) { | ||
unlinkSync(manager.getInputFile()); | ||
} | ||
}); | ||
|
||
describe('Test override functions', () => { | ||
let hash = Date.now(); | ||
let manager; | ||
let stream; | ||
let content = '/*' + hash + '*/body{color:red}/*' + hash + '*/'; | ||
|
||
beforeEach(() => { | ||
const file = createLessFile(content); | ||
const inputFile = file.path; | ||
const outputFile = '../build/output.css'; | ||
manager = new FileManager(inputFile, outputFile, process.cwd()); | ||
stream = createStream(manager.getInputFile()); | ||
}); | ||
|
||
it('Should replace new content by hash', (ended) => { | ||
stream | ||
.pipe(through(function (file, enc, done) { | ||
currentFile = file; | ||
manager.setRootFile(file); | ||
done(); | ||
})) | ||
.on('finish', () => { | ||
const newContent = '.tested{color: tomato;}'; | ||
manager.replaceContentInRootFileByHash(hash, newContent); | ||
expect(manager.getRootFileContentString()).to.have.string(newContent); | ||
ended(); | ||
}); | ||
}); | ||
|
||
it('Should replace new content by hash', (ended) => { | ||
stream | ||
.pipe(through(function (file, enc, done) { | ||
currentFile = file; | ||
manager.setRootFile(file); | ||
done(); | ||
})) | ||
.on('finish', () => { | ||
hash = ''; | ||
manager.replaceContentInRootFileByHash(hash, ''); | ||
expect(manager.getRootFileContentString()).to.equal(content); | ||
ended(); | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
if (existsSync(manager.getInputFile())) { | ||
unlinkSync(manager.getInputFile()); | ||
} | ||
}); | ||
|
||
}); | ||
|
||
}); |
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,62 @@ | ||
const expect = require('chai').expect; | ||
const Hasher = require('../../lib/lessPlugin/Hasher'); | ||
const { | ||
hash | ||
} = require('../../lib/utils-functions'); | ||
const path = require('path'); | ||
|
||
const filename = 'tested.less'; | ||
const extra = { | ||
fileInfo: { | ||
filename, | ||
} | ||
}; | ||
|
||
const styles = 'body{color: red;}'; | ||
|
||
describe('Test Hasher plugin', () => { | ||
let hasher; | ||
|
||
beforeEach(() => { | ||
hasher = new Hasher(process.cwd(), new Map()); | ||
}); | ||
|
||
it('Should create a valid instance', () => { | ||
expect(hasher).to.be.an('object').to.have.own.property('cwd', process.cwd()).that.is.a('string'); | ||
expect(hasher).to.be.an('object').to.have.own.property('map').to.be.an.instanceOf(Map); | ||
}); | ||
|
||
it('Should Hasher contains \'process\' function', () => { | ||
expect(hasher.process).to.not.throw(); | ||
}); | ||
|
||
it('Should hash file name', () => { | ||
const hashed = hash(filename); | ||
expect(hasher.hashFile(filename)).to.equal(hashed); | ||
}); | ||
|
||
it('Should convert hashed file name to less comment', () => { | ||
const hashed = hasher.hashFile(filename); | ||
expect(hasher.toLessComment(hashed)).to.equal(`/*${hashed}*/`); | ||
}); | ||
|
||
it('Should process styles adding hashed file on start and end of file', () => { | ||
const _file = path.resolve(process.cwd(), filename); | ||
const hashed = hasher.hashFile(_file); | ||
const comment = hasher.toLessComment(hashed); | ||
let expected = comment; | ||
expected += "\n" + styles + "\n"; | ||
expected += comment; | ||
|
||
hasher.process(styles, extra); | ||
expect(hasher.process(styles, extra)).to.equal(expected); | ||
}); | ||
|
||
it('Should add filename to map', () => { | ||
const _file = path.resolve(process.cwd(), filename); | ||
const hashed = hasher.hashFile(_file); | ||
hasher.process(styles, extra); | ||
|
||
expect(hasher.getHashedFileByFileName(_file)).to.equal(hashed); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,28 +1,30 @@ | ||
const fs = require('fs'); | ||
const { | ||
resolve | ||
} = require('path'); | ||
const path = require('path'); | ||
const vfs = require('vinyl-fs'); | ||
|
||
const createFakeContent = (hash) => { | ||
return (` | ||
@import "${hash}"; | ||
`); | ||
} | ||
|
||
exports.createLessFile = () => { | ||
exports.createLessFile = (content) => { | ||
const now = Date.now(); | ||
const filename = `${now}.less`; | ||
const path = resolve('__tests__', 'build', filename); | ||
const content = createFakeContent(now); | ||
|
||
fs.writeFileSync(path, content, { | ||
const filePath = path.resolve('__tests__', 'build', filename); | ||
content = content || createFakeContent(now); | ||
fs.writeFileSync(filePath, content, { | ||
flag: 'w' | ||
}); | ||
|
||
return { | ||
filename, | ||
path, | ||
path: filePath, | ||
hash: now, | ||
content, | ||
} | ||
} | ||
|
||
exports.createStream = (filePath) => { | ||
return vfs.src(filePath); | ||
} |
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,47 @@ | ||
const expect = require('chai').expect; | ||
const utils = require('../../lib/utils-functions'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const loadGraph = require('../../lib/graph/loader'); | ||
const DirectoryAlreadyExistsException = require('../../lib/exceptions/DirectoryAlreadyExistsException'); | ||
|
||
const { | ||
createLessFile | ||
} = require('../test-functions'); | ||
|
||
const dir = path.resolve(process.cwd(), '__tests__', 'build', 'test'); | ||
|
||
describe('Test utils functions', () => { | ||
it('Should create recursive folder', () => { | ||
utils.mkdirp(dir); | ||
const stats = fs.lstatSync(path.dirname(dir)); | ||
|
||
expect(stats.isDirectory()).to.be.true; | ||
}); | ||
|
||
it('Should throw error when directory already exists', () => { | ||
expect(() => { | ||
utils.mkdirp(dir); | ||
}).to.throw(DirectoryAlreadyExistsException); | ||
}); | ||
|
||
it('Should validate if a directory already exists', () => { | ||
expect(utils.existsDirectory(dir)).to.be.true; | ||
expect(utils.existsDirectory('notFounded')).to.be.false; | ||
}); | ||
|
||
it('Should create paths for less plugin', () => { | ||
const file = createLessFile(); | ||
const graph = loadGraph(file.path); | ||
const paths = utils.getPathsForLessPlugin(graph, process.cwd(), 'tested.less'); | ||
|
||
expect(paths).to.be.an('array').that.include('__tests__/build/'); | ||
|
||
fs.unlinkSync(file.path); | ||
}); | ||
|
||
after(() => { | ||
fs.rmdirSync(path.dirname(path.join(dir, 'test'))); | ||
}); | ||
|
||
}); |
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.