| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 94b7f53 commit fabed9b
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,25 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + require('../common'); | ||
| 3 | + | ||
| 4 | + // This tests Module._stat. | ||
| 5 | + | ||
| 6 | + const Module = require('module'); | ||
| 7 | + const fs = require('fs'); | ||
| 8 | + const tmpdir = require('../common/tmpdir'); | ||
| 9 | + const { ok, strictEqual } = require('assert'); | ||
| 10 | + const { join } = require('path'); | ||
| 11 | + | ||
| 12 | + const directory = join(tmpdir.path, 'directory'); | ||
| 13 | + const doesNotExist = join(tmpdir.path, 'does-not-exist'); | ||
| 14 | + const file = join(tmpdir.path, 'file.js'); | ||
| 15 | + | ||
| 16 | + tmpdir.refresh(); | ||
| 17 | + fs.writeFileSync(file, "module.exports = { a: 'b' }"); | ||
| 18 | + fs.mkdirSync(directory); | ||
| 19 | + | ||
| 20 | + strictEqual(Module._stat(directory), 1); // Returns 1 for directories. | ||
| 21 | + strictEqual(Module._stat(file), 0); // Returns 0 for files. | ||
| 22 | + ok(Module._stat(doesNotExist) < 0); // Returns a negative integer for any other kind of strings. | ||
| 23 | + | ||
| 24 | + // TODO(RaisinTen): Add tests that make sure that Module._stat() does not crash when called | ||
| 25 | + // with a non-string data type. It crashes currently. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,88 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common'); | ||
| 3 | + | ||
| 4 | + // This tests the creation of a vfs by monkey-patching fs and Module._stat. | ||
| 5 | + | ||
| 6 | + const Module = require('module'); | ||
| 7 | + const fs = require('fs'); | ||
| 8 | + const tmpdir = require('../common/tmpdir'); | ||
| 9 | + const { deepStrictEqual, ok, strictEqual, throws } = require('assert'); | ||
| 10 | + const { join } = require('path'); | ||
| 11 | + | ||
| 12 | + const directory = join(tmpdir.path, 'directory'); | ||
| 13 | + const doesNotExist = join(tmpdir.path, 'does-not-exist'); | ||
| 14 | + const file = join(tmpdir.path, 'file.js'); | ||
| 15 | + | ||
| 16 | + tmpdir.refresh(); | ||
| 17 | + fs.writeFileSync(file, "module.exports = { a: 'b' }"); | ||
| 18 | + fs.mkdirSync(directory); | ||
| 19 | + | ||
| 20 | + strictEqual(Module._stat(directory), 1); | ||
| 21 | + ok(Module._stat(doesNotExist) < 0); | ||
| 22 | + strictEqual(Module._stat(file), 0); | ||
| 23 | + | ||
| 24 | + const vfsDirectory = join(process.execPath, 'directory'); | ||
| 25 | + const vfsDoesNotExist = join(process.execPath, 'does-not-exist'); | ||
| 26 | + const vfsFile = join(process.execPath, 'file.js'); | ||
| 27 | + | ||
| 28 | + ok(Module._stat(vfsDirectory) < 0); | ||
| 29 | + ok(Module._stat(vfsDoesNotExist) < 0); | ||
| 30 | + ok(Module._stat(vfsFile) < 0); | ||
| 31 | + | ||
| 32 | + deepStrictEqual(require(file), { a: 'b' }); | ||
| 33 | + throws(() => require(vfsFile), { code: 'MODULE_NOT_FOUND' }); | ||
| 34 | + | ||
| 35 | + common.expectWarning( | ||
| 36 | + 'ExperimentalWarning', | ||
| 37 | + 'Module._stat is an experimental feature. This feature could change at any time'); | ||
| 38 | + | ||
| 39 | + process.on('warning', common.mustCall()); | ||
| 40 | + | ||
| 41 | + const originalStat = Module._stat; | ||
| 42 | + Module._stat = function(filename) { | ||
| 43 | + if (!filename.startsWith(process.execPath)) { | ||
| 44 | + return originalStat(filename); | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + if (filename === process.execPath) { | ||
| 48 | + return 1; | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + switch (filename) { | ||
| 52 | + case vfsDirectory: | ||
| 53 | + return 1; | ||
| 54 | + case vfsDoesNotExist: | ||
| 55 | + return -2; | ||
| 56 | + case vfsFile: | ||
| 57 | + return 0; | ||
| 58 | + } | ||
| 59 | + }; | ||
| 60 | + | ||
| 61 | + const originalReadFileSync = fs.readFileSync; | ||
| 62 | + // TODO(aduh95): We'd like to have a better way to achieve this without monkey-patching fs. | ||
| 63 | + fs.readFileSync = function readFileSync(pathArgument, options) { | ||
| 64 | + if (!pathArgument.startsWith(process.execPath)) { | ||
| 65 | + return originalReadFileSync.apply(this, arguments); | ||
| 66 | + } | ||
| 67 | + if (pathArgument === vfsFile) { | ||
| 68 | + return "module.exports = { x: 'y' };"; | ||
| 69 | + } | ||
| 70 | + throw new Error(); | ||
| 71 | + }; | ||
| 72 | + | ||
| 73 | + fs.realpathSync = function realpathSync(pathArgument, options) { | ||
| 74 | + return pathArgument; | ||
| 75 | + }; | ||
| 76 | + | ||
| 77 | + strictEqual(Module._stat(directory), 1); | ||
| 78 | + ok(Module._stat(doesNotExist) < 0); | ||
| 79 | + strictEqual(Module._stat(file), 0); | ||
| 80 | + | ||
| 81 | + strictEqual(Module._stat(vfsDirectory), 1); | ||
| 82 | + ok(Module._stat(vfsDoesNotExist) < 0); | ||
| 83 | + strictEqual(Module._stat(vfsFile), 0); | ||
| 84 | + | ||
| 85 | + strictEqual(Module._stat(process.execPath), 1); | ||
| 86 | + | ||
| 87 | + deepStrictEqual(require(file), { a: 'b' }); | ||
| 88 | + deepStrictEqual(require(vfsFile), { x: 'y' }); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments