pax_global_header00006660000000000000000000000064117211255130014510gustar00rootroot0000000000000052 comment=2206412b5095470de83a2c03c2593ee6315423ab felixge-node-require-like-2206412/000077500000000000000000000000001172112551300166345ustar00rootroot00000000000000felixge-node-require-like-2206412/.gitignore000066400000000000000000000000241172112551300206200ustar00rootroot00000000000000*.un~ /node_modules felixge-node-require-like-2206412/.travis.yml000066400000000000000000000000531172112551300207430ustar00rootroot00000000000000language: node_js node_js: - 0.4 - 0.6 felixge-node-require-like-2206412/License000066400000000000000000000021151172112551300201400ustar00rootroot00000000000000Copyright (c) 2011 Felix Geisendörfer (felix@debuggable.com) 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. felixge-node-require-like-2206412/Makefile000066400000000000000000000001141172112551300202700ustar00rootroot00000000000000SHELL := /bin/bash NODE = node test: @$(NODE) ./test/run.js .PHONY: test felixge-node-require-like-2206412/Readme.md000066400000000000000000000024201172112551300203510ustar00rootroot00000000000000# require-like [![Build Status](https://secure.travis-ci.org/felixge/node-require-like.png)](http://travis-ci.org/felixge/node-require-like) Generates require functions that act as if they were operating in a given path. ## Install ``` bash npm install require-like ``` ## Usage A require function that acts as if it was executed in `'./lib/bar'`: ``` javascript var requireLike = require('require-like'); var myRequire = requireLike(__dirname + '/lib/bar.js'); var myFoo = myRequire('./foo.js'); var foo = require('./lib/foo'); require('assert').strictEqual(myFoo, foo); ``` ## API ### requireLike(path, [uncached]) Returns a require function that acts as if it was operating in the given `path`. Setting the `uncached` parameter to true returns a function that by-passes the module cache. ## Implementation Details This module works by accessing some private node APIs. You shouldn't worry about that so, since I will make sure this module does not break in the future by either patching it, or making a patch for node that makes the needed APIs public. ## What to do with this I use this library for dependency injection in unit tests. However, you could also use it to create experimental require addons yourself. ## License require-like is licensed under the MIT license. felixge-node-require-like-2206412/lib/000077500000000000000000000000001172112551300174025ustar00rootroot00000000000000felixge-node-require-like-2206412/lib/require-like.js000066400000000000000000000021071172112551300223360ustar00rootroot00000000000000var Module = require('module'); var dirname = require('path').dirname; module.exports = function requireLike(path, uncached) { var parentModule = new Module(path); parentModule.filename = path; parentModule.paths = Module._nodeModulePaths(dirname(path)); function requireLike(file) { var cache = Module._cache; if (uncached) { Module._cache = {}; } var exports = Module._load(file, parentModule); Module._cache = cache; return exports; }; requireLike.resolve = function(request) { var resolved = Module._resolveFilename(request, parentModule); // Module._resolveFilename returns a string since node v0.6.10, // it used to return an array prior to that return (resolved instanceof Array) ? resolved[1] : resolved; } try { requireLike.paths = require.paths; } catch (e) { //require.paths was deprecated in node v0.5.x //it now throws an exception when called } requireLike.main = process.mainModule; requireLike.extensions = require.extensions; requireLike.cache = require.cache; return requireLike; }; felixge-node-require-like-2206412/package.json000066400000000000000000000011711172112551300211220ustar00rootroot00000000000000{ "author": "Felix Geisendörfer (http://debuggable.com/)", "name": "require-like", "description": "Generates require functions that act as if they were operating in a given path.", "version": "0.1.2", "homepage": "https://github.com/felixge/node-require-like", "repository": { "type": "git", "url": "git://github.com/felixge/node-require-like.git" }, "main": "./lib/require-like", "engines": { "node": "*" }, "scripts": { "test": "make test" }, "dependencies": {}, "devDependencies": { "hashish": "0.0.3", "urun": "0.0.4" }, "optionalDependencies": {} }felixge-node-require-like-2206412/test/000077500000000000000000000000001172112551300176135ustar00rootroot00000000000000felixge-node-require-like-2206412/test/common.js000066400000000000000000000003071172112551300214410ustar00rootroot00000000000000var common = exports; var path = require('path'); var root = path.dirname(__dirname); common.dir = { lib: root + '/lib', fixture: root + '/test/fixture', }; common.assert = require('assert'); felixge-node-require-like-2206412/test/fixture/000077500000000000000000000000001172112551300213015ustar00rootroot00000000000000felixge-node-require-like-2206412/test/fixture/foo.js000066400000000000000000000000271172112551300224210ustar00rootroot00000000000000exports.foo = ['foo']; felixge-node-require-like-2206412/test/integration/000077500000000000000000000000001172112551300221365ustar00rootroot00000000000000felixge-node-require-like-2206412/test/integration/test-basics.js000066400000000000000000000030731172112551300247200ustar00rootroot00000000000000var common = require('../common'); var assert = common.assert; var requireLike = require(common.dir.lib + '/require-like'); (function testWithCache() { var foo = require(common.dir.fixture + '/foo.js'); var myRequire = requireLike(common.dir.fixture + '/bar.js'); var myFoo = myRequire('./foo'); assert.strictEqual(myFoo, foo); })(); (function testWithoutCache() { var foo = require(common.dir.fixture + '/foo.js'); var myRequire = requireLike(common.dir.fixture + '/bar.js', true); var myFoo = myRequire('./foo'); assert.notStrictEqual(myFoo, foo); assert.deepEqual(myFoo, foo); })(); (function testResolve() { var myRequire = requireLike(common.dir.fixture + '/bar.js'); var fooPath = myRequire.resolve('./foo'); assert.strictEqual(fooPath, common.dir.fixture + '/foo.js'); })(); if (process.version <= 'v0.5') { (function testPaths() { var myRequire = requireLike(common.dir.fixture + '/bar.js'); assert.strictEqual(myRequire.paths, require.paths); })(); } (function testMain() { var myRequire = requireLike(common.dir.fixture + '/bar.js'); assert.strictEqual(myRequire.main, process.mainModule); })(); (function testExtensions() { var myRequire = requireLike(common.dir.fixture + '/bar.js'); assert.strictEqual(myRequire.extensions, require.extensions); })(); (function testCache() { var myRequire = requireLike(common.dir.fixture + '/bar.js'); assert.strictEqual(myRequire.cache, require.cache); })(); (function testLoadNodeModule() { var myRequire = requireLike(common.dir.lib + '/foo.js', true); myRequire('hashish'); })(); felixge-node-require-like-2206412/test/run.js000066400000000000000000000000341172112551300207520ustar00rootroot00000000000000require('urun')(__dirname);