0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

fix(astro): getSecret in dev and build (#11198)

Co-authored-by: Paul Valladares <85648028+dreyfus92@users.noreply.github.com>
This commit is contained in:
Florian Lefebvre 2024-06-06 22:19:44 +02:00 committed by GitHub
parent 48d53094cd
commit 8b9a499d37
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 47 additions and 6 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes a case where `astro:env` `getSecret` would not retrieve environment variables properly in dev and build modes

View file

@ -47,6 +47,12 @@ export function astroEnv({
fileURLToPath(settings.config.root),
''
);
for (const [key, value] of Object.entries(loadedEnv)) {
if (value !== undefined) {
process.env[key] = value;
}
}
const validatedVariables = validatePublicVariables({ schema, loadedEnv });
const clientTemplates = getClientTemplates({ validatedVariables });

View file

@ -1,17 +1,38 @@
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import { after, describe, it } from 'node:test';
import * as cheerio from 'cheerio';
import testAdapter from './test-adapter.js';
import { loadFixture } from './test-utils.js';
import { writeFileSync } from 'node:fs';
describe('astro:env public variables', () => {
/** @type {Awaited<ReturnType<typeof loadFixture>>} */
let fixture;
/** @type {Awaited<ReturnType<(typeof fixture)["loadTestAdapterApp"]>>} */
let app;
/** @type {Awaited<ReturnType<(typeof fixture)["startDevServer"]>>} */
let devServer = undefined;
describe('Server variables', () => {
before(async () => {
after(async () => {
await devServer?.stop();
});
it('works in dev', async () => {
writeFileSync(
new URL('./fixtures/astro-env-server-secret/.env', import.meta.url),
'KNOWN_SECRET=5',
'utf-8'
);
fixture = await loadFixture({
root: './fixtures/astro-env-server-secret/',
});
devServer = await fixture.startDevServer();
const response = await fixture.fetch('/');
assert.equal(response.status, 200);
});
it('builds without throwing', async () => {
fixture = await loadFixture({
root: './fixtures/astro-env-server-secret/',
output: 'server',
@ -24,13 +45,22 @@ describe('astro:env public variables', () => {
});
await fixture.build();
app = await fixture.loadTestAdapterApp();
});
it('builds without throwing', async () => {
assert.equal(true, true);
});
it('adapter can set how env is retrieved', async () => {
fixture = await loadFixture({
root: './fixtures/astro-env-server-secret/',
output: 'server',
adapter: testAdapter({
env: {
KNOWN_SECRET: '123456',
UNKNOWN_SECRET: 'abc',
},
}),
});
await fixture.build();
app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/');
const response = await app.render(request);
assert.equal(response.status, 200);

View file

@ -5,7 +5,7 @@ export default defineConfig({
experimental: {
env: {
schema: {
KNOWN_SECRET: envField.number({ context: "server", access: "secret", optional: true })
KNOWN_SECRET: envField.number({ context: "server", access: "secret" })
}
}
}