Skip to content

Commit 32c3475

Browse files
chore: end-to-end test setup script (#146)
* chore: end-to-end test setup script * Add back finally * A couple e2e fixups * Moved auth into conditional block
1 parent 2795039 commit 32c3475

File tree

4 files changed

+130
-80
lines changed

4 files changed

+130
-80
lines changed

.github/workflows/setup.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: Test Setup Script
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
9+
jobs:
10+
setup:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v3
14+
- uses: ./.github/actions/prepare
15+
- run: pnpm run setup:test

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"lint:spelling": "cspell \"**\" \".github/**/*\"",
6464
"prepare": "husky install",
6565
"setup": "npx --yes zx --quiet script/setup.js",
66+
"setup:test": "npx --yes zx --quiet script/setup-test-e2e.js",
6667
"test": "vitest"
6768
},
6869
"type": "module",

script/setup-test-e2e.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* global $ */
2+
3+
import { strict as assert } from "node:assert";
4+
5+
import { promises as fs } from "fs";
6+
7+
const description = "New Description Test";
8+
const owner = "NewOwnerTest";
9+
const title = "New Title Test";
10+
const repository = "new-repository-test";
11+
12+
const result =
13+
await $`pnpm run setup --description ${description} --owner ${owner} --title ${title} --repository ${repository} --skip-api`;
14+
console.log({ result });
15+
16+
const newPackageJson = JSON.parse(
17+
(await fs.readFile("./package.json")).toString()
18+
);
19+
20+
assert.equal(newPackageJson.description, description);
21+
assert.equal(newPackageJson.name, repository);

script/setup.js

Lines changed: 93 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,14 @@ try {
2323
);
2424
console.log();
2525

