|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Flags: --expose-internals |
| 4 | + |
| 5 | +constcommon=require('../common'); |
| 6 | +if(!common.isWindows){ |
| 7 | +common.skip('this test is Windows-specific.'); |
| 8 | +} |
| 9 | + |
| 10 | +constfs=require('fs'); |
| 11 | +constpath=require('path'); |
| 12 | +consttmpdir=require('../common/tmpdir'); |
| 13 | +const{ describe, it }=require('node:test'); |
| 14 | +const{ legacyMainResolve }=require('node:internal/modules/esm/resolve'); |
| 15 | +const{ pathToFileURL }=require('node:url'); |
| 16 | +const{ spawnPromisified }=require('../common'); |
| 17 | +constassert=require('assert'); |
| 18 | +const{ execPath }=require('node:process'); |
| 19 | + |
| 20 | +describe('long path on Windows',()=>{ |
| 21 | +it('check long path in ReadPackageJSON',()=>{ |
| 22 | +// Module layout will be the following: |
| 23 | +// package.json |
| 24 | +// main |
| 25 | +// main.js |
| 26 | +constpackageDirNameLen=Math.max(260-tmpdir.path.length,1); |
| 27 | +constpackageDirName=tmpdir.resolve('x'.repeat(packageDirNameLen)); |
| 28 | +constpackageDirPath=path.resolve(packageDirName); |
| 29 | +constpackageJsonFilePath=path.join(packageDirPath,'package.json'); |
| 30 | +constmainDirName='main'; |
| 31 | +constmainDirPath=path.resolve(packageDirPath,mainDirName); |
| 32 | +constmainJsFile='main.js'; |
| 33 | +constmainJsFilePath=path.resolve(mainDirPath,mainJsFile); |
| 34 | + |
| 35 | +tmpdir.refresh(); |
| 36 | + |
| 37 | +fs.mkdirSync(packageDirPath); |
| 38 | +fs.writeFileSync( |
| 39 | +packageJsonFilePath, |
| 40 | +`{"main":"${mainDirName}/${mainJsFile}"}` |
| 41 | +); |
| 42 | +fs.mkdirSync(mainDirPath); |
| 43 | +fs.writeFileSync(mainJsFilePath,'console.log("hello world");'); |
| 44 | + |
| 45 | +require('internal/modules/run_main').executeUserEntryPoint(packageDirPath); |
| 46 | + |
| 47 | +tmpdir.refresh(); |
| 48 | +}); |
| 49 | + |
| 50 | +it('check long path in LegacyMainResolve - 1',()=>{ |
| 51 | +// Module layout will be the following: |
| 52 | +// package.json |
| 53 | +// index.js |
| 54 | +constpackageDirNameLen=Math.max(260-tmpdir.path.length,1); |
| 55 | +constpackageDirPath=tmpdir.resolve('y'.repeat(packageDirNameLen)); |
| 56 | +constpackageJSPath=path.join(packageDirPath,'package.json'); |
| 57 | +constindexJSPath=path.join(packageDirPath,'index.js'); |
| 58 | +constpackageConfig={}; |
| 59 | + |
| 60 | +tmpdir.refresh(); |
| 61 | + |
| 62 | +fs.mkdirSync(packageDirPath); |
| 63 | +fs.writeFileSync(packageJSPath,''); |
| 64 | +fs.writeFileSync(indexJSPath,''); |
| 65 | + |
| 66 | +constpackageJsonUrl=pathToFileURL( |
| 67 | +path.resolve(packageJSPath) |
| 68 | +); |
| 69 | + |
| 70 | +console.log(legacyMainResolve(packageJsonUrl,packageConfig,'')); |
| 71 | +}); |
| 72 | + |
| 73 | +it('check long path in LegacyMainResolve - 2',()=>{ |
| 74 | +// Module layout will be the following: |
| 75 | +// package.json |
| 76 | +// main.js |
| 77 | +constpackageDirNameLen=Math.max(260-tmpdir.path.length,1); |
| 78 | +constpackageDirPath=tmpdir.resolve('z'.repeat(packageDirNameLen)); |
| 79 | +constpackageJSPath=path.join(packageDirPath,'package.json'); |
| 80 | +constindexJSPath=path.join(packageDirPath,'main.js'); |
| 81 | +constpackageConfig={main: 'main.js'}; |
| 82 | + |
| 83 | +tmpdir.refresh(); |
| 84 | + |
| 85 | +fs.mkdirSync(packageDirPath); |
| 86 | +fs.writeFileSync(packageJSPath,''); |
| 87 | +fs.writeFileSync(indexJSPath,''); |
| 88 | + |
| 89 | +constpackageJsonUrl=pathToFileURL( |
| 90 | +path.resolve(packageJSPath) |
| 91 | +); |
| 92 | + |
| 93 | +console.log(legacyMainResolve(packageJsonUrl,packageConfig,'')); |
| 94 | +}); |
| 95 | + |
| 96 | +it('check long path in GetNearestParentPackageJSON',async()=>{ |
| 97 | +// Module layout will be the following: |
| 98 | +// node_modules |
| 99 | +// test-module |
| 100 | +// package.json (path is less than 260 chars long) |
| 101 | +// cjs |
| 102 | +// package.json (path is more than 260 chars long) |
| 103 | +// index.js |
| 104 | +consttestModuleDirPath='node_modules/test-module'; |
| 105 | +letcjsDirPath=path.join(testModuleDirPath,'cjs'); |
| 106 | +letmodulePackageJSPath=path.join(testModuleDirPath,'package.json'); |
| 107 | +letcjsPackageJSPath=path.join(cjsDirPath,'package.json'); |
| 108 | +letcjsIndexJSPath=path.join(cjsDirPath,'index.js'); |
| 109 | + |
| 110 | +consttmpDirNameLen=Math.max( |
| 111 | +260-tmpdir.path.length-cjsPackageJSPath.length,1); |
| 112 | +consttmpDirPath=tmpdir.resolve('k'.repeat(tmpDirNameLen)); |
| 113 | + |
| 114 | +cjsDirPath=path.join(tmpDirPath,cjsDirPath); |
| 115 | +modulePackageJSPath=path.join(tmpDirPath,modulePackageJSPath); |
| 116 | +cjsPackageJSPath=path.join(tmpDirPath,cjsPackageJSPath); |
| 117 | +cjsIndexJSPath=path.join(tmpDirPath,cjsIndexJSPath); |
| 118 | + |
| 119 | +tmpdir.refresh(); |
| 120 | + |
| 121 | +fs.mkdirSync(cjsDirPath,{recursive: true}); |
| 122 | +fs.writeFileSync( |
| 123 | +modulePackageJSPath, |
| 124 | +`{ |
| 125 | + "type": "module" |
| 126 | + }` |
| 127 | +); |
| 128 | +fs.writeFileSync( |
| 129 | +cjsPackageJSPath, |
| 130 | +`{ |
| 131 | + "type": "commonjs" |
| 132 | + }` |
| 133 | +); |
| 134 | +fs.writeFileSync( |
| 135 | +cjsIndexJSPath, |
| 136 | +'const fs = require("fs");' |
| 137 | +); |
| 138 | +const{ code, signal, stderr }=awaitspawnPromisified(execPath,[cjsIndexJSPath]); |
| 139 | +assert.strictEqual(stderr.trim(),''); |
| 140 | +assert.strictEqual(code,0); |
| 141 | +assert.strictEqual(signal,null); |
| 142 | +}); |
| 143 | + |
| 144 | +it('check long path in GetNearestParentPackageJSONType',async()=>{ |
| 145 | +// Module layout will be the following: |
| 146 | +// node_modules |
| 147 | +// test-module |
| 148 | +// package.json (path is less than 260 chars long) |
| 149 | +// cjs |
| 150 | +// package.json (path is more than 260 chars long) |
| 151 | +// index.js |
| 152 | +consttestModuleDirPath='node_modules/test-module'; |
| 153 | +letcjsDirPath=path.join(testModuleDirPath,'cjs'); |
| 154 | +letmodulePackageJSPath=path.join(testModuleDirPath,'package.json'); |
| 155 | +letcjsPackageJSPath=path.join(cjsDirPath,'package.json'); |
| 156 | +letcjsIndexJSPath=path.join(cjsDirPath,'index.js'); |
| 157 | + |
| 158 | +consttmpDirNameLen=Math.max(260-tmpdir.path.length-cjsPackageJSPath.length,1); |
| 159 | +consttmpDirPath=tmpdir.resolve('l'.repeat(tmpDirNameLen)); |
| 160 | + |
| 161 | +cjsDirPath=path.join(tmpDirPath,cjsDirPath); |
| 162 | +modulePackageJSPath=path.join(tmpDirPath,modulePackageJSPath); |
| 163 | +cjsPackageJSPath=path.join(tmpDirPath,cjsPackageJSPath); |
| 164 | +cjsIndexJSPath=path.join(tmpDirPath,cjsIndexJSPath); |
| 165 | + |
| 166 | +tmpdir.refresh(); |
| 167 | + |
| 168 | +fs.mkdirSync(cjsDirPath,{recursive: true}); |
| 169 | +fs.writeFileSync( |
| 170 | +modulePackageJSPath, |
| 171 | +`{ |
| 172 | + "type": "module" |
| 173 | + }` |
| 174 | +); |
| 175 | +fs.writeFileSync( |
| 176 | +cjsPackageJSPath, |
| 177 | +`{ |
| 178 | + "type": "commonjs" |
| 179 | + }` |
| 180 | +); |
| 181 | + |
| 182 | +fs.writeFileSync(cjsIndexJSPath,'import fs from "node:fs/promises";'); |
| 183 | +const{ code, signal, stderr }=awaitspawnPromisified(execPath,[cjsIndexJSPath]); |
| 184 | + |
| 185 | +assert.ok(stderr.includes('Warning: To load an ES module')); |
| 186 | +assert.strictEqual(code,1); |
| 187 | +assert.strictEqual(signal,null); |
| 188 | +}); |
| 189 | +}); |
0 commit comments