xref: /third_party/node/test/parallel/test-vfs.js (revision 1cb0ef41)
11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci// This tests the creation of a vfs by monkey-patching fs and Module._stat.
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst Module = require('module');
71cb0ef41Sopenharmony_ciconst fs = require('fs');
81cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
91cb0ef41Sopenharmony_ciconst { deepStrictEqual, ok, strictEqual, throws } = require('assert');
101cb0ef41Sopenharmony_ciconst { join } = require('path');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst directory = join(tmpdir.path, 'directory');
131cb0ef41Sopenharmony_ciconst doesNotExist = join(tmpdir.path, 'does-not-exist');
141cb0ef41Sopenharmony_ciconst file = join(tmpdir.path, 'file.js');
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_citmpdir.refresh();
171cb0ef41Sopenharmony_cifs.writeFileSync(file, "module.exports = { a: 'b' }");
181cb0ef41Sopenharmony_cifs.mkdirSync(directory);
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_cistrictEqual(Module._stat(directory), 1);
211cb0ef41Sopenharmony_ciok(Module._stat(doesNotExist) < 0);
221cb0ef41Sopenharmony_cistrictEqual(Module._stat(file), 0);
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ciconst vfsDirectory = join(process.execPath, 'directory');
251cb0ef41Sopenharmony_ciconst vfsDoesNotExist = join(process.execPath, 'does-not-exist');
261cb0ef41Sopenharmony_ciconst vfsFile = join(process.execPath, 'file.js');
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ciok(Module._stat(vfsDirectory) < 0);
291cb0ef41Sopenharmony_ciok(Module._stat(vfsDoesNotExist) < 0);
301cb0ef41Sopenharmony_ciok(Module._stat(vfsFile) < 0);
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_cideepStrictEqual(require(file), { a: 'b' });
331cb0ef41Sopenharmony_cithrows(() => require(vfsFile), { code: 'MODULE_NOT_FOUND' });
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_cicommon.expectWarning(
361cb0ef41Sopenharmony_ci  'ExperimentalWarning',
371cb0ef41Sopenharmony_ci  'Module._stat is an experimental feature and might change at any time'
381cb0ef41Sopenharmony_ci);
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ciprocess.on('warning', common.mustCall());
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ciconst originalStat = Module._stat;
431cb0ef41Sopenharmony_ciModule._stat = function(filename) {
441cb0ef41Sopenharmony_ci  if (!filename.startsWith(process.execPath)) {
451cb0ef41Sopenharmony_ci    return originalStat(filename);
461cb0ef41Sopenharmony_ci  }
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  if (filename === process.execPath) {
491cb0ef41Sopenharmony_ci    return 1;
501cb0ef41Sopenharmony_ci  }
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  switch (filename) {
531cb0ef41Sopenharmony_ci    case vfsDirectory:
541cb0ef41Sopenharmony_ci      return 1;
551cb0ef41Sopenharmony_ci    case vfsDoesNotExist:
561cb0ef41Sopenharmony_ci      return -2;
571cb0ef41Sopenharmony_ci    case vfsFile:
581cb0ef41Sopenharmony_ci      return 0;
591cb0ef41Sopenharmony_ci  }
601cb0ef41Sopenharmony_ci};
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ciconst originalReadFileSync = fs.readFileSync;
631cb0ef41Sopenharmony_ci// TODO(aduh95): We'd like to have a better way to achieve this without monkey-patching fs.
641cb0ef41Sopenharmony_cifs.readFileSync = function readFileSync(pathArgument, options) {
651cb0ef41Sopenharmony_ci  if (!pathArgument.startsWith(process.execPath)) {
661cb0ef41Sopenharmony_ci    return originalReadFileSync.apply(this, arguments);
671cb0ef41Sopenharmony_ci  }
681cb0ef41Sopenharmony_ci  if (pathArgument === vfsFile) {
691cb0ef41Sopenharmony_ci    return "module.exports = { x: 'y' };";
701cb0ef41Sopenharmony_ci  }
711cb0ef41Sopenharmony_ci  throw new Error();
721cb0ef41Sopenharmony_ci};
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_cifs.realpathSync = function realpathSync(pathArgument, options) {
751cb0ef41Sopenharmony_ci  return pathArgument;
761cb0ef41Sopenharmony_ci};
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_cistrictEqual(Module._stat(directory), 1);
791cb0ef41Sopenharmony_ciok(Module._stat(doesNotExist) < 0);
801cb0ef41Sopenharmony_cistrictEqual(Module._stat(file), 0);
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_cistrictEqual(Module._stat(vfsDirectory), 1);
831cb0ef41Sopenharmony_ciok(Module._stat(vfsDoesNotExist) < 0);
841cb0ef41Sopenharmony_cistrictEqual(Module._stat(vfsFile), 0);
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_cistrictEqual(Module._stat(process.execPath), 1);
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_cideepStrictEqual(require(file), { a: 'b' });
891cb0ef41Sopenharmony_cideepStrictEqual(require(vfsFile), { x: 'y' });
90