26-
console.log(chalk.gray`Checking gh auth status...`);
27-
let auth;
28-
try {
29-
await $`gh auth status`;
30-
auth = (await $`gh auth token`).toString().trim();
31-
} catch (error) {
32-
throw new Error(error.stderr);
33-
}
34-
35-
console.log(chalk.gray`✔️ Done.`);
36-
console.log();
37-
3826
const { values } = parseArgs({
3927
args: process.argv.slice(2),
4028
options: {
4129
description: { type: "string" },
4230
owner: { type: "string" },
4331
repository: { type: "string" },
4432
title: { type: "string" },
33+
"skip-api": { type: "boolean" },
4534
},
4635
tokens: true,
4736
strict: false,
@@ -80,6 +69,11 @@ try {
8069
"How would you describe the new package?"
8170
);
8271

72+
const skipApi = await getPrefillOrPromptedValue(
73+
"skip-api",
74+
"Whether to skip calling the GitHub API (effectively making this a local-only change)."
75+
);
76+
8377
console.log();
8478
console.log(chalk.gray`Hydrating package metadata locally...`);
8579

@@ -118,6 +112,7 @@ try {
118112
["JoshuaKGoldberg", owner],
119113
["template-typescript-node-package", repository],
120114
[/"setup": ".*",/, ``, "./package.json"],
115+
[/"setup:test": ".*",/, ``, "./package.json"],
121116
[
122117
`"version": "${existingPackage.version}"`,
123118
`"version": "0.0.0"`,
@@ -159,87 +154,105 @@ try {
159154

160155
console.log(chalk.gray`✔️ Done.`);
161156

157+
console.log(chalk.gray`✔️ Done.`);
162158
console.log();
163-
console.log(chalk.gray`Hydrating repository labels...`);
164159

165-
const existingLabels = JSON.parse(
166-
(await $`gh label list --json name`).stdout || "[]"
167-
);
160+
if (skipApi) {
161+
console.log(chalk.gray`➖ Skipping API hydration.`);
162+
} else {
163+
console.log(chalk.gray`Checking gh auth status...`);
164+
let auth;
165+
try {
166+
await $`gh auth status`;
167+
auth = (await $`gh auth token`).toString().trim();
168+
} catch (error) {
169+
throw new Error(error.stderr);
170+
}
168171

169-
for (const outcome of outcomeLabels) {
170-
const action = existingLabels.some(
171-
(existing) => existing.name === outcome.name
172-
)
173-
? "edit"
174-
: "create";
175-
await $`gh label ${action} ${outcome.name} --color ${outcome.color} --description ${outcome.description}`;
176-
}
172+
console.log(chalk.gray`✔️ Done.`);
173+
console.log();
177174

178-
console.log(chalk.gray`✔️ Done.`);
175+
console.log(chalk.gray`Hydrating repository labels...`);
179176

180-
console.log();
181-
console.log(chalk.gray`Hydrating initial repository settings...`);
182-
183-
const octokit = new Octokit({ auth });
184-
185-
octokit.rest.repos.update({
186-
allow_auto_merge: true,
187-
allow_rebase_merge: false,
188-
allow_squash_merge: true,
189-
default_branch: "main",
190-
delete_branch_on_merge: true,
191-
description,
192-
has_wiki: false,
193-
owner,
194-
repo: repository,
195-
});
177+
const existingLabels = JSON.parse(
178+
(await $`gh label list --json name`).stdout || "[]"
179+
);
196180

197-
console.log();
198-
console.log(chalk.gray`Hydrating branch protection settings...`);
199-
200-
// Note: keep this inline script in sync with .github/workflows/release.yml!
201-
// Todo: it would be nice to not have two sources of truth...
202-
// https://github.com/JoshuaKGoldberg/template-typescript-node-package/issues/145
203-
await octokit.request(
204-
`PUT /repos/${owner}/${repository}/branches/main/protection`,
205-
{
206-
allow_deletions: false,
207-
allow_force_pushes: true,
208-
allow_fork_pushes: false,
209-
allow_fork_syncing: true,
210-
block_creations: false,
211-
branch: "main",
212-
enforce_admins: false,
213-
owner,
214-
repo: repository,
215-
required_conversation_resolution: true,
216-
required_linear_history: false,
217-
required_pull_request_reviews: null,
218-
required_status_checks: {
219-
checks: [
220-
{ context: "build" },
221-
{ context: "compliance" },
222-
{ context: "lint" },
223-
{ context: "markdown" },
224-
{ context: "package" },
225-
{ context: "packages" },
226-
{ context: "prettier" },
227-
{ context: "prune" },
228-
{ context: "spelling" },
229-
{ context: "test" },
230-
],
231-
strict: false,
232-
},
233-
restrictions: null,
181+
for (const outcome of outcomeLabels) {
182+
const action = existingLabels.some(
183+
(existing) => existing.name === outcome.name
184+
)
185+
? "edit"
186+
: "create";
187+
await $`gh label ${action} ${outcome.name} --color ${outcome.color} --description ${outcome.description}`;
234188
}
235-
);
189+
console.log(chalk.gray`✔️ Done.`);
236190

237-
console.log(chalk.gray`✔️ Done.`);
191+
console.log();
192+
console.log(chalk.gray`Hydrating initial repository settings...`);
193+
194+
const octokit = new Octokit({ auth });
195+
196+
octokit.rest.repos.update({
197+
allow_auto_merge: true,
198+
allow_rebase_merge: false,
199+
allow_squash_merge: true,
200+
default_branch: "main",
201+
delete_branch_on_merge: true,
202+
description,
203+
has_wiki: false,
204+
owner,
205+
repo: repository,
206+
});
207+
208+
console.log();
209+
console.log(chalk.gray`Hydrating branch protection settings...`);
210+
211+
// Note: keep this inline script in sync with .github/workflows/release.yml!
212+
// Todo: it would be nice to not have two sources of truth...
213+
// https://github.com/JoshuaKGoldberg/template-typescript-node-package/issues/145
214+
await octokit.request(
215+
`PUT /repos/${owner}/${repository}/branches/main/protection`,
216+
{
217+
allow_deletions: false,
218+
allow_force_pushes: true,
219+
allow_fork_pushes: false,
220+
allow_fork_syncing: true,
221+
block_creations: false,
222+
branch: "main",
223+
enforce_admins: false,
224+
owner,
225+
repo: repository,
226+
required_conversation_resolution: true,
227+
required_linear_history: false,
228+
required_pull_request_reviews: null,
229+
required_status_checks: {
230+
checks: [
231+
{ context: "build" },
232+
{ context: "compliance" },
233+
{ context: "lint" },
234+
{ context: "markdown" },
235+
{ context: "package" },
236+
{ context: "packages" },
237+
{ context: "prettier" },
238+
{ context: "prune" },
239+
{ context: "spelling" },
240+
{ context: "test" },
241+
],
242+
strict: false,
243+
},
244+
restrictions: null,
245+
}
246+
);
247+
248+
console.log(chalk.gray`✔️ Done.`);
249+
}
238250

239251
console.log();
240252
console.log(chalk.gray`Removing setup script...`);
241253

242254
await fs.rm("./script", { force: true, recursive: true });
255+
await fs.rm(".github/workflows/setup.yml");
243256

244257
console.log(chalk.gray`✔️ Done.`);
245258
} catch (error) {

0 commit comments

Comments
 (0)