From ea5de85bdd3dbb9db7b8facbb1e297f7c205e4de Mon Sep 17 00:00:00 2001 From: Globegitter Date: Wed, 1 Apr 2015 02:30:21 +0100 Subject: [PATCH] Added acceptance test for dryRun. --- .gitignore | 1 + README.md | 10 +++++++--- lib/do-npm-command.js | 9 ++++++++- lib/install-dry-run.js | 12 ++++++++++-- tests/acceptance/basic-test.js | 10 +++++++--- tests/runner.js | 2 ++ 6 files changed, 35 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index a72b52e..67f944f 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ results npm-debug.log node_modules +tmp diff --git a/README.md b/README.md index c3f9749..95a9842 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,8 @@ $ npm install enpeem --save var npm = require('enpeem'); ``` +For all supported options look into the `index.js` `install` and `update` function. + #### npm install ```javascript @@ -31,7 +33,11 @@ npm.install({ 'sails@0.10.1', 'sails-disk@git://github.com/balderdashy/sails-disk.git#associations', 'lodash' - ] + ], + prefix: 'custom/path/to/install', + saveDev: true, //--save-dev flag + //saves package to package.json without installing. Only works with save/saveDev option + dryRun: true, loglevel: 'silent', 'cache-min': 999999999 }, function (err) { /* ... */ }); @@ -45,5 +51,3 @@ npm.update({ loglevel: 'silent' }, function (err) { /* ... */ }); ``` - - diff --git a/lib/do-npm-command.js b/lib/do-npm-command.js index b45f60c..38621b1 100644 --- a/lib/do-npm-command.js +++ b/lib/do-npm-command.js @@ -49,7 +49,14 @@ module.exports = function doNpmCommand(options) { if ('dryRun' in options && options.dryRun === true) { if (options.npmCommand === 'install') { - installDryRun(options.cmdArgs, options.cmdOptions); + var result = installDryRun(options.cmdArgs, options.cmdOptions); + return new Promise(function (resolve, reject) { + if (result === true) { + resolve(0); + } else { + reject(result); + } + }); } else { return new Promise(function (resolve) { resolve(0); diff --git a/lib/install-dry-run.js b/lib/install-dry-run.js index 66a586e..62384fe 100644 --- a/lib/install-dry-run.js +++ b/lib/install-dry-run.js @@ -24,7 +24,11 @@ module.exports = function installDryRun(dependencies, cmdOptions) { if (cmdOptions.save || cmdOptions['save-dev']) { var prefix = cmdOptions.prefix || ''; var fileName = path.join(process.cwd(), prefix, 'package.json'); - var packageInfo = fs.readFileSync(fileName); + try { + var packageInfo = fs.readFileSync(fileName); + } catch (error) { + return error; + } var content = JSON.parse(packageInfo); var packageToSave = ''; var versionToSave = ''; @@ -47,7 +51,11 @@ module.exports = function installDryRun(dependencies, cmdOptions) { } } - fs.writeFileSync(fileName, JSON.stringify(content, null, 2)); + try { + fs.writeFileSync(fileName, JSON.stringify(content, null, 2)); + } catch (error) { + return error; + } return true; } return true; diff --git a/tests/acceptance/basic-test.js b/tests/acceptance/basic-test.js index 20e1e5a..a4c5932 100644 --- a/tests/acceptance/basic-test.js +++ b/tests/acceptance/basic-test.js @@ -236,7 +236,7 @@ describe('with dryRun option', function () { before(function () { tmpdir = tmp.in(tmproot); - this.pathToDep = getPathToDep(dependency, tmpdir); + this.pathToDep = getPathToDep(dependencies[1], tmpdir); this.pathToPackage = path.resolve( __dirname, tmpdir, 'package.json' ); @@ -268,8 +268,12 @@ describe('with dryRun option', function () { //require does not work because of caching var devDependencies = JSON.parse(fs.readFileSync(this.pathToPackage)).devDependencies; expect(devDependencies).to.include({ 'sane-cli': '^0.0.24', 'koa': '*' }); - console.log(fs.lstatSync(this.pathToDep)); - // expect(fs.lstatSync(this.pathToDep)).to.be.an('object'); + try { + var result = fs.lstatSync(this.pathToDep); + expect(result, 'The module folder should not exist.').to.not.exist; //eslint-disable-line no-unused-expressions + } catch (error) { + expect(error.message).to.include('no such file or directory'); + } }); }); diff --git a/tests/runner.js b/tests/runner.js index 6f2b857..1daf127 100644 --- a/tests/runner.js +++ b/tests/runner.js @@ -28,6 +28,8 @@ if (!arg) { files = 'eslint-test.js'; } else if (arg === 'unit') { root = 'tests/unit'; +} else if (arg === 'acceptance') { + root = 'tests/acceptance'; } else { root = 'tests/{unit,acceptance}'; }