Skip to content
Open
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
17 changes: 17 additions & 0 deletions src/lib/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ describe('loadConfig', () => {
).toBe('option-profile');
});

it('treats empty TESTSPRITE_PROFILE as unset (falls back to default)', () => {
expect(loadConfig({ env: { TESTSPRITE_PROFILE: '' }, credentialsPath }).profile).toBe(
'default',
);
});

it('treats whitespace-only TESTSPRITE_PROFILE as unset (falls back to default)', () => {
const config = loadConfig({ env: { TESTSPRITE_PROFILE: ' ' }, credentialsPath });
expect(config.profile).toBe('default');
});

it('reads credentials from the default profile when TESTSPRITE_PROFILE is blank', () => {
writeProfile('default', { apiKey: 'sk-default' }, { path: credentialsPath });
const config = loadConfig({ env: { TESTSPRITE_PROFILE: ' ' }, credentialsPath });
expect(config.apiKey).toBe('sk-default');
});

it('option.endpointUrl overrides everything', () => {
writeProfile('default', { apiUrl: 'https://file' }, { path: credentialsPath });
const config = loadConfig({
Expand Down
10 changes: 7 additions & 3 deletions src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,17 @@ export function defaultConfigPath(): string {
*/
export function loadConfig(options: LoadConfigOptions = {}): Config {
const env = options.env ?? process.env;
const profile = options.profile ?? env.TESTSPRITE_PROFILE ?? DEFAULT_PROFILE;
const profile = options.profile ?? normalizeEnvVar(env.TESTSPRITE_PROFILE) ?? DEFAULT_PROFILE;
const credentialsPath = options.credentialsPath ?? defaultCredentialsPath();
const fileEntry = readProfile(profile, { path: credentialsPath });

// Empty / whitespace-only env vars are treated as unset so they do not
// short-circuit the `??` chain (e.g. `export TESTSPRITE_API_URL=` in a shell
// profile). Matches the normalization in auth configure and init/setup.
// short-circuit the `??` chain (e.g. `export TESTSPRITE_API_URL=` or
// `export TESTSPRITE_PROFILE=` in a shell profile). For the profile this
// also avoids a confusing VALIDATION_ERROR: an empty name fails the INI
// section-name guard, so without normalization a blank env var would break
// every command instead of falling back to the default profile. Matches the
// normalization in auth configure and init/setup.
const envApiUrl = normalizeEnvVar(env.TESTSPRITE_API_URL);
const envApiKey = normalizeEnvVar(env.TESTSPRITE_API_KEY);

Expand Down
Loading