<commit_before>'use strict';

const readFileSync = require( 'fs' ).readFileSync;
const chai = require( 'chai' );
const expect = chai.expect;
const rollup = require( 'rollup' ).rollup;
const plugin = require( '../dist/rollup-plugin-babel-minify' );
const babel = require( 'babel-core' );
const validateSourcemap = require( 'sourcemap-validator' );

const bundleOptions = {
	format: 'es'
};

process.chdir( 'tests' );

describe( 'rollup-plugin-babel-minify', () => {
	it( 'is a function', () => {
		expect( plugin ).to.be.a( 'function' );
	} );

	it( 'minifies code just like babel-minify', () => {
		const path = 'fixtures/index.js';
		const code = readFileSync( path, 'utf8' );
		const babeledCode = babel.transform( code, { presets: [ 'babili' ], comments: true } );

		return rollup( {
			entry: path,
			plugins: [ plugin() ]
		} ).then( ( bundle ) => {
			bundle.generate( bundleOptions ).then( ( result ) => {
				expect( result.code.trim() ).to.equal( babeledCode.code );
			} );
		} );
	} );

	it( 'passes options to babel', () => {
		const path = 'fixtures/sourcemap.js';
		const code = readFileSync( path );
		const babeledCode = babel.transform( code, { presets: [ [ 'babili', {
			removeConsole: true
		} ] ] } );

		return rollup( {
			entry: path,
			plugins: [ plugin( {
				removeConsole: true
			} ) ]
		} ).then( ( bundle ) => {
			bundle.generate( bundleOptions ).then( ( result ) => {
				expect( result.code.trim() ).to.equal( babeledCode.code );
			} );
		} );
	} );

	it( 'removes comments', () => {
		const path = 'fixtures/index.js';
		const code = readFileSync( path );
		const babeledCode = babel.transform( code, { presets: [ 'babili' ], comments: false } );

		return rollup( {
			entry: path,
			plugins: [ plugin( {
				comments: false
			} ) ]
		} ).then( ( bundle ) => {
			bundle.generate( bundleOptions ).then( ( result ) => {
				expect( result.code.trim() ).to.equal( babeledCode.code );
			} );
		} );
	} );

	it( 'adds banner even if comments are removed', () => {
		return rollup( {
			entry: 'fixtures/index.js',
			plugins: [ plugin( {
				comments: false,
				banner: '/* hublabubla */'
			} ) ],
		} ).then( ( bundle ) => {
			bundle.generate( bundleOptions ).then( ( result ) => {
				expect( result.code ).to.match( /^\/\* hublabubla \*\// );
			} );
		} );
	} );

	it( 'adds banner inherited from bundle.generate', () => {
		return rollup( {
			entry: 'fixtures/index.js',
			plugins: [ plugin() ],
		} ).then( ( bundle ) => {
			bundle.generate( Object.assign( {}, bundleOptions, {
				banner: '/* hublabubla */'
			} ) ).then( ( result ) => {
				expect( result.code ).to.match( /^\/\* hublabubla \*\// );
			} );
		} );
	} );

	it ( 'adds banner inherited from root configuration', () => {
		// while you can ask: WTF? banner is not an option for an rollup
		// function, this is how options from config file are passed
		return rollup( {
			entry: 'fixtures/index.js',
			banner: '/* hublabubla */',
			plugins: [ plugin() ],
		} ).then( ( bundle ) => {
			bundle.generate( bundleOptions ).then( ( result ) => {
				expect ( result.code ).to.match( /^\/\* hublabubla \*\// );
			} );
		} );
	} );

	it ( 'adds banner as a result of call if plugin banner option is fn itself', () => {
		return rollup( {
			entry: 'fixtures/index.js',
			plugins: [ plugin( {
				banner: () => {
					return '/* hublabubla */';
				}
			} ) ],
		} ).then ( ( bundle ) => {
			bundle.generate( bundleOptions ).then( ( result ) => {
				expect ( result.code ).to.match( /^\/\* hublabubla \*\// );
			} );
		} );
	} );

	it( 'preserves comments alongside banner if no comments option is passed', () => {
		return rollup( {
			entry: 'fixtures/index.js',
			plugins: [ plugin( {
				banner: '/* hublabubla */'
			} ) ],
		} ).then( ( bundle ) => {
			bundle.generate( bundleOptions ).then( ( result ) => {
				expect( result.code ).to.match( /^\/\* hublabubla \*\// );
				expect( result.code ).to.match( /.+\/\* Simple comment \*\/.+/g );
			} );
		} );
	} );

	it( 'preserves comments alongside banner if comments option is set to true', () => {
		return rollup( {
			entry: 'fixtures/index.js',
			plugins: [ plugin( {
				comments: true,
				banner: '/* hublabubla */'
			} ) ],
		} ).then( ( bundle ) => {
			bundle.generate( bundleOptions ).then( ( result ) => {
				expect( result.code ).to.match( /^\/\* hublabubla \*\// );
				expect( result.code ).to.match( /.+\/\* Simple comment \*\/.+/g );
			} );
		} );
	} );

	it( 'generates source map by default', () => {
		return rollup( {
			entry: 'fixtures/sourcemap.js',
			plugins: [ plugin() ],
		} ).then( ( bundle ) => {
			bundle.generate( Object.assign( {}, bundleOptions, {
				sourceMap: true
			} ) ).then( ( result ) => {
				expect( result.map ).to.not.equal( null );
				expect( () => {
					validateSourcemap( result.code, result.map );
				} ).not.to.throw();
			} );
		} );
	} );

	it( 'does not generate source map when the proper option is passed', () => {
		return rollup( {
			entry: 'fixtures/sourcemap.js',
			plugins: [ plugin( {
				sourceMap: false
			} ) ],
		} ).then( ( bundle ) => {
			bundle.generate( Object.assign( {}, bundleOptions, {
				sourceMap: false
			} ) ).then( ( result ) => {
				expect( result.map ).to.equal( null );
			} );
		} );
	} );

	it( 'generates source map for UMD bundle', () => {
		return rollup( {
			entry: 'fixtures/sourcemap.js',
			plugins: [ plugin() ],
		} ).then( ( bundle ) => {
			bundle.generate( {
				format: 'umd',
				moduleName: 'Test',
				sourceMap: true
			} ).then( ( result ) => {
				expect( result.map ).to.not.equal( null );
				expect( () => {
					validateSourcemap( result.code, result.map );
				} ).not.to.throw();
			} );
		} );
	} );

	it( 'generates source map for empty bundle', () => {
		return rollup( {
			entry: 'fixtures/empty.js',
			plugins: [ plugin() ],
		} ).then( ( bundle ) => {
			bundle.generate( {
				format: 'es',
				sourceMap: true
			} ).then( ( result ) => {
				expect( result.map ).to.not.equal( null );
			} );
		} );
	} );

	it( 'generates valid source map after heavy processing by fixMappings', () => {
		return rollup( {
			entry: 'fixtures/invalidMappings.js',
			plugins: [ plugin() ],
		} ).then( ( bundle ) => {
			bundle.generate( {
				format: 'es',
				sourceMap: true
			} ).then( ( result ) => {
				expect( result.map ).to.not.equal( null );
				expect( () => {
					validateSourcemap( result.code, result.map );
				} ).not.to.throw();
			} );
		} );
	} );
} );
<commit_msg>test(index): fix references to minify presets<commit_after>'use strict';

const readFileSync = require( 'fs' ).readFileSync;
const chai = require( 'chai' );
const expect = chai.expect;
const rollup = require( 'rollup' ).rollup;
const plugin = require( '../dist/rollup-plugin-babel-minify' );
const babel = require( 'babel-core' );
const validateSourcemap = require( 'sourcemap-validator' );

const bundleOptions = {
	format: 'es'
};

process.chdir( 'tests' );

describe( 'rollup-plugin-babel-minify', () => {
	it( 'is a function', () => {
		expect( plugin ).to.be.a( 'function' );
	} );

	it( 'minifies code just like babel-minify', () => {
		const path = 'fixtures/index.js';
		const code = readFileSync( path, 'utf8' );
		const babeledCode = babel.transform( code, { presets: [ 'minify' ], comments: true } );

		return rollup( {
			entry: path,
			plugins: [ plugin() ]
		} ).then( ( bundle ) => {
			bundle.generate( bundleOptions ).then( ( result ) => {
				expect( result.code.trim() ).to.equal( babeledCode.code );
			} );
		} );
	} );

	it( 'passes options to babel', () => {
		const path = 'fixtures/sourcemap.js';
		const code = readFileSync( path );
		const babeledCode = babel.transform( code, { presets: [ [ 'minify', {
			removeConsole: true
		} ] ] } );

		return rollup( {
			entry: path,
			plugins: [ plugin( {
				removeConsole: true
			} ) ]
		} ).then( ( bundle ) => {
			bundle.generate( bundleOptions ).then( ( result ) => {
				expect( result.code.trim() ).to.equal( babeledCode.code );
			} );
		} );
	} );

	it( 'removes comments', () => {
		const path = 'fixtures/index.js';
		const code = readFileSync( path );
		const babeledCode = babel.transform( code, { presets: [ 'minify' ], comments: false } );

		return rollup( {
			entry: path,
			plugins: [ plugin( {
				comments: false
			} ) ]
		} ).then( ( bundle ) => {
			bundle.generate( bundleOptions ).then( ( result ) => {
				expect( result.code.trim() ).to.equal( babeledCode.code );
			} );
		} );
	} );

	it( 'adds banner even if comments are removed', () => {
		return rollup( {
			entry: 'fixtures/index.js',
			plugins: [ plugin( {
				comments: false,
				banner: '/* hublabubla */'
			} ) ],
		} ).then( ( bundle ) => {
			bundle.generate( bundleOptions ).then( ( result ) => {
				expect( result.code ).to.match( /^\/\* hublabubla \*\// );
			} );
		} );
	} );

	it( 'adds banner inherited from bundle.generate', () => {
		return rollup( {
			entry: 'fixtures/index.js',
			plugins: [ plugin() ],
		} ).then( ( bundle ) => {
			bundle.generate( Object.assign( {}, bundleOptions, {
				banner: '/* hublabubla */'
			} ) ).then( ( result ) => {
				expect( result.code ).to.match( /^\/\* hublabubla \*\// );
			} );
		} );
	} );

	it ( 'adds banner inherited from root configuration', () => {
		// while you can ask: WTF? banner is not an option for an rollup
		// function, this is how options from config file are passed
		return rollup( {
			entry: 'fixtures/index.js',
			banner: '/* hublabubla */',
			plugins: [ plugin() ],
		} ).then( ( bundle ) => {
			bundle.generate( bundleOptions ).then( ( result ) => {
				expect ( result.code ).to.match( /^\/\* hublabubla \*\// );
			} );
		} );
	} );

	it ( 'adds banner as a result of call if plugin banner option is fn itself', () => {
		return rollup( {
			entry: 'fixtures/index.js',
			plugins: [ plugin( {
				banner: () => {
					return '/* hublabubla */';
				}
			} ) ],
		} ).then ( ( bundle ) => {
			bundle.generate( bundleOptions ).then( ( result ) => {
				expect ( result.code ).to.match( /^\/\* hublabubla \*\// );
			} );
		} );
	} );

	it( 'preserves comments alongside banner if no comments option is passed', () => {
		return rollup( {
			entry: 'fixtures/index.js',
			plugins: [ plugin( {
				banner: '/* hublabubla */'
			} ) ],
		} ).then( ( bundle ) => {
			bundle.generate( bundleOptions ).then( ( result ) => {
				expect( result.code ).to.match( /^\/\* hublabubla \*\// );
				expect( result.code ).to.match( /.+\/\* Simple comment \*\/.+/g );
			} );
		} );
	} );

	it( 'preserves comments alongside banner if comments option is set to true', () => {
		return rollup( {
			entry: 'fixtures/index.js',
			plugins: [ plugin( {
				comments: true,
				banner: '/* hublabubla */'
			} ) ],
		} ).then( ( bundle ) => {
			bundle.generate( bundleOptions ).then( ( result ) => {
				expect( result.code ).to.match( /^\/\* hublabubla \*\// );
				expect( result.code ).to.match( /.+\/\* Simple comment \*\/.+/g );
			} );
		} );
	} );

	it( 'generates source map by default', () => {
		return rollup( {
			entry: 'fixtures/sourcemap.js',
			plugins: [ plugin() ],
		} ).then( ( bundle ) => {
			bundle.generate( Object.assign( {}, bundleOptions, {
				sourceMap: true
			} ) ).then( ( result ) => {
				expect( result.map ).to.not.equal( null );
				expect( () => {
					validateSourcemap( result.code, result.map );
				} ).not.to.throw();
			} );
		} );
	} );

	it( 'does not generate source map when the proper option is passed', () => {
		return rollup( {
			entry: 'fixtures/sourcemap.js',
			plugins: [ plugin( {
				sourceMap: false
			} ) ],
		} ).then( ( bundle ) => {
			bundle.generate( Object.assign( {}, bundleOptions, {
				sourceMap: false
			} ) ).then( ( result ) => {
				expect( result.map ).to.equal( null );
			} );
		} );
	} );

	it( 'generates source map for UMD bundle', () => {
		return rollup( {
			entry: 'fixtures/sourcemap.js',
			plugins: [ plugin() ],
		} ).then( ( bundle ) => {
			bundle.generate( {
				format: 'umd',
				moduleName: 'Test',
				sourceMap: true
			} ).then( ( result ) => {
				expect( result.map ).to.not.equal( null );
				expect( () => {
					validateSourcemap( result.code, result.map );
				} ).not.to.throw();
			} );
		} );
	} );

	it( 'generates source map for empty bundle', () => {
		return rollup( {
			entry: 'fixtures/empty.js',
			plugins: [ plugin() ],
		} ).then( ( bundle ) => {
			bundle.generate( {
				format: 'es',
				sourceMap: true
			} ).then( ( result ) => {
				expect( result.map ).to.not.equal( null );
			} );
		} );
	} );

	it( 'generates valid source map after heavy processing by fixMappings', () => {
		return rollup( {
			entry: 'fixtures/invalidMappings.js',
			plugins: [ plugin() ],
		} ).then( ( bundle ) => {
			bundle.generate( {
				format: 'es',
				sourceMap: true
			} ).then( ( result ) => {
				expect( result.map ).to.not.equal( null );
				expect( () => {
					validateSourcemap( result.code, result.map );
				} ).not.to.throw();
			} );
		} );
	} );
} );

{'fraction_non_alphanumeric': 0.20309135650569055, 'fraction_numerical': 0.00015379883112888343, 'mean_word_length': 2.610494169905608, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 99, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '177440', 'n_tokens_mistral': 5270, 'n_tokens_neox': 4528, 'n_words': 1051}