-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Module._stat landed in #44537 without a test, so this change adds one. Signed-off-by: Darshan Sen <raisinten@gmail.com>
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'use strict'; | ||
require('../common'); | ||
|
||
// This tests Module._stat. | ||
|
||
const Module = require('module'); | ||
const path = require('path'); | ||
const tmpdir = require('../common/tmpdir'); | ||
const { mkdirSync, writeFileSync } = require('fs'); | ||
const { strictEqual } = require('assert'); | ||
|
||
const directory = path.join(tmpdir.path, 'directory'); | ||
const doesNotExist = path.join(tmpdir.path, 'does-not-exist'); | ||
const file = path.join(tmpdir.path, 'file'); | ||
|
||
tmpdir.refresh(); | ||
writeFileSync(file, ''); | ||
mkdirSync(directory); | ||
|
||
strictEqual(Module._stat(directory), 1); | ||
strictEqual(Module._stat(doesNotExist), -2); | ||
strictEqual(Module._stat(file), 0); | ||
|
||
const vfsDirectory = path.join(process.execPath, 'directory'); | ||
const vfsDoesNotExist = path.join(process.execPath, 'does-not-exist'); | ||
const vfsFile = path.join(process.execPath, 'file'); | ||
|
||
strictEqual(Module._stat(vfsDirectory), -20); | ||
strictEqual(Module._stat(vfsDoesNotExist), -20); | ||
strictEqual(Module._stat(vfsFile), -20); | ||
|
||
const originalStat = Module._stat; | ||
Module._stat = function(filename) { | ||
if (!filename.startsWith(process.execPath)) { | ||
return originalStat(filename); | ||
} | ||
|
||
switch (filename) { | ||
case vfsDirectory: | ||
return 1; | ||
case vfsDoesNotExist: | ||
return -2; | ||
case vfsFile: | ||
return 0; | ||
} | ||
}; | ||
|
||
strictEqual(Module._stat(directory), 1); | ||
strictEqual(Module._stat(doesNotExist), -2); | ||
strictEqual(Module._stat(file), 0); | ||
|
||
strictEqual(Module._stat(vfsDirectory), 1); | ||
strictEqual(Module._stat(vfsDoesNotExist), -2); | ||
strictEqual(Module._stat(vfsFile), 0); |