Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 6 additions & 10 deletions scripts/write-test-license-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/

Expand Down Expand Up @@ -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)
Expand All @@ -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`)
62 changes: 62 additions & 0 deletions tests/module/tests/write-test-license-env.spec.ts
Original file line number Diff line number Diff line change
@@ -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')
})
})