Skip to content
Draft
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
58 changes: 58 additions & 0 deletions src/utils/__tests__/env-api-key.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from 'fs';
import path from 'path';
import { tmpdir } from 'os';
import { readApiKeyFromEnv } from '@utils/env-api-key';

describe('readApiKeyFromEnv', () => {
let tmpDir: string;
let cwdSpy: jest.SpyInstance;

beforeEach(() => {
tmpDir = mkdtempSync(path.join(tmpdir(), 'wizard-env-test-'));
cwdSpy = jest.spyOn(process, 'cwd').mockReturnValue(tmpDir);
});

afterEach(() => {
cwdSpy.mockRestore();
rmSync(tmpDir, { recursive: true, force: true });
});

it('returns undefined when no env file exists', () => {
expect(readApiKeyFromEnv()).toBeUndefined();
});

it('reads the key from .env', () => {
writeFileSync(
path.join(tmpDir, '.env'),
'POSTHOG_PERSONAL_API_KEY=phx_from_env\n',
);
expect(readApiKeyFromEnv()).toBe('phx_from_env');
});

it('prefers .env.local over .env', () => {
writeFileSync(
path.join(tmpDir, '.env'),
'POSTHOG_PERSONAL_API_KEY=phx_from_env\n',
);
writeFileSync(
path.join(tmpDir, '.env.local'),
'POSTHOG_PERSONAL_API_KEY=phx_from_local\n',
);
expect(readApiKeyFromEnv()).toBe('phx_from_local');
});

it('does not crash when .env is a directory (e.g. a Python virtualenv)', () => {
mkdirSync(path.join(tmpDir, '.env'));
expect(() => readApiKeyFromEnv()).not.toThrow();
expect(readApiKeyFromEnv()).toBeUndefined();
});

it('falls back to .env when .env.local is a directory', () => {
mkdirSync(path.join(tmpDir, '.env.local'));
writeFileSync(
path.join(tmpDir, '.env'),
'POSTHOG_PERSONAL_API_KEY=phx_from_env\n',
);
expect(readApiKeyFromEnv()).toBe('phx_from_env');
});
});
19 changes: 13 additions & 6 deletions src/utils/env-api-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@ export function readApiKeyFromEnv(): string | undefined {
const envFiles = ['.env.local', '.env'];
for (const envFile of envFiles) {
const envPath = path.join(process.cwd(), envFile);
if (fs.existsSync(envPath)) {
const content = fs.readFileSync(envPath, 'utf8');
const match = content.match(/^POSTHOG_PERSONAL_API_KEY=(.+)$/m);
if (match) {
return match[1].trim();
}
let content: string;
try {
// `.env`/`.env.local` is occasionally a directory (e.g. a Python
// virtualenv named `.env`), so read directly and skip on failure rather
// than guarding with existsSync, which returns true for directories and
// lets readFileSync throw EISDIR.
content = fs.readFileSync(envPath, 'utf8');
} catch {
continue;
}
const match = content.match(/^POSTHOG_PERSONAL_API_KEY=(.+)$/m);
if (match) {
return match[1].trim();
}
}
return undefined;
Expand Down
Loading