-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathmigrate-test-e2e.js
160 lines (139 loc) · 4.97 KB
/
migrate-test-e2e.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import chalk from "chalk";
import { $, execaCommand } from "execa";
import * as fs from "node:fs/promises";
import { rimraf } from "rimraf";
import { assert, describe, expect, test } from "vitest";
import packageData from "../package.json" assert { type: "json" };
const filesExpectedToBeChanged = [
"README.md",
"knip.json",
".eslintrc.cjs",
".github/workflows/test.yml",
".gitignore",
".prettierignore",
"cspell.json",
];
const filesThatMightBeChanged = new Set([
...filesExpectedToBeChanged,
"script/__snapshots__/migrate-test-e2e.js.snap",
]);
const {
author: { email: emailNpm, name: authorName },
description,
name: repository,
} = packageData;
const emailGithub = "[email protected]";
const bin = "./bin/index.js";
const guide =
"https://www.joshuakgoldberg.com/blog/contributing-to-a-create-typescript-app-repository";
const guideTitle = "Contributing to a create-typescript-app Repository";
const logo = "./docs/create-typescript-app.png";
const logoAlt = `Project logo: the TypeScript blue square with rounded corners, but a plus sign instead of 'TS'`;
const owner = "JoshuaKGoldberg";
const title = "Create TypeScript App";
await rimraf("coverage*");
const originalReadme = (await fs.readFile("README.md")).toString();
const originalSnapshots = (
await fs.readFile("script/__snapshots__/migrate-test-e2e.js.snap")
).toString();
await $({
stdio: "inherit",
})`c8 -o ./coverage -r html -r lcov --src src node ${bin} --base everything --author ${authorName} --mode migrate --bin ${bin} --description ${description} --email-github ${emailGithub} --email-npm ${emailNpm} --guide ${guide} --guide-title ${guideTitle} --logo ${logo} --logo-alt ${logoAlt} --owner ${owner} --title ${title} --repository ${repository} --skip-all-contributors-api --skip-github-api --skip-install`;
// All Contributors seems to not be using Prettier to format files...
await fs.writeFile(
".all-contributorsrc",
JSON.stringify(
JSON.parse((await fs.readFile(".all-contributorsrc")).toString()),
null,
2,
) + "\n",
);
// Ignore changes to the README.md all-contributor count and contributors table...
const updatedReadme = (await fs.readFile("README.md")).toString();
await fs.writeFile(
"README.md",
[
updatedReadme.slice(0, updatedReadme.indexOf("## Contributors")) +
originalReadme.slice(
originalReadme.indexOf("## Contributors"),
originalReadme.indexOf("<!-- markdownlint-restore -->"),
),
updatedReadme.slice(updatedReadme.indexOf("<!-- markdownlint-restore -->")),
]
.join("")
.replaceAll(
/All Contributors: \d+/g,
originalReadme.match(/All Contributors: \d+/)[0],
)
.replaceAll(
/all_contributors-\d+/g,
originalReadme.match(/all_contributors-\d+/)[0],
),
);
// ...and even to the snapshot file, so diffs don't mind it.
await fs.writeFile(
"script/__snapshots__/migrate-test-e2e.js.snap",
originalSnapshots
.replaceAll(
/All Contributors: \d+/g,
originalReadme.match(/All Contributors: \d+/)[0],
)
.replaceAll(
/all_contributors-\d+/g,
originalReadme.match(/all_contributors-\d+/)[0],
),
);
describe("expected file changes", () => {
test.each(filesExpectedToBeChanged)("%s", async (file) => {
const { stdout } = await execaCommand(`git diff HEAD -- ${file}`);
const contentsAfterGitMarkers = stdout
.split("\n")
.slice(2)
.join("\n")
.replaceAll(/@@ -\d+,\d+ \+\d+,\d+ @@/g, "@@ ... @@");
assert(
stdout,
`Looks like there were no changes to ${file} from migration?`,
);
// If this fails, see .github/DEVELOPMENT.md > Setup Scripts for context.
// Then see .github/DEVELOPMENT.md > Migration Snapshot Failures.
expect(contentsAfterGitMarkers).toMatchSnapshot();
});
});
// eslint-disable-next-line vitest/expect-expect
test("unexpected file changes", async () => {
const { stdout: gitStatus } = await $`git status`;
console.log(`Stdout from running \`git status\`:\n${gitStatus}`);
const indexOfUnstagedFilesMessage = gitStatus.indexOf(
"Changes not staged for commit:",
);
assert(
indexOfUnstagedFilesMessage !== -1,
`Looks like migrate didn't cause any file changes? That's ...probably incorrect? 😬`,
);
const unstagedModifiedFiles = gitStatus
.slice(indexOfUnstagedFilesMessage)
.match(/modified: {3}(\S+)\n/g)
.map((match) => match.split(/\s+/g)[1])
.filter((filePath) => !filesThatMightBeChanged.has(filePath));
console.log("Unexpected modified files are:", unstagedModifiedFiles);
if (unstagedModifiedFiles.length) {
const gitDiffCommand = `git diff HEAD -- ${unstagedModifiedFiles.join(
" ",
)}`;
const { stdout } = await execaCommand(gitDiffCommand);
console.log(`Stdout from running \`${gitDiffCommand}\`:\n${stdout}`);
throw new Error(
[
"",
"Oh no! Running the migrate script unexpectedly modified:",
...unstagedModifiedFiles.map((filePath) => ` - ${filePath}`),
"",
"See .github/DEVELOPMENT.md > Setup Scripts for context.",
"Then see .github/DEVELOPMENT.md > Unexpected File Modifications.",
]
.map((line) => chalk.red(line))
.join("\n"),
);
}
});