Skip to content

Commit 8240afc

Browse files
committed
refactor: improve environment variable handling and test cleanup
- Move envConfigs to global scope for better reusability - Add clearCoDevelopedByEnvVars utility function for centralized environment variable cleanup - Update tests to use the new utility function instead of manual delete operations - Improve code organization and maintainability - All tests continue to pass successfully This refactoring makes the code more maintainable and provides a centralized way to manage environment variables used by the getCoDevelopedBy function. Change-Id: Iac187f3977cdbffb28af9b316717288b50001045 Co-developed-by: Cursor <noreply@cursor.com>
1 parent d806f52 commit 8240afc

2 files changed

Lines changed: 35 additions & 21 deletions

File tree

src/commands/exec.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,35 @@
55
import * as fs from 'fs';
66
import { spawnSync } from 'child_process';
77

8+
// Define environment variable configurations and their corresponding CoDevelopedBy values
9+
// Format: ["key=value", "co-developed-by-string"]
10+
const envConfigs: [string, string][] = [
11+
// We can run CLI in IDE (such as Cursor and Qoder), so check CLI env variables first
12+
['CLAUDECODE=1', 'Claude <noreply@anthropic.com>'],
13+
['GEMINI_CLI=1', 'Gemini <noreply@developers.google.com>'],
14+
// Check env variables for IDEs
15+
['VSCODE_BRAND=Qoder', 'Qoder <noreply@qoder.com>'],
16+
['CURSOR_TRACE_ID=*', 'Cursor <noreply@cursor.com>'],
17+
];
18+
19+
/**
20+
* Clear all environment variables used by getCoDevelopedBy function
21+
* This is useful for testing to ensure clean state
22+
*/
23+
function clearCoDevelopedByEnvVars(): void {
24+
for (const [envConfig] of envConfigs) {
25+
const equalIndex = envConfig.indexOf('=');
26+
if (equalIndex === -1) {
27+
// No '=' found, just a key
28+
delete process.env[envConfig];
29+
} else {
30+
// Split into key and value
31+
const key = envConfig.substring(0, equalIndex);
32+
delete process.env[key];
33+
}
34+
}
35+
}
36+
837
async function exec(messageFile: string): Promise<void> {
938
console.log(`Executing commit-msg hook on file: ${messageFile}`);
1039

@@ -147,17 +176,6 @@ function isMergeCommit(): boolean {
147176
* @returns The CoDevelopedBy value or empty string if not configured
148177
*/
149178
function getCoDevelopedBy(): string {
150-
// Define environment variable configurations and their corresponding CoDevelopedBy values
151-
// Format: ["key=value", "co-developed-by-string"]
152-
const envConfigs: [string, string][] = [
153-
// We can run CLI in IDE (such as Cursor and Qoder), so check CLI env variables first
154-
['CLAUDECODE=1', 'Claude <noreply@anthropic.com>'],
155-
['GEMINI_CLI=1', 'Gemini <noreply@developers.google.com>'],
156-
// Check env variables for IDEs
157-
['VSCODE_BRAND=Qoder', 'Qoder <noreply@qoder.com>'],
158-
['CURSOR_TRACE_ID=*', 'Cursor <noreply@cursor.com>'],
159-
];
160-
161179
// Check each environment configuration in order
162180
for (const [envConfig, coDevelopedBy] of envConfigs) {
163181
// Parse the environment configuration
@@ -715,4 +733,5 @@ export {
715733
isMergeCommit,
716734
hasCoDevelopedBy,
717735
needsCoDevelopedBy,
736+
clearCoDevelopedByEnvVars,
718737
};

test/commands/exec.test.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
getCoDevelopedBy,
1111
hasCoDevelopedBy,
1212
needsCoDevelopedBy,
13+
clearCoDevelopedByEnvVars,
1314
} from '../../src/commands/exec';
1415

1516
describe('exec command utilities', () => {
@@ -513,11 +514,8 @@ describe('exec command utilities', () => {
513514
});
514515

515516
it('should return empty string when environment variables are set to incorrect values', () => {
516-
// Explicitly unset all environment variables we're testing
517-
delete process.env.CLAUDECODE;
518-
delete process.env.GEMINI_CLI;
519-
delete process.env.VSCODE_BRAND;
520-
delete process.env.CURSOR_TRACE_ID;
517+
// Clear all environment variables using the utility function
518+
clearCoDevelopedByEnvVars();
521519

522520
process.env.CLAUDECODE = '0';
523521
process.env.GEMINI_CLI = '0';
@@ -526,11 +524,8 @@ describe('exec command utilities', () => {
526524
});
527525

528526
it('should return empty string when environment variables exist but have no value', () => {
529-
// Explicitly unset all environment variables we're testing
530-
delete process.env.CLAUDECODE;
531-
delete process.env.GEMINI_CLI;
532-
delete process.env.VSCODE_BRAND;
533-
delete process.env.CURSOR_TRACE_ID;
527+
// Clear all environment variables using the utility function
528+
clearCoDevelopedByEnvVars();
534529

535530
process.env.CLAUDECODE = '';
536531
expect(getCoDevelopedBy()).toBe('');

0 commit comments

Comments
 (0)