|
6 | 6 |
|
7 | 7 | import { mkdirSync, mkdtempSync, rmSync } from 'fs' |
8 | 8 | import { tmpdir } from 'os' |
9 | | -import { join, posix as pathPosix, relative } from 'path' |
| 9 | +import { join, posix as pathPosix, relative, win32 as pathWin32 } from 'path' |
10 | 10 | import { toJSONSchema, z } from 'zod/v4' |
11 | 11 | import { getPlatform } from '../../utils/platform.js' |
12 | 12 | import { |
13 | 13 | extractLeadingCdCommand, |
14 | 14 | isSameBashCwd, |
15 | 15 | normalizeBashExecutionInput, |
| 16 | + normalizeForHostFs, |
16 | 17 | resolveBashPathFrom, |
17 | 18 | } from './bashWorkdir.js' |
18 | 19 |
|
@@ -215,6 +216,97 @@ async function main(): Promise<void> { |
215 | 216 | ) |
216 | 217 | }) |
217 | 218 |
|
| 219 | + await test('maps only the Windows host /tmp alias to the OS temp directory', () => { |
| 220 | + for (const [target, suffix] of [ |
| 221 | + ['/tmp', ''], |
| 222 | + ['/tmp/', ''], |
| 223 | + ['/tmp/dummy-project', 'dummy-project'], |
| 224 | + ['/tmp/project with spaces/nested', 'project with spaces/nested'], |
| 225 | + ] as const) { |
| 226 | + assert( |
| 227 | + normalizeForHostFs(target, 'windows') === pathWin32.join(tmpdir(), suffix), |
| 228 | + `wrong host mapping for ${target}`, |
| 229 | + ) |
| 230 | + } |
| 231 | + for (const target of ['/tmp-other/repo', '/temporary/repo', 'tmp/repo', '/var/tmp/repo']) { |
| 232 | + assert(normalizeForHostFs(target, 'windows') === target, `must preserve ${target}`) |
| 233 | + } |
| 234 | + }) |
| 235 | + |
| 236 | + await test('preserves native /tmp semantics on Linux, macOS, and WSL', () => { |
| 237 | + for (const platform of ['linux', 'macos', 'wsl'] as const) { |
| 238 | + assert(normalizeForHostFs('/tmp/project', platform) === '/tmp/project', platform) |
| 239 | + const result = normalizeBashExecutionInput( |
| 240 | + { command: 'cd /tmp/project && pwd' }, |
| 241 | + '/workspace', |
| 242 | + platform, |
| 243 | + ) |
| 244 | + assert(result.workdir === '/tmp/project', `wrong ${platform} workdir`) |
| 245 | + assert(result.command === 'pwd' && result._workdirFromCd === true, platform) |
| 246 | + } |
| 247 | + }) |
| 248 | + |
| 249 | + await test('leaves remote /tmp paths and git -C commands untouched', () => { |
| 250 | + for (const command of [ |
| 251 | + 'git -C /tmp/dummy-project status', |
| 252 | + 'docker exec -w /tmp app sh -c "cd /tmp/project && pwd"', |
| 253 | + 'docker compose exec namenode hadoop fs -ls /tmp/', |
| 254 | + 'hadoop fs -ls /tmp/', |
| 255 | + 'hdfs dfs -cat /tmp/result', |
| 256 | + ]) { |
| 257 | + const input = { command } |
| 258 | + assert(normalizeBashExecutionInput(input, 'Q:\\workspace', 'windows') === input, command) |
| 259 | + const result = normalizeBashExecutionInput( |
| 260 | + { command: `cd /tmp && ${command}` }, |
| 261 | + 'Q:\\workspace', |
| 262 | + 'windows', |
| 263 | + ) |
| 264 | + assert(result.command === command, `remote arguments changed: ${command}`) |
| 265 | + } |
| 266 | + }) |
| 267 | + |
| 268 | + if (getPlatform() === 'windows') { |
| 269 | + await test('fixes /tmp cd chains without changing cwd flags or command contents', () => { |
| 270 | + const command = 'git add -A && git commit -m "Initial commit"' |
| 271 | + const result = normalizeBashExecutionInput( |
| 272 | + { command: `cd /tmp/dummy-project && ${command}` }, |
| 273 | + tmpdir(), |
| 274 | + ) |
| 275 | + assert(result.workdir === join(tmpdir(), 'dummy-project'), `got ${result.workdir}`) |
| 276 | + assert(result.command === command, 'git command must stay unchanged') |
| 277 | + assert(result._workdirFromCd === true, 'cd persistence flag must survive') |
| 278 | + assert(normalizeBashExecutionInput(result, tmpdir()) === result, 'must be idempotent') |
| 279 | + |
| 280 | + const chained = normalizeBashExecutionInput( |
| 281 | + { command: 'cd /tmp && cd "project with spaces" && pwd' }, |
| 282 | + tmpdir(), |
| 283 | + ) |
| 284 | + assert(chained.workdir === join(tmpdir(), 'project with spaces'), 'chained cd target') |
| 285 | + assert(chained.command === 'pwd', 'chained cd remainder') |
| 286 | + |
| 287 | + const explicit = normalizeBashExecutionInput({ command: 'pwd', workdir: '/tmp' }, tmpdir()) |
| 288 | + assert(explicit.workdir === tmpdir(), 'explicit /tmp workdir') |
| 289 | + assert(explicit._workdirFromCd === undefined, 'explicit workdir must remain one-off') |
| 290 | + assert(isSameBashCwd('/tmp', tmpdir()), 'temp alias must match native cwd') |
| 291 | + }) |
| 292 | + |
| 293 | + await test('uses configured temp roots without assuming a drive or caching the path', () => { |
| 294 | + const previousTemp = process.env.TEMP |
| 295 | + try { |
| 296 | + for (const temp of ['Q:\\custom temp', 'Z:\\', '\\\\server\\share\\temp']) { |
| 297 | + process.env.TEMP = temp |
| 298 | + assert( |
| 299 | + resolveBashPathFrom('R:\\workspace', '/tmp/project', 'windows') === pathWin32.join(temp, 'project'), |
| 300 | + `must use current temp root ${temp}`, |
| 301 | + ) |
| 302 | + } |
| 303 | + } finally { |
| 304 | + if (previousTemp === undefined) delete process.env.TEMP |
| 305 | + else process.env.TEMP = previousTemp |
| 306 | + } |
| 307 | + }) |
| 308 | + } |
| 309 | + |
218 | 310 | await test('collapses a repeated cwd suffix from a leading cd', () => { |
219 | 311 | const root = mkdtempSync(join(tmpdir(), 'tau-workdir-overlap-')) |
220 | 312 | try { |
|
0 commit comments