pax_global_header00006660000000000000000000000064140767144650014530gustar00rootroot0000000000000052 comment=1880cbb697c5027855a60c169202a2d8a03ce739
rechoir-0.8.0/000077500000000000000000000000001407671446500131705ustar00rootroot00000000000000rechoir-0.8.0/.editorconfig000066400000000000000000000003261407671446500156460ustar00rootroot00000000000000# http://editorconfig.org
root = true
[*]
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
end_of_line = lf
[*.md]
trim_trailing_whitespace = false
rechoir-0.8.0/.eslintrc000066400000000000000000000001021407671446500150050ustar00rootroot00000000000000{
"extends": "gulp",
"rules": {
"max-statements": 0
}
}
rechoir-0.8.0/.gitattributes000066400000000000000000000000161407671446500160600ustar00rootroot00000000000000* text eol=lf
rechoir-0.8.0/.github/000077500000000000000000000000001407671446500145305ustar00rootroot00000000000000rechoir-0.8.0/.github/workflows/000077500000000000000000000000001407671446500165655ustar00rootroot00000000000000rechoir-0.8.0/.github/workflows/dev.yml000066400000000000000000000031051407671446500200650ustar00rootroot00000000000000name: dev
on:
pull_request:
push:
branches:
- master
- main
env:
CI: true
jobs:
prettier:
name: Format code
runs-on: ubuntu-latest
if: ${{ github.event_name == 'push' }}
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Prettier
uses: gulpjs/prettier_action@v3.0
with:
commit_message: 'chore: Run prettier'
prettier_options: '--write .'
test:
name: Tests for Node ${{ matrix.node }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
node: [10, 12, 14, 16]
os: [ubuntu-latest, windows-latest, macos-latest]
steps:
- name: Clone repository
uses: actions/checkout@v2
- name: Set Node.js version
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node }}
- run: node --version
- run: npm --version
- name: Install npm dependencies
run: npm install
- name: Run lint
run: npm run lint
- name: Run tests
run: npm test
- name: Coveralls
uses: coverallsapp/github-action@v1.1.2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
flag-name: ${{matrix.os}}-node-${{ matrix.node }}
parallel: true
coveralls:
needs: test
name: Finish up
runs-on: ubuntu-latest
steps:
- name: Coveralls Finished
uses: coverallsapp/github-action@v1.1.2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
parallel-finished: true
rechoir-0.8.0/.github/workflows/release.yml000066400000000000000000000005511407671446500207310ustar00rootroot00000000000000name: release
on:
push:
branches:
- master
- main
jobs:
release-please:
runs-on: ubuntu-latest
steps:
- uses: GoogleCloudPlatform/release-please-action@v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
release-type: node
package-name: release-please-action
bump-minor-pre-major: true
rechoir-0.8.0/.gitignore000066400000000000000000000017101407671446500151570ustar00rootroot00000000000000# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# nyc test coverage
.nyc_output
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# TypeScript v1 declaration files
typings/
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
# next.js build output
.next
# Garbage files
.DS_Store
# Test results
test.xunit
rechoir-0.8.0/.npmrc000066400000000000000000000000231407671446500143030ustar00rootroot00000000000000package-lock=false
rechoir-0.8.0/.prettierignore000066400000000000000000000000441407671446500162310ustar00rootroot00000000000000coverage/
.nyc_output/
CHANGELOG.md
rechoir-0.8.0/CHANGELOG.md000066400000000000000000000006471407671446500150100ustar00rootroot00000000000000# Changelog
## [0.8.0](https://www.github.com/gulpjs/rechoir/compare/v0.7.1...v0.8.0) (2021-07-24)
### ⚠ BREAKING CHANGES
* Normalize repository, dropping node <10.13 support (#40)
### Miscellaneous Chores
* Normalize repository, dropping node <10.13 support ([#40](https://www.github.com/gulpjs/rechoir/issues/40)) ([00f5968](https://www.github.com/gulpjs/rechoir/commit/00f59689d0eb9668d939a85e06428a0906587a6f))
rechoir-0.8.0/LICENSE000066400000000000000000000022501407671446500141740ustar00rootroot00000000000000The MIT License (MIT)
Copyright (c) 2014-2019, 2021 Tyler Kellen , Blaine Bublitz , and Eric Schoffstall
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.
rechoir-0.8.0/README.md000066400000000000000000000055641407671446500144610ustar00rootroot00000000000000
# rechoir
[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][ci-image]][ci-url] [![Coveralls Status][coveralls-image]][coveralls-url]
Prepare a node environment to require files with different extensions.
## What is it?
This module, in conjunction with [interpret]-like objects, can register any filetype the npm ecosystem has a module loader for. This library is a dependency of [liftoff].
**Note:** While `rechoir` will automatically load and register transpilers like `coffee-script`, you must provide a local installation. The transpilers are **not** bundled with this module.
## Usage
```js
const config = require('interpret').extensions;
const rechoir = require('rechoir');
rechoir.prepare(config, './test/fixtures/test.coffee');
rechoir.prepare(config, './test/fixtures/test.csv');
rechoir.prepare(config, './test/fixtures/test.toml');
console.log(require('./test/fixtures/test.coffee'));
console.log(require('./test/fixtures/test.csv'));
console.log(require('./test/fixtures/test.toml'));
```
## API
### `prepare(config, filepath, [cwd], [noThrow])`
Look for a module loader associated with the provided file and attempt require it. If necessary, run any setup required to inject it into [require.extensions].
`config` An [interpret]-like configuration object.
`filepath` A file whose type you'd like to register a module loader for.
`cwd` An optional path to start searching for the module required to load the requested file. Defaults to the directory of `filepath`.
`noThrow` An optional boolean indicating if the method should avoid throwing.
If calling this method is successful (e.g. it doesn't throw), you can now require files of the type you requested natively.
An error with a `failures` property will be thrown if the module loader(s) configured for a given extension cannot be registered.
If a loader is already registered, this will simply return `true`.
## License
MIT
[downloads-image]: https://img.shields.io/npm/dm/rechoir.svg?style=flat-square
[npm-url]: https://www.npmjs.com/package/rechoir
[npm-image]: https://img.shields.io/npm/v/rechoir.svg?style=flat-square
[ci-url]: https://github.com/gulpjs/rechoir/actions?query=workflow:dev
[ci-image]: https://img.shields.io/github/workflow/status/gulpjs/rechoir/dev?style=flat-square
[coveralls-url]: https://coveralls.io/r/gulpjs/rechoir
[coveralls-image]: https://img.shields.io/coveralls/gulpjs/rechoir/master.svg
[interpret]: https://github.com/gulpjs/interpret
[require.extensions]: https://nodejs.org/api/modules.html#modules_require_extensions
[liftoff]: https://github.com/js-cli/js-liftoff
rechoir-0.8.0/index.js000066400000000000000000000027251407671446500146430ustar00rootroot00000000000000var path = require('path');
var extension = require('./lib/extension');
var normalize = require('./lib/normalize');
var register = require('./lib/register');
exports.prepare = function (extensions, filepath, cwd, nothrow) {
var config, usedExtension, err, option, attempt, error;
var attempts = [];
var onlyErrors = true;
var exts = extension(filepath);
if (exts) {
exts.some(function (ext) {
usedExtension = ext;
config = normalize(extensions[ext]);
return !!config;
});
}
if (Object.keys(require.extensions).indexOf(usedExtension) !== -1) {
return true;
}
if (!config) {
if (nothrow) {
return;
}
throw new Error('No module loader found for "' + usedExtension + '".');
}
if (!cwd) {
cwd = path.dirname(path.resolve(filepath));
}
if (!Array.isArray(config)) {
config = [config];
}
for (var i in config) {
option = config[i];
attempt = register(cwd, option.module, option.register);
error = attempt instanceof Error ? attempt : null;
if (error) {
attempt = null;
}
attempts.push({
moduleName: option.module,
module: attempt,
error: error,
});
if (!error) {
onlyErrors = false;
break;
}
}
if (onlyErrors) {
err = new Error(
'Unable to use specified module loaders for "' + usedExtension + '".'
);
err.failures = attempts;
if (nothrow) {
return err;
}
throw err;
}
return attempts;
};
rechoir-0.8.0/lib/000077500000000000000000000000001407671446500137365ustar00rootroot00000000000000rechoir-0.8.0/lib/extension.js000066400000000000000000000016321407671446500163120ustar00rootroot00000000000000'use strict';
var path = require('path');
function getLongExtension(basename) {
if (basename[basename.length - 1] === '.') {
return null;
}
var startIndex = basename[0] === '.' ? 1 : 0;
var dotIndex = basename.indexOf('.', startIndex);
if (dotIndex <= startIndex) {
return null;
}
return basename.slice(dotIndex);
}
function getPossibleExtensions(longExtension) {
var arr = [longExtension];
var len = longExtension.length;
var startIndex = 1;
while (startIndex < len) {
var dotIndex = longExtension.indexOf('.', startIndex);
if (dotIndex < 0) {
break;
}
arr.push(longExtension.slice(dotIndex));
startIndex = dotIndex + 1;
}
return arr;
}
module.exports = function (input) {
var basename = path.basename(input);
var longExtension = getLongExtension(basename);
if (!longExtension) {
return;
}
return getPossibleExtensions(longExtension);
};
rechoir-0.8.0/lib/normalize.js000066400000000000000000000004041407671446500162720ustar00rootroot00000000000000function normalizer(config) {
if (typeof config === 'string') {
return { module: config };
}
return config;
}
module.exports = function (config) {
if (Array.isArray(config)) {
return config.map(normalizer);
}
return normalizer(config);
};
rechoir-0.8.0/lib/register.js000066400000000000000000000005241407671446500161210ustar00rootroot00000000000000var resolve = require('resolve');
module.exports = function (cwd, moduleName, register) {
var result;
try {
var modulePath = resolve.sync(moduleName, { basedir: cwd });
result = require(modulePath);
if (typeof register === 'function') {
register(result);
}
} catch (e) {
result = e;
}
return result;
};
rechoir-0.8.0/package.json000066400000000000000000000020651407671446500154610ustar00rootroot00000000000000{
"name": "rechoir",
"version": "0.8.0",
"description": "Prepare a node environment to require files with different extensions.",
"author": "Gulp Team (http://gulpjs.com/)",
"contributors": [
"Blaine Bublitz ",
"Tyler Kellen (http://goingslowly.com/)"
],
"repository": "gulpjs/rechoir",
"license": "MIT",
"engines": {
"node": ">= 10.13.0"
},
"main": "index.js",
"files": [
"LICENSE",
"index.js",
"lib/"
],
"scripts": {
"lint": "eslint .",
"pretest": "rm -rf tmp/ && npm run lint",
"test": "nyc mocha --async-only"
},
"dependencies": {
"resolve": "^1.20.0"
},
"devDependencies": {
"eslint": "^7.21.0",
"eslint-config-gulp": "^5.0.1",
"expect": "^27.0.0",
"mocha": "^8.3.0",
"nyc": "^15.1.0"
},
"nyc": {
"reporter": [
"lcov",
"text-summary"
]
},
"prettier": {
"singleQuote": true
},
"keywords": [
"require",
"loader",
"extension",
"extensions",
"prepare"
]
}
rechoir-0.8.0/test/000077500000000000000000000000001407671446500141475ustar00rootroot00000000000000rechoir-0.8.0/test/extension.test.js000066400000000000000000000102701407671446500174770ustar00rootroot00000000000000'use strict';
var expect = require('expect');
var extension = require('../lib/extension');
describe('Take out possible extensions from a file path', function () {
it('should return an extension: ".js" from "app.js"', function (done) {
expect(extension('app.js')).toEqual(['.js']);
expect(extension('a/b/c/app.js')).toEqual(['.js']);
done();
});
it('should return extensions: ".babel.js" and ".js" from "app.babel.js"', function (done) {
expect(extension('app.babel.js')).toEqual(['.babel.js', '.js']);
expect(extension('a/b/c/app.babel.js')).toEqual(['.babel.js', '.js']);
done();
});
it('should return extensions: ".aaa.bbb.ccc", ".aaa.bbb" and ".ccc" from "app.aaa.bbb.ccc"', function (done) {
expect(extension('app.aaa.bbb.ccc')).toEqual([
'.aaa.bbb.ccc',
'.bbb.ccc',
'.ccc',
]);
expect(extension('a/b/c/app.aaa.bbb.ccc')).toEqual([
'.aaa.bbb.ccc',
'.bbb.ccc',
'.ccc',
]);
done();
});
it('should return an extension: ".j" from "app.j"', function (done) {
expect(extension('app.j')).toEqual(['.j']);
expect(extension('a/b/c/app.j')).toEqual(['.j']);
done();
});
it('should return extensions: ".b.j" and ".j" from "app.b.j"', function (done) {
expect(extension('app.b.j')).toEqual(['.b.j', '.j']);
expect(extension('a/b/c/app.b.j')).toEqual(['.b.j', '.j']);
done();
});
it('should return extensions: ".a.b.c", ".a.b" and ".c" from "app.a.b.c"', function (done) {
expect(extension('app.a.b.c')).toEqual(['.a.b.c', '.b.c', '.c']);
expect(extension('a/b/c/app.a.b.c')).toEqual(['.a.b.c', '.b.c', '.c']);
done();
});
it('should return undefined from "."', function (done) {
expect(extension('.')).toBe(undefined);
expect(extension('a/b/c/.')).toBe(undefined);
done();
});
it('should return undefined from ".."', function (done) {
expect(extension('..')).toBe(undefined);
expect(extension('a/b/c/..')).toBe(undefined);
done();
});
it('should return undefined from "..."', function (done) {
expect(extension('...')).toBe(undefined);
expect(extension('a/b/c/...')).toBe(undefined);
done();
});
it('should return undefined from "a."', function (done) {
expect(extension('a.')).toBe(undefined);
expect(extension('a/b/c/a.')).toBe(undefined);
done();
});
it('should return undefined from "app."', function (done) {
expect(extension('app.')).toBe(undefined);
expect(extension('a/b/c/app.')).toBe(undefined);
done();
});
it('should return undefined from "a.b.c."', function (done) {
expect(extension('a.b.c.')).toBe(undefined);
expect(extension('a/b/c/a.b.c.')).toBe(undefined);
done();
});
it('should return undefined from ".a"', function (done) {
expect(extension('.a')).toBe(undefined);
expect(extension('a/b/c/.a')).toBe(undefined);
done();
});
it('should return undefined from ".app"', function (done) {
expect(extension('.app')).toBe(undefined);
expect(extension('a/b/c/.app')).toBe(undefined);
done();
});
it('should return undefined from ".a."', function (done) {
expect(extension('.a.')).toBe(undefined);
expect(extension('a/b/c/.a.')).toBe(undefined);
done();
});
it('should return undefined from ".app."', function (done) {
expect(extension('.app.')).toBe(undefined);
expect(extension('a/b/c/.app.')).toBe(undefined);
done();
});
it('should return undefined from ".a.b.c."', function (done) {
expect(extension('.a.b.c.')).toBe(undefined);
expect(extension('a/b/c/.a.b.c.')).toBe(undefined);
done();
});
it('should return ".b.c" and ".c" from ".a.b.c"', function (done) {
expect(extension('.a.b.c')).toEqual(['.b.c', '.c']);
expect(extension('a/b/c/.a.b.c')).toEqual(['.b.c', '.c']);
done();
});
it('should return ".bb.cc" and ".cc" from ".aa.bb.cc"', function (done) {
expect(extension('.aa.bb.cc')).toEqual(['.bb.cc', '.cc']);
expect(extension('a/b/c/.aa.bb.cc')).toEqual(['.bb.cc', '.cc']);
done();
});
it('should return "..b" and ".b" from ".a..b"', function (done) {
expect(extension('.a..b')).toEqual(['..b', '.b']);
expect(extension('a/b/c/.a..b')).toEqual(['..b', '.b']);
done();
});
});
rechoir-0.8.0/test/fixtures/000077500000000000000000000000001407671446500160205ustar00rootroot00000000000000rechoir-0.8.0/test/fixtures/.testrc000066400000000000000000000001611407671446500173230ustar00rootroot00000000000000module.exports = {
data: {
trueKey: true,
falseKey: false,
subKey: {
subProp: 1
}
}
};
rechoir-0.8.0/test/fixtures/cwd/000077500000000000000000000000001407671446500165755ustar00rootroot00000000000000rechoir-0.8.0/test/fixtures/cwd/test.foo000066400000000000000000000000001407671446500202470ustar00rootroot00000000000000rechoir-0.8.0/test/fixtures/folder.with.dots/000077500000000000000000000000001407671446500212155ustar00rootroot00000000000000rechoir-0.8.0/test/fixtures/folder.with.dots/test.stub000066400000000000000000000001611407671446500230710ustar00rootroot00000000000000module.exports = {
data: {
trueKey: true,
falseKey: false,
subKey: {
subProp: 1
}
}
};
rechoir-0.8.0/test/fixtures/test.s000066400000000000000000000001611407671446500171610ustar00rootroot00000000000000module.exports = {
data: {
trueKey: true,
falseKey: false,
subKey: {
subProp: 1
}
}
};
rechoir-0.8.0/test/fixtures/test.stub000066400000000000000000000001611407671446500176740ustar00rootroot00000000000000module.exports = {
data: {
trueKey: true,
falseKey: false,
subKey: {
subProp: 1
}
}
};
rechoir-0.8.0/test/index.js000066400000000000000000000202301407671446500156110ustar00rootroot00000000000000var path = require('path');
var Module = require('module');
var expect = require('expect');
var rechoir = require('../');
var extension = require('../lib/extension');
var normalize = require('../lib/normalize');
var register = require('../lib/register');
// save the original Module._extensions
var originalExtensions = Object.keys(Module._extensions);
var original = originalExtensions.reduce(function (result, key) {
result[key] = require.extensions[key];
return result;
}, {});
// save the original cache keys
var originalCacheKeys = Object.keys(require.cache);
function cleanupCache(key) {
if (originalCacheKeys.indexOf(key) === -1) {
delete require.cache[key];
}
}
function cleanupExtensions(ext) {
if (originalExtensions.indexOf(ext) === -1) {
delete Module._extensions[ext];
} else {
Module._extensions[ext] = original[ext];
}
}
function cleanup(done) {
// restore the require.cache to startup state
Object.keys(require.cache).forEach(cleanupCache);
// restore the original Module._extensions
Object.keys(Module._extensions).forEach(cleanupExtensions);
done();
}
describe('rechoir', function () {
describe('extension', function () {
it('should extract extension from filename/path from the first dot', function (done) {
expect(extension('file.js')[0]).toEqual('.js');
expect(extension('file.tmp.dot.js')[0]).toEqual('.tmp.dot.js');
expect(extension('file.tmp.dot.js')[1]).toEqual('.dot.js');
expect(extension('file.tmp.dot.js')[2]).toEqual('.js');
expect(extension('relative/path/to/file.js')[0]).toEqual('.js');
expect(extension('relative/path/to/file.dot.js')[0]).toEqual('.dot.js');
expect(extension('relative/path/to/file.dot.js')[1]).toEqual('.js');
expect(extension('relative/path.with.dot/to/file.dot.js')[0]).toEqual(
'.dot.js'
);
expect(extension('relative/path.with.dot/to/file.dot.js')[1]).toEqual(
'.js'
);
done();
});
it('does not match any if the path ends in a dot', function (done) {
expect(extension('file.js.')).toEqual(undefined);
done();
});
it('treats additional dots as a separate extension', function (done) {
// Double
expect(extension('file.babel..js')).toEqual([
'.babel..js',
'..js',
'.js',
]);
expect(extension('file..babel.js')).toEqual([
'..babel.js',
'.babel.js',
'.js',
]);
// Triple
expect(extension('file.babel...js')).toEqual([
'.babel...js',
'...js',
'..js',
'.js',
]);
expect(extension('file...babel.js')).toEqual([
'...babel.js',
'..babel.js',
'.babel.js',
'.js',
]);
done();
});
it('does not consider a leading dot to be an extension', function (done) {
expect(extension('.config')).toEqual(undefined);
done();
});
});
describe('normalize', function () {
it('should convert a string input into array/object format', function (done) {
expect(normalize('foo')).toEqual({ module: 'foo' });
done();
});
it('should convert object input into array format', function (done) {
var input = {
module: 'foo',
};
expect(normalize(input)).toEqual(input);
done();
});
it('should iterate an array, normalizing each item', function (done) {
var input = [{ module: 'foo' }, 'bar'];
expect(normalize(input)).toEqual([{ module: 'foo' }, { module: 'bar' }]);
done();
});
});
describe('register', function () {
it('should return the specified module relative to the provided cwd', function (done) {
expect(register(__dirname, 'expect')).toEqual(expect);
done();
});
it('should call a register function if provided, passing in the module', function (done) {
register(__dirname, 'expect', function (attempt) {
expect(attempt).toEqual(expect);
});
done();
});
it('should return an error if the specified module cannot be registered', function (done) {
expect(register(__dirname, 'whatev')).toBeInstanceOf(Error);
done();
});
});
describe('prepare', function () {
var testFilePath = path.join(__dirname, 'fixtures', 'test.stub');
beforeEach(cleanup);
it('should throw if extension is unknown', function (done) {
expect(function () {
rechoir.prepare({}, './test/fixtures/test.whatever');
}).toThrow(/No module loader found for/);
done();
});
it('should return undefined if an unknown extension is specified when nothrow is enabled', function (done) {
expect(
rechoir.prepare({}, './test/fixtures/.testrc', null, true)
).toEqual(undefined);
done();
});
it('should throw if a module loader cannot be found or loaded', function (done) {
expect(function () {
rechoir.prepare(
{
'.stub': ['nothere'],
},
testFilePath
);
require(testFilePath);
}).toThrow();
done();
});
describe('all module loaders that were attempted failed to load', function () {
var exts = {
'.stub': ['nothere', 'orhere'],
};
// Check the failure entries in the thrown or returned error object.
function checkFailures(e) {
expect(e.failures).toBeInstanceOf(Array);
expect(e.failures[0].error).toBeInstanceOf(Error);
expect(e.failures[0].moduleName).toEqual('nothere');
expect(e.failures[0].module).toEqual(null);
expect(e.failures[1].error).toBeInstanceOf(Error);
expect(e.failures[1].moduleName).toEqual('orhere');
expect(e.failures[1].module).toEqual(null);
}
it('should throw error listing each module loader that was attempted when nothrow = false', function (done) {
var err;
try {
rechoir.prepare(exts, testFilePath);
} catch (e) {
err = e;
checkFailures(e);
}
expect(err).toBeInstanceOf(Error);
done();
});
it('should return error listing each module loader that was attempted when nothrow = true', function (done) {
checkFailures(rechoir.prepare(exts, testFilePath, null, true));
done();
});
});
it('should register a module loader for the specified extension', function (done) {
rechoir.prepare(
{
'.stub': ['nothere', '../require-stub'],
},
testFilePath
);
expect(function () {
require(testFilePath);
}).not.toThrow(Error);
done();
});
it('should return true if the module loader for the specified extension is already available', function (done) {
rechoir.prepare(
{
'.stub': ['nothere', '../require-stub'],
},
testFilePath
);
expect(
rechoir.prepare(
{
'.stub': ['nothere', '../require-stub'],
},
testFilePath
)
).toEqual(true);
done();
});
it('must not fail on folders with dots', function (done) {
rechoir.prepare(
{ '.stub': '../../require-stub' },
'./test/fixtures/folder.with.dots/test.stub'
);
expect(require('./fixtures/folder.with.dots/test.stub')).toEqual({
data: {
trueKey: true,
falseKey: false,
subKey: {
subProp: 1,
},
},
});
done();
});
it('should register a module loader even if the extension is single character (issue #38)', function (done) {
var fpath = path.join(__dirname, 'fixtures', 'test.s');
rechoir.prepare(
{
'.s': ['nothere', '../require-stub'],
},
fpath
);
expect(function () {
require(fpath);
}).not.toThrow(Error);
done();
});
it('should register a module loader for the specified extension with cwd', function (done) {
rechoir.prepare(
{
'.foo': ['nothere', '../require-stub'],
},
path.join('cwd', 'test.foo'),
path.join(__dirname, 'fixtures')
);
expect(function () {
require(testFilePath);
}).not.toThrow(Error);
done();
});
});
});
rechoir-0.8.0/test/require-stub.js000066400000000000000000000000711407671446500171320ustar00rootroot00000000000000require.extensions['.stub'] = require.extensions['.js'];