Fix directory not found exception when deleting stale repository (#136)

This commit is contained in:
Brendan Kellam 2024-12-16 20:23:42 -08:00 committed by GitHub
parent 1cc9320a30
commit d4e72566e2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 3 deletions

View file

@ -15,6 +15,10 @@ vi.mock('glob', () => ({
glob: vi.fn().mockReturnValue(['fake_index.zoekt']), glob: vi.fn().mockReturnValue(['fake_index.zoekt']),
})); }));
vi.mock('fs', () => ({
existsSync: vi.fn().mockReturnValue(true),
}));
const createMockContext = (rootPath: string = '/app') => { const createMockContext = (rootPath: string = '/app') => {
return { return {
configPath: path.join(rootPath, 'config.json'), configPath: path.join(rootPath, 'config.json'),
@ -159,7 +163,7 @@ test('deleteStaleRepository can delete a git repository', async () => {
await deleteStaleRepository(repo, db, ctx); await deleteStaleRepository(repo, db, ctx);
expect(db.data.repos['github.com/sourcebot-dev/sourcebot']).toBeUndefined();; expect(db.data.repos['github.com/sourcebot-dev/sourcebot']).toBeUndefined();
expect(rm).toHaveBeenCalledWith(`${ctx.reposPath}/github.com/sourcebot-dev/sourcebot`, { expect(rm).toHaveBeenCalledWith(`${ctx.reposPath}/github.com/sourcebot-dev/sourcebot`, {
recursive: true, recursive: true,
}); });

View file

@ -73,10 +73,10 @@ export const deleteStaleRepository = async (repo: Repository, db: Database, ctx:
logger.info(`Deleting stale repository ${repo.id}:`); logger.info(`Deleting stale repository ${repo.id}:`);
// Delete the checked out git repository (if applicable) // Delete the checked out git repository (if applicable)
if (repo.vcs === "git") { if (repo.vcs === "git" && existsSync(repo.path)) {
logger.info(`\tDeleting git directory ${repo.path}...`); logger.info(`\tDeleting git directory ${repo.path}...`);
await rm(repo.path, { await rm(repo.path, {
recursive: true recursive: true,
}); });
} }
@ -116,6 +116,10 @@ export const deleteStaleRepository = async (repo: Repository, db: Database, ctx:
}); });
await Promise.all(indexFiles.map((file) => { await Promise.all(indexFiles.map((file) => {
if (!existsSync(file)) {
return;
}
logger.info(`\tDeleting index file ${file}...`); logger.info(`\tDeleting index file ${file}...`);
return rm(file); return rm(file);
})); }));