Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 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
53 changes: 47 additions & 6 deletions src/misc-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import {
isErrorWithStack,
wrapError,
resolveExecutable,
getStdoutFromCommand,
runCommand,
getStdoutFromCommand,
getLinesFromCommand,
} from './misc-utils';

jest.mock('which');
Expand Down Expand Up @@ -135,6 +136,24 @@ describe('misc-utils', () => {
});
});

describe('runCommand', () => {
it('runs the command, discarding its output', async () => {
const execaSpy = jest
.spyOn(execaModule, 'default')
// Typecast: It's difficult to provide a full return value for execa
.mockResolvedValue({ stdout: ' some output ' } as any);

const result = await runCommand('some command', ['arg1', 'arg2'], {
all: true,
});

expect(execaSpy).toHaveBeenCalledWith('some command', ['arg1', 'arg2'], {
all: true,
});
expect(result).toBeUndefined();
});
});

describe('getStdoutFromCommand', () => {
it('executes the given command and returns a version of the standard out from the command with whitespace trimmed', async () => {
const execaSpy = jest
Expand All @@ -155,21 +174,43 @@ describe('misc-utils', () => {
});
});

describe('runCommand', () => {
it('runs the command, discarding its output', async () => {
describe('getLinesFromCommand', () => {
it('executes the given command and returns the standard out from the command split into lines', async () => {
const execaSpy = jest
.spyOn(execaModule, 'default')
// Typecast: It's difficult to provide a full return value for execa
.mockResolvedValue({ stdout: ' some output ' } as any);
.mockResolvedValue({ stdout: 'line 1\nline 2\nline 3' } as any);

const result = await runCommand('some command', ['arg1', 'arg2'], {
const lines = await getLinesFromCommand(
'some command',
['arg1', 'arg2'],
{ all: true },
);

expect(execaSpy).toHaveBeenCalledWith('some command', ['arg1', 'arg2'], {
all: true,
});
expect(lines).toStrictEqual(['line 1', 'line 2', 'line 3']);
});

it('does not strip leading and trailing whitespace from the output, but does remove empty lines', async () => {
const execaSpy = jest
.spyOn(execaModule, 'default')
// Typecast: It's difficult to provide a full return value for execa
.mockResolvedValue({
stdout: ' line 1\nline 2\n\n line 3 \n',
} as any);

const lines = await getLinesFromCommand(
'some command',
['arg1', 'arg2'],
{ all: true },
);

expect(execaSpy).toHaveBeenCalledWith('some command', ['arg1', 'arg2'], {
all: true,
});
expect(result).toBeUndefined();
expect(lines).toStrictEqual([' line 1', 'line 2', ' line 3 ']);
});
});
});
27 changes: 23 additions & 4 deletions src/misc-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,23 @@ export async function resolveExecutable(
}
}

/**
* Runs a command, discarding its output.
*
* @param command - The command to execute.
* @param args - The positional arguments to the command.
* @param options - The options to `execa`.
* @throws An `execa` error object if the command fails in some way.
* @see `execa`.
*/
export async function runCommand(
command: string,
args?: readonly string[] | undefined,
options?: execa.Options<string> | undefined,
): Promise<void> {
await execa(command, args, options);
}

/**
* Runs a command, retrieving the standard output with leading and trailing
* whitespace removed.
Expand All @@ -138,18 +155,20 @@ export async function getStdoutFromCommand(
}

/**
* Runs a command, discarding its output.
* Runs a Git command, splitting up the immediate output into lines.
*
* @param command - The command to execute.
* @param args - The positional arguments to the command.
* @param options - The options to `execa`.
* @returns The standard output of the command.
* @throws An `execa` error object if the command fails in some way.
* @see `execa`.
*/
export async function runCommand(
export async function getLinesFromCommand(
command: string,
args?: readonly string[] | undefined,
options?: execa.Options<string> | undefined,
): Promise<void> {
await execa(command, args, options);
): Promise<string[]> {
const { stdout } = await execa(command, args, options);
return stdout.split('\n').filter((value) => value !== '');
}
Loading