-
Notifications
You must be signed in to change notification settings - Fork 7
Refactor testCloneRepository function and update .gitignore #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 12 commits
18c57de
209fe0a
6aa90ce
90fee20
7eb2f24
b1c5d9e
8a0a2a1
ce98ee6
448311b
366d42e
6754885
5318018
f8ae2ff
eccd922
88b1597
07a50d6
87a489c
5f9d8fc
6d68173
1a8c8b7
06a9732
46be12b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,4 @@ jobs: | |
with: | ||
python-version: '3.12' | ||
- run: make test | ||
- run: make test_with_git |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,5 @@ dist | |
__pycache__ | ||
vendor | ||
.history/ | ||
.idea/ | ||
.devcontainer/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,9 @@ package internal | |
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"os/exec" | ||
"path" | ||
|
||
"github.com/codecrafters-io/tester-utils/random" | ||
|
@@ -31,7 +33,7 @@ func (r TestRepo) randomFile() TestFile { | |
return r.exampleFiles[random.RandomInt(0, len(r.exampleFiles))] | ||
} | ||
|
||
var testRepos []TestRepo = []TestRepo{ | ||
var testRepos = []TestRepo{ | ||
{ | ||
url: "https://github.com/codecrafters-io/git-sample-1", | ||
exampleCommits: []string{ | ||
|
@@ -90,6 +92,46 @@ func testCloneRepository(harness *test_case_harness.TestCaseHarness) error { | |
testRepo := randomRepo() | ||
|
||
logger.Infof("$ ./%s clone %s <testDir>", path.Base(executable.Path), testRepo.url) | ||
|
||
oldGitPath, err := exec.LookPath("git") | ||
if err != nil { | ||
return fmt.Errorf("git executable not found: %v", err) | ||
} | ||
oldGitDir := path.Dir(oldGitPath) | ||
logger.Debugf("Found git executable at: %s", oldGitPath) | ||
|
||
tmpGitDir, err := os.MkdirTemp("/tmp", "git-*") | ||
if err != nil { | ||
return err | ||
} | ||
logger.Debugf("Created temporary directory for git clone: %s", tmpGitDir) | ||
tmpGitPath := path.Join(tmpGitDir, "git") | ||
defer os.RemoveAll(tmpGitDir) | ||
|
||
// Copy the custom_executable to the output path | ||
command := fmt.Sprintf("sudo mv %s %s", oldGitPath, tmpGitDir) | ||
logger.Debugf(command) | ||
copyCmd := exec.Command("sh", "-c", command) | ||
copyCmd.Stdout = os.Stdout | ||
copyCmd.Stderr = os.Stderr | ||
if err := copyCmd.Run(); err != nil { | ||
return fmt.Errorf("CodeCrafters Internal Error: mv1 failed: %w", err) | ||
} | ||
logger.Debugf("mv-ed git to temp directory: %s", tmpGitDir) | ||
|
||
logger.Infof("$ git clone %s %s", testRepo.url, "test_dir") | ||
|
||
//////// which git | ||
command = fmt.Sprint("which git") | ||
logger.Debugf(command) | ||
cmd := exec.Command("sh", "-c", command) | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
if err := cmd.Run(); err != nil { | ||
return fmt.Errorf("CodeCrafters Internal Error: mv1 failed: %w", err) | ||
} | ||
logger.Debugf("mv-ed git to temp directory: %s", tmpGitDir) | ||
|
||
result, err := executable.Run("clone", testRepo.url, "test_dir") | ||
if err != nil { | ||
return err | ||
|
@@ -136,5 +178,23 @@ func testCloneRepository(harness *test_case_harness.TestCaseHarness) error { | |
} | ||
logger.Successf("File contents verified") | ||
|
||
defer func() error { | ||
// Copy the custom_executable to the output path | ||
command := fmt.Sprintf("sudo mv %s %s", tmpGitPath, oldGitDir) | ||
logger.Debugf(command) | ||
copyCmd := exec.Command("sh", "-c", command) | ||
copyCmd.Stdout = io.Discard | ||
copyCmd.Stderr = io.Discard | ||
if err := copyCmd.Run(); err != nil { | ||
return fmt.Errorf("CodeCrafters Internal Error: mv2 failed: %w", err) | ||
} | ||
logger.Debugf("mv-ed git to og directory: %s", oldGitDir) | ||
|
||
if err := os.RemoveAll(tmpGitDir); err != nil { | ||
return fmt.Errorf("CodeCrafters Internal Error: delete directory failed: %s", tmpGitDir) | ||
} | ||
return nil | ||
}() | ||
Comment on lines
+181
to
+197
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Fix error handling in deferred function and use Go's file operations The deferred function has several issues:
-defer func() error {
+defer func() {
- // Copy the custom_executable to the output path
- command := fmt.Sprintf("sudo mv %s %s", tmpGitPath, oldGitDir)
- logger.Debugf(command)
- copyCmd := exec.Command("sh", "-c", command)
- copyCmd.Stdout = io.Discard
- copyCmd.Stderr = io.Discard
- if err := copyCmd.Run(); err != nil {
- return fmt.Errorf("CodeCrafters Internal Error: mv2 failed: %w", err)
- }
- logger.Debugf("mv-ed git to og directory: %s", oldGitDir)
+ // Restore the git executable to its original location
+ originalPath := path.Join(oldGitDir, "git")
+ logger.Debugf("Restoring git executable to: %s", originalPath)
+
+ // Copy the git executable back to its original location
+ srcFile, err := os.Open(tmpGitPath)
+ if err != nil {
+ logger.Errorf("Failed to open temporary git executable: %v", err)
+ return
+ }
+ defer srcFile.Close()
+
+ destFile, err := os.Create(originalPath)
+ if err != nil {
+ logger.Errorf("Failed to create original git executable: %v", err)
+ return
+ }
+ defer destFile.Close()
+
+ if _, err := io.Copy(destFile, srcFile); err != nil {
+ logger.Errorf("Failed to copy git executable back: %v", err)
+ return
+ }
+
+ if err := os.Chmod(originalPath, 0755); err != nil {
+ logger.Errorf("Failed to make original git executable: %v", err)
+ return
+ }
+
+ logger.Debugf("Restored git executable to original location: %s", originalPath)
- if err := os.RemoveAll(tmpGitDir); err != nil {
- return fmt.Errorf("CodeCrafters Internal Error: delete directory failed: %s", tmpGitDir)
- }
- return nil
}()
+ // Verify git is available after restoration
+ if _, err := exec.LookPath("git"); err != nil {
+ logger.Errorf("Git executable was not restored correctly: %v", err)
+ }
+}() Note: The 🧰 Tools🪛 golangci-lint (1.64.8)197-197: Error return value is not checked (errcheck) 🤖 Prompt for AI Agents
|
||
|
||
return nil | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,5 +1,9 @@ | ||||||||||||||||||||
#!/bin/sh | ||||||||||||||||||||
|
||||||||||||||||||||
git config --global user.email "[email protected]" | ||||||||||||||||||||
git config --global user.name "Your Name" | ||||||||||||||||||||
Comment on lines
+3
to
+4
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Avoid mutating the user's global Git config - git config --global user.email "[email protected]"
- git config --global user.name "Your Name"
- exec git "$@"
+ # Apply email/name only for this invocation, without touching ~/.gitconfig
+ exec git \
+ -c user.email="[email protected]" \
+ -c user.name="Your Name" \
+ "$@" This keeps your test setup isolated and avoids side-effects on the developer’s environment. 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||
git config --global init.defaultBranch "main" | ||||||||||||||||||||
|
||||||||||||||||||||
if [ "$1" = "write-tree" ] | ||||||||||||||||||||
then | ||||||||||||||||||||
git add . | ||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Simplify checking git path and improve error message
This code has several issues:
fmt.Sprint
unnecessarily (flagged by static analysis)📝 Committable suggestion
🧰 Tools
🪛 golangci-lint (1.64.8)
125-125: S1039: unnecessary use of fmt.Sprint
(gosimple)
🤖 Prompt for AI Agents