|
| 1 | +import { join } from 'path'; |
| 2 | +import { NxlsWrapper } from '../nxls-wrapper'; |
| 3 | +import { |
| 4 | + defaultVersion, |
| 5 | + e2eCwd, |
| 6 | + newWorkspace, |
| 7 | + uniq, |
| 8 | +} from '@nx-console/shared-e2e-utils'; |
| 9 | +import { |
| 10 | + NxGeneratorOptionsRequest, |
| 11 | + NxGeneratorOptionsRequestOptions, |
| 12 | +} from '@nx-console/language-server-types'; |
| 13 | + |
| 14 | +let nxlsWrapper: NxlsWrapper; |
| 15 | +const workspaceName = uniq('workspace'); |
| 16 | + |
| 17 | +describe('generator options - Windows file URI handling', () => { |
| 18 | + beforeAll(async () => { |
| 19 | + // Create a new workspace with basic React setup |
| 20 | + newWorkspace({ |
| 21 | + name: workspaceName, |
| 22 | + options: { |
| 23 | + preset: 'react-monorepo', |
| 24 | + bundler: 'vite', |
| 25 | + e2eTestRunner: 'cypress', |
| 26 | + style: 'css', |
| 27 | + }, |
| 28 | + version: defaultVersion, |
| 29 | + }); |
| 30 | + |
| 31 | + // Start the language server |
| 32 | + nxlsWrapper = new NxlsWrapper(); |
| 33 | + await nxlsWrapper.startNxls(join(e2eCwd, workspaceName)); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should handle Windows file URIs with file:// prefix correctly', async () => { |
| 37 | + const schemaPath = join( |
| 38 | + e2eCwd, |
| 39 | + workspaceName, |
| 40 | + 'node_modules', |
| 41 | + '@nx/js/src/generators/library/schema.json', |
| 42 | + ); |
| 43 | + |
| 44 | + // Test with Windows-style file URI (simulating what happens on Windows) |
| 45 | + const windowsFileUri = `file:///${schemaPath.replace(/\\/g, '/')}`; |
| 46 | + |
| 47 | + const generatorOptions = await nxlsWrapper.sendRequest({ |
| 48 | + ...NxGeneratorOptionsRequest, |
| 49 | + params: { |
| 50 | + options: { |
| 51 | + collection: '@nx/js', |
| 52 | + name: 'library', |
| 53 | + path: windowsFileUri, |
| 54 | + } satisfies NxGeneratorOptionsRequestOptions, |
| 55 | + }, |
| 56 | + }); |
| 57 | + |
| 58 | + // Verify the result is successful and contains expected options |
| 59 | + expect(generatorOptions.result).toBeDefined(); |
| 60 | + expect(generatorOptions.error).toBeUndefined(); |
| 61 | + |
| 62 | + const options = generatorOptions.result as any[]; |
| 63 | + expect(options.length).toBeGreaterThan(0); |
| 64 | + |
| 65 | + // Check for some expected options to ensure the schema was loaded correctly |
| 66 | + const optionNames = options.map((o) => o.name); |
| 67 | + expect(optionNames).toContain('name'); |
| 68 | + expect(optionNames).toContain('directory'); |
| 69 | + expect(optionNames).toContain('bundler'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should handle Windows file URIs with /c:/ pattern', async () => { |
| 73 | + // Only run this test on Windows or in CI |
| 74 | + if (process.platform !== 'win32' && !process.env.CI) { |
| 75 | + console.log( |
| 76 | + 'Skipping Windows-specific path test on non-Windows platform', |
| 77 | + ); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + const schemaPath = join( |
| 82 | + e2eCwd, |
| 83 | + workspaceName, |
| 84 | + 'node_modules', |
| 85 | + '@nx/js/src/generators/library/schema.json', |
| 86 | + ); |
| 87 | + |
| 88 | + // Create a Windows-style path with /c:/ pattern |
| 89 | + // This simulates the exact issue that was fixed |
| 90 | + const windowsDriveLetter = schemaPath.match(/^([a-zA-Z]):/)?.[1] || 'c'; |
| 91 | + const pathWithoutDrive = schemaPath.replace(/^[a-zA-Z]:/, ''); |
| 92 | + const problematicWindowsUri = `file:///${windowsDriveLetter}:${pathWithoutDrive.replace(/\\/g, '/')}`; |
| 93 | + |
| 94 | + const generatorOptions = await nxlsWrapper.sendRequest({ |
| 95 | + ...NxGeneratorOptionsRequest, |
| 96 | + params: { |
| 97 | + options: { |
| 98 | + collection: '@nx/js', |
| 99 | + name: 'library', |
| 100 | + path: problematicWindowsUri, |
| 101 | + } satisfies NxGeneratorOptionsRequestOptions, |
| 102 | + }, |
| 103 | + }); |
| 104 | + |
| 105 | + // The fix should handle this correctly |
| 106 | + expect(generatorOptions.result).toBeDefined(); |
| 107 | + expect(generatorOptions.error).toBeUndefined(); |
| 108 | + |
| 109 | + const options = generatorOptions.result as any[]; |
| 110 | + expect(options.length).toBeGreaterThan(0); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should handle regular file paths without file:// prefix', async () => { |
| 114 | + const schemaPath = join( |
| 115 | + e2eCwd, |
| 116 | + workspaceName, |
| 117 | + 'node_modules', |
| 118 | + '@nx/js/src/generators/library/schema.json', |
| 119 | + ); |
| 120 | + |
| 121 | + const generatorOptions = await nxlsWrapper.sendRequest({ |
| 122 | + ...NxGeneratorOptionsRequest, |
| 123 | + params: { |
| 124 | + options: { |
| 125 | + collection: '@nx/js', |
| 126 | + name: 'library', |
| 127 | + path: schemaPath, |
| 128 | + } satisfies NxGeneratorOptionsRequestOptions, |
| 129 | + }, |
| 130 | + }); |
| 131 | + |
| 132 | + // Should work with regular paths too |
| 133 | + expect(generatorOptions.result).toBeDefined(); |
| 134 | + expect(generatorOptions.error).toBeUndefined(); |
| 135 | + |
| 136 | + const options = generatorOptions.result as any[]; |
| 137 | + expect(options.length).toBeGreaterThan(0); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should return error for non-existent schema file with file:// URI', async () => { |
| 141 | + const nonExistentPath = join( |
| 142 | + e2eCwd, |
| 143 | + workspaceName, |
| 144 | + 'node_modules', |
| 145 | + '@nx/js/src/generators/non-existent/schema.json', |
| 146 | + ); |
| 147 | + |
| 148 | + const windowsFileUri = `file:///${nonExistentPath.replace(/\\/g, '/')}`; |
| 149 | + |
| 150 | + const generatorOptions = await nxlsWrapper.sendRequest({ |
| 151 | + ...NxGeneratorOptionsRequest, |
| 152 | + params: { |
| 153 | + options: { |
| 154 | + collection: '@nx/js', |
| 155 | + name: 'non-existent', |
| 156 | + path: windowsFileUri, |
| 157 | + } satisfies NxGeneratorOptionsRequestOptions, |
| 158 | + }, |
| 159 | + }); |
| 160 | + |
| 161 | + // Should handle errors gracefully |
| 162 | + expect(generatorOptions.error).toBeDefined(); |
| 163 | + }); |
| 164 | + |
| 165 | + afterAll(async () => { |
| 166 | + return await nxlsWrapper.stopNxls(); |
| 167 | + }); |
| 168 | +}); |
0 commit comments