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

jest.mock('which');
Expand Down Expand Up @@ -135,6 +137,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 +175,75 @@ 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 ']);
});
});

describe('placeInSpecificOrder', () => {
it('returns the first set of strings if the second set of strings is the same', () => {
expect(
placeInSpecificOrder(['foo', 'bar', 'baz'], ['foo', 'bar', 'baz']),
).toStrictEqual(['foo', 'bar', 'baz']);
});

it('returns the first set of strings if the second set of strings is completely different', () => {
expect(
placeInSpecificOrder(['foo', 'bar', 'baz'], ['qux', 'blargh']),
).toStrictEqual(['foo', 'bar', 'baz']);
});

it('returns the second set of strings if both sets of strings exactly have the same elements (just in a different order)', () => {
expect(
placeInSpecificOrder(
['foo', 'qux', 'bar', 'baz'],
['baz', 'foo', 'bar', 'qux'],
),
).toStrictEqual(['baz', 'foo', 'bar', 'qux']);
});

it('returns the first set of strings with the items common to both sets rearranged according to the second set, placing those unique to the first set last', () => {
expect(
placeInSpecificOrder(
['foo', 'zing', 'qux', 'bar', 'baz', 'zam'],
['baz', 'foo', 'bar', 'bam', 'qux', 'zox'],
),
).toStrictEqual(['baz', 'foo', 'bar', 'qux', 'zing', 'zam']);
});
});
});
53 changes: 49 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,46 @@ 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 !== '');
}

/**
* Reorders the given set of strings according to the sort order.
*
* @param unsortedStrings - A set of strings that need to be sorted.
* @param sortedStrings - A set of strings that designate the order in which
* the first set of strings should be placed.
* @returns A sorted version of `unsortedStrings`.
*/
export function placeInSpecificOrder(
unsortedStrings: string[],
sortedStrings: string[],
): string[] {
const unsortedStringsCopy = unsortedStrings.slice();
const newSortedStrings: string[] = [];
sortedStrings.forEach((string) => {
const index = unsortedStringsCopy.indexOf(string);

if (index !== -1) {
unsortedStringsCopy.splice(index, 1);
newSortedStrings.push(string);
}
});
newSortedStrings.push(...unsortedStringsCopy);
return newSortedStrings;
}
Loading