Skip to content

Commit

Permalink
Replace outdent with our own implementation (#117)
Browse files Browse the repository at this point in the history
Currently we use the `outdent` package so that tests that involve
writing multi-line strings to files look a bit nicer. This package
doesn't play nice in an ESM context, and we actually already have
equivalents in `normalizeWhitespaceString` and `buildChangelog` that we
can use instead. So this commit removes `outdent` in favor of these
helpers.
  • Loading branch information
mcmire authored Dec 4, 2023
1 parent efea88c commit 66c90b4
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 106 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
"jest-it-up": "^2.0.2",
"jest-when": "^3.5.2",
"nanoid": "^3.3.4",
"outdent": "^0.8.0",
"prettier": "^2.2.1",
"prettier-plugin-packagejson": "^2.3.0",
"rimraf": "^4.0.5",
Expand Down
30 changes: 15 additions & 15 deletions src/functional.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { withMonorepoProjectEnvironment } from '../tests/functional/helpers/with';
import { buildChangelog } from '../tests/functional/helpers/utils';
import { buildChangelog } from '../tests/helpers';

jest.setTimeout(10_000);

Expand Down Expand Up @@ -474,23 +474,23 @@ describe('create-release-branch (functional)', () => {
'a',
'CHANGELOG.md',
buildChangelog(`
## [Unreleased]
## [Unreleased]
[Unreleased]: https://github.com/example-org/example-repo
[Unreleased]: https://github.com/example-org/example-repo
`),
);
await environment.writeFileWithinPackage(
'b',
'CHANGELOG.md',
buildChangelog(`
## [Unreleased]
## [Unreleased]
## [1.0.0]
### Added
- Initial release
## [1.0.0]
### Added
- Initial release
[Unreleased]: https://github.com/example-org/example-repo/compare/@scope/[email protected]
[1.0.0]: https://github.com/example-org/example-repo/releases/tag/@scope/[email protected]
[Unreleased]: https://github.com/example-org/example-repo/compare/@scope/[email protected]
[1.0.0]: https://github.com/example-org/example-repo/releases/tag/@scope/[email protected]
`),
);
await environment.createCommit('Initial commit');
Expand Down Expand Up @@ -533,14 +533,14 @@ describe('create-release-branch (functional)', () => {
await environment.readFileWithinPackage('b', 'CHANGELOG.md'),
).toStrictEqual(
buildChangelog(`
## [Unreleased]
## [Unreleased]
## [1.0.0]
### Added
- Initial release
## [1.0.0]
### Added
- Initial release
[Unreleased]: https://github.com/example-org/example-repo/compare/@scope/[email protected]
[1.0.0]: https://github.com/example-org/example-repo/releases/tag/@scope/[email protected]
[Unreleased]: https://github.com/example-org/example-repo/compare/@scope/[email protected]
[1.0.0]: https://github.com/example-org/example-repo/releases/tag/@scope/[email protected]
`),
);
},
Expand Down
63 changes: 27 additions & 36 deletions src/package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { when } from 'jest-when';
import * as autoChangelog from '@metamask/auto-changelog';
import { SemVer } from 'semver';
import { MockWritable } from 'stdio-mock';
import _outdent from 'outdent';
import { withSandbox } from '../tests/helpers';
import { buildChangelog, withSandbox } from '../tests/helpers';
import {
buildMockPackage,
buildMockProject,
Expand All @@ -22,8 +21,6 @@ import * as fsModule from './fs';
import * as packageManifestModule from './package-manifest';
import * as repoModule from './repo';

const outdent = _outdent({ trimTrailingNewline: false });

jest.mock('./package-manifest');
jest.mock('./repo');

Expand Down Expand Up @@ -471,20 +468,18 @@ describe('package', () => {

await fs.promises.writeFile(
changelogPath,
outdent`
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
### Uncategorized
- Add \`isNewFunction\` ([#2](https://repo.url/compare/package/pull/2))
## [1.0.0] - 2020-01-01
### Changed
- Something else
[Unreleased]: https://repo.url/compare/[email protected]
[1.0.0]: https://repo.url/releases/tag/[email protected]
`,
buildChangelog(`
## [Unreleased]
### Uncategorized
- Add isNewFunction ([#2](https://repo.url/compare/package/pull/2))
## [1.0.0] - 2020-01-01
### Changed
- Something else
[Unreleased]: https://repo.url/compare/[email protected]
[1.0.0]: https://repo.url/releases/tag/[email protected]
`),
);

await updatePackage({ project, packageReleasePlan });
Expand All @@ -494,27 +489,23 @@ describe('package', () => {
'utf8',
);

expect(newChangelogContent).toBe(outdent`
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
expect(newChangelogContent).toBe(
buildChangelog(`
## [Unreleased]
## [Unreleased]
## [2.0.0]
### Uncategorized
- Add isNewFunction ([#2](https://repo.url/compare/package/pull/2))
## [2.0.0]
### Uncategorized
- Add \`isNewFunction\` ([#2](https://repo.url/compare/package/pull/2))
## [1.0.0] - 2020-01-01
### Changed
- Something else
## [1.0.0] - 2020-01-01
### Changed
- Something else
[Unreleased]: https://repo.url/compare/[email protected]
[2.0.0]: https://repo.url/compare/[email protected]@2.0.0
[1.0.0]: https://repo.url/releases/tag/[email protected]
`);
[Unreleased]: https://repo.url/compare/[email protected]
[2.0.0]: https://repo.url/compare/[email protected]@2.0.0
[1.0.0]: https://repo.url/releases/tag/[email protected]
`),
);
});
});

Expand Down
2 changes: 1 addition & 1 deletion tests/functional/helpers/local-repo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';
import { buildChangelog } from '../../helpers';
import Repo, { RepoOptions } from './repo';
import { buildChangelog } from './utils';

/**
* A set of configuration options for a {@link LocalRepo}. In addition to the
Expand Down
43 changes: 0 additions & 43 deletions tests/functional/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,6 @@ import createDebug from 'debug';

export const debug = createDebug('create-release-branch:tests');

/**
* Given a string, resets its indentation and removes leading and trailing
* whitespace (except for a trailing newline).
*
* @param string - The string.
* @returns The normalized string.
*/
function normalizeMultilineString(string: string): string {
const lines = string
.replace(/^[\n\r]+/u, '')
.replace(/[\n\r]+$/u, '')
.split('\n');
const indentation = lines[0].match(/^([ ]+)/u)?.[1] ?? '';
const normalizedString = lines
.map((line) => {
return line.replace(new RegExp(`^${indentation}`, 'u'), '');
})
.join('\n')
.trim();
return `${normalizedString}\n`;
}

/**
* `Object.keys()` is intentionally generic: it returns the keys of an object,
* but it cannot make guarantees about the contents of that object, so the type
Expand Down Expand Up @@ -61,24 +39,3 @@ export function isErrorWithCode(error: unknown): error is { code: string } {
export async function sleepFor(duration: number): Promise<void> {
await new Promise((resolve) => setTimeout(resolve, duration));
}

/**
* Builds a changelog by filling in the first part automatically, which never
* changes.
*
* @param variantContent - The part of the changelog that can change depending
* on what is expected or what sort of changes have been made to the repo so
* far.
* @returns The full changelog.
*/
export function buildChangelog(variantContent: string): string {
const invariantContent = normalizeMultilineString(`
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
`);

return `${invariantContent}\n${normalizeMultilineString(variantContent)}`;
}
46 changes: 44 additions & 2 deletions tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import fs from 'fs';
import os from 'os';
import path from 'path';
import { nanoid } from 'nanoid';
import { rimraf } from 'rimraf';
import type { ExecaError } from 'execa';
import { hasProperty, isObject } from '@metamask/utils';

Expand Down Expand Up @@ -60,7 +59,7 @@ export async function withSandbox(fn: (sandbox: Sandbox) => any) {
try {
await fn({ directoryPath });
} finally {
await rimraf(directoryPath);
await fs.promises.rm(directoryPath, { force: true, recursive: true });
}
}

Expand Down Expand Up @@ -92,3 +91,46 @@ export function isExecaError(error: unknown): error is ExecaError {
hasProperty(error, 'exitCode')
);
}

/**
* Given a string, resets its indentation and removes leading and trailing
* whitespace (except for a trailing newline).
*
* @param string - The string.
* @returns The normalized string.
*/
export function normalizeMultilineString(string: string): string {
const lines = string
.replace(/^[\n\r]+/u, '')
.replace(/[\n\r]+$/u, '')
.split('\n');
const indentation = lines[0].match(/^([ ]+)/u)?.[1] ?? '';
const normalizedString = lines
.map((line) => {
return line.replace(new RegExp(`^${indentation}`, 'u'), '');
})
.join('\n')
.trim();
return `${normalizedString}\n`;
}

/**
* Builds a changelog by filling in the first part automatically, which never
* changes.
*
* @param variantContent - The part of the changelog that can change depending
* on what is expected or what sort of changes have been made to the repo so
* far.
* @returns The full changelog.
*/
export function buildChangelog(variantContent: string): string {
const invariantContent = normalizeMultilineString(`
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
`);

return `${invariantContent}\n${normalizeMultilineString(variantContent)}`;
}
8 changes: 0 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,6 @@ __metadata:
jest-it-up: ^2.0.2
jest-when: ^3.5.2
nanoid: ^3.3.4
outdent: ^0.8.0
pony-cause: ^2.1.9
prettier: ^2.2.1
prettier-plugin-packagejson: ^2.3.0
Expand Down Expand Up @@ -4837,13 +4836,6 @@ __metadata:
languageName: node
linkType: hard

"outdent@npm:^0.8.0":
version: 0.8.0
resolution: "outdent@npm:0.8.0"
checksum: 72b7c1a287674317ea477999ec24e73a9eda21de35eb9429218f4a5bab899e964afaee7508265898118fee5cbee1d79397916b66dd8aeee285cd948ea5b1f562
languageName: node
linkType: hard

"p-limit@npm:^1.1.0":
version: 1.3.0
resolution: "p-limit@npm:1.3.0"
Expand Down

0 comments on commit 66c90b4

Please sign in to comment.