diff --git a/Earthfile b/Earthfile index dcb89f119..9c05b36a6 100644 --- a/Earthfile +++ b/Earthfile @@ -206,7 +206,8 @@ npm-test: FROM +test-sources WORKDIR /repo ENV CI=true - RUN --secret FORMENGINE_TEST_LICENSE_KEY --secret FORMENGINE_TEST_LICENSE_KEYS_JSON --secret FORMENGINE_FORMS_WITH_VERSIONS_LICENSE_KEY \ + # Secret values are not part of the cache key. Load them afresh for each run. + RUN --no-cache --secret FORMENGINE_TEST_LICENSE_KEY --secret FORMENGINE_TEST_LICENSE_KEYS_JSON --secret FORMENGINE_FORMS_WITH_VERSIONS_LICENSE_KEY \ node scripts/write-test-license-env.ts TRY RUN npm run test:module @@ -266,7 +267,7 @@ npm-e2e: ENV PLAYWRIGHT_SKIP_VALIDATE_HOST_REQUIREMENTS=1 COPY +test-sources/repo+ /repo WORKDIR /repo - RUN --secret FORMENGINE_TEST_LICENSE_KEY --secret FORMENGINE_TEST_LICENSE_KEYS_JSON --secret FORMENGINE_FORMS_WITH_VERSIONS_LICENSE_KEY \ + RUN --no-cache --secret FORMENGINE_TEST_LICENSE_KEY --secret FORMENGINE_TEST_LICENSE_KEYS_JSON --secret FORMENGINE_FORMS_WITH_VERSIONS_LICENSE_KEY \ node scripts/write-test-license-env.ts RUN --no-cache npm run test:e2e; echo $? > /tmp/npm-e2e-exit-code WAIT diff --git a/scripts/write-test-license-env.ts b/scripts/write-test-license-env.ts index b7986584e..5b68c24eb 100644 --- a/scripts/write-test-license-env.ts +++ b/scripts/write-test-license-env.ts @@ -3,7 +3,7 @@ /* eslint-disable no-console */ /** - * Writes root `.env` for CI tests from GitHub Actions / Earthly secrets (test keys only). + * Writes root `.env` for CI tests from optional GitHub Actions / Earthly test license secrets. * Deploy keys (demo/example) are not written here. */ @@ -31,17 +31,13 @@ function quoteDotenvValue(value: string): string { } const repoRoot = process.cwd() -const testKey = process.env[ENV_TEST]?.trim() const matrixRaw = process.env[ENV_MATRIX]?.trim() -if (!testKey || !matrixRaw) { - console.error(`❌ ${ENV_TEST} and ${ENV_MATRIX} must be set for CI tests`) - process.exit(1) -} - try { - const keys = parseTestLicenseMatrix(matrixRaw) - assertTestLicenseMatrixComplete(keys, discoverTestLicenseExportNames(repoRoot)) + if (matrixRaw) { + const keys = parseTestLicenseMatrix(matrixRaw) + assertTestLicenseMatrixComplete(keys, discoverTestLicenseExportNames(repoRoot)) + } } catch (error) { console.error(`❌ ${error instanceof Error ? error.message : String(error)}`) process.exit(1) @@ -57,4 +53,4 @@ for (const envName of GHA_TEST_LICENSE_ENV_NAMES) { } writeFileSync(join(repoRoot, '.env'), `${lines.join('\n')}\n`, 'utf8') -console.log(`Wrote ${join(repoRoot, '.env')} (test license keys only)`) +console.log(`Wrote ${join(repoRoot, '.env')} for CI tests`) diff --git a/tests/module/tests/write-test-license-env.spec.ts b/tests/module/tests/write-test-license-env.spec.ts new file mode 100644 index 000000000..75166ca41 --- /dev/null +++ b/tests/module/tests/write-test-license-env.spec.ts @@ -0,0 +1,62 @@ +import {spawnSync} from 'node:child_process' +import {mkdir, mkdtemp, readFile, rm, writeFile} from 'node:fs/promises' +import {tmpdir} from 'node:os' +import {join} from 'node:path' +import {fileURLToPath} from 'node:url' +import {afterEach, beforeEach, describe, expect, it} from 'vitest' + +const script = fileURLToPath(new URL('../../../scripts/write-test-license-env.ts', import.meta.url)) + +describe('write-test-license-env', () => { + let repoDir: string + + beforeEach(async () => { + repoDir = await mkdtemp(join(tmpdir(), 'formengine-test-license-env-')) + }) + + afterEach(async () => { + await rm(repoDir, {recursive: true, force: true}) + }) + + const run = (env: NodeJS.ProcessEnv = {}) => + spawnSync(process.execPath, [script], { + cwd: repoDir, + encoding: 'utf8', + env: { + ...process.env, + NODE_OPTIONS: '', + FORMENGINE_TEST_LICENSE_KEY: '', + FORMENGINE_TEST_LICENSE_KEYS_JSON: '', + FORMENGINE_FORMS_WITH_VERSIONS_LICENSE_KEY: '', + ...env, + }, + }) + + it('writes a clean environment file when CI provides no test licenses', async () => { + const envFile = join(repoDir, '.env') + await writeFile(envFile, 'FORMENGINE_TEST_LICENSE_KEY=old-fixture\n') + + const result = run() + + expect(result.status, result.stderr).toBe(0) + expect(await readFile(envFile, 'utf8')).not.toContain('FORMENGINE_TEST_LICENSE_') + }) + + it('writes the standalone test key when the matrix is absent', async () => { + const result = run({FORMENGINE_TEST_LICENSE_KEY: 'test-fixture'}) + + expect(result.status, result.stderr).toBe(0) + expect(await readFile(join(repoDir, '.env'), 'utf8')).toContain("FORMENGINE_TEST_LICENSE_KEY='test-fixture'") + }) + + it('validates a provided matrix even when the standalone key is absent', async () => { + const fixtureDir = join(repoDir, 'tests/common') + await mkdir(fixtureDir, {recursive: true}) + await writeFile(join(fixtureDir, 'test-licenses.ts'), 'export const testLicense = __FORMENGINE_TEST_LICENSE_testLicense__\n') + + const result = run({FORMENGINE_TEST_LICENSE_KEYS_JSON: '{"keys":{}}'}) + + expect(result.status).toBe(1) + expect(result.stderr).toContain('FORMENGINE_TEST_LICENSE_KEYS_JSON missing non-empty keys for: testLicense') + }) +})