Skip to content

Update release spec to only list changed packages #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Sep 30, 2022
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
6 changes: 6 additions & 0 deletions src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import {
} from '@metamask/action-utils';
import { wrapError, isErrorWithCode } from './misc-utils';

/**
* Represents a writeable stream, such as that represented by `process.stdout`
* or `process.stderr`, or a fake one provided in tests.
*/
export type WriteStreamLike = Pick<fs.WriteStream, 'write'>;

/**
* Reads the file at the given path, assuming its content is encoded as UTF-8.
*
Expand Down
77 changes: 49 additions & 28 deletions src/initial-parameters.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import os from 'os';
import path from 'path';
import { when } from 'jest-when';
import { buildMockProject, buildMockPackage } from '../tests/unit/helpers';
import {
buildMockProject,
buildMockPackage,
createNoopWriteStream,
} from '../tests/unit/helpers';
import { determineInitialParameters } from './initial-parameters';
import * as commandLineArgumentsModule from './command-line-arguments';
import * as envModule from './env';
Expand All @@ -23,6 +27,7 @@ describe('initial-parameters', () => {

it('returns an object derived from command-line arguments and environment variables that contains data necessary to run the workflow', async () => {
const project = buildMockProject();
const stderr = createNoopWriteStream();
when(jest.spyOn(commandLineArgumentsModule, 'readCommandLineArguments'))
.calledWith(['arg1', 'arg2'])
.mockResolvedValue({
Expand All @@ -34,13 +39,14 @@ describe('initial-parameters', () => {
.spyOn(envModule, 'getEnvironmentVariables')
.mockReturnValue({ EDITOR: undefined });
when(jest.spyOn(projectModule, 'readProject'))
.calledWith('/path/to/project')
.calledWith('/path/to/project', { stderr })
.mockResolvedValue(project);

const config = await determineInitialParameters(
['arg1', 'arg2'],
'/path/to/somewhere',
);
const config = await determineInitialParameters({
argv: ['arg1', 'arg2'],
cwd: '/path/to/somewhere',
stderr,
});

expect(config).toStrictEqual({
project,
Expand All @@ -53,6 +59,7 @@ describe('initial-parameters', () => {
const project = buildMockProject({
rootPackage: buildMockPackage(),
});
const stderr = createNoopWriteStream();
when(jest.spyOn(commandLineArgumentsModule, 'readCommandLineArguments'))
.calledWith(['arg1', 'arg2'])
.mockResolvedValue({
Expand All @@ -67,13 +74,20 @@ describe('initial-parameters', () => {
.spyOn(projectModule, 'readProject')
.mockResolvedValue(project);

await determineInitialParameters(['arg1', 'arg2'], '/path/to/cwd');
await determineInitialParameters({
argv: ['arg1', 'arg2'],
cwd: '/path/to/cwd',
stderr,
});

expect(readProjectSpy).toHaveBeenCalledWith('/path/to/cwd/project');
expect(readProjectSpy).toHaveBeenCalledWith('/path/to/cwd/project', {
stderr,
});
});

it('resolves the given temporary directory relative to the current working directory', async () => {
const project = buildMockProject();
const stderr = createNoopWriteStream();
when(jest.spyOn(commandLineArgumentsModule, 'readCommandLineArguments'))
.calledWith(['arg1', 'arg2'])
.mockResolvedValue({
Expand All @@ -85,13 +99,14 @@ describe('initial-parameters', () => {
.spyOn(envModule, 'getEnvironmentVariables')
.mockReturnValue({ EDITOR: undefined });
when(jest.spyOn(projectModule, 'readProject'))
.calledWith('/path/to/project')
.calledWith('/path/to/project', { stderr })
.mockResolvedValue(project);

const config = await determineInitialParameters(
['arg1', 'arg2'],
'/path/to/cwd',
);
const config = await determineInitialParameters({
argv: ['arg1', 'arg2'],
cwd: '/path/to/cwd',
stderr,
});

expect(config.tempDirectoryPath).toStrictEqual('/path/to/cwd/tmp');
});
Expand All @@ -100,6 +115,7 @@ describe('initial-parameters', () => {
const project = buildMockProject({
rootPackage: buildMockPackage('@foo/bar'),
});
const stderr = createNoopWriteStream();
when(jest.spyOn(commandLineArgumentsModule, 'readCommandLineArguments'))
.calledWith(['arg1', 'arg2'])
.mockResolvedValue({
Expand All @@ -111,13 +127,14 @@ describe('initial-parameters', () => {
.spyOn(envModule, 'getEnvironmentVariables')
.mockReturnValue({ EDITOR: undefined });
when(jest.spyOn(projectModule, 'readProject'))
.calledWith('/path/to/project')
.calledWith('/path/to/project', { stderr })
.mockResolvedValue(project);

const config = await determineInitialParameters(
['arg1', 'arg2'],
'/path/to/cwd',
);
const config = await determineInitialParameters({
argv: ['arg1', 'arg2'],
cwd: '/path/to/cwd',
stderr,
});

expect(config.tempDirectoryPath).toStrictEqual(
path.join(os.tmpdir(), 'create-release-branch', '@foo__bar'),
Expand All @@ -126,6 +143,7 @@ describe('initial-parameters', () => {

it('returns initial parameters including reset: true, derived from a command-line argument of "--reset true"', async () => {
const project = buildMockProject();
const stderr = createNoopWriteStream();
when(jest.spyOn(commandLineArgumentsModule, 'readCommandLineArguments'))
.calledWith(['arg1', 'arg2'])
.mockResolvedValue({
Expand All @@ -137,19 +155,21 @@ describe('initial-parameters', () => {
.spyOn(envModule, 'getEnvironmentVariables')
.mockReturnValue({ EDITOR: undefined });
when(jest.spyOn(projectModule, 'readProject'))
.calledWith('/path/to/project')
.calledWith('/path/to/project', { stderr })
.mockResolvedValue(project);

const config = await determineInitialParameters(
['arg1', 'arg2'],
'/path/to/somewhere',
);
const config = await determineInitialParameters({
argv: ['arg1', 'arg2'],
cwd: '/path/to/somewhere',
stderr,
});

expect(config.reset).toBe(true);
});

it('returns initial parameters including reset: false, derived from a command-line argument of "--reset false"', async () => {
const project = buildMockProject();
const stderr = createNoopWriteStream();
when(jest.spyOn(commandLineArgumentsModule, 'readCommandLineArguments'))
.calledWith(['arg1', 'arg2'])
.mockResolvedValue({
Expand All @@ -161,13 +181,14 @@ describe('initial-parameters', () => {
.spyOn(envModule, 'getEnvironmentVariables')
.mockReturnValue({ EDITOR: undefined });
when(jest.spyOn(projectModule, 'readProject'))
.calledWith('/path/to/project')
.calledWith('/path/to/project', { stderr })
.mockResolvedValue(project);

const config = await determineInitialParameters(
['arg1', 'arg2'],
'/path/to/somewhere',
);
const config = await determineInitialParameters({
argv: ['arg1', 'arg2'],
cwd: '/path/to/somewhere',
stderr,
});

expect(config.reset).toBe(false);
});
Expand Down
22 changes: 15 additions & 7 deletions src/initial-parameters.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os from 'os';
import path from 'path';
import { readCommandLineArguments } from './command-line-arguments';
import { WriteStreamLike } from './fs';
import { readProject, Project } from './project';

interface InitialParameters {
Expand All @@ -13,18 +14,25 @@ interface InitialParameters {
* Reads the inputs given to this tool via `process.argv` and uses them to
* gather information about the project the tool can use to run.
*
* @param argv - The arguments to this executable.
* @param cwd - The directory in which this executable was run.
* @param args - The arguments to this function.
* @param args.argv - The arguments to this executable.
* @param args.cwd - The directory in which this executable was run.
* @param args.stderr - A stream that can be used to write to standard error.
* @returns The initial parameters.
*/
export async function determineInitialParameters(
argv: string[],
cwd: string,
): Promise<InitialParameters> {
export async function determineInitialParameters({
argv,
cwd,
stderr,
}: {
argv: string[];
cwd: string;
stderr: WriteStreamLike;
}): Promise<InitialParameters> {
const inputs = await readCommandLineArguments(argv);

const projectDirectoryPath = path.resolve(cwd, inputs.projectDirectory);
const project = await readProject(projectDirectoryPath);
const project = await readProject(projectDirectoryPath, { stderr });
const tempDirectoryPath =
inputs.tempDirectory === undefined
? path.join(
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export async function main({
stderr: Pick<WriteStream, 'write'>;
}) {
const { project, tempDirectoryPath, reset } =
await determineInitialParameters(argv, cwd);
await determineInitialParameters({ argv, cwd, stderr });

if (project.isMonorepo) {
stdout.write(
Expand Down
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