Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 59 additions & 20 deletions src/test/normalize_test.ts
Original file line number Diff line number Diff line change
@@ -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`', () => {
Expand All @@ -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: {}
});
});
});
});
1 change: 1 addition & 0 deletions test/fixtures/hello_world.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo Hello, World!