2
0
Fork 0
node-ff-assets/test/test-asset-assembler.js

224 lines
7 KiB
JavaScript
Raw Permalink Normal View History

2015-02-27 02:31:44 +00:00
'use strict';
var fs = require('fs-extra');
2015-06-28 15:44:01 +00:00
var assert = require('assert');
var assets = require('../lib/node-ff-assets');
2015-02-27 02:31:44 +00:00
function createTestAssembler(suite, readFile) {
2015-02-27 20:50:19 +00:00
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);
2015-06-28 14:41:57 +00:00
assetAssembler.on ('log',function (logMessage) {
console.log(' ' + ' ' + suite.title + ' ' +logMessage);
2015-02-27 20:50:19 +00:00
});
return assetAssembler;
}
function createTestData(suite,extraData0) {
2015-02-27 20:50:19 +00:00
var testName = suite.title;
2015-02-27 02:31:44 +00:00
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);
}
2015-02-27 02:31:44 +00:00
fs.ensureFileSync(dataCss1);
fs.appendFileSync(dataCss1,'input__1');
2015-02-27 02:31:44 +00:00
return outputFile;
}
describe('lib/asset-assembler.js', function() {
2015-02-27 14:20:01 +00:00
2015-02-27 20:50:19 +00:00
describe('config-null', function() {
2015-02-27 02:31:44 +00:00
this.timeout(10000);
2015-02-27 20:50:19 +00:00
try {
2015-06-28 15:44:01 +00:00
var result = new assets.AssetAssembler();
assert.ok(result);
2015-02-27 20:50:19 +00:00
assert.ok(false);
} catch (err) {
assert.ok(err !== undefined);
}
});
describe('aggregate-two', function() {
this.timeout(10000);
var outputFile = createTestData(this);
var assetAssembler = createTestAssembler(this);
2015-02-27 02:31:44 +00:00
it('run builder and check output', function (done) {
assetAssembler.run(function (err) {
2015-06-28 15:44:01 +00:00
assert.ok(err === undefined);
2015-02-27 02:31:44 +00:00
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);
}
});
});
});
2015-02-27 14:20:01 +00:00
2015-02-27 20:50:19 +00:00
describe('aggregate-two-and-remote', function() {
2015-02-27 02:31:44 +00:00
this.timeout(10000);
2015-06-28 15:44:01 +00:00
var remoteFile = 'test/data/'+this.title+'/fonts-sans-pro.css';
2015-02-27 20:50:19 +00:00
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');
2015-02-27 02:31:44 +00:00
it('run builder and check output', function (done) {
assetAssembler.run(function (err) {
2015-06-28 15:44:01 +00:00
assert.ok(err === undefined);
2015-02-27 02:31:44 +00:00
try {
assert.ok(true);
assert.equal(true, fs.existsSync(outputFile));
2015-02-27 20:50:19 +00:00
assert.equal(true, fs.existsSync(remoteFile));
2015-02-27 02:31:44 +00:00
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) {
2015-06-28 15:44:01 +00:00
//console.log('sdf: '+Object.keys(err));
2015-02-27 02:31:44 +00:00
done(err);
}
});
});
});
2015-02-27 20:50:19 +00:00
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) {
2015-06-28 15:44:01 +00:00
assert.ok(err !== undefined);
2015-02-27 20:50:19 +00:00
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);
2015-06-28 15:44:01 +00:00
var remoteFile = 'test/data/'+this.title+'/font-closed.css';
2015-02-27 20:50:19 +00:00
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) {
2015-06-28 15:44:01 +00:00
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);
}
});
});
});
2015-06-28 15:44:01 +00:00
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) {
2015-06-28 15:44:01 +00:00
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);
}
});
});
});
2015-02-27 02:31:44 +00:00
});