-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support --env option for the runtime build (#1090)
- Loading branch information
1 parent
ee409bd
commit e81d252
Showing
11 changed files
with
358 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
import test from 'brittle'; | ||
import { EnvParser } from '../../src/env.js'; | ||
|
||
test('EnvParser should parse single key-value pair', function (t) { | ||
const parser = new EnvParser(); | ||
parser.parse('NODE_ENV=production'); | ||
|
||
t.alike(parser.getEnv(), { | ||
NODE_ENV: 'production', | ||
}); | ||
}); | ||
|
||
test('EnvParser should parse multiple comma-separated values', function (t) { | ||
const parser = new EnvParser(); | ||
parser.parse('NODE_ENV=production,DEBUG=true,PORT=3000'); | ||
|
||
t.alike(parser.getEnv(), { | ||
NODE_ENV: 'production', | ||
DEBUG: 'true', | ||
PORT: '3000', | ||
}); | ||
}); | ||
|
||
test('EnvParser should merge multiple parse calls', function (t) { | ||
const parser = new EnvParser(); | ||
parser.parse('NODE_ENV=production'); | ||
parser.parse('DEBUG=true'); | ||
|
||
t.alike(parser.getEnv(), { | ||
NODE_ENV: 'production', | ||
DEBUG: 'true', | ||
}); | ||
}); | ||
|
||
test('EnvParser should inherit existing environment variables', function (t) { | ||
const parser = new EnvParser(); | ||
|
||
// Set up some test environment variables | ||
process.env.TEST_VAR1 = 'value1'; | ||
process.env.TEST_VAR2 = 'value2'; | ||
|
||
parser.parse('TEST_VAR1,TEST_VAR2'); | ||
|
||
t.alike(parser.getEnv(), { | ||
TEST_VAR1: 'value1', | ||
TEST_VAR2: 'value2', | ||
}); | ||
|
||
// Cleanup | ||
delete process.env.TEST_VAR1; | ||
delete process.env.TEST_VAR2; | ||
}); | ||
|
||
test('EnvParser should handle mixed inheritance and setting', function (t) { | ||
const parser = new EnvParser(); | ||
|
||
process.env.TEST_VAR = 'inherited'; | ||
|
||
parser.parse('TEST_VAR,NEW_VAR=set'); | ||
|
||
t.alike(parser.getEnv(), { | ||
TEST_VAR: 'inherited', | ||
NEW_VAR: 'set', | ||
}); | ||
|
||
// Cleanup | ||
delete process.env.TEST_VAR; | ||
}); | ||
|
||
test('EnvParser should handle values with spaces', function (t) { | ||
const parser = new EnvParser(); | ||
parser.parse('MESSAGE=Hello World'); | ||
|
||
t.alike(parser.getEnv(), { | ||
MESSAGE: 'Hello World', | ||
}); | ||
}); | ||
|
||
test('EnvParser should handle values with equals signs', function (t) { | ||
const parser = new EnvParser(); | ||
parser.parse('DATABASE_URL=postgres://user:pass@localhost:5432/db'); | ||
|
||
t.alike(parser.getEnv(), { | ||
DATABASE_URL: 'postgres://user:pass@localhost:5432/db', | ||
}); | ||
}); | ||
|
||
test('EnvParser should handle empty values', function (t) { | ||
const parser = new EnvParser(); | ||
parser.parse('EMPTY='); | ||
|
||
t.alike(parser.getEnv(), { | ||
EMPTY: '', | ||
}); | ||
}); | ||
|
||
test('EnvParser should handle whitespace', function (t) { | ||
const parser = new EnvParser(); | ||
parser.parse(' KEY = value with spaces '); | ||
|
||
t.alike(parser.getEnv(), { | ||
KEY: ' value with spaces', // Leading whitespace preserved, trailing removed | ||
}); | ||
|
||
// Test multiple values with whitespace | ||
parser.parse(' KEY2 = value2 , KEY3 = value3 '); | ||
|
||
t.alike(parser.getEnv(), { | ||
KEY: ' value with spaces', | ||
KEY2: ' value2', | ||
KEY3: ' value3', | ||
}); | ||
}); | ||
|
||
test('EnvParser should merge and override values', function (t) { | ||
const parser = new EnvParser(); | ||
parser.parse('KEY=first'); | ||
parser.parse('KEY=second'); | ||
|
||
t.alike(parser.getEnv(), { | ||
KEY: 'second', | ||
}); | ||
}); | ||
|
||
test('EnvParser should throw on missing equal sign', function (t) { | ||
const parser = new EnvParser(); | ||
|
||
t.exception( | ||
() => parser.parse('INVALID_FORMAT'), | ||
'Invalid environment variable format: INVALID_FORMAT\nMust be in format KEY=VALUE', | ||
); | ||
}); | ||
|
||
test('EnvParser should throw on empty key', function (t) { | ||
const parser = new EnvParser(); | ||
|
||
t.exception( | ||
() => parser.parse('=value'), | ||
'Invalid environment variable format: =value\nMust be in format KEY=VALUE', | ||
); | ||
}); | ||
|
||
test('EnvParser should handle empty constructor', function (t) { | ||
const parser = new EnvParser(); | ||
|
||
t.alike(parser.getEnv(), {}); | ||
}); | ||
|
||
test('EnvParser should handle multiple commas and whitespace', function (t) { | ||
const parser = new EnvParser(); | ||
parser.parse('KEY1=value1, KEY2=value2,,,KEY3=value3'); | ||
|
||
t.alike(parser.getEnv(), { | ||
KEY1: 'value1', | ||
KEY2: 'value2', | ||
KEY3: 'value3', | ||
}); | ||
}); | ||
|
||
test('EnvParser should handle values containing escaped characters', function (t) { | ||
const parser = new EnvParser(); | ||
|
||
// This is how Node.js argv will receive it after shell processing | ||
parser.parse('A=VERBATIM CONTENTS\\, GO HERE'); // Users will type: --env 'A=VERBATIM CONTENTS\, GO HERE' | ||
|
||
t.alike(parser.getEnv(), { | ||
A: 'VERBATIM CONTENTS, GO HERE', // Comma should be unescaped in final value | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,28 @@ | ||
/* eslint-env serviceworker */ | ||
import { env } from 'fastly:env'; | ||
import { routes, isRunningLocally } from './routes.js'; | ||
import { assert } from './assertions.js'; | ||
import { strictEqual } from './assertions.js'; | ||
|
||
// hostname didn't exist at initialization, so can still be captured at runtime | ||
const wizerHostname = env('FASTLY_HOSTNAME'); | ||
const wizerLocal = env('LOCAL_TEST'); | ||
|
||
routes.set('/env', () => { | ||
strictEqual(wizerHostname, undefined); | ||
|
||
if (isRunningLocally()) { | ||
assert( | ||
strictEqual( | ||
env('FASTLY_HOSTNAME'), | ||
'localhost', | ||
`env("FASTLY_HOSTNAME") === "localhost"`, | ||
); | ||
} else { | ||
strictEqual(env('FASTLY_HOSTNAME'), undefined); | ||
} | ||
|
||
strictEqual(wizerLocal, 'local val'); | ||
|
||
// at runtime these remain captured from Wizer time, even if we didn't call env | ||
strictEqual(env('LOCAL_TEST'), 'local val'); | ||
strictEqual(env('TEST'), 'foo'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.