-
Notifications
You must be signed in to change notification settings - Fork 8
Move git binary before starting tests
#88
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
58
commits into
main
Choose a base branch
from
remove-git-before-tests
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 all commits
Commits
Show all changes
58 commits
Select commit
Hold shift + click to select a range
305fb98
feat: implement basic Git functionality with init, cat-file, hash-obj…
ryan-gang 766e0a1
feat: enhance cloneRepo function to support multiple expected commit …
ryan-gang 2f731fc
feat: introduce cloneRepoNew function to streamline repository clonin…
ryan-gang beeb91e
refactor: remove cloneRepoNew function and integrate its logic direct…
ryan-gang be082e3
refactor: use go-git features as much as possible
ryan-gang 4b2375f
chore: update go.mod and go.sum to include new dependencies and speci…
ryan-gang e1b46db
feat: add your_program.sh script to automate Go module tidy, build, a…
ryan-gang 9718dc7
chore: remove obsolete debug configuration and scripts, and add new m…
ryan-gang 402f52a
feat: implement mygit command-line tool with core Git functionalities…
ryan-gang 6387bf3
test: add new test case for 'pass_all' scenario in stages_test.go
ryan-gang c52b1c6
feat: add platform-specific mygit binaries and enhance your_program.s…
ryan-gang bf8c085
feat: add build script for cross-platform compilation and simplify bi…
ryan-gang f9a638f
test: add pass_all fixture for comprehensive testing of your_program.…
ryan-gang 6249244
test: modify test command to run without caching and enhance output n…
ryan-gang 05f5802
chore: remove obsolete mygit binaries for darwin-arm64, linux-amd64, …
ryan-gang 9052cb9
chore: update .gitignore to exclude ryan-git directory
ryan-gang b001f6a
feat: add Dockerfile and test script for local testing environment wi…
ryan-gang 095238b
chore: remove build script for cross-platform compilation as it is no…
ryan-gang 6bc0d52
feat: enhance testCloneRepository function to manage git executable i…
ryan-gang 8be626d
fix: remove sudo from mv commands in testCloneRepository function to …
ryan-gang 62e18a0
feat: implement GitTempDir to manage git executable in temporary dire…
ryan-gang 8f1659a
fix: add sudo to mv commands in MoveGitToTemp and RestoreGit function…
ryan-gang 15caef2
fix: suppress output of go build command in your_program.sh to reduce…
ryan-gang 298beb2
refactor: unify gitTempDir management across test functions to improv…
ryan-gang d022f18
chore: update Dockerfile to use apt-get for package installation and …
ryan-gang 591de8e
fix: comment out debug logging in MoveGitToTemp and RestoreGit functi…
ryan-gang 1ec8426
ci: add regenerated fixtures
ryan-gang 4b7376c
feat: add local testing script to build and run Docker container for …
ryan-gang 99d8016
feat: implement MoveGitToTemp and RestoreGit methods for managing git…
ryan-gang 389d950
fix: update RestoreGit documentation to clarify error handling behavior
ryan-gang b9c4bc7
feat: add configuration and script for Git management in testing envi…
ryan-gang 5f04278
refactor: replace temporary directory creation in MoveGitToTemp with …
ryan-gang 58007bc
feat: enhance your_program.sh to add support for git add command when…
ryan-gang 893400e
fix: update your_program.sh to clarify git add requirement before wri…
ryan-gang 8bedeae
test: rename and add new test cases for stages to improve coverage ag…
ryan-gang d935017
test: add new fixture files for comprehensive testing of git commands…
ryan-gang c503076
refactor: update MoveGitToTemp to use os.MkdirTemp for dynamic tempor…
ryan-gang eb00ede
refactor: simplify your_program.sh by removing redundant checks for g…
ryan-gang 893e271
refactor: update your_program.sh to correctly match git binaries in t…
ryan-gang b357713
refactor: modify MoveGitToTemp to accept TestCaseHarness and register…
ryan-gang e06e574
refactor: update test functions to use modified MoveGitToTemp with Te…
ryan-gang e485d87
chore: remove ryan-git and associated tests
ryan-gang 84d45ca
refactor: modify MoveGitToTemp to eliminate return values and use pan…
ryan-gang 9255e68
refactor: streamline test functions by removing error handling for Mo…
ryan-gang 9abf371
chore: fix lint issues
ryan-gang aa35a9f
refactor: enhance your_program.sh to correctly configure git settings…
ryan-gang c0a869c
refactor: remove sudo from MoveGitToTemp and restoreGit functions, re…
ryan-gang 010f162
chore: update GitHub Actions workflow to run tests in a container wit…
ryan-gang e9c9389
chore: update GitHub Actions workflow to install dependencies and run…
ryan-gang aee5fa5
chore: comment out dependency installation in GitHub Actions workflow
ryan-gang 7bae508
chore: remove error message for missing git binary in your_program.sh
ryan-gang ed4e488
refactor: simplify git configuration in your_program.sh by setting in…
ryan-gang 2e5f3ae
refactor: add conditional check for init.defaultBranch in your_progra…
ryan-gang a709cef
refactor: update MoveGitToTemp to use oldGitPath directly for restori…
ryan-gang 2a87dc3
fix: add error message for missing git binary in your_program.sh
ryan-gang c475b18
refactor: rename MoveGitToTemp to RelocateSystemGit for clarity and u…
ryan-gang 96a1493
refactor: improve comments in your_program.sh for clarity and accuracy
ryan-gang 3fa89a6
refactor: update comments in stage_write_tree.go to clarify the requi…
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 |
|---|---|---|
|
|
@@ -19,4 +19,4 @@ jobs: | |
| with: | ||
| python-version: '3.13' | ||
|
|
||
| - run: make test | ||
| - run: sudo make test | ||
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
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
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
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
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
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
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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| Debug = true | ||
|
|
||
| [33m[tester::#MG6] [0m[94mRunning tests for Stage #MG6 (mg6)[0m | ||
| [33m[tester::#MG6] [0m[94m$ ./your_program.sh clone https://github.com/codecrafters-io/git-sample-1 <testDir>[0m | ||
| [33m[your_program] [0mCloning into 'test_dir'... | ||
| [33m[tester::#MG6] [0m[94m$ git cat-file commit 3b0466d22854e57bf9ad3ccf82008a2d3f199550[0m | ||
| [33m[tester::#MG6] [0m[92mCommit contents verified[0m | ||
| [33m[tester::#MG6] [0m[36mReading contents of a sample file[0m | ||
| [33m[tester::#MG6] [0m[92mFile contents verified[0m | ||
| [33m[tester::#MG6] [0m[92mTest passed.[0m | ||
|
|
||
| [33m[tester::#JM9] [0m[94mRunning tests for Stage #JM9 (jm9)[0m | ||
| [33m[tester::#JM9] [0m[36mRunning git init[0m | ||
| [33m[tester::#JM9] [0m[36mCreating some files & directories[0m | ||
| [33m[tester::#JM9] [0m[36mRunning git commit --all[0m | ||
| [33m[tester::#JM9] [0m[36mCreating another file[0m | ||
| [33m[tester::#JM9] [0m[94m$ ./your_program.sh commit-tree <tree_sha> -p <commit_sha> -m <message>[0m | ||
| [33m[your_program] [0m1b6618b6c3647ff62b3941c13245f89ff309f1d6 | ||
| [33m[tester::#JM9] [0m[36mRunning git cat-file commit <sha>[0m | ||
| [33m[tester::#JM9] [0m[92mTest passed.[0m | ||
|
|
||
| [33m[tester::#FE4] [0m[94mRunning tests for Stage #FE4 (fe4)[0m | ||
| [33m[tester::#FE4] [0m[94m$ ./your_program.sh init[0m | ||
| [33m[your_program] [0mInitialized empty Git repository in /tmp/worktree576017363/.git/ | ||
| [33m[tester::#FE4] [0m[94mCreating some files & directories[0m | ||
| [33m[tester::#FE4] [0m[94m$ ./your_program.sh write-tree[0m | ||
| [33m[your_program] [0m5794e0cecb636294c4c6753742dcf616f21b7f8e | ||
| [33m[tester::#FE4] [0m[36mReading file at .git/objects/57/94e0cecb636294c4c6753742dcf616f21b7f8e[0m | ||
| [33m[tester::#FE4] [0m[92mFound git object file written at .git/objects/57/94e0cecb636294c4c6753742dcf616f21b7f8e.[0m | ||
| [33m[tester::#FE4] [0m[94m$ git ls-tree --name-only 5794e0cecb636294c4c6753742dcf616f21b7f8e[0m | ||
| [33m[tester::#FE4] [0m[92mTest passed.[0m | ||
|
|
||
| [33m[tester::#KP1] [0m[94mRunning tests for Stage #KP1 (kp1)[0m | ||
| [33m[tester::#KP1] [0m[94m$ ./your_program.sh init[0m | ||
| [33m[your_program] [0mInitialized empty Git repository in /tmp/worktree1592248827/.git/ | ||
| [33m[tester::#KP1] [0m[36mWriting a tree to git storage..[0m | ||
| [33m[tester::#KP1] [0m[94m$ ./your_program.sh ls-tree --name-only b8b2ce4032667654a481b94b38818b24fe84c460[0m | ||
| [33m[your_program] [0mgrape | ||
| [33m[your_program] [0morange | ||
| [33m[your_program] [0mstrawberry | ||
| [33m[tester::#KP1] [0m[92mTest passed.[0m | ||
|
|
||
| [33m[tester::#JT4] [0m[94mRunning tests for Stage #JT4 (jt4)[0m | ||
| [33m[tester::#JT4] [0m[94m$ ./your_program.sh init[0m | ||
| [33m[your_program] [0mInitialized empty Git repository in /tmp/worktree1527710594/.git/ | ||
| [33m[tester::#JT4] [0m[94m$ echo "raspberry blueberry banana pear pineapple apple" > banana.txt[0m | ||
| [33m[tester::#JT4] [0m[94m$ ./your_program.sh hash-object -w banana.txt[0m | ||
| [33m[your_program] [0m27ece14445d59efd51d58d64afe55b2d912b2503 | ||
| [33m[tester::#JT4] [0m[92mOutput is a 40-char SHA.[0m | ||
| [33m[tester::#JT4] [0m[92mBlob file contents are valid.[0m | ||
| [33m[tester::#JT4] [0m[92mReturned SHA matches expected SHA.[0m | ||
| [33m[tester::#JT4] [0m[92mTest passed.[0m | ||
|
|
||
| [33m[tester::#IC4] [0m[94mRunning tests for Stage #IC4 (ic4)[0m | ||
| [33m[tester::#IC4] [0m[94m$ ./your_program.sh init[0m | ||
| [33m[your_program] [0mInitialized empty Git repository in /tmp/worktree1802656906/.git/ | ||
| [33m[tester::#IC4] [0m[94mAdded blob object to .git/objects: da6ad811e0463b5b347359cc21fb53a3e5010d10[0m | ||
| [33m[tester::#IC4] [0m[94m$ ./your_program.sh cat-file -p da6ad811e0463b5b347359cc21fb53a3e5010d10[0m | ||
| [33m[your_program] [0mpineapple grape orange blueberry apple mango | ||
| [33m[tester::#IC4] [0m[92mOutput is valid.[0m | ||
| [33m[tester::#IC4] [0m[92mTest passed.[0m | ||
|
|
||
| [33m[tester::#GG4] [0m[94mRunning tests for Stage #GG4 (gg4)[0m | ||
| [33m[tester::#GG4] [0m[94m$ ./your_program.sh init[0m | ||
| [33m[your_program] [0mInitialized empty Git repository in /tmp/worktree164141277/.git/ | ||
| [33m[tester::#GG4] [0m[92m.git directory found.[0m | ||
| [33m[tester::#GG4] [0m[92m.git/objects directory found.[0m | ||
| [33m[tester::#GG4] [0m[92m.git/refs directory found.[0m | ||
| [33m[tester::#GG4] [0m[92m.git/HEAD file is valid.[0m | ||
| [33m[tester::#GG4] [0m[92mTest passed.[0m |
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 |
|---|---|---|
| @@ -1,11 +1,11 @@ | ||
| # This is the current stage that you're on. | ||
| # | ||
| # Whenever you want to advance to the next stage, | ||
| # bump this to the next number. | ||
| current_stage: 1 | ||
|
|
||
| # Set this to true if you want debug logs. | ||
| # | ||
| # These can be VERY verbose, so we suggest turning them off | ||
| # unless you really need them. | ||
| debug: true | ||
|
|
||
| # Use this to change the Go version used to run your code | ||
| # on Codecrafters. | ||
| # | ||
| # Available versions: go-1.24 | ||
| language_pack: go-1.24 |
This file was deleted.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| #!/bin/sh | ||
|
|
||
| # Check if git from PATH is working first | ||
| if command -v git >/dev/null 2>&1; then | ||
| if [ "$1" = "write-tree" ]; then | ||
| git add . | ||
| fi | ||
| exec git "$@" | ||
| fi | ||
|
|
||
| # Find git binary in /tmp locations | ||
| for tmpdir in /tmp/git-*/git; do | ||
| if [ -x "$tmpdir" ]; then | ||
| # If defaultBranch config is not set, we set it to main (doesn't work without global config) | ||
| if ! "$tmpdir" config --global --get init.defaultBranch >/dev/null 2>&1; then | ||
| "$tmpdir" config --global init.defaultBranch main | ||
| fi | ||
|
|
||
| # commit-tree stage doesn't use this script for init | ||
| # So we need to run this setup again | ||
| if [ "$1" = "commit-tree" ]; then | ||
| "$tmpdir" config --local user.email "hello@codecrafters.io" | ||
| "$tmpdir" config --local user.name "CodeCrafters-Bot" | ||
| fi | ||
|
|
||
| if [ "$1" = "write-tree" ]; then | ||
| "$tmpdir" add . | ||
| fi | ||
|
|
||
| exec "$tmpdir" "$@" | ||
| fi | ||
| done | ||
|
|
||
| echo "git binary not found in PATH or /tmp directories" | ||
| exit 1 |
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 |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package internal | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "os/exec" | ||
| "path" | ||
|
|
||
| "github.com/codecrafters-io/tester-utils/logger" | ||
| "github.com/codecrafters-io/tester-utils/test_case_harness" | ||
| ) | ||
|
|
||
| // RelocateSystemGit moves the system git binary to a temporary directory | ||
| func RelocateSystemGit(harness *test_case_harness.TestCaseHarness, logger *logger.Logger) { | ||
| oldGitPath, err := exec.LookPath("git") | ||
| if err != nil { | ||
| panic(fmt.Sprintf("CodeCrafters Internal Error: git executable not found: %v", err)) | ||
| } | ||
|
|
||
| tmpGitDir, err := os.MkdirTemp("/tmp", "git-*") | ||
| if err != nil { | ||
| panic(fmt.Sprintf("CodeCrafters Internal Error: create tmp git directory failed: %v", err)) | ||
| } | ||
| tmpGitPath := path.Join(tmpGitDir, "git") | ||
|
|
||
| command := fmt.Sprintf("mv %s %s", oldGitPath, tmpGitPath) | ||
| moveCmd := exec.Command("sh", "-c", command) | ||
| moveCmd.Stdout = io.Discard | ||
| moveCmd.Stderr = io.Discard | ||
| if err := moveCmd.Run(); err != nil { | ||
| os.RemoveAll(tmpGitDir) | ||
| panic(fmt.Sprintf("CodeCrafters Internal Error: mv git to tmp directory failed: %v", err)) | ||
| } | ||
|
|
||
| // Register teardown function to automatically restore git | ||
| harness.RegisterTeardownFunc(func() { restoreSystemGit(tmpGitPath, oldGitPath) }) | ||
| } | ||
|
|
||
| // RestoreSystemGit moves the git binary back to its original location and cleans up | ||
| func restoreSystemGit(newPath string, originalPath string) error { | ||
| command := fmt.Sprintf("mv %s %s", newPath, originalPath) | ||
| moveCmd := exec.Command("sh", "-c", command) | ||
| moveCmd.Stdout = io.Discard | ||
ryan-gang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| moveCmd.Stderr = io.Discard | ||
| if err := moveCmd.Run(); err != nil { | ||
| panic(fmt.Sprintf("CodeCrafters Internal Error: mv restore for git failed: %v", err)) | ||
| } | ||
|
|
||
| if err := os.RemoveAll(path.Dir(newPath)); err != nil { | ||
| panic(fmt.Sprintf("CodeCrafters Internal Error: delete tmp git directory failed: %s", path.Dir(newPath))) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| FROM golang:1.24 | ||
|
|
||
| # Install required packages | ||
| RUN apt-get update && apt-get install -y \ | ||
| # Required for make test | ||
| make \ | ||
| # Required to run bash tests | ||
| bash \ | ||
| # Required for fixtures | ||
| python3 \ | ||
| # Required for testing | ||
| git \ | ||
| && rm -rf /var/lib/apt/lists/* | ||
|
|
||
| # Set working directory | ||
| WORKDIR /app | ||
|
|
||
| # Starting from Go 1.20, the go standard library is no loger compiled. | ||
| # Setting GODEBUG to "installgoroot=all" restores the old behavior | ||
| RUN GODEBUG="installgoroot=all" go install std | ||
|
|
||
| # Copy go.mod and go.sum first to cache dependencies | ||
| COPY go.mod go.sum ./ | ||
| RUN go mod download | ||
|
|
||
| # Default command | ||
| CMD ["/bin/bash"] |
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 |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| #!/bin/bash | ||
|
|
||
| set -e | ||
|
|
||
| # Ensure we're in the correct directory | ||
| cd "$(dirname "$0")/.." | ||
|
|
||
| # Build and run | ||
| docker build -t local-git-tester -f local_testing/Dockerfile . | ||
| # Run make test | ||
| # docker run --rm -it -v $(pwd):/app local-git-tester make test | ||
| # Generate fixtures | ||
| # docker run --rm -it -e CODECRAFTERS_RECORD_FIXTURES=true -v $(pwd):/app local-git-tester make test | ||
| # Run test_with_git | ||
| docker run --rm -it -v $(pwd):/app local-git-tester make test_with_git |
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.
Seems weird behaviour to not do this here too. Can't we move the git binary back for verifying and then place it back again?
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.
Or can we just use the path of the temporary git to execute that?