diff options
author | Rebecca Turner <me@re-becca.org> | 2015-10-07 01:45:41 +0300 |
---|---|---|
committer | Rebecca Turner <me@re-becca.org> | 2015-10-09 02:24:32 +0300 |
commit | b78fec9dac12176f29618c783aaf231a2c767b54 (patch) | |
tree | bdd2301f9740ed197dd54e6fd1d65ec24d21c5da /test | |
parent | 2289234e11b30c878585aebee68d4a3a5bd63cce (diff) |
module-name: Factor out module name reading
Bring consistent guarding against null/undefined and consistent
business logic.
PR-URL: //github.com/npm/npm/pull/9890
Fixes: #9766
Diffstat (limited to 'test')
-rw-r--r-- | test/tap/add-remote-git-shrinkwrap.js | 4 | ||||
-rw-r--r-- | test/tap/unit-module-name.js | 40 |
2 files changed, 42 insertions, 2 deletions
diff --git a/test/tap/add-remote-git-shrinkwrap.js b/test/tap/add-remote-git-shrinkwrap.js index f2982355e..d734c11cf 100644 --- a/test/tap/add-remote-git-shrinkwrap.js +++ b/test/tap/add-remote-git-shrinkwrap.js @@ -61,9 +61,9 @@ test('shrinkwrap gets correct _from and _resolved (#7121)', function (t) { { cwd: pkg }, function (er, code, stdout, stderr) { t.ifError(er, 'npm shrinkwrapped without errors') - t.notOk(code, '`npm shrinkwrap` exited with 0') + t.is(code, 0, '`npm shrinkwrap` exited ok') t.equal(stdout.trim(), 'wrote npm-shrinkwrap.json') - t.notOk(stderr, 'no error output on successful shrinkwrap') + t.equal(stderr.trim(), '', 'no error output on successful shrinkwrap') var shrinkwrap = require(resolve(pkg, 'npm-shrinkwrap.json')) t.equal( diff --git a/test/tap/unit-module-name.js b/test/tap/unit-module-name.js new file mode 100644 index 000000000..59d4b44f0 --- /dev/null +++ b/test/tap/unit-module-name.js @@ -0,0 +1,40 @@ +'use strict' +var test = require('tap').test +var moduleName = require('../../lib/utils/module-name.js') + +test('pathToPackageName', function (t) { + var pathToPackageName = moduleName.test.pathToPackageName + t.is(pathToPackageName('/foo/bar/baz/bark'), 'bark', 'simple module name') + t.is(pathToPackageName('/foo/bar/@baz/bark'), '@baz/bark', 'scoped module name') + t.is(pathToPackageName('/foo'), 'foo', 'module at top') + t.is(pathToPackageName('/@foo'), '@foo', 'invalid module at top') + t.is(pathToPackageName('/'), '', 'root, empty result') + t.is(pathToPackageName(''), '', 'empty, empty') + t.is(pathToPackageName(undefined), '', 'undefined is empty') + t.is(pathToPackageName(null), '', 'null is empty') + t.done() +}) + +test('isNotEmpty', function (t) { + var isNotEmpty = moduleName.test.isNotEmpty + t.is(isNotEmpty('abc'), true, 'string is not empty') + t.is(isNotEmpty(''), false, 'empty string is empty') + t.is(isNotEmpty(null), false, 'null is empty') + t.is(isNotEmpty(undefined), false, 'undefined is empty') + t.is(isNotEmpty(0), true, 'zero is not empty') + t.is(isNotEmpty(true), true, 'true is not empty') + t.is(isNotEmpty([]), true, 'empty array is not empty') + t.is(isNotEmpty({}), true, 'object is not empty') + t.done() +}) + +test('moduleName', function (t) { + t.is(moduleName({package: {name: 'foo'}}), 'foo', 'package named') + t.is(moduleName({name: 'foo'}), 'foo', 'package named, no tree') + t.is(moduleName({path: '/foo/bar'}), 'bar', 'path named') + t.is(moduleName({}), '!invalid#1', 'no named') + t.is(moduleName({path: '/'}), '!invalid#2', 'invalid named') + var obj = {} + t.is(moduleName(obj), moduleName(obj), 'once computed, an invalid module name will not change') + t.done() +}) |