mirror of
https://github.com/AutoMaker-Org/automaker.git
synced 2026-02-03 08:53:36 +00:00
refactor: use environment variables for git configuration in test repositories
- Updated test repository creation functions to utilize environment variables for git author and committer information, preventing modifications to the user's global git configuration. - This change enhances test isolation and ensures consistent behavior across different environments.
This commit is contained in:
@@ -22,13 +22,21 @@ export async function createTestGitRepo(): Promise<TestRepo> {
|
|||||||
|
|
||||||
// Initialize git repo
|
// Initialize git repo
|
||||||
await execAsync('git init', { cwd: tmpDir });
|
await execAsync('git init', { cwd: tmpDir });
|
||||||
await execAsync('git config user.email "test@example.com"', { cwd: tmpDir });
|
|
||||||
await execAsync('git config user.name "Test User"', { cwd: tmpDir });
|
// Use environment variables instead of git config to avoid affecting user's git config
|
||||||
|
// These env vars override git config without modifying it
|
||||||
|
const gitEnv = {
|
||||||
|
...process.env,
|
||||||
|
GIT_AUTHOR_NAME: 'Test User',
|
||||||
|
GIT_AUTHOR_EMAIL: 'test@example.com',
|
||||||
|
GIT_COMMITTER_NAME: 'Test User',
|
||||||
|
GIT_COMMITTER_EMAIL: 'test@example.com',
|
||||||
|
};
|
||||||
|
|
||||||
// Create initial commit
|
// Create initial commit
|
||||||
await fs.writeFile(path.join(tmpDir, 'README.md'), '# Test Project\n');
|
await fs.writeFile(path.join(tmpDir, 'README.md'), '# Test Project\n');
|
||||||
await execAsync('git add .', { cwd: tmpDir });
|
await execAsync('git add .', { cwd: tmpDir, env: gitEnv });
|
||||||
await execAsync('git commit -m "Initial commit"', { cwd: tmpDir });
|
await execAsync('git commit -m "Initial commit"', { cwd: tmpDir, env: gitEnv });
|
||||||
|
|
||||||
// Create main branch explicitly
|
// Create main branch explicitly
|
||||||
await execAsync('git branch -M main', { cwd: tmpDir });
|
await execAsync('git branch -M main', { cwd: tmpDir });
|
||||||
|
|||||||
@@ -15,10 +15,8 @@ describe('worktree create route - repositories without commits', () => {
|
|||||||
async function initRepoWithoutCommit() {
|
async function initRepoWithoutCommit() {
|
||||||
repoPath = await fs.mkdtemp(path.join(os.tmpdir(), 'automaker-no-commit-'));
|
repoPath = await fs.mkdtemp(path.join(os.tmpdir(), 'automaker-no-commit-'));
|
||||||
await execAsync('git init', { cwd: repoPath });
|
await execAsync('git init', { cwd: repoPath });
|
||||||
await execAsync('git config user.email "test@example.com"', {
|
// Don't set git config - use environment variables in commit operations instead
|
||||||
cwd: repoPath,
|
// to avoid affecting user's git config
|
||||||
});
|
|
||||||
await execAsync('git config user.name "Test User"', { cwd: repoPath });
|
|
||||||
// Intentionally skip creating an initial commit
|
// Intentionally skip creating an initial commit
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -80,13 +80,21 @@ export async function createTestGitRepo(tempDir: string): Promise<TestRepo> {
|
|||||||
|
|
||||||
// Initialize git repo
|
// Initialize git repo
|
||||||
await execAsync('git init', { cwd: tmpDir });
|
await execAsync('git init', { cwd: tmpDir });
|
||||||
await execAsync('git config user.email "test@example.com"', { cwd: tmpDir });
|
|
||||||
await execAsync('git config user.name "Test User"', { cwd: tmpDir });
|
// Use environment variables instead of git config to avoid affecting user's git config
|
||||||
|
// These env vars override git config without modifying it
|
||||||
|
const gitEnv = {
|
||||||
|
...process.env,
|
||||||
|
GIT_AUTHOR_NAME: 'Test User',
|
||||||
|
GIT_AUTHOR_EMAIL: 'test@example.com',
|
||||||
|
GIT_COMMITTER_NAME: 'Test User',
|
||||||
|
GIT_COMMITTER_EMAIL: 'test@example.com',
|
||||||
|
};
|
||||||
|
|
||||||
// Create initial commit
|
// Create initial commit
|
||||||
fs.writeFileSync(path.join(tmpDir, 'README.md'), '# Test Project\n');
|
fs.writeFileSync(path.join(tmpDir, 'README.md'), '# Test Project\n');
|
||||||
await execAsync('git add .', { cwd: tmpDir });
|
await execAsync('git add .', { cwd: tmpDir, env: gitEnv });
|
||||||
await execAsync('git commit -m "Initial commit"', { cwd: tmpDir });
|
await execAsync('git commit -m "Initial commit"', { cwd: tmpDir, env: gitEnv });
|
||||||
|
|
||||||
// Create main branch explicitly
|
// Create main branch explicitly
|
||||||
await execAsync('git branch -M main', { cwd: tmpDir });
|
await execAsync('git branch -M main', { cwd: tmpDir });
|
||||||
@@ -248,9 +256,18 @@ export async function commitFile(
|
|||||||
content: string,
|
content: string,
|
||||||
message: string
|
message: string
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
|
// Use environment variables instead of git config to avoid affecting user's git config
|
||||||
|
const gitEnv = {
|
||||||
|
...process.env,
|
||||||
|
GIT_AUTHOR_NAME: 'Test User',
|
||||||
|
GIT_AUTHOR_EMAIL: 'test@example.com',
|
||||||
|
GIT_COMMITTER_NAME: 'Test User',
|
||||||
|
GIT_COMMITTER_EMAIL: 'test@example.com',
|
||||||
|
};
|
||||||
|
|
||||||
fs.writeFileSync(path.join(repoPath, filePath), content);
|
fs.writeFileSync(path.join(repoPath, filePath), content);
|
||||||
await execAsync(`git add "${filePath}"`, { cwd: repoPath });
|
await execAsync(`git add "${filePath}"`, { cwd: repoPath, env: gitEnv });
|
||||||
await execAsync(`git commit -m "${message}"`, { cwd: repoPath });
|
await execAsync(`git commit -m "${message}"`, { cwd: repoPath, env: gitEnv });
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user