|
| 1 | +import * as assert from 'assert'; |
| 2 | +import * as path from 'path'; |
| 3 | +import * as fs from 'fs'; |
| 4 | +import { getBinaryPath, BinaryResolverDeps } from '../../utils/binaryResolver'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Unit tests for getBinaryPath() binary resolution logic. |
| 8 | + * |
| 9 | + * The function resolves the gosqlx executable via a three-step fallback: |
| 10 | + * 1. User-configured explicit path (gosqlx.executablePath) |
| 11 | + * 2. Bundled binary at <extensionPath>/bin/gosqlx[.exe] |
| 12 | + * 3. PATH lookup ("gosqlx") |
| 13 | + */ |
| 14 | + |
| 15 | +// --------------------------------------------------------------------------- |
| 16 | +// Helpers |
| 17 | +// --------------------------------------------------------------------------- |
| 18 | + |
| 19 | +/** Builds a BinaryResolverDeps with sensible defaults that can be overridden. */ |
| 20 | +function makeDeps(overrides: Partial<BinaryResolverDeps> = {}): BinaryResolverDeps { |
| 21 | + return { |
| 22 | + extensionPath: '/mock/extension', |
| 23 | + platform: 'linux', |
| 24 | + getConfig: (_key: string, defaultValue: string) => defaultValue, |
| 25 | + checkAccess: () => Promise.reject(new Error('ENOENT')), |
| 26 | + ...overrides, |
| 27 | + }; |
| 28 | +} |
| 29 | + |
| 30 | +// --------------------------------------------------------------------------- |
| 31 | +// 1. User-configured path |
| 32 | +// --------------------------------------------------------------------------- |
| 33 | + |
| 34 | +suite('getBinaryPath — user-configured path', () => { |
| 35 | + |
| 36 | + test('returns user-configured path when setting is non-empty', async () => { |
| 37 | + const deps = makeDeps({ |
| 38 | + getConfig: (key: string, defaultValue: string) => |
| 39 | + key === 'executablePath' ? '/usr/local/bin/gosqlx-custom' : defaultValue, |
| 40 | + }); |
| 41 | + const result = await getBinaryPath(deps); |
| 42 | + assert.strictEqual(result, '/usr/local/bin/gosqlx-custom'); |
| 43 | + }); |
| 44 | + |
| 45 | + test('skips user path when setting is empty string', async () => { |
| 46 | + const deps = makeDeps({ |
| 47 | + getConfig: (key: string, defaultValue: string) => |
| 48 | + key === 'executablePath' ? '' : defaultValue, |
| 49 | + }); |
| 50 | + // With no bundled binary and empty user path, should fall back to 'gosqlx' |
| 51 | + const result = await getBinaryPath(deps); |
| 52 | + assert.strictEqual(result, 'gosqlx'); |
| 53 | + }); |
| 54 | + |
| 55 | + test('returns user path even when bundled binary exists', async () => { |
| 56 | + const deps = makeDeps({ |
| 57 | + getConfig: (key: string, defaultValue: string) => |
| 58 | + key === 'executablePath' ? '/custom/gosqlx' : defaultValue, |
| 59 | + // Bundled binary would succeed, but user path takes priority |
| 60 | + checkAccess: () => Promise.resolve(), |
| 61 | + }); |
| 62 | + const result = await getBinaryPath(deps); |
| 63 | + assert.strictEqual(result, '/custom/gosqlx'); |
| 64 | + }); |
| 65 | +}); |
| 66 | + |
| 67 | +// --------------------------------------------------------------------------- |
| 68 | +// 2. Bundled binary detection |
| 69 | +// --------------------------------------------------------------------------- |
| 70 | + |
| 71 | +suite('getBinaryPath — bundled binary', () => { |
| 72 | + |
| 73 | + test('returns bundled binary path when it exists and is executable', async () => { |
| 74 | + const deps = makeDeps({ |
| 75 | + extensionPath: '/home/user/.vscode/extensions/gosqlx-1.0.0', |
| 76 | + platform: 'linux', |
| 77 | + checkAccess: () => Promise.resolve(), |
| 78 | + }); |
| 79 | + const result = await getBinaryPath(deps); |
| 80 | + assert.strictEqual( |
| 81 | + result, |
| 82 | + path.join('/home/user/.vscode/extensions/gosqlx-1.0.0', 'bin', 'gosqlx') |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + test('falls back to PATH when bundled binary does not exist', async () => { |
| 87 | + const deps = makeDeps({ |
| 88 | + extensionPath: '/ext', |
| 89 | + checkAccess: () => Promise.reject(new Error('ENOENT')), |
| 90 | + }); |
| 91 | + const result = await getBinaryPath(deps); |
| 92 | + assert.strictEqual(result, 'gosqlx'); |
| 93 | + }); |
| 94 | + |
| 95 | + test('falls back to PATH when extensionPath is undefined', async () => { |
| 96 | + const deps = makeDeps({ |
| 97 | + extensionPath: undefined, |
| 98 | + checkAccess: () => Promise.resolve(), // would succeed, but path is undefined |
| 99 | + }); |
| 100 | + const result = await getBinaryPath(deps); |
| 101 | + assert.strictEqual(result, 'gosqlx'); |
| 102 | + }); |
| 103 | + |
| 104 | + test('checks correct bundled path using extensionPath + bin + binaryName', async () => { |
| 105 | + let checkedPath = ''; |
| 106 | + const deps = makeDeps({ |
| 107 | + extensionPath: '/my/ext', |
| 108 | + platform: 'darwin', |
| 109 | + checkAccess: (filePath: string) => { |
| 110 | + checkedPath = filePath; |
| 111 | + return Promise.resolve(); |
| 112 | + }, |
| 113 | + }); |
| 114 | + await getBinaryPath(deps); |
| 115 | + assert.strictEqual(checkedPath, path.join('/my/ext', 'bin', 'gosqlx')); |
| 116 | + }); |
| 117 | +}); |
| 118 | + |
| 119 | +// --------------------------------------------------------------------------- |
| 120 | +// 3. PATH fallback |
| 121 | +// --------------------------------------------------------------------------- |
| 122 | + |
| 123 | +suite('getBinaryPath — PATH fallback', () => { |
| 124 | + |
| 125 | + test('returns "gosqlx" when no user path and no bundled binary', async () => { |
| 126 | + const deps = makeDeps({ |
| 127 | + extensionPath: '/ext', |
| 128 | + getConfig: () => '', |
| 129 | + checkAccess: () => Promise.reject(new Error('ENOENT')), |
| 130 | + }); |
| 131 | + const result = await getBinaryPath(deps); |
| 132 | + assert.strictEqual(result, 'gosqlx'); |
| 133 | + }); |
| 134 | + |
| 135 | + test('returns "gosqlx" when extensionPath is undefined and no user path', async () => { |
| 136 | + const deps = makeDeps({ |
| 137 | + extensionPath: undefined, |
| 138 | + getConfig: () => '', |
| 139 | + }); |
| 140 | + const result = await getBinaryPath(deps); |
| 141 | + assert.strictEqual(result, 'gosqlx'); |
| 142 | + }); |
| 143 | +}); |
| 144 | + |
| 145 | +// --------------------------------------------------------------------------- |
| 146 | +// 4. Windows-specific behavior |
| 147 | +// --------------------------------------------------------------------------- |
| 148 | + |
| 149 | +suite('getBinaryPath — Windows platform handling', () => { |
| 150 | + |
| 151 | + test('appends .exe on win32 platform', async () => { |
| 152 | + let checkedPath = ''; |
| 153 | + const deps = makeDeps({ |
| 154 | + extensionPath: '/ext', |
| 155 | + platform: 'win32', |
| 156 | + checkAccess: (filePath: string) => { |
| 157 | + checkedPath = filePath; |
| 158 | + return Promise.resolve(); |
| 159 | + }, |
| 160 | + }); |
| 161 | + const result = await getBinaryPath(deps); |
| 162 | + assert.ok( |
| 163 | + checkedPath.endsWith('gosqlx.exe'), |
| 164 | + `Expected path to end with gosqlx.exe, got: ${checkedPath}` |
| 165 | + ); |
| 166 | + assert.strictEqual(result, path.join('/ext', 'bin', 'gosqlx.exe')); |
| 167 | + }); |
| 168 | + |
| 169 | + test('does not append .exe on non-win32 platform', async () => { |
| 170 | + let checkedPath = ''; |
| 171 | + const deps = makeDeps({ |
| 172 | + extensionPath: '/ext', |
| 173 | + platform: 'darwin', |
| 174 | + checkAccess: (filePath: string) => { |
| 175 | + checkedPath = filePath; |
| 176 | + return Promise.resolve(); |
| 177 | + }, |
| 178 | + }); |
| 179 | + await getBinaryPath(deps); |
| 180 | + assert.ok( |
| 181 | + checkedPath.endsWith('gosqlx') && !checkedPath.endsWith('gosqlx.exe'), |
| 182 | + `Expected path to end with gosqlx (no .exe), got: ${checkedPath}` |
| 183 | + ); |
| 184 | + }); |
| 185 | + |
| 186 | + test('uses F_OK access mode on Windows', async () => { |
| 187 | + let usedMode: number | undefined; |
| 188 | + const deps = makeDeps({ |
| 189 | + extensionPath: '/ext', |
| 190 | + platform: 'win32', |
| 191 | + checkAccess: (_filePath: string, mode: number) => { |
| 192 | + usedMode = mode; |
| 193 | + return Promise.resolve(); |
| 194 | + }, |
| 195 | + }); |
| 196 | + await getBinaryPath(deps); |
| 197 | + assert.strictEqual(usedMode, fs.constants.F_OK); |
| 198 | + }); |
| 199 | + |
| 200 | + test('uses X_OK access mode on non-Windows platforms', async () => { |
| 201 | + let usedMode: number | undefined; |
| 202 | + const deps = makeDeps({ |
| 203 | + extensionPath: '/ext', |
| 204 | + platform: 'linux', |
| 205 | + checkAccess: (_filePath: string, mode: number) => { |
| 206 | + usedMode = mode; |
| 207 | + return Promise.resolve(); |
| 208 | + }, |
| 209 | + }); |
| 210 | + await getBinaryPath(deps); |
| 211 | + assert.strictEqual(usedMode, fs.constants.X_OK); |
| 212 | + }); |
| 213 | + |
| 214 | + test('uses X_OK access mode on macOS (darwin)', async () => { |
| 215 | + let usedMode: number | undefined; |
| 216 | + const deps = makeDeps({ |
| 217 | + extensionPath: '/ext', |
| 218 | + platform: 'darwin', |
| 219 | + checkAccess: (_filePath: string, mode: number) => { |
| 220 | + usedMode = mode; |
| 221 | + return Promise.resolve(); |
| 222 | + }, |
| 223 | + }); |
| 224 | + await getBinaryPath(deps); |
| 225 | + assert.strictEqual(usedMode, fs.constants.X_OK); |
| 226 | + }); |
| 227 | +}); |
| 228 | + |
| 229 | +// --------------------------------------------------------------------------- |
| 230 | +// 5. Priority / precedence |
| 231 | +// --------------------------------------------------------------------------- |
| 232 | + |
| 233 | +suite('getBinaryPath — fallback chain precedence', () => { |
| 234 | + |
| 235 | + test('user setting > bundled binary > PATH (full chain)', async () => { |
| 236 | + // All three options available — user setting wins |
| 237 | + const deps = makeDeps({ |
| 238 | + extensionPath: '/ext', |
| 239 | + getConfig: (key: string, defaultValue: string) => |
| 240 | + key === 'executablePath' ? '/user/bin/gosqlx' : defaultValue, |
| 241 | + checkAccess: () => Promise.resolve(), |
| 242 | + }); |
| 243 | + const result = await getBinaryPath(deps); |
| 244 | + assert.strictEqual(result, '/user/bin/gosqlx'); |
| 245 | + }); |
| 246 | + |
| 247 | + test('bundled binary > PATH when user setting is empty', async () => { |
| 248 | + const deps = makeDeps({ |
| 249 | + extensionPath: '/ext', |
| 250 | + getConfig: () => '', |
| 251 | + checkAccess: () => Promise.resolve(), |
| 252 | + }); |
| 253 | + const result = await getBinaryPath(deps); |
| 254 | + assert.strictEqual(result, path.join('/ext', 'bin', 'gosqlx')); |
| 255 | + }); |
| 256 | + |
| 257 | + test('PATH is last resort', async () => { |
| 258 | + const deps = makeDeps({ |
| 259 | + extensionPath: '/ext', |
| 260 | + getConfig: () => '', |
| 261 | + checkAccess: () => Promise.reject(new Error('ENOENT')), |
| 262 | + }); |
| 263 | + const result = await getBinaryPath(deps); |
| 264 | + assert.strictEqual(result, 'gosqlx'); |
| 265 | + }); |
| 266 | +}); |
0 commit comments