Code coverage report for lib/options.js

Statements: 91.3% (42 / 46)      Branches: 100% (22 / 22)      Functions: 100% (5 / 5)      Lines: 91.3% (42 / 46)      Ignored: none     

All files » lib/ » options.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 1661 1 1               1     1   1 1                       1   2   1 1                             1     4     4     3     3 2 2         4 1 1   4 1 1   4 1 1   4 1 1     4 1   4 1       4   4                 1 1                 1 1               1                                                                                   1  
var os = require('os');
var fs = require('fs');
var path = require('path');
 
/**
 * Creates a temp configuration file
 *
 * @method createTempConfig
 * @param {object} config Configuration options
 */
var createTempConfig = function (config) {
 
	// Get temp-file
	optionsFn.tmpPath = path.join(optionsFn.getTmpDir(), +(new Date()) + '.json');
 
	try {
		fs.writeFileSync(optionsFn.tmpPath, JSON.stringify(config, null, 4));
	} catch (err) {
		optionsFn.tmpPath = null;
		console.error(err.stack);
	}
};
 
/**
 * Removes the temp files for configuration
 *
 * @method removeTempConfig
 */
var removeTempConfig = function () {
 
	if (optionsFn.tmpPath) {
 
		try {
			fs.unlinkSync(optionsFn.tmpPath);
		} catch (err) {
			optionsFn.tmpPath = null;
			console.error(err.stack);
		}
	}
};
 
/**
 * Loads all Kobold arguments from the options
 *
 * @method buildKoboldArguments
 * @param {object} options Kobold image comparison options
 * @return {string[]} List of arguments for Kobold
 */
var buildKoboldArguments = function (options) {
 
	// Get arguments for Mocha
	var args = options.mochaArgs || [];
 
	// Add config file if config is given
	if (options.config) {
 
		// Create temp file for configuration
		optionsFn.createTempConfig(options.config);
 
		// Add config file
		if (optionsFn.tmpPath) {
			args.push('--config');
			args.push(optionsFn.tmpPath);
		}
	}
 
	// Get other options
	if (options.approvedFolder) {
		args.push('--approved-folder');
		args.push(options.approvedFolder);
	}
	if (options.buildFolder) {
		args.push('--build-folder');
		args.push(options.buildFolder);
	}
	if (options.highlightFolder) {
		args.push('--highlight-folder');
		args.push(options.highlightFolder);
	}
	if (options.configFolder) {
		args.push('--config-folder');
		args.push(options.configFolder);
	}
 
	if (options.failOrphans) {
		args.push('--fail-orphans');
	}
	if (options.failAdditions) {
		args.push('--fail-additions');
	}
 
	// Test-path
	args.push(options.testPath || process.cwd());
 
	return args;
};
 
/**
 * Gets the Kobold path
 *
 * @method getKoboldPath
 * @return {string} Path to the Kobold binary
 */
var getKoboldPath = function () {
	return path.resolve(path.join(__dirname, '..', 'node_modules', 'kobold', 'bin', 'kobold'));
};
 
/**
 * Determines the temp directory
 *
 * @method getTmpDir
 * @return {string} Path to temp directory
 */
var getTmpDir = function () {
	return os.tmpdir();
};
 
 
/**
 * @static
 * @type {object}
 */
var optionsFn = {
 
	/**
	 * @property tmpPath
	 * @type {string|null}
	 */
	tmpPath: null,
 
 
	/**
	 * @property createTempConfig
	 * @type {function}
	 */
	createTempConfig: createTempConfig,
 
	/**
	 * @property removeTempConfig
	 * @type {function}
	 */
	removeTempConfig: removeTempConfig,
 
 
	/**
	 * @property buildKoboldArguments
	 * @type {function}
	 */
	buildKoboldArguments: buildKoboldArguments,
 
 
	/**
	 * @property getKoboldPath
	 * @type {function}
	 */
	getKoboldPath: getKoboldPath,
 
	/**
	 * @property getTmpDir
	 * @type {function}
	 */
	getTmpDir: getTmpDir
};
 
module.exports = optionsFn;