You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

320 lines
8.2 KiB

10 years ago
require( 'source-map-support' ).install();
require( 'console-group' ).install();
10 years ago
var path = require( 'path' );
var sander = require( 'sander' );
var assert = require( 'assert' );
10 years ago
var exec = require( 'child_process' ).exec;
var babel = require( 'babel-core' );
var sequence = require( './utils/promiseSequence' );
10 years ago
var rollup = require( '../dist/rollup' );
var FUNCTION = path.resolve( __dirname, 'function' );
var FORM = path.resolve( __dirname, 'form' );
10 years ago
var SOURCEMAPS = path.resolve( __dirname, 'sourcemaps' );
10 years ago
var CLI = path.resolve( __dirname, 'cli' );
10 years ago
var PROFILES = [
{ format: 'amd' },
{ format: 'cjs' },
{ format: 'es6' },
{ format: 'iife' },
{ format: 'umd' }
];
10 years ago
function extend ( target ) {
[].slice.call( arguments, 1 ).forEach( function ( source ) {
source && Object.keys( source ).forEach( function ( key ) {
target[ key ] = source[ key ];
});
});
return target;
}
10 years ago
describe( 'rollup', function () {
describe( 'sanity checks', function () {
it( 'exists', function () {
assert.ok( !!rollup );
});
10 years ago
it( 'has a rollup method', function () {
assert.equal( typeof rollup.rollup, 'function' );
});
10 years ago
});
describe( 'function', function () {
sander.readdirSync( FUNCTION ).sort().forEach( function ( dir ) {
if ( dir[0] === '.' ) return; // .DS_Store...
var config;
try {
config = require( FUNCTION + '/' + dir + '/_config' );
} catch ( err ) {
config = { description: dir };
}
( config.skip ? it.skip : config.solo ? it.only : it )( dir, function () {
var options = extend( {}, config.options, {
entry: FUNCTION + '/' + dir + '/main.js'
});
if ( config.solo ) console.group( dir );
return rollup.rollup( options )
.then( function ( bundle ) {
var unintendedError;
if ( config.error ) {
throw new Error( 'Expected an error while rolling up' );
}
// try to generate output
try {
var result = bundle.generate( extend( {}, config.bundleOptions, {
format: 'cjs'
}));
if ( config.error ) {
unintendedError = new Error( 'Expected an error while generating output' );
}
} catch ( err ) {
if ( config.error ) {
config.error( err );
} else {
unintendedError = err;
}
}
if ( unintendedError ) throw unintendedError;
var code;
try {
if ( config.babel ) {
10 years ago
code = babel.transform( result.code, {
blacklist: [ 'es6.modules' ],
loose: [ 'es6.classes' ]
}).code;
} else {
code = result.code;
}
var module = {
exports: {}
};
var context = extend({
require: require,
module: module,
exports: module.exports,
assert: assert
}, config.context || {} );
var contextKeys = Object.keys( context );
var contextValues = contextKeys.map( function ( key ) {
return context[ key ];
});
var fn = new Function( contextKeys, code );
fn.apply( {}, contextValues );
if ( config.error ) {
unintendedError = new Error( 'Expected an error while executing output' );
}
if ( config.exports ) {
config.exports( module.exports );
}
} catch ( err ) {
if ( config.error ) {
config.error( err );
} else {
unintendedError = err;
}
}
if ( config.show || unintendedError ) {
console.log( code + '\n\n\n' );
}
if ( config.solo ) console.groupEnd();
if ( unintendedError ) throw unintendedError;
}, function ( err ) {
if ( config.error ) {
config.error( err );
} else {
throw err;
}
});
});
});
});
describe( 'form', function () {
sander.readdirSync( FORM ).sort().forEach( function ( dir ) {
if ( dir[0] === '.' ) return; // .DS_Store...
10 years ago
describe( dir, function () {
var config = require( FORM + '/' + dir + '/_config' );
var options = extend( {}, config.options, {
entry: FORM + '/' + dir + '/main.js'
});
var bundlePromise = rollup.rollup( options );
10 years ago
PROFILES.forEach( function ( profile ) {
10 years ago
( config.skip ? it.skip : config.solo ? it.only : it )( 'generates ' + profile.format, function () {
if ( config.solo ) console.group( dir );
10 years ago
return bundlePromise.then( function ( bundle ) {
var options = extend( {}, config.options, {
dest: FORM + '/' + dir + '/_actual/' + profile.format + '.js',
10 years ago
format: profile.format
});
return bundle.write( options ).then( function () {
var actualCode = sander.readFileSync( FORM, dir, '_actual', profile.format + '.js' ).toString().trim();
var expectedCode;
var actualMap;
var expectedMap;
try {
expectedCode = sander.readFileSync( FORM, dir, '_expected', profile.format + '.js' ).toString().trim();
} catch ( err ) {
expectedCode = 'missing file';
}
try {
actualMap = JSON.parse( sander.readFileSync( FORM, dir, '_actual', profile.format + '.js.map' ).toString() );
} catch ( err ) {}
try {
expectedMap = JSON.parse( sander.readFileSync( FORM, dir, '_expected', profile.format + '.js.map' ).toString() );
} catch ( err ) {}
assert.equal( actualCode, expectedCode );
assert.deepEqual( actualMap, expectedMap );
if ( config.solo ) console.groupEnd();
});
10 years ago
});
});
10 years ago
});
});
10 years ago
});
});
10 years ago
describe( 'sourcemaps', function () {
sander.readdirSync( SOURCEMAPS ).sort().forEach( function ( dir ) {
if ( dir[0] === '.' ) return; // .DS_Store...
describe( dir, function () {
var config = require( SOURCEMAPS + '/' + dir + '/_config' );
var options = extend( {}, config.options, {
entry: SOURCEMAPS + '/' + dir + '/main.js'
});
var bundlePromise = rollup.rollup( options );
10 years ago
PROFILES.forEach( function ( profile ) {
( config.skip ? it.skip : config.solo ? it.only : it )( 'generates ' + profile.format, function () {
return bundlePromise.then( function ( bundle ) {
var result = bundle.generate({
format: profile.format,
sourceMap: true,
10 years ago
sourceMapFile: 'bundle.js'
});
config.test( result.code, result.map );
});
});
});
});
});
});
10 years ago
describe( 'cli', function () {
sander.readdirSync( CLI ).sort().forEach( function ( dir ) {
if ( dir[0] === '.' ) return; // .DS_Store...
describe( dir, function () {
var config = require( CLI + '/' + dir + '/_config' );
( config.skip ? it.skip : config.solo ? it.only : it )( dir, function ( done ) {
process.chdir( path.resolve( CLI, dir ) );
exec( config.command, {
env: {
PATH: path.resolve( __dirname, '../bin' ) + ':' + process.env.PATH
}
}, function ( err, code, stderr ) {
10 years ago
if ( err ) return done( err );
if ( stderr ) console.error( stderr );
var unintendedError;
if ( config.execute ) {
try {
if ( config.babel ) {
code = babel.transform( code, {
blacklist: [ 'es6.modules' ],
loose: [ 'es6.classes' ]
10 years ago
}).code;
}
var fn = new Function( 'require', 'module', 'exports', 'assert', code );
var module = {
exports: {}
};
fn( require, module, module.exports, assert );
if ( config.error ) {
unintendedError = new Error( 'Expected an error while executing output' );
}
if ( config.exports ) {
config.exports( module.exports );
}
} catch ( err ) {
if ( config.error ) {
config.error( err );
} else {
unintendedError = err;
}
}
if ( config.show || unintendedError ) {
console.log( code + '\n\n\n' );
}
if ( config.solo ) console.groupEnd();
unintendedError ? done( unintendedError ) : done();
}
else if ( config.result ) {
try {
config.result( code );
} catch ( err ) {
done( err );
}
}
else {
var expected = sander.readFileSync( '_expected.js' ).toString();
assert.equal( code.trim(), expected.trim() );
done();
}
10 years ago
});
});
});
});
});
});