source: imaps-frontend/node_modules/is-core-module/test/index.js@ 0c6b92a

main
Last change on this file since 0c6b92a was 0c6b92a, checked in by stefan toskovski <stefantoska84@…>, 5 weeks ago

Pred finalna verzija

  • Property mode set to 100644
File size: 4.4 KB
Line 
1'use strict';
2
3var test = require('tape');
4var keys = require('object-keys');
5var semver = require('semver');
6var mockProperty = require('mock-property');
7
8var isCore = require('../');
9var data = require('../core.json');
10
11var supportsNodePrefix = semver.satisfies(process.versions.node, '^14.18 || >= 16', { includePrerelease: true });
12
13test('core modules', function (t) {
14 t.test('isCore()', function (st) {
15 st.ok(isCore('fs'));
16 st.ok(isCore('net'));
17 st.ok(isCore('http'));
18
19 st.ok(!isCore('seq'));
20 st.ok(!isCore('../'));
21
22 st.ok(!isCore('toString'));
23
24 st.end();
25 });
26
27 t.test('core list', function (st) {
28 var cores = keys(data);
29 st.plan(cores.length);
30
31 for (var i = 0; i < cores.length; ++i) {
32 var mod = cores[i];
33 var requireFunc = function () { require(mod); }; // eslint-disable-line no-loop-func
34 if (isCore(mod)) {
35 st.doesNotThrow(requireFunc, mod + ' supported; requiring does not throw');
36 } else {
37 st['throws'](requireFunc, mod + ' not supported; requiring throws');
38 }
39 }
40
41 st.end();
42 });
43
44 t.test('core via repl module', { skip: !data.repl }, function (st) {
45 var libs = require('repl')._builtinLibs; // eslint-disable-line no-underscore-dangle
46 if (!libs) {
47 st.skip('repl._builtinLibs does not exist');
48 } else {
49 for (var i = 0; i < libs.length; ++i) {
50 var mod = libs[i];
51 st.ok(data[mod], mod + ' is a core module');
52 st.doesNotThrow(
53 function () { require(mod); }, // eslint-disable-line no-loop-func
54 'requiring ' + mod + ' does not throw'
55 );
56 if (mod.slice(0, 5) !== 'node:') {
57 if (supportsNodePrefix) {
58 st.doesNotThrow(
59 function () { require('node:' + mod); }, // eslint-disable-line no-loop-func
60 'requiring node:' + mod + ' does not throw'
61 );
62 } else {
63 st['throws'](
64 function () { require('node:' + mod); }, // eslint-disable-line no-loop-func
65 'requiring node:' + mod + ' throws'
66 );
67 }
68 }
69 }
70 }
71 st.end();
72 });
73
74 t.test('core via builtinModules list', { skip: !data.module }, function (st) {
75 var Module = require('module');
76 var libs = Module.builtinModules;
77 if (!libs) {
78 st.skip('module.builtinModules does not exist');
79 } else {
80 var excludeList = [
81 '_debug_agent',
82 'v8/tools/tickprocessor-driver',
83 'v8/tools/SourceMap',
84 'v8/tools/tickprocessor',
85 'v8/tools/profile'
86 ];
87
88 // see https://github.com/nodejs/node/issues/42785
89 if (semver.satisfies(process.version, '>= 18')) {
90 libs = libs.concat('node:test');
91 }
92 if (semver.satisfies(process.version, '^20.12 || >= 21.7')) {
93 libs = libs.concat('node:sea');
94 }
95
96 for (var i = 0; i < libs.length; ++i) {
97 var mod = libs[i];
98 if (excludeList.indexOf(mod) === -1) {
99 st.ok(data[mod], mod + ' is a core module');
100
101 if (Module.isBuiltin) {
102 st.ok(Module.isBuiltin(mod), 'module.isBuiltin(' + mod + ') is true');
103 }
104
105 st.doesNotThrow(
106 function () { require(mod); }, // eslint-disable-line no-loop-func
107 'requiring ' + mod + ' does not throw'
108 );
109
110 if (process.getBuiltinModule) {
111 st.equal(
112 process.getBuiltinModule(mod),
113 require(mod),
114 'process.getBuiltinModule(' + mod + ') === require(' + mod + ')'
115 );
116 }
117
118 if (mod.slice(0, 5) !== 'node:') {
119 if (supportsNodePrefix) {
120 st.doesNotThrow(
121 function () { require('node:' + mod); }, // eslint-disable-line no-loop-func
122 'requiring node:' + mod + ' does not throw'
123 );
124 } else {
125 st['throws'](
126 function () { require('node:' + mod); }, // eslint-disable-line no-loop-func
127 'requiring node:' + mod + ' throws'
128 );
129 }
130 }
131 }
132 }
133 }
134
135 st.end();
136 });
137
138 t.test('Object.prototype pollution', function (st) {
139 var nonKey = 'not a core module';
140 st.teardown(mockProperty(Object.prototype, 'fs', { value: false }));
141 st.teardown(mockProperty(Object.prototype, 'path', { value: '>= 999999999' }));
142 st.teardown(mockProperty(Object.prototype, 'http', { value: data.http }));
143 st.teardown(mockProperty(Object.prototype, nonKey, { value: true }));
144
145 st.equal(isCore('fs'), true, 'fs is a core module even if Object.prototype lies');
146 st.equal(isCore('path'), true, 'path is a core module even if Object.prototype lies');
147 st.equal(isCore('http'), true, 'path is a core module even if Object.prototype matches data');
148 st.equal(isCore(nonKey), false, '"' + nonKey + '" is not a core module even if Object.prototype lies');
149
150 st.end();
151 });
152
153 t.end();
154});
Note: See TracBrowser for help on using the repository browser.