2
0
Fork 0
node-ff-assets/test/test-asset-assembler.js
2015-06-28 17:44:01 +02:00

224 lines
7 KiB
JavaScript

'use strict';
var fs = require('fs-extra');
var assert = require('assert');
var assets = require('../lib/node-ff-assets');
function createTestAssembler(suite, readFile) {
var assetAssembler = new assets.AssetAssembler({
linkMapping: {'/static-test/': 'test/data/'+suite.title+'/'},
linkTarget: '/static-test/output.css',
linkSources: [
'/static-test/input0.css',
'/static-test/input1.css',
],
},readFile);
assetAssembler.on ('log',function (logMessage) {
console.log(' ' + ' ' + suite.title + ' ' +logMessage);
});
return assetAssembler;
}
function createTestData(suite,extraData0) {
var testName = suite.title;
var outputFile = 'test/data/'+testName+'/output.css';
var dataCss0 = 'test/data/'+testName+'/input0.css';
var dataCss1 = 'test/data/'+testName+'/input1.css';
fs.ensureFileSync(dataCss0);
fs.appendFileSync(dataCss0,'input__0');
if (extraData0 !== undefined) {
fs.appendFileSync(dataCss0,extraData0);
}
fs.ensureFileSync(dataCss1);
fs.appendFileSync(dataCss1,'input__1');
return outputFile;
}
describe('lib/asset-assembler.js', function() {
describe('config-null', function() {
this.timeout(10000);
try {
var result = new assets.AssetAssembler();
assert.ok(result);
assert.ok(false);
} catch (err) {
assert.ok(err !== undefined);
}
});
describe('aggregate-two', function() {
this.timeout(10000);
var outputFile = createTestData(this);
var assetAssembler = createTestAssembler(this);
it('run builder and check output', function (done) {
assetAssembler.run(function (err) {
assert.ok(err === undefined);
try {
assert.ok(true);
assert.equal(true, fs.existsSync(outputFile));
var outputData = ''+fs.readFileSync(outputFile);
assert.equal(true, outputData.indexOf('input__0') > 0);
assert.equal(true, outputData.indexOf('input__1') > 0);
done();
} catch (err) {
done(err);
}
});
});
});
describe('aggregate-two-and-remote', function() {
this.timeout(10000);
var remoteFile = 'test/data/'+this.title+'/fonts-sans-pro.css';
var outputFile = createTestData(this);
var assetAssembler = createTestAssembler(this);
assetAssembler.config.linkSources.push('/static-test/fonts-sans-pro.css@http://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700');
it('run builder and check output', function (done) {
assetAssembler.run(function (err) {
assert.ok(err === undefined);
try {
assert.ok(true);
assert.equal(true, fs.existsSync(outputFile));
assert.equal(true, fs.existsSync(remoteFile));
var outputData = ''+fs.readFileSync(outputFile);
assert.equal(true, outputData.indexOf('input__0') > 0);
assert.equal(true, outputData.indexOf('input__1') > 0);
assert.equal(true, outputData.indexOf('Source Sans Pro') > 0);
done();
} catch (err) {
//console.log('sdf: '+Object.keys(err));
done(err);
}
});
});
});
describe('remote-404', function() {
this.timeout(10000);
var remoteFile = 'test/data/'+this.title+'/font-404.css';
var outputFile = createTestData(this);
var assetAssembler = createTestAssembler(this);
assetAssembler.config.linkSources.push('/static-test/font-404.css@http://fonts.googleapis.com/404');
it('run builder and check output', function (done) {
assetAssembler.run(function (err) {
assert.ok(err !== undefined);
try {
assert.equal(true, err.message.indexOf('404') >= 0);
assert.equal(true, fs.existsSync(outputFile));
assert.equal(true, fs.existsSync(remoteFile));
var outputData = ''+fs.readFileSync(outputFile);
assert.equal(true, outputData.length !== 0);
assert.equal(true, outputData.indexOf('input__0') === -1);
assert.equal(true, outputData.indexOf('input__1') === -1);
assert.equal(true, outputData.indexOf('Source Sans Pro') === -1);
var remoteData = ''+fs.readFileSync(remoteFile);
assert.equal(true, remoteData.indexOf('404') > 0);
done();
} catch (err) {
done(err);
}
});
});
});
describe('remote-closed', function() {
this.timeout(10000);
var remoteFile = 'test/data/'+this.title+'/font-closed.css';
var outputFile = createTestData(this);
var assetAssembler = createTestAssembler(this);
assetAssembler.config.linkSources.push('/static-test/font-closed.css@http://localhost:12345/closed');
it('run builder and check output', function (done) {
assetAssembler.run(function (err) {
try {
assert.ok(err !== undefined);
assert.equal(true, err.message.indexOf('REFUSED') >= 0);
assert.equal(true, fs.existsSync(outputFile));
assert.equal(true, fs.existsSync(remoteFile));
var outputData = ''+fs.readFileSync(outputFile);
assert.equal(true, outputData.indexOf('input__0') === -1);
assert.equal(true, outputData.indexOf('input__1') === -1);
var remoteData = ''+fs.readFileSync(remoteFile);
assert.equal(true, remoteData.length === 0);
done();
} catch (err) {
done(err);
}
});
});
});
describe('aggregate-regex', function() {
this.timeout(10000);
var outputFile = createTestData(this,'\n// RM_THIS_COMMENT_LINE\n/* RM_THIS_COMMENT_BLOCK */\n');
var assetAssembler = createTestAssembler(this,assets.factory.assembler.constructor.readFileRegex());
it('run builder and check output', function (done) {
assetAssembler.run(function (err) {
assert.ok(err === undefined);
try {
assert.ok(true);
assert.equal(true, fs.existsSync(outputFile));
var outputData = ''+fs.readFileSync(outputFile);
assert.equal(true, outputData.indexOf('input__0') > 0);
assert.equal(true, outputData.indexOf('input__1') > 0);
assert.equal(false, outputData.indexOf('RM_THIS_COMMENT_LINE') > 0);
assert.equal(false, outputData.indexOf('RM_THIS_COMMENT_BLOCK') > 0);
done();
} catch (err) {
done(err);
}
});
});
});
describe('aggregate-no-comments', function() {
this.timeout(10000);
var outputFile = createTestData(this);
var assetAssembler = createTestAssembler(this);
assetAssembler.config.assetHeader = null;
assetAssembler.config.assetFooter = null;
assetAssembler.config.assetSeperator = null;
it('run builder and check output', function (done) {
assetAssembler.run(function (err) {
assert.ok(err === undefined);
try {
assert.ok(true);
assert.equal(true, fs.existsSync(outputFile));
var outputData = ''+fs.readFileSync(outputFile);
assert.equal(true, outputData.indexOf('input__0') >= 0);
assert.equal(true, outputData.indexOf('input__1') >= 0);
assert.equal(false, outputData.indexOf('node-ff-assets') > 0);
assert.equal(false, outputData.indexOf('begin') > 0);
assert.equal(false, outputData.indexOf('end') > 0);
assert.equal(false, outputData.indexOf('input0.css') > 0);
assert.equal(false, outputData.indexOf('input1.css') > 0);
done();
} catch (err) {
done(err);
}
});
});
});
});