Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

extracted package-utils and package-descriptor

to new ender-package-utils lib
  • Loading branch information...
commit 1bebb20709dda4652adb897ce8f69d443d2d892a 1 parent 8657ed9
@rvagg rvagg authored
View
39 LICENSE
@@ -0,0 +1,39 @@
+Copyright 2012, Rod Vagg, Dustin Diaz, Jacob Thornton and other contributors. (the "Original Author")
+All rights reserved.
+
+MIT +no-false-attribs License
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+Distributions of all or part of the Software intended to be used
+by the recipients as they would use the unmodified Software,
+containing modifications that substantially alter, remove, or
+disable functionality of the Software, outside of the documented
+configuration mechanisms provided by the Software, shall be
+modified such that the Original Author's bug reporting email
+addresses and urls are either replaced with the contact information
+of the parties responsible for the changes, or removed entirely.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+
+
+Except where noted, this license applies to any and all software
+programs and associated documentation files created by the
+Original Author, when distributed with the Software.
View
2  lib/dependency-tree.js
@@ -32,7 +32,7 @@
var path = require('path')
, async = require('async')
- , packageUtil = require('./package-util')
+ , packageUtil = require('ender-repository').util
, util = require('./util')
, DependencyTree = {
View
2  lib/install-util.js
@@ -30,7 +30,7 @@
var path = require('path')
, async = require('async')
- , packageUtil = require('./package-util')
+ , packageUtil = require('ender-repository').util
, util = require('./util')
// scan the tree starting at the 'packages' nodes and return any dependencies that
View
2  lib/install.js
@@ -40,7 +40,7 @@
var async = require('async')
, repository = require('ender-repository')
- , packageUtil = require('./package-util')
+ , packageUtil = repository.util
, installUtil = require('./install-util')
, DependencyTree = require('./dependency-tree')
View
2  lib/main-build-util.js
@@ -29,7 +29,7 @@
var async = require('async')
, path = require('path')
- , packageUtil = require('./package-util')
+ , packageUtil = require('ender-repository').util
, util = require('./util')
// unique packages by proper name
View
2  lib/main-build.js
@@ -32,11 +32,11 @@
*/
var path = require('path')
+ , packageUtil = require('ender-repository').util
, util = require('./util')
, write = require('./write')
, mainInfo = require('./main-info')
, buildUtil = require('./main-build-util')
- , packageUtil = require('./package-util')
, install = require('./install')
, SourceBuild = require('./source-build')
, SourcePackage = require('./source-package')
View
70 lib/package-descriptor.js
@@ -1,70 +0,0 @@
-/*!
- * ENDER - The open module JavaScript framework
- *
- * Copyright (c) 2011-2012 @ded, @fat, @rvagg and other contributors
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is furnished
- * to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-
-/******************************************************************************
- * Wrapper for parsed package.json data. Makes a copy of package.json but will
- * replace root properties where they are duplicated in an "ender" sub-object
- * or an "overlay->ender" sub-object (as per Packages/1.1)
- */
-
-var overrides = {
- 'name' : 'name',
- 'main' : 'main',
- 'bridge' : 'ender',
- 'dependencies' : 'dependencies',
- 'devDependencies' : 'devDependencies'
- }
-
- , create = function (json) {
- var newJson = Object.create(json) // original is available via Object.getPrototypeOf
- , key
-
- if (typeof json.overlay == 'object'
- && typeof json.overlay.ender == 'object') {
- for (key in overrides) {
- if (key in json.overlay.ender) {
- newJson[overrides[key]] = json.overlay.ender[key]
- }
- }
- }
-
- if (typeof json.ender == 'object') {
- for (key in overrides) {
- if (key in json.ender) {
- newJson[overrides[key]] = json.ender[key]
- }
- }
- }
-
- for (key in json) {
- if (!newJson.hasOwnProperty(key)) {
- newJson[key] = json[key]
- }
- }
-
- return newJson
- }
-
-module.exports.create = create
View
146 lib/package-util.js
@@ -1,146 +0,0 @@
-/*!
- * ENDER - The open module JavaScript framework
- *
- * Copyright (c) 2011-2012 @ded, @fat, @rvagg and other contributors
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is furnished
- * to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-
-/******************************************************************************
- * Collection of utility functions that deal with npm packages on the
- * filesystem. Anything to do with node_modules, package.json, etc. should be
- * done here, the rest of the app shouldn't have to know about any of this
- * detail.
- */
-
-var fs = require('fs')
- , path = require('path')
- , util = require('./util')
- , PackageDescriptor = require('./package-descriptor')
- , JSONParseError = require('./errors').JSONParseError
- , FilesystemError = require('./errors').FilesystemError
-
- , stripVersionRegex = /@.*$/
-
- // strip out the version if it exists, bean@0.4.5 -> bean
- , cleanName = function (name) {
- if (Array.isArray(name)) {
- return name.map(function (n) {
- return cleanName(n)
- })
- } else {
- return name.replace(stripVersionRegex, '')
- }
- }
-
- , isCWD = function (pkg) {
- return path.resolve(pkg) == path.resolve('.')
- }
-
- , isPath = function (pkg) {
- return (pkg = cleanName(pkg)) === '.' || /[\/\\]/.test(pkg)
- }
-
- // given an array of parent packages and a package name, give us a path to the
- // package inside the CWD node_modules directory
- , getPackageRoot = function (parents, pkg) {
- var dirs = [ 'node_modules' ]
-
- pkg = cleanName(pkg)
-
- if (!isPath(pkg)) { // not a directory reference of some kind
- parents.forEach(function (p) {
- dirs.push(p)
- dirs.push(dirs[0])
- })
- return path.resolve(path.join.apply(null, dirs), pkg)
- }
-
- return path.resolve(pkg)
- }
-
- // perhaps this could be done with `require()` since it can now read package.json?
- , readPackageJSON = function (parents, pkg, callback) {
- var root = getPackageRoot(parents, pkg)
- , file = path.resolve(root, 'package.json')
-
- fs.readFile(file, 'utf-8', function (err, data) {
- if (err) return callback(new FilesystemError(err))
- try {
- data = JSON.parse(data)
- } catch (err) {
- return callback(new JSONParseError(err.message + ' [' + file + ']', err))
- }
- callback(null, PackageDescriptor.create(data))
- })
- }
-
- // if one of the packages is a path that points to the CWD then get the
- // name of it
- , findRootPackageName = function (packages, callback) {
- var found = function (err, data) {
- if (err) return callback(err) // wrapped by readPackageJSON
- callback(null, data.name)
- }
-
- for (var i = 0; i < packages.length; i++) {
- if (isCWD(packages[i])) return readPackageJSON([], packages[i], found)
- }
-
- callback()
- }
-
- , getDependenciesFromJSON = function (packageJSON) {
- var dep = packageJSON.dependencies
- if (dep && !Array.isArray(dep)) dep = Object.keys(dep)
- return dep && dep.length ? dep : []
- }
-
- , getDependenciesFromDirectory = function (parents, pkg, callback) {
- var root = getPackageRoot(parents, pkg)
- , dir = path.resolve(path.join(root, 'node_modules'))
-
- fs.exists(dir, function (exists) {
- if (!exists) return callback(null, [])
- fs.readdir(dir, function (err, files) {
- if (err) return callback(new FilesystemError(err))
- files = files.filter(function(file) {
- return file !== '.bin'
- });
- callback.call(null, err, files)
- })
- })
- }
-
- , preparePackagesDirectory = function (callback) {
- util.mkdir('node_modules', callback)
- }
-
-module.exports = {
- cleanName : cleanName
- , isCWD : isCWD
- , isPath : isPath
- , getPackageRoot : getPackageRoot
- , readPackageJSON : readPackageJSON
- , findRootPackageName : findRootPackageName
- , getDependenciesFromJSON : getDependenciesFromJSON
- , preparePackagesDirectory : preparePackagesDirectory
- , getDependenciesFromDirectory : getDependenciesFromDirectory
-}
View
2  lib/source-package.js
@@ -33,8 +33,8 @@
var path = require('path')
, async = require('async')
+ , packageUtil = require('ender-repository').util
, template = require('./template')
- , packageUtil = require('./package-util')
, sourcePackageUtil = require('./source-package-util')
, templateFiles = {
View
4 package.json
@@ -9,9 +9,9 @@
]
, "keywords": [ "ender", "modules", "builder", "framework", "packager" ]
, "main": "./lib/main.js"
- , "homepage": "https://ender.no.de"
+ , "homepage": "https://ender.jit.su"
, "dependencies": {
- "ender-repository" : "~0.0.1"
+ "ender-repository" : "~0.0.2"
, "ender-minify" : "~0.0.4"
, "colors" : "~0.6.0"
, "async" : "~0.1.22"
View
3  test/unit/dependency-tree-test.js
@@ -26,8 +26,9 @@
var buster = require('buster')
, path = require('path')
, assert = buster.assert
+ , packageUtil = require('ender-repository').util
, DependencyTree = require('../../lib/dependency-tree')
- , packageUtil = require('../../lib/package-util')
+
buster.testCase('Dependency tree', {
'constructDependencyTree': {
View
3  test/unit/install-test.js
@@ -40,7 +40,8 @@ testCase('Install', {
this.optionsArg = { options: 1 }
this.packagesArg = [ 'yee', 'haw' ] // length 2
- this.mockUtil.expects('mkdir').once().withArgs('node_modules').callsArg(1)
+ //this.mockUtil.expects('mkdir').once().withArgs('node_modules').callsArg(1)
+ // done by mkdirp now, probably should be mocked out...
this.mockRepository.expects('setup').once().callsArg(0)
this.mockRepository.expects('packup').once().callsArg(1)
View
2  test/unit/install-util-test.js
@@ -26,9 +26,9 @@
var buster = require('buster')
, path = require('path')
, assert = buster.assert
+ , packageUtil = require('ender-repository').util
, DependencyTree = require('../../lib/dependency-tree.js')
, installUtil = require('../../lib/install-util')
- , packageUtil = require('../../lib/package-util')
buster.testCase('Install util', {
'findMissingDependencies': {
View
2  test/unit/main-build-util-test.js
@@ -26,8 +26,8 @@
var buster = require('buster')
, path = require('path')
, assert = buster.assert
+ , packageUtil = require('ender-repository').util
, buildUtil = require('../../lib/main-build-util')
- , packageUtil = require('../../lib/package-util')
buster.testCase('Build util', {
'packageList': {
View
210 test/unit/package-descriptor-test.js
@@ -1,210 +0,0 @@
-var testCase = require('buster').testCase
- , PackageDescriptor = require('../../lib/package-descriptor')
-testCase('PackageDescriptor', {
- 'setUp': function () {
- this.runTest = function (json, key, expected, same) {
- var packageDescriptor = PackageDescriptor.create(json)
- key = Array.isArray(key) ? key : [ key ]
- expected = Array.isArray(expected) ? expected : [ expected ]
- same = typeof same == 'undefined' ? [ false ] : Array.isArray(same) ? same : [ same ]
- key.forEach(function (k, i) {
- assert[same[i] ? 'same' : 'equals'](packageDescriptor[key[i]], expected[i])
- })
- }
- }
- , 'test missing "ender" property': function () {
- var packageDescriptor = PackageDescriptor.create({
- name: 'foobar!'
- })
- refute.defined(packageDescriptor.ender)
- }
- , 'test standard "ender" property': function () {
- this.runTest(
- {
- name: 'foobar!'
- , ender: 'noop'
- }
- , 'ender'
- , 'noop'
- )
- }
- , 'test ender override "ender" property': function () {
- this.runTest(
- {
- name: 'foobar!'
- , ender: { bridge: 'yohoho' }
- }
- , 'ender'
- , 'yohoho'
- )
- }
- , 'test "ender->name" property replaces non-existent root': function () {
- this.runTest(
- { ender: { name: 'yohoho' } }
- , 'name'
- , 'yohoho'
- )
- }
- , 'test "overlay->ender->name" property replaces non-existent root': function () {
- this.runTest(
- { overlay: { ender: { name: 'yohoho' } } }
- , 'name'
- , 'yohoho'
- )
- }
- , 'test ender override "ender" property with "overlay->bridge"': function () {
- this.runTest(
- {
- ender: 'foobar!'
- , overlay: { ender: { bridge: 'yohoho' } }
- }
- , 'ender'
- , 'yohoho'
- )
- }
- , 'test standard name': function () {
- this.runTest(
- { name: 'foobar!' }
- , 'name'
- , 'foobar!'
- )
- }
- , 'test ender override name': function () {
- this.runTest(
- {
- name: 'foobar!'
- , ender: { name: 'bam!' }
- }
- , 'name'
- , 'bam!'
- )
- }
- , 'test ender override name wtih "overlay->ender"': function () {
- this.runTest(
- {
- name: 'foobar!'
- , overlay: { ender: { name: 'bam!' } }
- }
- , 'name'
- , 'bam!'
- )
- }
- , 'test standard dependencies': function () {
- var expected = { foo: '*', bar: '*' }
- this.runTest(
- { dependencies: expected }
- , 'dependencies'
- , expected
- , true
- )
- }
- , 'test ender override dependencies': function () {
- var expected = { fat: '*', fatter: '*' }
- this.runTest(
- {
- dependencies: { foo: '*', bar: '*' }
- , ender: { dependencies: expected }
- }
- , 'dependencies'
- , expected
- , true
- )
- }
- , 'test ender override dependencies with "overlay->ender"': function () {
- var expected = { fat: '*', fatter: '*' }
- this.runTest(
- {
- dependencies: { foo: '*', bar: '*' }
- , overlay: { ender: { dependencies: expected } }
- }
- , 'dependencies'
- , expected
- , true
- )
- }
- , 'test standard main': function () {
- var expected = [ 'foobar!' ]
- this.runTest(
- { main: expected }
- , [ 'main' ]
- , [ expected ]
- , [ true ]
- )
- }
- , 'test ender override main': function () {
- var expected = [ 'iiii am a mannnnn of constant sorrowwwww' ]
- this.runTest(
- {
- main: [ 'wha?' ]
- , ender: { main: expected }
- }
- , [ 'main' ]
- , [ expected ]
- , [ true ]
- )
- }
- , 'test ender override main with "overlay->ender"': function () {
- var expected = [ 'iiii am a mannnnn of constant sorrowwwww' ]
- this.runTest(
- {
- main: [ 'wha?' ]
- , overlay: { ender: { main: expected } }
- }
- , [ 'main' ]
- , [ expected ]
- , [ true ]
- )
- }
- , 'test if no override property then no override': function () {
- var expectedMain = [ 'wha?' ]
- this.runTest(
- {
- main: expectedMain
- , name: 'who?'
- , ender: { bogus: 'main and name properties not in here so should use root values' }
- }
- , [ 'main', 'name' ]
- , [ expectedMain, 'who?' ]
- , [ true, false ]
- )
- }
- , 'test if no overlay override property then no override': function () {
- var expectedMain = [ 'wha?' ]
- this.runTest(
- {
- main: expectedMain
- , name: 'who?'
- , overlay: {
- ender: {
- bogus: 'main and name properties not in here so should use root values'
- }
- }
- }
- , [ 'main', 'name' ]
- , [ expectedMain, 'who?' ]
- , [ true, false ]
- )
- }
- , 'test "ender" gets preference over "overlay->ender"': function () {
- var expectedMain = [ 'wha?' ]
- this.runTest(
- {
- main: [ 'nah' ]
- , name: 'who?"'
- , ender: {
- main: expectedMain
- , name: 'mary had a little lamb'
- }
- , overlay: {
- ender: {
- main: [ 'bam!' ]
- , name: 'nonono"'
- }
- }
- }
- , [ 'main', 'name' ]
- , [ expectedMain, 'mary had a little lamb' ]
- , [ true, false ]
- )
- }
-})
View
392 test/unit/package-util-test.js
@@ -1,392 +0,0 @@
-/*!
- * ENDER - The open module JavaScript framework
- *
- * Copyright (c) 2011-2012 @ded, @fat, @rvagg and other contributors
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is furnished
- * to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-
-var testCase = require('buster').testCase
- , fs = require('fs')
- , path = require('path')
- , xregexp = require('xregexp')
- , packageUtil = require('../../lib/package-util')
- , FilesystemError = require('../../lib/errors').FilesystemError
- , JSONParseError = require('../../lib/errors').JSONParseError
-
- , setupReadPackageJSON = function (testPath, data, callback) {
- var mock = this.mock(fs)
- , resolvedPath = path.resolve(testPath, 'package.json')
- mock.expects('readFile')
- .once()
- .withArgs(resolvedPath, 'utf-8')
- .callsArgWith(2, null, JSON.stringify(data))
- }
-
-testCase('Package util', {
- 'findRootPackageName': {
- 'test no root package': function (done) {
- packageUtil.findRootPackageName(
- [ 'pkg1', 'pkg2', '/foo/bar', 'foo/bar', '..', '../', '/' ]
- , function (err, rootPackageName) {
- refute(err)
- refute(rootPackageName)
- done()
- }
- )
- }
-
- , 'test "." root package': function (done) {
- var data = { name: 'a root package' }
-
- setupReadPackageJSON.call(this, '.', data)
- packageUtil.findRootPackageName(
- [ 'pkg1', 'pkg2', '/foo/bar', 'foo/bar', '..', '.', '../', '/' ]
- , function (err, rootPackageName) {
- refute(err)
- assert.equals(rootPackageName, data.name)
- done()
- }
- )
- }
-
- , 'test relative path root package': function (done) {
- var data = { name: 'foobar to youbar!' }
- , testPath = 'some/path/../..'
-
- setupReadPackageJSON.call(this, '.', data)
- packageUtil.findRootPackageName(
- [ 'pkg1', 'pkg2', '/foo/bar', 'foo/bar', '..', '../', '/', testPath ]
- , function (err, rootPackageName) {
- refute(err)
- assert.equals(rootPackageName, data.name)
- done()
- }
- )
- }
-
- , 'test package name with same name as cwd': function (done) {
- var testPath = path.basename(path.resolve())
- packageUtil.findRootPackageName(
- [ 'pkg1', 'pkg2', '/foo/bar', 'foo/bar', '..', '../', '/', testPath ]
- , function (err, rootPackageName) {
- refute(err)
- refute(rootPackageName)
- done()
- }
- )
- }
- }
-
- , 'readPackageJSON': {
- 'test standard module read': function (done) {
- var expected = { some: 'package', json: 'data' }
-
- setupReadPackageJSON.call(this, 'node_modules/amodule/', expected)
- packageUtil.readPackageJSON([], 'amodule', function (err, actual) {
- refute(err)
- assert.equals(Object.getPrototypeOf(actual), expected)
- assert.equals(actual, expected)
- done()
- })
- }
-
- , 'test module with "." in name read': function (done) {
- var expected = { some: 'package', json: 'data' }
-
- setupReadPackageJSON.call(this, 'node_modules/amodule.js/', expected)
- packageUtil.readPackageJSON([], 'amodule.js', function (err, actual) {
- refute(err)
- assert.equals(Object.getPrototypeOf(actual), expected)
- assert.equals(actual, expected)
- done()
- })
- }
-
- , 'test versioned module read': function (done) {
- var expected = { some: 'package', json: 'data' }
-
- setupReadPackageJSON.call(this, 'node_modules/amodule/', expected)
- packageUtil.readPackageJSON([], 'amodule@0.1.200', function (err, actual) {
- refute(err)
- assert.equals(Object.getPrototypeOf(actual), expected)
- assert.equals(actual, expected)
- done()
- })
- }
-
- , 'test module with parent read': function (done) {
- var expected = { some: 'package', json: 'data' }
-
- setupReadPackageJSON.call(this, 'node_modules/aparent/node_modules/amodule/', expected)
- packageUtil.readPackageJSON([ 'aparent' ], 'amodule', function (err, actual) {
- refute(err)
- assert.equals(Object.getPrototypeOf(actual), expected)
- assert.equals(actual, expected)
- done()
- })
- }
-
- , 'test module with multiple parents read': function (done) {
- var expected = { some: 'package', json: 'data' }
-
- setupReadPackageJSON.call(
- this
- , 'node_modules/aparent1/node_modules/aparent2/node_modules/aparent3/node_modules/amodule/'
- , expected
- )
- packageUtil.readPackageJSON(
- [ 'aparent1', 'aparent2', 'aparent3' ]
- , 'amodule', function (err, actual) {
- refute(err)
- assert.equals(Object.getPrototypeOf(actual), expected)
- assert.equals(actual, expected)
- done()
- }
- )
- }
-
- , 'test "./" module read': function (done) {
- var expected = { some: 'package', json: 'data' }
-
- setupReadPackageJSON.call(this, '.', expected)
- packageUtil.readPackageJSON([ 'this shouldn\'t matter' ], './', function (err, actual) {
- refute(err)
- assert.equals(Object.getPrototypeOf(actual), expected)
- assert.equals(actual, expected)
- done()
- })
- }
-
- , 'test relative path, no ".", module read': function (done) {
- var expected = { some: 'package', json: 'data' }
-
- setupReadPackageJSON.call(this, 'some/path/without/dots', expected)
- packageUtil.readPackageJSON([ 'foobar' ], 'some/path/without/dots', function (err, actual) {
- refute(err)
- assert.equals(Object.getPrototypeOf(actual), expected)
- assert.equals(actual, expected)
- done()
- })
- }
-
- , 'test relative path with "." module read': function (done) {
- var expected = { some: 'package', json: 'data' }
-
- setupReadPackageJSON.call(this, 'some/path/without/dots', expected)
- packageUtil.readPackageJSON([ 'what???' ], './some/path/../path/without/dots', function (err, actual) {
- refute(err)
- assert.equals(Object.getPrototypeOf(actual), expected)
- assert.equals(actual, expected)
- done()
- })
- }
-
- , 'test fs error': function (done) {
- var mockFs = this.mock(fs)
- , errArg = new Error('this is an error')
-
- mockFs.expects('readFile').once().callsArgWith(2, errArg)
-
- packageUtil.readPackageJSON([], 'whatevs', function (err, data) {
- assert(err)
- refute(data)
- assert(err instanceof FilesystemError)
- assert.same(err.cause, errArg)
- assert.same(err.message, errArg.message)
- done()
- })
- }
-
- , 'test JSON.parse error': function (done) {
- var mockFs = this.mock(fs)
-
- mockFs.expects('readFile').once().callsArgWith(2, null, 'not;json!@#$%^&*()')
-
- packageUtil.readPackageJSON([], 'whatevs', function (err, data) {
- assert(err)
- refute(data)
- assert(err instanceof JSONParseError)
- assert(err.cause)
- assert.match(err.message, /Unexpected token/)
- // includes reference to filename:
- assert.match(
- err.message
- , new RegExp(xregexp.XRegExp.escape(path.join('node_modules', 'whatevs', 'package.json')))
- )
- done()
- })
- }
- }
-
- , 'getPackageRoot': {
- 'test standard module name': function () {
- assert.equals(packageUtil.getPackageRoot([], 'amodule'), path.resolve('node_modules/amodule'))
- }
-
- , 'test module with "." in name': function () {
- assert.equals(packageUtil.getPackageRoot([], 'amodule.js'), path.resolve('node_modules/amodule.js'))
- }
-
- , 'test versioned module': function () {
- assert.equals(packageUtil.getPackageRoot([], 'amodule@0.1.200'), path.resolve('node_modules/amodule'))
- }
-
- , 'test "./" module': function () {
- assert.equals(packageUtil.getPackageRoot(['this shouldn\'t matter'], './'), path.resolve('.'))
- }
-
- , 'test relative path, no ".", module': function () {
- assert.equals(packageUtil.getPackageRoot(['foobar'], 'some/path/without/dots'), path.resolve('some/path/without/dots'))
- }
-
- , 'test relative path with "." module': function () {
- assert.equals(packageUtil.getPackageRoot(['what??'], 'some/path/../path/with/dots'), path.resolve('some/path/with/dots'))
- }
- }
-
- , 'getDependenciesFromJSON': {
- 'test missing dependencies': function () {
- assert.equals(packageUtil.getDependenciesFromJSON({}), [])
- }
-
- , 'test empty dependencies array': function () {
- assert.equals(packageUtil.getDependenciesFromJSON({ dependencies: [] }), [])
- }
-
- , 'test empty dependencies object': function () {
- assert.equals(packageUtil.getDependenciesFromJSON({ dependencies: {} }), [])
- }
-
- , 'test dependencies array': function () {
- assert.equals(packageUtil.getDependenciesFromJSON({ dependencies: [ 'dep1', 'dep2', 'dep3' ] }), [ 'dep1', 'dep2', 'dep3' ])
- }
-
- , 'test dependencies object': function () {
- assert.equals(packageUtil.getDependenciesFromJSON({ dependencies: {
- 'dep1': '*'
- , 'dep2': '0.0.1'
- , 'dep3': '>=0.1.1'
- }}), [ 'dep1', 'dep2', 'dep3' ])
- }
- }
-
- , 'getDependenciesFromDirectory': {
- 'test no node_modules directory': function (done) {
- var fsMock = this.mock(fs)
- , packageName = 'apackage'
- , parents = []
-
- fsMock.expects('exists')
- .withArgs(path.resolve('node_modules/apackage/node_modules'))
- .callsArgWith(1, false)
-
- packageUtil.getDependenciesFromDirectory(parents, packageName, function (err, dependencies) {
- refute(err)
- assert.equals(dependencies, [])
- done()
- })
- }
-
- , 'test empty node_modules directory': function (done) {
- var fsMock = this.mock(fs)
- , packageName = 'apackage'
- , parents = []
- , resolvedDir = path.resolve('node_modules/apackage/node_modules')
-
- fsMock.expects('exists').withArgs(resolvedDir).callsArgWith(1, true)
- fsMock.expects('readdir').withArgs(resolvedDir).callsArgWith(1, null, [])
-
- packageUtil.getDependenciesFromDirectory(parents, packageName, function (err, dependencies) {
- refute(err)
- assert.equals(dependencies, [])
- done()
- })
- }
-
- , 'test non-empty node_modules directory': function (done) {
- var fsMock = this.mock(fs)
- , packageName = 'apackage'
- , parents = []
- , resolvedDir = path.resolve('node_modules/apackage/node_modules')
-
- fsMock.expects('exists').withArgs(resolvedDir).callsArgWith(1, true)
- fsMock.expects('readdir').withArgs(resolvedDir).callsArgWith(1, null, [ 'a', 'list', 'of', 'files' ])
-
- packageUtil.getDependenciesFromDirectory(parents, packageName, function (err, dependencies) {
- refute(err)
- assert.equals(dependencies, [ 'a', 'list', 'of', 'files' ])
- done()
- })
- }
-
- , 'test non-empty node_modules directory, deep nesting': function (done) {
- var fsMock = this.mock(fs)
- , packageName = 'apackage'
- , parents = [ 'parent1', 'parent2', 'parent3' ]
- , resolvedDir = path.resolve(
- 'node_modules/parent1/node_modules/parent2/node_modules/parent3/node_modules/apackage/node_modules'
- )
-
- fsMock.expects('exists').withArgs(resolvedDir).callsArgWith(1, true)
- fsMock.expects('readdir').withArgs(resolvedDir).callsArgWith(1, null, [ 'a', 'list', 'of', 'files' ])
-
- packageUtil.getDependenciesFromDirectory(parents, packageName, function (err, dependencies) {
- refute(err)
- assert.equals(dependencies, [ 'a', 'list', 'of', 'files' ])
- done()
- })
- }
-
- , 'test fs error': function (done) {
- var mockFs = this.mock(fs)
- , errArg = new Error('this is an error')
-
- mockFs.expects('exists').once().callsArgWith(1, true)
- mockFs.expects('readdir').once().callsArgWith(1, errArg)
-
- packageUtil.getDependenciesFromDirectory([], 'whatevs', function (err, data) {
- assert(err)
- refute(data)
- assert(err instanceof FilesystemError)
- assert.same(err.cause, errArg)
- assert.same(err.message, errArg.message)
- done()
- })
- }
-
- , 'test filtering of .bin directory': function (done) {
- var fsMock = this.mock(fs)
- , packageName = 'apackage'
- , parents = []
- , resolvedDir = path.resolve('node_modules/apackage/node_modules')
-
- fsMock.expects('exists').withArgs(resolvedDir).callsArgWith(1, true)
- fsMock.expects('readdir').withArgs(resolvedDir).callsArgWith(1, null, [ '.bin', 'some', 'other', 'modules' ])
-
- packageUtil.getDependenciesFromDirectory(parents, packageName, function (err, dependencies) {
- refute(err)
- assert.equals(dependencies, [ 'some', 'other', 'modules' ])
- done()
- })
- }
- }
-})
-

0 comments on commit 1bebb20

Please sign in to comment.
Something went wrong with that request. Please try again.