From 03971fd151b69ed980ba0997c636d930dd507587 Mon Sep 17 00:00:00 2001 From: Tommy Parnell Date: Sun, 6 Mar 2016 12:32:44 -0500 Subject: [PATCH] add tests work on a refactor --- .gitignore | 1 + Gulpfile.js | 25 +++++++ package.json | 23 ++++--- src/CommandBuilder.js | 47 +++++++++++++ src/exec.js | 2 +- test/unit/CommandBuilder.test.js | 109 +++++++++++++++++++++++++++++++ test/unit/index.test.js | 1 - 7 files changed, 196 insertions(+), 12 deletions(-) create mode 100644 src/CommandBuilder.js create mode 100644 test/unit/CommandBuilder.test.js delete mode 100644 test/unit/index.test.js diff --git a/.gitignore b/.gitignore index 3c3629e..ba2a97b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +coverage diff --git a/Gulpfile.js b/Gulpfile.js index a7d2b10..063e558 100644 --- a/Gulpfile.js +++ b/Gulpfile.js @@ -2,6 +2,11 @@ var gulp = require('gulp'); var unzip = require('gulp-unzip'); var request = require('request'); var fs = require('fs'); +var mocha = require('gulp-mocha'); +var istanbul = require('gulp-istanbul'); +var isparta = require('isparta'); +require('babel-core/register'); + gulp.task('download', function () { return request('http://wixtoolset.org/downloads/v3.11.0.129/wix311-binaries.zip').pipe(fs.createWriteStream('wixToolset.zip')); }); @@ -11,3 +16,23 @@ gulp.task('getwix',['download'], function(){ .pipe(unzip()) .pipe(gulp.dest('./lib/wixFiles')); }); + +gulp.task('pre-test', function () { + return gulp.src('src/**/*.js') + // Covering files + .pipe(istanbul({ + instrumenter: isparta.Instrumenter, + includeUntested: true} + + )) + // Force `require` to return covered files + .pipe(istanbul.hookRequire()); +}); + +gulp.task('test', function(){ + return gulp.src('test/unit/*.js') + .pipe(mocha()) + .pipe(istanbul.writeReports()); + //.pipe(istanbul.enforceThresholds({ thresholds: { global: 90 } })); + //usemocha +}); diff --git a/package.json b/package.json index b175340..4243481 100644 --- a/package.json +++ b/package.json @@ -9,23 +9,26 @@ "scripts": { "compile": "babel src --out-dir lib", "coveralls": "cat ./coverage/lcov.info | coveralls", - "prepublish": "npm run compile", - "test": "babel-node ./node_modules/.bin/isparta cover _mocha" + "prepublish": "npm run compile" }, - "dependencies": { - "underscore": "^1.8.3" + "dependencies": { + "lodash": "^4.6.1" }, "devDependencies": { "babel-cli": "*", + "babel-core": "^6.6.5", "babel-preset-es2015-node4": "*", - "coveralls": "*", "chai": "*", - "isparta": "*", - "mocha": "*", - "sinon": "*", + "coveralls": "*", "gulp": "^3.9.1", + "gulp-istanbul": "^0.10.3", + "gulp-mocha": "^2.2.0", "gulp-unzip": "^0.1.3", - "request": "^2.69.0" - + "isparta": "^4.0.0", + "jscover": "^1.0.0", + "mocha": "*", + "mocha-lcov-reporter": "^1.2.0", + "request": "^2.69.0", + "sinon": "*" } } diff --git a/src/CommandBuilder.js b/src/CommandBuilder.js new file mode 100644 index 0000000..3e87bea --- /dev/null +++ b/src/CommandBuilder.js @@ -0,0 +1,47 @@ +'use strict'; +var fs = require('fs'), + _ = require('lodash'), + path = require('path'); + + +var calculateCommands = function(options){ + var commands = { + heatPath: options.heatPath || __dirname + "/wixFiles/heat.exe", + lightPath: options.lightPath || __dirname + "/wixFiles/light.exe", + candlePath: options.candlePath || __dirname + "/wixFiles/candle.exe" + } + if(options.version){ + process.env.BUILD_VERSION = version; + } + if(options.heatCommands && _.isArray(options.heatCommands)){ + commands.heatCommands = options.heatCommands + } + else{ + if(options.heatFiles && _.isArray(options.heatFiles) && options.heatFiles.length > 0 ){ + commands.heatCommands = _.map(options.heatFiles, (file)=>`@${path.normalize(file)}`) //heat commands can be empty as heat is a harvester and thus optional + } + } + + if(options.candleCommands && _.isArray(options.candleCommands)){ + commands.candleCommands = options.candleCommands + } + else{ + if(!options.candleFiles || !_.isArray(options.candleFiles) || options.candleFiles.length < 1 ){ + throw "light files are required if light commands are not specified"; + } + commands.candleCommands = _.map(options.candleFiles, (file)=>`@${path.normalize(file)}`) + } + + if(options.lightCommands && _.isArray(options.lightCommands)){ + commands.lightCommands = options.lightCommands + } + else{ + if(!options.lightFiles || !_.isArray(options.lightFiles) || options.lightFiles.length < 1 ){ + throw "light files are required if light commands are not specified"; + } + commands.lightCommands = _.map(options.lightFiles, (file)=>`@${path.normalize(file)}`) + } + return commands; +}; + +module.exports = calculateCommands; diff --git a/src/exec.js b/src/exec.js index 9f5b2d5..fe4de5b 100644 --- a/src/exec.js +++ b/src/exec.js @@ -3,7 +3,7 @@ /* jshint -W097 */ 'use strict'; var fs = require('fs'), - _ = require('underscore'), + _ = require('lodash'), path = require('path'), child_process = require('child_process'); diff --git a/test/unit/CommandBuilder.test.js b/test/unit/CommandBuilder.test.js new file mode 100644 index 0000000..ccfcf7d --- /dev/null +++ b/test/unit/CommandBuilder.test.js @@ -0,0 +1,109 @@ +var assert = require('chai').assert; +var expect = require('chai').expect; +var commandBuilder = require('../../src/CommandBuilder'); + + +describe('CommandBuilderWorks', function(){ + + it('Should not throw when files are passed in', function(){ + var testObject = { + heatFiles: ['tst'], + candleFiles: ['awesome'], + lightFiles: ['filesss'] + }; + commandBuilder(testObject) + + }); + it('Should not throw when commands are passed in', function(){ + var testObject = { + heatCommands: ['tst'], + candleCommands: ['awesome'], + lightCommands: ['filesss'] + }; + commandBuilder(testObject) + + }); + + it('Should Throw if missing light files', function(){ + var testObject = { + heatFiles: ['tst'], + candleFiles: ['awesome'] + }; + assert.throws(()=>commandBuilder(testObject)); + + }); + it('Should Throw if missing candle files', function(){ + var testObject = { + heatFiles: ['tst'], + lightFiles: ['awesome'] + }; + assert.throws(()=>commandBuilder(testObject)); + + }); + it('Should not Throw if missing heat files or commands', function(){ + var testObject = { + lightFiles: ['awesome'], + candleFiles: ['awesome'] + }; + assert.doesNotThrow(()=>commandBuilder(testObject)); + + }); + + it('should run as expected with files', function(){ + var testObject = { + lightFiles: ['lightfile'], + candleFiles: ['candlefile'], + heatFiles: ['heatfile'] + }; + var result = commandBuilder(testObject); + expect(result.heatCommands).to.eql(['@heatfile']); + expect(result.lightCommands).to.eql(['@lightfile']); + expect(result.candleCommands).to.eql(['@candlefile']); + }); + + it('should run as expected with commands', function(){ + var testObject = { + lightCommands: ['lightfile'], + candleCommands: ['candlefile'], + heatCommands: ['heatfile'] + }; + var result = commandBuilder(testObject); + expect(result.heatCommands).to.eql(['heatfile']); + expect(result.lightCommands).to.eql(['lightfile']); + expect(result.candleCommands).to.eql(['candlefile']); + }); + + it('should use alternate heat location', function(){ + var testObject = { + lightCommands: ['lightfile'], + candleCommands: ['candlefile'], + heatCommands: ['heatfile'], + heatPath: "../awesome" + }; + var result = commandBuilder(testObject); + expect(result.heatPath).to.eql("../awesome"); + }); + + it('should use alternate candle location', function(){ + var testObject = { + lightCommands: ['lightfile'], + candleCommands: ['candlefile'], + heatCommands: ['heatfile'], + candlePath: "../awesome" + }; + var result = commandBuilder(testObject); + expect(result.candlePath).to.eql("../awesome"); + }); + + it('should use alternate light location', function(){ + var testObject = { + lightCommands: ['lightfile'], + candleCommands: ['candlefile'], + heatCommands: ['heatfile'], + lightPath: "../awesome" + }; + var result = commandBuilder(testObject); + expect(result.lightPath).to.eql("../awesome"); + }); + +}); diff --git a/test/unit/index.test.js b/test/unit/index.test.js deleted file mode 100644 index e69fded..0000000 --- a/test/unit/index.test.js +++ /dev/null @@ -1 +0,0 @@ -import { assert } from 'chai';