<commit_before>'use strict'

const crypto     = require('crypto')
const fs         = require('fs')
const assert     = require('chai').assert
const temp       = require('temp').track()
const browserify = require('./../src/browserify')

let validFile   = null
let invalidFile = null

describe('browserify()', function() {

	before(function() {

		// Create file in __dirname so Babel can load its presets
		validFile = temp.openSync({
			dir    : __dirname,
			suffix : '.js'
		})

		fs.writeFileSync(validFile.path, `const fn = () => process.env.NODE_ENV`)

		// Create file in __dirname so Babel can load its presets
		invalidFile = temp.openSync({
			dir    : __dirname,
			suffix : '.js'
		})

		fs.writeFileSync(invalidFile.path, `=`)

	})

	it('should return an error when called with a fictive filePath', function() {

		return browserify('test.js', null).then((result) => {

			throw new Error('Returned without error')

		}, (err) => {

			assert.isNotNull(err)

		})

	})

	it('should return JS when called without a valid filePath', function() {

		return browserify(null, null).then((result) => {

			assert.isString(result)

		})

	})

	it('should return JS when called with a valid JS file', function() {

		return browserify(validFile.path, null).then((result) => {

			assert.isString(result)

		})

	})

	it('should return JS and replace process.env.NODE_ENV when optimize is true', function() {

		return browserify(validFile.path, { optimize: true }).then((result) => {

			assert.include(result, 'production')

		})

	})

	it('should return JS and not replace process.env.NODE_ENV when optimize is false', function() {

		return browserify(validFile.path, { optimize: false }).then((result) => {

			assert.include(result, 'process.env.NODE_ENV')

		})

	})

	it('should return an error when called with an invalid JS file', function() {

		return browserify(invalidFile.path, null).then((result) => {

			throw new Error('Returned without error')

		}, (err) => {

			assert.isNotNull(err)

		})

	})

})<commit_msg>Each test task with its own file (newFile)<commit_after>'use strict'

const crypto     = require('crypto')
const fs         = require('fs')
const assert     = require('chai').assert
const temp       = require('temp').track()
const browserify = require('./../src/browserify')

const newFile = function(content, suffix) {

	// Create file in __dirname so Babel can load its presets
	const file = temp.openSync({
		dir    : __dirname,
		suffix : suffix
	})

	fs.writeFileSync(file.path, content)

	return file.path

}

describe('browserify()', function() {

	it('should return an error when called with a fictive filePath', function() {

		return browserify('test.js', null).then((result) => {

			throw new Error('Returned without error')

		}, (err) => {

			assert.isNotNull(err)

		})

	})

	it('should return JS when called without a valid filePath', function() {

		return browserify(null, null).then((result) => {

			assert.isString(result)

		})

	})

	it('should return JS when called with a valid JS file', function() {

		const file = newFile(`const fn = () => process.env.NODE_ENV`, '.js')

		return browserify(file, null).then((result) => {

			assert.isString(result)

		})

	})

	it('should return JS and replace process.env.NODE_ENV when optimize is true', function() {

		const file = newFile(`const fn = () => process.env.NODE_ENV`, '.js')

		return browserify(file, { optimize: true }).then((result) => {

			assert.include(result, 'production')

		})

	})

	it('should return JS and not replace process.env.NODE_ENV when optimize is false', function() {

		const file = newFile(`const fn = () => process.env.NODE_ENV`, '.js')

		return browserify(file, { optimize: false }).then((result) => {

			assert.include(result, 'process.env.NODE_ENV')

		})

	})

	it('should return an error when called with an invalid JS file', function() {

		const file = newFile(`=`, '.js')

		return browserify(file, null).then((result) => {

			throw new Error('Returned without error')

		}, (err) => {

			assert.isNotNull(err)

		})

	})

})
{'fraction_non_alphanumeric': 0.152243196861976, 'fraction_numerical': 0.0, 'mean_word_length': 3.5842696629213484, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '2313209', 'n_tokens_mistral': 1407, 'n_tokens_neox': 1229, 'n_words': 354}