From cb07b13cad54aec48291669eda1f5fe9e2e06da5 Mon Sep 17 00:00:00 2001 From: Jia Hao Date: Mon, 25 Jan 2016 01:10:28 +0800 Subject: [PATCH] Add mocha basic test to check if nativefier.json is set properly --- gulpfile.babel.js | 12 +++++-- package.json | 4 +++ test/module/index-spec.js | 70 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 test/module/index-spec.js diff --git a/gulpfile.babel.js b/gulpfile.babel.js index 8af5754..1056db5 100644 --- a/gulpfile.babel.js +++ b/gulpfile.babel.js @@ -7,6 +7,7 @@ import runSequence from 'run-sequence'; import path from 'path'; import childProcess from 'child_process'; import eslint from 'gulp-eslint'; +import mocha from 'gulp-mocha'; const PATHS = setUpPaths(); @@ -65,14 +66,21 @@ gulp.task('release', callback => { return runSequence('test', 'build', 'publish', callback); }); -gulp.task('lint', function() { +gulp.task('lint', () => { return gulp.src(['**/*.js', '!node_modules/**', '!app/node_modules/**', '!app/lib/**', '!lib/**']) .pipe(eslint()) .pipe(eslint.format()) .pipe(eslint.failAfterError()); }); -gulp.task('test', ['lint']); +gulp.task('test', ['build'], () => { + return gulp.src('test/**/*js', {read: false}) + .pipe(mocha({compilers: 'js:babel-register'})); +}); + +gulp.task('ci', callback => { + return runSequence('test', 'lint', callback); +}); function setUpPaths() { const paths = { diff --git a/package.json b/package.json index 7c00699..0b5048e 100644 --- a/package.json +++ b/package.json @@ -54,14 +54,18 @@ "babel-core": "^6.4.5", "babel-loader": "^6.2.1", "babel-preset-es2015": "^6.3.13", + "babel-register": "^6.4.3", + "chai": "^3.4.1", "del": "^2.2.0", "electron-prebuilt": "^0.36.5", "eslint-config-google": "^0.3.0", "gulp": "^3.9.0", "gulp-babel": "^6.1.1", "gulp-eslint": "^1.1.1", + "gulp-mocha": "^2.2.0", "gulp-sourcemaps": "^1.6.0", "run-sequence": "^1.1.5", + "tmp": "0.0.28", "webpack-stream": "^3.1.0" } } diff --git a/test/module/index-spec.js b/test/module/index-spec.js new file mode 100644 index 0000000..3d3eb33 --- /dev/null +++ b/test/module/index-spec.js @@ -0,0 +1,70 @@ +import tmp from 'tmp'; +import chai from 'chai'; +import fs from 'fs'; +import os from 'os'; +import path from 'path'; +import nativefier from './../../lib/index'; + +let assert = chai.assert; + +function checkApp(appPath, inputOptions, callback) { + try { + let relPathToConfig; + + switch (inputOptions.platform) { + case 'darwin': + relPathToConfig = path.join('google-test-app.app', 'Contents/Resources/app'); + break; + case 'linux': + relPathToConfig = 'resources/app'; + break; + case 'win32': + relPathToConfig = 'resources/app'; + break; + default: + throw 'Unknown app platform'; + } + + const nativefierConfigPath = path.join(appPath, relPathToConfig, 'nativefier.json'); + const nativefierConfig = JSON.parse(fs.readFileSync(nativefierConfigPath)); + + assert.strictEqual(inputOptions.targetUrl, nativefierConfig.targetUrl, 'Packaged app must have the same targetUrl as the input parameters'); + assert.strictEqual(inputOptions.appName, nativefierConfig.name, 'Packaged app must have the same name as the input parameters'); + callback(); + } catch (exception) { + callback(exception); + } +} + +describe('index module', function () { + this.timeout(20000); + it('Can build an app from a target url', function (done) { + try { + var tmpObj = tmp.dirSync({unsafeCleanup: true}); + after(function() { + tmpObj.removeCallback(); + }); + + const tmpPath = tmpObj.name; + const options = { + appName: 'google-test-app', + targetUrl: 'http://google.com', + outDir: tmpPath, + overwrite: true, + platform: 'win32' + }; + nativefier(options, (error, appPath) => { + if (error) { + done(error); + return; + } + + checkApp(appPath, options, error => { + done(error); + }); + }); + } catch (exception) { + done(exception); + } + }); +});