Skip to content

Commit 66c90b4

Browse files
authored
Replace outdent with our own implementation (#117)
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.
1 parent efea88c commit 66c90b4

File tree

7 files changed

+87
-106
lines changed

7 files changed

+87
-106
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
"jest-it-up": "^2.0.2",
6262
"jest-when": "^3.5.2",
6363
"nanoid": "^3.3.4",
64-
"outdent": "^0.8.0",
6564
"prettier": "^2.2.1",
6665
"prettier-plugin-packagejson": "^2.3.0",
6766
"rimraf": "^4.0.5",

src/functional.test.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { withMonorepoProjectEnvironment } from '../tests/functional/helpers/with';
2-
import { buildChangelog } from '../tests/functional/helpers/utils';
2+
import { buildChangelog } from '../tests/helpers';
33

44
jest.setTimeout(10_000);
55

@@ -474,23 +474,23 @@ describe('create-release-branch (functional)', () => {
474474
'a',
475475
'CHANGELOG.md',
476476
buildChangelog(`
477-
## [Unreleased]
477+
## [Unreleased]
478478
479-
[Unreleased]: https://github.com/example-org/example-repo
479+
[Unreleased]: https://github.com/example-org/example-repo
480480
`),
481481
);
482482
await environment.writeFileWithinPackage(
483483
'b',
484484
'CHANGELOG.md',
485485
buildChangelog(`
486-
## [Unreleased]
486+
## [Unreleased]
487487
488-
## [1.0.0]
489-
### Added
490-
- Initial release
488+
## [1.0.0]
489+
### Added
490+
- Initial release
491491
492-
[Unreleased]: https://github.com/example-org/example-repo/compare/@scope/[email protected]
493-
[1.0.0]: https://github.com/example-org/example-repo/releases/tag/@scope/[email protected]
492+
[Unreleased]: https://github.com/example-org/example-repo/compare/@scope/[email protected]
493+
[1.0.0]: https://github.com/example-org/example-repo/releases/tag/@scope/[email protected]
494494
`),
495495
);
496496
await environment.createCommit('Initial commit');
@@ -533,14 +533,14 @@ describe('create-release-branch (functional)', () => {
533533
await environment.readFileWithinPackage('b', 'CHANGELOG.md'),
534534
).toStrictEqual(
535535
buildChangelog(`
536-
## [Unreleased]
536+
## [Unreleased]
537537
538-
## [1.0.0]
539-
### Added
540-
- Initial release
538+
## [1.0.0]
539+
### Added
540+
- Initial release
541541
542-
[Unreleased]: https://github.com/example-org/example-repo/compare/@scope/[email protected]
543-
[1.0.0]: https://github.com/example-org/example-repo/releases/tag/@scope/[email protected]
542+
[Unreleased]: https://github.com/example-org/example-repo/compare/@scope/[email protected]
543+
[1.0.0]: https://github.com/example-org/example-repo/releases/tag/@scope/[email protected]
544544
`),
545545
);
546546
},

src/package.test.ts

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import { when } from 'jest-when';
44
import * as autoChangelog from '@metamask/auto-changelog';
55
import { SemVer } from 'semver';
66
import { MockWritable } from 'stdio-mock';
7-
import _outdent from 'outdent';
8-
import { withSandbox } from '../tests/helpers';
7+
import { buildChangelog, withSandbox } from '../tests/helpers';
98
import {
109
buildMockPackage,
1110
buildMockProject,
@@ -22,8 +21,6 @@ import * as fsModule from './fs';
2221
import * as packageManifestModule from './package-manifest';
2322
import * as repoModule from './repo';
2423

25-
const outdent = _outdent({ trimTrailingNewline: false });
26-
2724
jest.mock('./package-manifest');
2825
jest.mock('./repo');
2926

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

472469
await fs.promises.writeFile(
473470
changelogPath,
474-
outdent`
475-
# Changelog
476-
All notable changes to this project will be documented in this file.
477-
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
478-
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
479-
## [Unreleased]
480-
### Uncategorized
481-
- Add \`isNewFunction\` ([#2](https://repo.url/compare/package/pull/2))
482-
## [1.0.0] - 2020-01-01
483-
### Changed
484-
- Something else
485-
[Unreleased]: https://repo.url/compare/[email protected]
486-
[1.0.0]: https://repo.url/releases/tag/[email protected]
487-
`,
471+
buildChangelog(`
472+
## [Unreleased]
473+
### Uncategorized
474+
- Add isNewFunction ([#2](https://repo.url/compare/package/pull/2))
475+
476+
## [1.0.0] - 2020-01-01
477+
### Changed
478+
- Something else
479+
480+
[Unreleased]: https://repo.url/compare/[email protected]
481+
[1.0.0]: https://repo.url/releases/tag/[email protected]
482+
`),
488483
);
489484

490485
await updatePackage({ project, packageReleasePlan });
@@ -494,27 +489,23 @@ describe('package', () => {
494489
'utf8',
495490
);
496491

497-
expect(newChangelogContent).toBe(outdent`
498-
# Changelog
499-
All notable changes to this project will be documented in this file.
500-
501-
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
502-
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
492+
expect(newChangelogContent).toBe(
493+
buildChangelog(`
494+
## [Unreleased]
503495
504-
## [Unreleased]
496+
## [2.0.0]
497+
### Uncategorized
498+
- Add isNewFunction ([#2](https://repo.url/compare/package/pull/2))
505499
506-
## [2.0.0]
507-
### Uncategorized
508-
- Add \`isNewFunction\` ([#2](https://repo.url/compare/package/pull/2))
500+
## [1.0.0] - 2020-01-01
501+
### Changed
502+
- Something else
509503
510-
## [1.0.0] - 2020-01-01
511-
### Changed
512-
- Something else
513-
514-
[Unreleased]: https://repo.url/compare/[email protected]
515-
[2.0.0]: https://repo.url/compare/[email protected]@2.0.0
516-
[1.0.0]: https://repo.url/releases/tag/[email protected]
517-
`);
504+
[Unreleased]: https://repo.url/compare/[email protected]
505+
[2.0.0]: https://repo.url/compare/[email protected]@2.0.0
506+
[1.0.0]: https://repo.url/releases/tag/[email protected]
507+
`),
508+
);
518509
});
519510
});
520511

tests/functional/helpers/local-repo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'path';
2+
import { buildChangelog } from '../../helpers';
23
import Repo, { RepoOptions } from './repo';
3-
import { buildChangelog } from './utils';
44

55
/**
66
* A set of configuration options for a {@link LocalRepo}. In addition to the

tests/functional/helpers/utils.ts

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,6 @@ import createDebug from 'debug';
22

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

5-
/**
6-
* Given a string, resets its indentation and removes leading and trailing
7-
* whitespace (except for a trailing newline).
8-
*
9-
* @param string - The string.
10-
* @returns The normalized string.
11-
*/
12-
function normalizeMultilineString(string: string): string {
13-
const lines = string
14-
.replace(/^[\n\r]+/u, '')
15-
.replace(/[\n\r]+$/u, '')
16-
.split('\n');
17-
const indentation = lines[0].match(/^([ ]+)/u)?.[1] ?? '';
18-
const normalizedString = lines
19-
.map((line) => {
20-
return line.replace(new RegExp(`^${indentation}`, 'u'), '');
21-
})
22-
.join('\n')
23-
.trim();
24-
return `${normalizedString}\n`;
25-
}
26-
275
/**
286
* `Object.keys()` is intentionally generic: it returns the keys of an object,
297
* but it cannot make guarantees about the contents of that object, so the type
@@ -61,24 +39,3 @@ export function isErrorWithCode(error: unknown): error is { code: string } {
6139
export async function sleepFor(duration: number): Promise<void> {
6240
await new Promise((resolve) => setTimeout(resolve, duration));
6341
}
64-
65-
/**
66-
* Builds a changelog by filling in the first part automatically, which never
67-
* changes.
68-
*
69-
* @param variantContent - The part of the changelog that can change depending
70-
* on what is expected or what sort of changes have been made to the repo so
71-
* far.
72-
* @returns The full changelog.
73-
*/
74-
export function buildChangelog(variantContent: string): string {
75-
const invariantContent = normalizeMultilineString(`
76-
# Changelog
77-
All notable changes to this project will be documented in this file.
78-
79-
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
80-
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
81-
`);
82-
83-
return `${invariantContent}\n${normalizeMultilineString(variantContent)}`;
84-
}

tests/helpers.ts

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import fs from 'fs';
22
import os from 'os';
33
import path from 'path';
44
import { nanoid } from 'nanoid';
5-
import { rimraf } from 'rimraf';
65
import type { ExecaError } from 'execa';
76
import { hasProperty, isObject } from '@metamask/utils';
87

@@ -60,7 +59,7 @@ export async function withSandbox(fn: (sandbox: Sandbox) => any) {
6059
try {
6160
await fn({ directoryPath });
6261
} finally {
63-
await rimraf(directoryPath);
62+
await fs.promises.rm(directoryPath, { force: true, recursive: true });
6463
}
6564
}
6665

@@ -92,3 +91,46 @@ export function isExecaError(error: unknown): error is ExecaError {
9291
hasProperty(error, 'exitCode')
9392
);
9493
}
94+
95+
/**
96+
* Given a string, resets its indentation and removes leading and trailing
97+
* whitespace (except for a trailing newline).
98+
*
99+
* @param string - The string.
100+
* @returns The normalized string.
101+
*/
102+
export function normalizeMultilineString(string: string): string {
103+
const lines = string
104+
.replace(/^[\n\r]+/u, '')
105+
.replace(/[\n\r]+$/u, '')
106+
.split('\n');
107+
const indentation = lines[0].match(/^([ ]+)/u)?.[1] ?? '';
108+
const normalizedString = lines
109+
.map((line) => {
110+
return line.replace(new RegExp(`^${indentation}`, 'u'), '');
111+
})
112+
.join('\n')
113+
.trim();
114+
return `${normalizedString}\n`;
115+
}
116+
117+
/**
118+
* Builds a changelog by filling in the first part automatically, which never
119+
* changes.
120+
*
121+
* @param variantContent - The part of the changelog that can change depending
122+
* on what is expected or what sort of changes have been made to the repo so
123+
* far.
124+
* @returns The full changelog.
125+
*/
126+
export function buildChangelog(variantContent: string): string {
127+
const invariantContent = normalizeMultilineString(`
128+
# Changelog
129+
All notable changes to this project will be documented in this file.
130+
131+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
132+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
133+
`);
134+
135+
return `${invariantContent}\n${normalizeMultilineString(variantContent)}`;
136+
}

yarn.lock

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,6 @@ __metadata:
10161016
jest-it-up: ^2.0.2
10171017
jest-when: ^3.5.2
10181018
nanoid: ^3.3.4
1019-
outdent: ^0.8.0
10201019
pony-cause: ^2.1.9
10211020
prettier: ^2.2.1
10221021
prettier-plugin-packagejson: ^2.3.0
@@ -4837,13 +4836,6 @@ __metadata:
48374836
languageName: node
48384837
linkType: hard
48394838

4840-
"outdent@npm:^0.8.0":
4841-
version: 0.8.0
4842-
resolution: "outdent@npm:0.8.0"
4843-
checksum: 72b7c1a287674317ea477999ec24e73a9eda21de35eb9429218f4a5bab899e964afaee7508265898118fee5cbee1d79397916b66dd8aeee285cd948ea5b1f562
4844-
languageName: node
4845-
linkType: hard
4846-
48474839
"p-limit@npm:^1.1.0":
48484840
version: 1.3.0
48494841
resolution: "p-limit@npm:1.3.0"

0 commit comments

Comments
 (0)