Skip to content

Commit 18c57de

Browse files
author
Your Name
committed
refactor: enhance testCloneRepository function with git executable handling and temporary directory management
1 parent 573da04 commit 18c57de

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

internal/stage_clone_repository.go

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package internal
22

33
import (
44
"fmt"
5+
"io"
56
"os"
7+
"os/exec"
68
"path"
79

810
"github.com/codecrafters-io/tester-utils/random"
@@ -31,7 +33,7 @@ func (r TestRepo) randomFile() TestFile {
3133
return r.exampleFiles[random.RandomInt(0, len(r.exampleFiles))]
3234
}
3335

34-
var testRepos []TestRepo = []TestRepo{
36+
var testRepos = []TestRepo{
3537
{
3638
url: "https://github.com/codecrafters-io/git-sample-1",
3739
exampleCommits: []string{
@@ -90,6 +92,54 @@ func testCloneRepository(harness *test_case_harness.TestCaseHarness) error {
9092
testRepo := randomRepo()
9193

9294
logger.Infof("$ ./%s clone %s <testDir>", path.Base(executable.Path), testRepo.url)
95+
96+
gitPath, err := exec.LookPath("git")
97+
if err != nil {
98+
return fmt.Errorf("git executable not found: %v", err)
99+
}
100+
logger.Debugf("Found git executable at: %s", gitPath)
101+
102+
gitDir, err := os.MkdirTemp("/tmp", "git-*")
103+
if err != nil {
104+
return err
105+
}
106+
logger.Debugf("Created temporary directory for git clone: %s", gitDir)
107+
defer os.RemoveAll(gitDir)
108+
109+
// Copy the custom_executable to the output path
110+
command := fmt.Sprintf("cp %s %s", gitPath, gitDir)
111+
logger.Debugf("Cp-ed git to temp directory: %s", gitDir)
112+
//fmt.Println(command)
113+
copyCmd := exec.Command("sh", "-c", command)
114+
copyCmd.Stdout = io.Discard
115+
copyCmd.Stderr = io.Discard
116+
if err := copyCmd.Run(); err != nil {
117+
return fmt.Errorf("CodeCrafters Internal Error: cp failed: %w", err)
118+
}
119+
120+
defer func() error {
121+
// Copy the custom_executable to the output path
122+
command := fmt.Sprintf("cp %s %s", gitDir, gitPath)
123+
logger.Debugf("Cp-ed git to temp directory: %s", gitPath)
124+
//fmt.Println(command)
125+
copyCmd := exec.Command("sh", "-c", command)
126+
copyCmd.Stdout = io.Discard
127+
copyCmd.Stderr = io.Discard
128+
129+
if err := copyCmd.Run(); err != nil {
130+
return fmt.Errorf("CodeCrafters Internal Error: cp failed: %w", err)
131+
}
132+
133+
return nil
134+
}()
135+
136+
defer func() error {
137+
if err := os.RemoveAll(gitDir); err != nil {
138+
return fmt.Errorf("CodeCrafters Internal Error: delete directory failed: %s", gitDir)
139+
}
140+
return nil
141+
}()
142+
93143
result, err := executable.Run("clone", testRepo.url, "test_dir")
94144
if err != nil {
95145
return err

0 commit comments

Comments
 (0)