-
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
Open
ryan-gang
wants to merge
23
commits into
main
Choose a base branch
from
CC-1716-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
18c57de
refactor: enhance testCloneRepository function with git executable ha…
209fe0a
chore: add .idea and .devcontainer to .gitignore
6aa90ce
fix: replace cp command with mv in testCloneRepository for better fil…
ryan-gang 90fee20
refactor: improve testCloneRepository by updating git executable hand…
ryan-gang 7eb2f24
chore: set global git user configuration in your_git.sh
ryan-gang b1c5d9e
chore: set default branch name to 'main' in your_git.sh
ryan-gang 8a0a2a1
fix: improve logging for git executable movement in testCloneRepository
ce98ee6
fix: remove unused debug subproject reference
ryan-gang 448311b
fix: update git executable handling in testCloneRepository to use sud…
366d42e
fix: ensure temporary git directory is removed after cloning
6754885
fix: improve logging in testCloneRepository and clean up deferred fun…
ryan-gang 5318018
fix: add logging for git command location in testCloneRepository
ryan-gang f8ae2ff
fix: comment out unused git command execution in testCloneRepository
ryan-gang eccd922
feat: add support for Rust and Python setup in GitHub Actions workflo…
ryan-gang 88b1597
fix: comment out git configuration commands in your_git.sh
ryan-gang 07a50d6
fix: uncomment git configuration commands in your_git.sh
ryan-gang 87a489c
feat: add test_with_gix step to GitHub Actions workflow
ryan-gang 5f9d8fc
fix: add export PATH command for cargo binaries in GitHub Actions wor…
ryan-gang 6d68173
fix: update PATH handling for cargo binaries in GitHub Actions workflow
ryan-gang 1a8c8b7
fix: add --force flag to cargo binstall for gitoxide
ryan-gang 06a9732
fix: uncomment cargo binstall command for gitoxide in GitHub Actions …
ryan-gang 46be12b
fix: add init command handling in your_git.sh script
9d43810
wip: add tmp config change
ryan-gang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,5 @@ dist | |
__pycache__ | ||
vendor | ||
.history/ | ||
.idea/ | ||
.devcontainer/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,54 @@ func testCloneRepository(harness *test_case_harness.TestCaseHarness) error { | |
testRepo := randomRepo() | ||
|
||
logger.Infof("$ ./%s clone %s <testDir>", path.Base(executable.Path), testRepo.url) | ||
|
||
gitPath, err := exec.LookPath("git") | ||
if err != nil { | ||
return fmt.Errorf("git executable not found: %v", err) | ||
} | ||
logger.Debugf("Found git executable at: %s", gitPath) | ||
|
||
gitDir, err := os.MkdirTemp("/tmp", "git-*") | ||
if err != nil { | ||
return err | ||
} | ||
logger.Debugf("Created temporary directory for git clone: %s", gitDir) | ||
defer os.RemoveAll(gitDir) | ||
|
||
// Copy the custom_executable to the output path | ||
command := fmt.Sprintf("cp %s %s", gitPath, gitDir) | ||
logger.Debugf("Cp-ed git to temp directory: %s", gitDir) | ||
//fmt.Println(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: cp failed: %w", err) | ||
} | ||
|
||
defer func() error { | ||
// Copy the custom_executable to the output path | ||
command := fmt.Sprintf("cp %s %s", gitDir, gitPath) | ||
logger.Debugf("Cp-ed git to temp directory: %s", gitPath) | ||
//fmt.Println(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: cp failed: %w", err) | ||
} | ||
|
||
return nil | ||
}() | ||
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. Fix the directory-to-file copy issue and error handling There are a few issues with this deferred function:
defer func() error {
- // Copy the custom_executable to the output path
- command := fmt.Sprintf("cp %s %s", gitDir, gitPath)
- logger.Debugf("Cp-ed git to temp directory: %s", gitPath)
+ // Restore the git executable from the temporary directory
+ command := fmt.Sprintf("cp %s/git %s", gitDir, gitPath)
+ logger.Debugf("Restoring git executable to original path: %s", gitPath)
//fmt.Println(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: cp failed: %w", err)
+ logger.Errorf("Failed to restore git executable: %v", err)
+ return err
}
return nil
-}()
+}
🧰 Tools🪛 golangci-lint (1.64.8)134-134: Error return value is not checked (errcheck) |
||
|
||
defer func() error { | ||
if err := os.RemoveAll(gitDir); err != nil { | ||
return fmt.Errorf("CodeCrafters Internal Error: delete directory failed: %s", gitDir) | ||
} | ||
return nil | ||
}() | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
result, err := executable.Run("clone", testRepo.url, "test_dir") | ||
if err != nil { | ||
return err | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
💡 Verification agent
🧩 Analysis chain
Consider edge cases for path handling
Your implementation assumes that the
git
executable path and temporary directory creation will always work, but there are some edge cases to consider:/tmp
directory (Windows)Consider adding more robust error handling and making the path handling more resilient:
And verify the git executable exists and is executable:
🏁 Script executed:
Length of output: 748
Use
os.MkdirTemp
’s default temp directory for portabilityThe hard-coded
"/tmp"
path isn’t portable (e.g. Windows), so switch to the system default:• File:
internal/stage_clone_repository.go
, around line 100The existing
exec.LookPath("git")
call already guarantees thegit
executable is found and executable, so no further checks are needed here.📝 Committable suggestion