|
| 1 | +'use strict'; |
| 2 | +constcommon=require('../common'); |
| 3 | + |
| 4 | +// This tests the creation of a vfs by monkey-patching fs and Module._stat. |
| 5 | + |
| 6 | +constModule=require('module'); |
| 7 | +constfs=require('fs'); |
| 8 | +consttmpdir=require('../common/tmpdir'); |
| 9 | +const{ deepStrictEqual, ok, strictEqual, throws }=require('assert'); |
| 10 | +const{ join }=require('path'); |
| 11 | + |
| 12 | +constdirectory=join(tmpdir.path,'directory'); |
| 13 | +constdoesNotExist=join(tmpdir.path,'does-not-exist'); |
| 14 | +constfile=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 | +constvfsDirectory=join(process.execPath,'directory'); |
| 25 | +constvfsDoesNotExist=join(process.execPath,'does-not-exist'); |
| 26 | +constvfsFile=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 | +constoriginalStat=Module._stat; |
| 42 | +Module._stat=function(filename){ |
| 43 | +if(!filename.startsWith(process.execPath)){ |
| 44 | +returnoriginalStat(filename); |
| 45 | +} |
| 46 | + |
| 47 | +if(filename===process.execPath){ |
| 48 | +return1; |
| 49 | +} |
| 50 | + |
| 51 | +switch(filename){ |
| 52 | +casevfsDirectory: |
| 53 | +return1; |
| 54 | +casevfsDoesNotExist: |
| 55 | +return-2; |
| 56 | +casevfsFile: |
| 57 | +return0; |
| 58 | +} |
| 59 | +}; |
| 60 | + |
| 61 | +constoriginalReadFileSync=fs.readFileSync; |
| 62 | +// TODO(aduh95): We'd like to have a better way to achieve this without monkey-patching fs. |
| 63 | +fs.readFileSync=functionreadFileSync(pathArgument,options){ |
| 64 | +if(!pathArgument.startsWith(process.execPath)){ |
| 65 | +returnoriginalReadFileSync.apply(this,arguments); |
| 66 | +} |
| 67 | +if(pathArgument===vfsFile){ |
| 68 | +return"module.exports = { x: 'y' };"; |
| 69 | +} |
| 70 | +thrownewError(); |
| 71 | +}; |
| 72 | + |
| 73 | +fs.realpathSync=functionrealpathSync(pathArgument,options){ |
| 74 | +returnpathArgument; |
| 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'}); |
0 commit comments