|
| 1 | +'use strict'; |
| 2 | +require('../common'); |
| 3 | + |
| 4 | +// This tests Module._stat. |
| 5 | + |
| 6 | +const Module = require('module'); |
| 7 | +const path = require('path'); |
| 8 | +const tmpdir = require('../common/tmpdir'); |
| 9 | +const { mkdirSync, writeFileSync } = require('fs'); |
| 10 | +const { strictEqual } = require('assert'); |
| 11 | + |
| 12 | +const directory = path.join(tmpdir.path, 'directory'); |
| 13 | +const doesNotExist = path.join(tmpdir.path, 'does-not-exist'); |
| 14 | +const file = path.join(tmpdir.path, 'file'); |
| 15 | + |
| 16 | +tmpdir.refresh(); |
| 17 | +writeFileSync(file, ''); |
| 18 | +mkdirSync(directory); |
| 19 | + |
| 20 | +strictEqual(Module._stat(directory), 1); |
| 21 | +strictEqual(Module._stat(doesNotExist), -2); |
| 22 | +strictEqual(Module._stat(file), 0); |
| 23 | + |
| 24 | +const vfsDirectory = path.join(process.execPath, 'directory'); |
| 25 | +const vfsDoesNotExist = path.join(process.execPath, 'does-not-exist'); |
| 26 | +const vfsFile = path.join(process.execPath, 'file'); |
| 27 | + |
| 28 | +strictEqual(Module._stat(vfsDirectory), -20); |
| 29 | +strictEqual(Module._stat(vfsDoesNotExist), -20); |
| 30 | +strictEqual(Module._stat(vfsFile), -20); |
| 31 | + |
| 32 | +const originalStat = Module._stat; |
| 33 | +Module._stat = function(filename) { |
| 34 | + if (!filename.startsWith(process.execPath)) { |
| 35 | + return originalStat(filename); |
| 36 | + } |
| 37 | + |
| 38 | + switch (filename) { |
| 39 | + case vfsDirectory: |
| 40 | + return 1; |
| 41 | + case vfsDoesNotExist: |
| 42 | + return -2; |
| 43 | + case vfsFile: |
| 44 | + return 0; |
| 45 | + } |
| 46 | +}; |
| 47 | + |
| 48 | +strictEqual(Module._stat(directory), 1); |
| 49 | +strictEqual(Module._stat(doesNotExist), -2); |
| 50 | +strictEqual(Module._stat(file), 0); |
| 51 | + |
| 52 | +strictEqual(Module._stat(vfsDirectory), 1); |
| 53 | +strictEqual(Module._stat(vfsDoesNotExist), -2); |
| 54 | +strictEqual(Module._stat(vfsFile), 0); |
0 commit comments