Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
11joselu committed Apr 12, 2019
1 parent 4f804ac commit d9b8d6a
Show file tree
Hide file tree
Showing 13 changed files with 412 additions and 85 deletions.
183 changes: 183 additions & 0 deletions __tests__/compiler/FileManager.test.js
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());
}
});

});

});
8 changes: 3 additions & 5 deletions __tests__/compiler/Renderer.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const {
assert,
expect
} = require('chai');
const expect = require('chai').expect;

const Renderer = require('../../lib/compiler/Renderer');

describe('Test Renderer', () => {
Expand All @@ -26,7 +24,7 @@ describe('Test Renderer', () => {
resultPromise.then(({
css
}) => {
assert.equal(css, toBuild);
expect(css).to.equal(toBuild);
});
});

Expand Down
62 changes: 62 additions & 0 deletions __tests__/lessPlugin/Hasher.test.js
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);
});
});
20 changes: 11 additions & 9 deletions __tests__/test-functions.js
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);
}
47 changes: 47 additions & 0 deletions __tests__/utils/utils-functions.test.js
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')));
});

});
16 changes: 7 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ const Renderer = require('./lib/compiler/Renderer');
const FileManager = require('./lib/compiler/FileManager');
const WatcherQueue = require('./lib/Watcher/WatcherQueue');

const {
mkdirp,
getPathsFromGraph,
} = require('./lib/utils-functions');

const utils = require('./lib/utils-functions');
const cwd = process.cwd();

const fileManager = new FileManager(argv.src, argv.output, cwd);
mkdirp(fileManager.getOutputFile());

if (!utils.existsDirectory(fileManager.getOutputDir())) {
utils.mkdirp(fileManager.getOutputFile());
}

const graph = loadGraph(fileManager.getInputFile());
const wQueue = new WatcherQueue(Object.keys(graph.index));
Expand All @@ -30,7 +29,7 @@ let isFirstBuildValid = false;

const watcher = chokidar.watch(wQueue.getQueue());

const paths = getPathsFromGraph(graph, cwd, fileManager.getInputFile());
const paths = utils.getPathsForLessPlugin(graph, cwd, fileManager.getInputFile());
const renderer = new Renderer(paths, cwd);

var isCompiling = false;
Expand Down Expand Up @@ -139,8 +138,7 @@ const getImportStateFromPath = (filePath) => {
let removedImports = [];
try {
const pathGraph = loadGraph(filePath);
const pathImports = pathGraph.index[filePath] || {};
pathImports.imports = pathImports.imports || [];
const pathImports = pathGraph.index[filePath];
newImports = pathImports.imports.filter(newImp => !imports.includes(newImp));
removedImports = imports.filter(imp => !pathImports.imports.includes(imp));

Expand Down
Loading

0 comments on commit d9b8d6a

Please sign in to comment.