fix(qenv): Handle falsy environment values correctly, improve env source resolution, add tests and update test script
This commit is contained in:
119
test/test.falsy.ts
Normal file
119
test/test.falsy.ts
Normal file
@@ -0,0 +1,119 @@
|
||||
import * as path from 'path';
|
||||
import { tap, expect } from '@git.zone/tstest/tapbundle';
|
||||
import * as qenv from '../ts/index.js';
|
||||
|
||||
const testDir = path.dirname(new URL(import.meta.url).pathname);
|
||||
|
||||
// Test falsy values handling
|
||||
tap.test('should handle falsy values correctly', async () => {
|
||||
// Set up environment variables with falsy values
|
||||
process.env['FALSY_FALSE'] = 'false';
|
||||
process.env['FALSY_ZERO'] = '0';
|
||||
process.env['FALSY_EMPTY'] = '';
|
||||
|
||||
const testQenv = new qenv.Qenv(testDir, testDir, false);
|
||||
|
||||
// Test that falsy values are returned, not undefined
|
||||
expect(await testQenv.getEnvVarOnDemand('FALSY_FALSE')).toEqual('false');
|
||||
expect(await testQenv.getEnvVarOnDemand('FALSY_ZERO')).toEqual('0');
|
||||
expect(await testQenv.getEnvVarOnDemand('FALSY_EMPTY')).toEqual('');
|
||||
|
||||
// Test sync versions
|
||||
expect(testQenv.getEnvVarOnDemandSync('FALSY_FALSE')).toEqual('false');
|
||||
expect(testQenv.getEnvVarOnDemandSync('FALSY_ZERO')).toEqual('0');
|
||||
expect(testQenv.getEnvVarOnDemandSync('FALSY_EMPTY')).toEqual('');
|
||||
|
||||
// Test that undefined is still returned for non-existent variables
|
||||
expect(await testQenv.getEnvVarOnDemand('NON_EXISTENT')).toBeUndefined();
|
||||
expect(testQenv.getEnvVarOnDemandSync('NON_EXISTENT')).toBeUndefined();
|
||||
|
||||
// Clean up
|
||||
delete process.env['FALSY_FALSE'];
|
||||
delete process.env['FALSY_ZERO'];
|
||||
delete process.env['FALSY_EMPTY'];
|
||||
});
|
||||
|
||||
tap.test('should handle falsy values in env.json file', async () => {
|
||||
// Create a test env.json with falsy values
|
||||
const testAssetsDir = path.join(testDir, 'assets-falsy');
|
||||
const fs = await import('fs');
|
||||
|
||||
// Create directory if it doesn't exist
|
||||
if (!fs.existsSync(testAssetsDir)) {
|
||||
fs.mkdirSync(testAssetsDir, { recursive: true });
|
||||
}
|
||||
|
||||
// Create env.json with falsy values
|
||||
const envJsonContent = {
|
||||
JSON_FALSE: false,
|
||||
JSON_ZERO: 0,
|
||||
JSON_EMPTY: ''
|
||||
};
|
||||
|
||||
fs.writeFileSync(
|
||||
path.join(testAssetsDir, 'env.json'),
|
||||
JSON.stringify(envJsonContent, null, 2)
|
||||
);
|
||||
|
||||
const testQenv = new qenv.Qenv(testAssetsDir, testAssetsDir, false);
|
||||
|
||||
// Test that falsy values from JSON are returned correctly
|
||||
expect(await testQenv.getEnvVarOnDemand('JSON_FALSE')).toEqual('false');
|
||||
expect(await testQenv.getEnvVarOnDemand('JSON_ZERO')).toEqual('0');
|
||||
expect(await testQenv.getEnvVarOnDemand('JSON_EMPTY')).toEqual('');
|
||||
|
||||
// Clean up
|
||||
fs.rmSync(testAssetsDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
tap.test('should throw error for undefined in strict mode', async () => {
|
||||
const testQenv = new qenv.Qenv(testDir, testDir, false);
|
||||
|
||||
// Set a falsy value
|
||||
process.env['FALSY_VALUE'] = '0';
|
||||
|
||||
// Should NOT throw for falsy value
|
||||
let result;
|
||||
try {
|
||||
result = await testQenv.getEnvVarOnDemandStrict('FALSY_VALUE');
|
||||
} catch (error) {
|
||||
// Should not reach here
|
||||
expect(true).toBeFalse();
|
||||
}
|
||||
expect(result).toEqual('0');
|
||||
|
||||
// Should throw for non-existent variable
|
||||
let threwError = false;
|
||||
try {
|
||||
await testQenv.getEnvVarOnDemandStrict('NON_EXISTENT_VAR');
|
||||
} catch (error) {
|
||||
threwError = true;
|
||||
expect(error.message).toContain('is not set');
|
||||
}
|
||||
expect(threwError).toBeTrue();
|
||||
|
||||
// Clean up
|
||||
delete process.env['FALSY_VALUE'];
|
||||
});
|
||||
|
||||
tap.test('should handle array of env vars with falsy values', async () => {
|
||||
const testQenv = new qenv.Qenv(testDir, testDir, false);
|
||||
|
||||
// Set up test environment
|
||||
process.env['FIRST_VAR'] = '0';
|
||||
process.env['SECOND_VAR'] = 'false';
|
||||
|
||||
// Test that it returns the first defined value, even if falsy
|
||||
const result = await testQenv.getEnvVarOnDemand(['NON_EXISTENT', 'FIRST_VAR', 'SECOND_VAR']);
|
||||
expect(result).toEqual('0');
|
||||
|
||||
// Test sync version
|
||||
const resultSync = testQenv.getEnvVarOnDemandSync(['NON_EXISTENT', 'FIRST_VAR', 'SECOND_VAR']);
|
||||
expect(resultSync).toEqual('0');
|
||||
|
||||
// Clean up
|
||||
delete process.env['FIRST_VAR'];
|
||||
delete process.env['SECOND_VAR'];
|
||||
});
|
||||
|
||||
export default tap.start();
|
Reference in New Issue
Block a user