-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Enhance git repository handling in tests and core functionality
- Introduced local repository support in the 'Clone' function to allow cloning from local paths. - Updated 'IsValidURL' to validate local git repositories in addition to standard URLs. - Enhanced test cases in 'git_test.go' and 'take_test.go' to create and utilize temporary git repositories for more robust testing. - Improved error handling and reporting in the test setup for better reliability. These changes improve the functionality of the 'take' command and its associated tests, ensuring better handling of both local and remote git repositories. Signed-off-by: Alessandro De Blasis <[email protected]>
- Loading branch information
Showing
4 changed files
with
164 additions
and
67 deletions.
There are no files selected for viewing
This file contains 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 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,11 +2,23 @@ package git | |
|
||
import ( | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func TestIsValidURL(t *testing.T) { | ||
// Create a temporary git repo for testing | ||
tmpDir, err := os.MkdirTemp("", "git-test-*") | ||
if err != nil { | ||
t.Fatalf("Failed to create temp dir: %v", err) | ||
} | ||
defer os.RemoveAll(tmpDir) | ||
|
||
if err := exec.Command("git", "init", tmpDir).Run(); err != nil { | ||
t.Fatalf("Failed to init test repo: %v", err) | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
url string | ||
|
@@ -22,6 +34,11 @@ func TestIsValidURL(t *testing.T) { | |
url: "[email protected]:user/repo.git", | ||
want: true, | ||
}, | ||
{ | ||
name: "valid local repo", | ||
url: tmpDir, | ||
want: true, | ||
}, | ||
{ | ||
name: "invalid URL - no .git suffix", | ||
url: "https://github.com/user/repo", | ||
|
@@ -147,6 +164,48 @@ func TestClone(t *testing.T) { | |
} | ||
defer os.RemoveAll(tmpDir) | ||
|
||
// Create a test repo | ||
testRepoDir, err := os.MkdirTemp("", "test-repo-*") | ||
if err != nil { | ||
t.Fatalf("Failed to create test repo dir: %v", err) | ||
} | ||
defer os.RemoveAll(testRepoDir) | ||
|
||
// Initialize test repo | ||
if err := exec.Command("git", "init", testRepoDir).Run(); err != nil { | ||
t.Fatalf("Failed to init test repo: %v", err) | ||
} | ||
|
||
// Create a test file and commit it | ||
testFile := filepath.Join(testRepoDir, "test.txt") | ||
if err := os.WriteFile(testFile, []byte("test"), 0644); err != nil { | ||
t.Fatalf("Failed to create test file: %v", err) | ||
} | ||
|
||
cmd := exec.Command("git", "add", "test.txt") | ||
cmd.Dir = testRepoDir | ||
if err := cmd.Run(); err != nil { | ||
t.Fatalf("Failed to add test file: %v", err) | ||
} | ||
|
||
cmd = exec.Command("git", "config", "user.email", "[email protected]") | ||
cmd.Dir = testRepoDir | ||
if err := cmd.Run(); err != nil { | ||
t.Fatalf("Failed to configure git email: %v", err) | ||
} | ||
|
||
cmd = exec.Command("git", "config", "user.name", "Test User") | ||
cmd.Dir = testRepoDir | ||
if err := cmd.Run(); err != nil { | ||
t.Fatalf("Failed to configure git username: %v", err) | ||
} | ||
|
||
cmd = exec.Command("git", "commit", "-m", "Initial commit") | ||
cmd.Dir = testRepoDir | ||
if err := cmd.Run(); err != nil { | ||
t.Fatalf("Failed to commit: %v", err) | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
opts CloneOptions | ||
|
@@ -161,10 +220,10 @@ func TestClone(t *testing.T) { | |
wantErr: true, | ||
}, | ||
{ | ||
name: "valid URL with target dir", | ||
name: "valid local repo", | ||
opts: CloneOptions{ | ||
URL: "https://github.com/octocat/Hello-World.git", | ||
TargetDir: filepath.Join(tmpDir, "hello-world"), | ||
URL: testRepoDir, | ||
TargetDir: filepath.Join(tmpDir, "valid"), | ||
Depth: 1, | ||
}, | ||
wantErr: false, | ||
|
This file contains 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 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 |
---|---|---|
|
@@ -72,6 +72,49 @@ func createMockZip(t *testing.T) string { | |
return zipPath | ||
} | ||
|
||
// Create a mock git repository for testing | ||
func createTestRepo(t *testing.T) string { | ||
repoDir, err := os.MkdirTemp("", "test-repo-*") | ||
if err != nil { | ||
t.Fatalf("Failed to create test repo dir: %v", err) | ||
} | ||
|
||
if err := exec.Command("git", "init", repoDir).Run(); err != nil { | ||
t.Fatalf("Failed to init test repo: %v", err) | ||
} | ||
|
||
testFile := filepath.Join(repoDir, "test.txt") | ||
if err := os.WriteFile(testFile, []byte("test"), 0644); err != nil { | ||
t.Fatalf("Failed to create test file: %v", err) | ||
} | ||
|
||
cmd := exec.Command("git", "add", "test.txt") | ||
cmd.Dir = repoDir | ||
if err := cmd.Run(); err != nil { | ||
t.Fatalf("Failed to add test file: %v", err) | ||
} | ||
|
||
cmd = exec.Command("git", "config", "user.email", "[email protected]") | ||
cmd.Dir = repoDir | ||
if err := cmd.Run(); err != nil { | ||
t.Fatalf("Failed to configure git email: %v", err) | ||
} | ||
|
||
cmd = exec.Command("git", "config", "user.name", "Test User") | ||
cmd.Dir = repoDir | ||
if err := cmd.Run(); err != nil { | ||
t.Fatalf("Failed to configure git username: %v", err) | ||
} | ||
|
||
cmd = exec.Command("git", "commit", "-m", "Initial commit") | ||
cmd.Dir = repoDir | ||
if err := cmd.Run(); err != nil { | ||
t.Fatalf("Failed to commit: %v", err) | ||
} | ||
|
||
return repoDir | ||
} | ||
|
||
func TestTake(t *testing.T) { | ||
// Create mock archives | ||
tarPath := createMockTarball(t) | ||
|
@@ -123,7 +166,19 @@ func TestTake(t *testing.T) { | |
return filepath.Join(tmpDir, path) | ||
} | ||
|
||
tests := []testCase{ | ||
// Create test repo | ||
testRepo := createTestRepo(t) | ||
defer os.RemoveAll(testRepo) | ||
|
||
tests := []struct { | ||
name string | ||
opts Options | ||
setup func(t *testing.T) | ||
cleanup func() error | ||
wantResult Result | ||
wantErr error | ||
checkResult func(t *testing.T, got Result) | ||
}{ | ||
{ | ||
name: "create new directory", | ||
opts: Options{ | ||
|
@@ -195,24 +250,11 @@ func TestTake(t *testing.T) { | |
wantErr: ErrInvalidPath, | ||
}, | ||
{ | ||
name: "handle git HTTPS URL", | ||
name: "handle git repo", | ||
opts: Options{ | ||
Path: "https://github.com/deblasis/take.git", | ||
}, | ||
setup: func(t *testing.T) { | ||
// Skip if no git credentials available | ||
cmd := exec.Command("git", "config", "--get", "credential.helper") | ||
if err := cmd.Run(); err != nil { | ||
t.Skip("Git credentials not configured, skipping clone test") | ||
} | ||
// Clean up any existing clone | ||
os.RemoveAll("take") | ||
Path: testRepo, | ||
}, | ||
checkResult: func(t *testing.T, got Result) { | ||
// Skip validation if test was skipped | ||
if t.Skipped() { | ||
return | ||
} | ||
if got.Error != nil { | ||
t.Errorf("Unexpected error: %v", got.Error) | ||
return | ||
|
@@ -222,24 +264,6 @@ func TestTake(t *testing.T) { | |
} | ||
}, | ||
}, | ||
{ | ||
name: "handle git SSH URL", | ||
opts: Options{ | ||
Path: "[email protected]:deblasis/take.git", | ||
}, | ||
setup: func(t *testing.T) { | ||
// Skip if no SSH key available | ||
_, err := os.Stat(filepath.Join(os.Getenv("HOME"), ".ssh", "id_rsa")) | ||
if err != nil { | ||
t.Skip("SSH key not found, skipping SSH clone test") | ||
} | ||
}, | ||
checkResult: func(t *testing.T, got Result) { | ||
if !got.WasCloned { | ||
t.Error("Expected repository to be cloned") | ||
} | ||
}, | ||
}, | ||
{ | ||
name: "handle tarball URL", | ||
opts: Options{ | ||
|