diff --git a/src/test/normalize_test.ts b/src/test/normalize_test.ts index 93b4c3a..fbc36ba 100644 --- a/src/test/normalize_test.ts +++ b/src/test/normalize_test.ts @@ -1,11 +1,15 @@ import {normalizeSpawnCommand} from '../normalize.js'; import {describe, test, expect} from 'vitest'; import os from 'node:os'; +import path from 'node:path'; +import {fileURLToPath} from 'node:url'; const isWindows = os.platform() === 'win32'; -const baseWindowsOptions = { - env: process.env -}; +const fixturesPath = path.join( + path.dirname(fileURLToPath(import.meta.url)), + '../test/fixtures' +); +const cwd = process.cwd(); describe('normalizeSpawnCommand', () => { test('return from arguments if `shell` option is `true`', () => { @@ -14,40 +18,75 @@ describe('normalizeSpawnCommand', () => { args: ['-v'], options: {shell: true} }); + expect( + normalizeSpawnCommand('nonexistent', ['somearg'], {shell: true}) + ).toEqual({ + command: 'nonexistent', + args: ['somearg'], + options: {shell: true} + }); }); describe.runIf(isWindows)('windows only', () => { - test('just return the same input if resolved', () => { - const normalized = normalizeSpawnCommand( - 'node', - ['-v'], - baseWindowsOptions - ); + test('returns input as-is if command was resolved', () => { + const normalized = normalizeSpawnCommand('node', ['-v'], {}); - expect(normalized.command).toBe('node'); - expect(normalized.args).toEqual(['-v']); + expect(normalized).toEqual({ + command: 'node', + args: ['-v'], + options: {} + }); }); - test('use shell if command are not resolved/available', () => { - const normalized = normalizeSpawnCommand( - 'notexist', - ['hi'], - baseWindowsOptions + test('use shell if command could not be resolved', () => { + const normalized = normalizeSpawnCommand('nonexistent', ['hi'], {}); + + expect(normalized).toEqual({ + command: 'cmd.exe', + args: ['/d', '/s', '/c', '"nonexistent ^"hi^""'], + options: {windowsVerbatimArguments: true} + }); + }); + + test('handles relative commands', () => { + const relativePath = path.relative( + cwd, + path.join(fixturesPath, 'hello_world.cmd') ); + const normalized = normalizeSpawnCommand(relativePath, []); + expect(normalized).toEqual({ + command: 'cmd.exe', + args: ['/d', '/s', '/c', `"${relativePath}"`], + options: {windowsVerbatimArguments: true} + }); + }); - expect(normalized.command.endsWith('cmd.exe')).ok; - expect(normalized.args).toEqual(['/d', '/s', '/c', '"notexist ^"hi^""']); - expect(normalized.options.windowsVerbatimArguments).toBe(true); + test('handles relative commands without extension', () => { + const relativePath = path.relative( + cwd, + path.join(fixturesPath, 'hello_world') + ); + const normalized = normalizeSpawnCommand(relativePath, []); + expect(normalized).toEqual({ + command: 'cmd.exe', + args: ['/d', '/s', '/c', `"${relativePath}"`], + options: {windowsVerbatimArguments: true} + }); }); }); describe.runIf(!isWindows)('unix only', () => { - test('return from arguments', () => { + test('returns command as-is', () => { expect(normalizeSpawnCommand('node', ['-v'])).toEqual({ command: 'node', args: ['-v'], options: {} }); + expect(normalizeSpawnCommand('nonexistent', ['somearg'])).toEqual({ + command: 'nonexistent', + args: ['somearg'], + options: {} + }); }); }); }); diff --git a/test/fixtures/hello_world.cmd b/test/fixtures/hello_world.cmd new file mode 100644 index 0000000..8fbf7fa --- /dev/null +++ b/test/fixtures/hello_world.cmd @@ -0,0 +1 @@ +echo Hello, World!