(
repoConfig: {
repoUrl: string
// The sha of the commit to checkout. If you have a commit with changes to replicate, you would check out the parent commit.
parentSha: string
initCommand?: string
env?: Record<string, string>
},
fn: (cwd: string) => Promise<T>,
)
| 10 | * Sets up a test repo, runs a function with the repo cwd, then cleans up |
| 11 | */ |
| 12 | export const withTestRepo = async <T>( |
| 13 | repoConfig: { |
| 14 | repoUrl: string |
| 15 | // The sha of the commit to checkout. If you have a commit with changes to replicate, you would check out the parent commit. |
| 16 | parentSha: string |
| 17 | initCommand?: string |
| 18 | env?: Record<string, string> |
| 19 | }, |
| 20 | fn: (cwd: string) => Promise<T>, |
| 21 | ): Promise<T> => { |
| 22 | const { repoUrl, parentSha, initCommand, env } = repoConfig |
| 23 | |
| 24 | // Create a temporary directory for the test repo |
| 25 | const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codebuff-eval-')) |
| 26 | const repoDir = path.join(tempDir, 'repo') |
| 27 | |
| 28 | try { |
| 29 | execSync(`git clone --depth 1 ${repoUrl} ${repoDir}`, { stdio: 'ignore' }) |
| 30 | |
| 31 | execSync(`git fetch --depth 1 origin ${parentSha}`, { |
| 32 | cwd: repoDir, |
| 33 | stdio: 'ignore', |
| 34 | }) |
| 35 | execSync(`git checkout ${parentSha}`, { cwd: repoDir, stdio: 'ignore' }) |
| 36 | |
| 37 | if (initCommand) { |
| 38 | console.log(`Running init command: ${initCommand}...`) |
| 39 | try { |
| 40 | execSync(initCommand, { |
| 41 | cwd: repoDir, |
| 42 | stdio: 'ignore', |
| 43 | env: { ...process.env, ...env }, |
| 44 | }) |
| 45 | } catch (error) { |
| 46 | console.error( |
| 47 | `Error running init command: ${getErrorObject(error).message}`, |
| 48 | ) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // Run the provided function with the repo directory |
| 53 | return await fn(repoDir) |
| 54 | } finally { |
| 55 | // Clean up the temporary directory |
| 56 | try { |
| 57 | fs.rmSync(tempDir, { recursive: true, force: true }) |
| 58 | } catch (error) { |
| 59 | console.warn(`Failed to clean up temporary directory: ${error}`) |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | export const withTestRepoAndParent = async <T>( |
| 65 | repoConfig: { |
no test coverage detected