Skip to content

Commit db45131

Browse files
committed
test: remove certs script
We currently have a `certs` npm script that generates self-signed certificates for use in a single zisi test. This script must be run before running tests, otherwise the zisi test in question will fail. This changeset moves this certificate generation into a test helper function, ensuring that a certificate will always be available when needed by a test. It would probably be simpler to instead generate a certificate that never expires and inline it into the fixture (eliminating the need for an available `openssl` binary), but this works fine.
1 parent 719c17c commit db45131

File tree

5 files changed

+80
-27
lines changed

5 files changed

+80
-27
lines changed

.github/workflows/integration-tests.yml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,6 @@ jobs:
7777
- name: Build project
7878
run: npm run build
7979

80-
- name: Generate self-signed certificates
81-
run: npm run certs
82-
if: '${{!steps.release-check.outputs.IS_RELEASE}}'
83-
shell: bash
84-
8580
- name: Prepare tests
8681
run: npm run test:init
8782
if: '${{ !steps.release-check.outputs.IS_RELEASE }}'
@@ -192,11 +187,6 @@ jobs:
192187
- name: Build project
193188
run: npm run build
194189

195-
- name: Generate self-signed certificates
196-
run: npm run certs
197-
if: '${{!steps.release-check.outputs.IS_RELEASE}}'
198-
shell: bash
199-
200190
- name: Prepare tests
201191
run: npm run test:init
202192
if: '${{ !steps.release-check.outputs.IS_RELEASE }}'

certconf

Lines changed: 0 additions & 8 deletions
This file was deleted.

package-lock.json

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
"scripts": {
3434
"_format": "prettier --loglevel=warn \"{src,tools,scripts,tests,.github}/**/*.{mjs,cjs,js,mts,md,yml,json,html,ts}\" \"*.{mjs,cjs,js,mts,yml,json,html,ts}\" \".*.{mjs,cjs,js,yml,json,html,ts}\" \"!CHANGELOG.md\" \"!**/*/package-lock.json\" \"!.github/**/*.md\"",
3535
"build": "tsc --project tsconfig.build.json",
36-
"certs": "openssl req -x509 -out localhost.crt -keyout localhost.key -newkey rsa:2048 -nodes -sha256 -subj \"/CN=localhost\" -extensions EXT -config certconf",
3736
"clean": "rm -rf dist/",
3837
"dev": "tsc --project tsconfig.build.json --watch",
3938
"docs": "npm run --prefix=site build",
@@ -44,7 +43,7 @@
4443
"test": "npm run test:dev",
4544
"lint": "eslint --cache \"{src,scripts,tests,.github}/**/*.{mjs,cjs,js,md,html}\" \"*.{mjs,cjs,js,md,html}\"",
4645
"lint:fix": "npm run lint --fix",
47-
"test:dev": "run-s certs test:init:* test:dev:*",
46+
"test:dev": "run-s test:init:* test:dev:*",
4847
"test:dev:vitest": "vitest run tests/unit/ && vitest run tests/integration",
4948
"test:init": "run-s test:init:*",
5049
"test:init:cli-help": "npm run start -- --help",
@@ -202,6 +201,7 @@
202201
"@vitest/eslint-plugin": "^1.1.36",
203202
"c8": "10.1.3",
204203
"cheerio": "1.0.0",
204+
"dedent": "^1.5.3",
205205
"eslint": "^9.21.0",
206206
"eslint-config-prettier": "^10.0.2",
207207
"eslint-plugin-n": "^17.16.1",

tests/integration/commands/dev/dev.zisi.test.ts

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
// Handlers are meant to be async outside tests
2-
import { Buffer } from 'buffer'
3-
import fs, { copyFile } from 'fs/promises'
2+
import { Buffer } from 'node:buffer'
3+
import fs from 'node:fs/promises'
44
import { Agent } from 'node:https'
5-
import os from 'os'
6-
import path from 'path'
7-
import { fileURLToPath } from 'url'
5+
import os from 'node:os'
6+
import path from 'node:path'
7+
import { fileURLToPath } from 'node:url'
88

99
import type { HandlerEvent } from '@netlify/functions'
1010
import { describe, test } from 'vitest'
1111
import nodeFetch from 'node-fetch'
12+
import execa from 'execa'
13+
import dedent from 'dedent'
1214

1315
import { curl } from '../../utils/curl.js'
1416
import { withDevServer } from '../../utils/dev-server.js'
@@ -19,6 +21,51 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url))
1921

2022
const testMatrix = [{ args: [] }]
2123

24+
const generateSSLCertificate = async () => {
25+
const tmpdir = await fs.mkdtemp(path.join(os.tmpdir(), 'certs-'))
26+
27+
await fs.writeFile(
28+
path.join(tmpdir, 'certconf'),
29+
dedent`
30+
[dn]
31+
CN=localhost
32+
[req]
33+
distinguished_name = dn
34+
[EXT]
35+
subjectAltName=DNS:localhost
36+
keyUsage=digitalSignature
37+
extendedKeyUsage=serverAuth
38+
`,
39+
)
40+
await execa(
41+
'openssl',
42+
[
43+
'req',
44+
'-x509',
45+
'-out',
46+
'localhost.crt',
47+
'-keyout',
48+
'localhost.key',
49+
'-newkey',
50+
'rsa:2048',
51+
'-nodes',
52+
'-sha256',
53+
'-subj',
54+
'/CN=localhost',
55+
'-extensions',
56+
'EXT',
57+
'-config',
58+
'certconf',
59+
],
60+
{ cwd: tmpdir },
61+
)
62+
63+
return {
64+
cert: path.join(tmpdir, 'localhost.crt'),
65+
key: path.join(tmpdir, 'localhost.key'),
66+
}
67+
}
68+
2269
describe.concurrent.each(testMatrix)('withSiteBuilder with args: $args', ({ args }) => {
2370
test('should handle query params in redirects', async (t) => {
2471
await withSiteBuilder(t, async (builder) => {
@@ -257,9 +304,10 @@ export const handler = async function () {
257304
})
258305
.build()
259306

307+
const certificatePaths = await generateSSLCertificate()
260308
await Promise.all([
261-
copyFile(path.join(__dirname, '../../../../localhost.crt'), path.join(builder.directory, 'localhost.crt')),
262-
copyFile(path.join(__dirname, '../../../../localhost.key'), path.join(builder.directory, 'localhost.key')),
309+
fs.copyFile(certificatePaths.cert, path.join(builder.directory, 'localhost.crt')),
310+
fs.copyFile(certificatePaths.key, path.join(builder.directory, 'localhost.key')),
263311
])
264312
await withDevServer({ cwd: builder.directory, args }, async ({ port }) => {
265313
const options = {

0 commit comments

Comments
 (0)