Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavohenke committed May 26, 2024
1 parent cad40b4 commit 3f61979
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 22 deletions.
58 changes: 38 additions & 20 deletions src/spawn.spec.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,52 @@
import { getSpawnOpts } from './spawn';
import { getSpawnOpts, spawn } from './spawn';

const baseProcess = {
platform: 'win32' as const,
cwd: () => '',
env: {},
};

it('sets detached mode to false for Windows platform', () => {
expect(getSpawnOpts({ process: baseProcess }).detached).toBe(false);
});
describe('spawn()', () => {
it('spawns the given command', async () => {
const fakeSpawn = jest.fn();
spawn('echo banana', {}, fakeSpawn, baseProcess);
expect(fakeSpawn).toHaveBeenCalled();
expect(fakeSpawn.mock.calls[0][1].join(' ')).toContain('echo banana');
});

it('sets stdio to inherit when raw', () => {
expect(getSpawnOpts({ raw: true }).stdio).toBe('inherit');
it('returns spawned process', async () => {
const childProcess = {};
const fakeSpawn = jest.fn().mockReturnValue(childProcess);
const child = spawn('echo banana', {}, fakeSpawn, baseProcess);
expect(child).toBe(childProcess);
});
});

it('merges FORCE_COLOR into env vars if color supported', () => {
const process = { ...baseProcess, env: { foo: 'bar' } };
expect(getSpawnOpts({ process, colorSupport: false }).env).toEqual(process.env);
expect(getSpawnOpts({ process, colorSupport: { level: 1 } }).env).toEqual({
FORCE_COLOR: '1',
foo: 'bar',
describe('getSpawnOpts()', () => {
it('sets detached mode to false for Windows platform', () => {
expect(getSpawnOpts({ process: baseProcess }).detached).toBe(false);
});
});

it('sets default cwd to process.cwd()', () => {
const process = { ...baseProcess, cwd: () => 'process-cwd' };
expect(getSpawnOpts({ process }).cwd).toBe('process-cwd');
});
it('sets stdio to inherit when raw', () => {
expect(getSpawnOpts({ raw: true }).stdio).toBe('inherit');
});

it('overrides default cwd', () => {
const cwd = 'foobar';
expect(getSpawnOpts({ cwd }).cwd).toBe(cwd);
it('merges FORCE_COLOR into env vars if color supported', () => {
const process = { ...baseProcess, env: { foo: 'bar' } };
expect(getSpawnOpts({ process, colorSupport: false }).env).toEqual(process.env);
expect(getSpawnOpts({ process, colorSupport: { level: 1 } }).env).toEqual({
FORCE_COLOR: '1',
foo: 'bar',
});
});

it('sets default cwd to process.cwd()', () => {
const process = { ...baseProcess, cwd: () => 'process-cwd' };
expect(getSpawnOpts({ process }).cwd).toBe('process-cwd');
});

it('overrides default cwd', () => {
const cwd = 'foobar';
expect(getSpawnOpts({ cwd }).cwd).toBe(cwd);
});
});
10 changes: 8 additions & 2 deletions src/spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@ import supportsColor from 'supports-color';
* Spawns a command using `cmd.exe` on Windows, or `/bin/sh` elsewhere.
*/
// Implementation based off of https://github.com/mmalecki/spawn-command/blob/v0.0.2-1/lib/spawn-command.js
export function spawn(command: string, options: SpawnOptions): ChildProcess {
export function spawn(
command: string,
options: SpawnOptions,
// For testing
spawn: (command: string, args: string[], options: SpawnOptions) => ChildProcess = baseSpawn,
process: Pick<NodeJS.Process, 'platform'> = global.process,
): ChildProcess {
let file = '/bin/sh';
let args = ['-c', command];
if (process.platform === 'win32') {
file = 'cmd.exe';
args = ['/s', '/c', `"${command}"`];
options.windowsVerbatimArguments = true;
}
return baseSpawn(file, args, options);
return spawn(file, args, options);
}

export const getSpawnOpts = ({
Expand Down

0 comments on commit 3f61979

Please sign in to comment.