This commit refactors the codebase to use dependency injection for mocking external dependencies, removing the need for in-code mocking with the `BORG_PLEXSUS` environment variable. - Interfaces have been created for external dependencies in the `pkg/vcs`, `pkg/github`, and `pkg/pwa` packages. - The `cmd` package has been refactored to use these interfaces, with dependencies exposed as public variables for easy mocking in tests. - The tests in `TDD/collect_commands_test.go` have been updated to inject mock implementations of these interfaces. - The `BORG_PLEXSUS` environment variable has been removed from the codebase.
86 lines
2.3 KiB
Go
86 lines
2.3 KiB
Go
package vcs
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestCloneGitRepository(t *testing.T) {
|
|
// Create a temporary directory for the bare repository
|
|
bareRepoPath, err := os.MkdirTemp("", "bare-repo-")
|
|
if err != nil {
|
|
t.Fatalf("Failed to create temp dir for bare repo: %v", err)
|
|
}
|
|
defer os.RemoveAll(bareRepoPath)
|
|
|
|
// Initialize a bare git repository
|
|
cmd := exec.Command("git", "init", "--bare")
|
|
cmd.Dir = bareRepoPath
|
|
if err := cmd.Run(); err != nil {
|
|
t.Fatalf("Failed to init bare repo: %v", err)
|
|
}
|
|
|
|
// Clone the bare repository to a temporary directory to add a commit
|
|
clonePath, err := os.MkdirTemp("", "clone-")
|
|
if err != nil {
|
|
t.Fatalf("Failed to create temp dir for clone: %v", err)
|
|
}
|
|
defer os.RemoveAll(clonePath)
|
|
|
|
cmd = exec.Command("git", "clone", bareRepoPath, clonePath)
|
|
if err := cmd.Run(); err != nil {
|
|
t.Fatalf("Failed to clone bare repo: %v", err)
|
|
}
|
|
|
|
// Create a file and commit it
|
|
filePath := filepath.Join(clonePath, "foo.txt")
|
|
if err := os.WriteFile(filePath, []byte("foo"), 0644); err != nil {
|
|
t.Fatalf("Failed to write file: %v", err)
|
|
}
|
|
cmd = exec.Command("git", "add", "foo.txt")
|
|
cmd.Dir = clonePath
|
|
if err := cmd.Run(); err != nil {
|
|
t.Fatalf("Failed to git add: %v", err)
|
|
}
|
|
|
|
cmd = exec.Command("git", "config", "user.email", "test@example.com")
|
|
cmd.Dir = clonePath
|
|
if err := cmd.Run(); err != nil {
|
|
t.Fatalf("Failed to set git user.email: %v", err)
|
|
}
|
|
|
|
cmd = exec.Command("git", "config", "user.name", "Test User")
|
|
cmd.Dir = clonePath
|
|
if err := cmd.Run(); err != nil {
|
|
t.Fatalf("Failed to set git user.name: %v", err)
|
|
}
|
|
|
|
cmd = exec.Command("git", "commit", "-m", "Initial commit")
|
|
cmd.Dir = clonePath
|
|
if err := cmd.Run(); err != nil {
|
|
t.Fatalf("Failed to git commit: %v", err)
|
|
}
|
|
cmd = exec.Command("git", "push", "origin", "master")
|
|
cmd.Dir = clonePath
|
|
if err := cmd.Run(); err != nil {
|
|
t.Fatalf("Failed to git push: %v", err)
|
|
}
|
|
|
|
// Clone the repository using the function we're testing
|
|
cloner := NewGitCloner()
|
|
dn, err := cloner.CloneGitRepository("file://"+bareRepoPath, os.Stdout)
|
|
if err != nil {
|
|
t.Fatalf("CloneGitRepository failed: %v", err)
|
|
}
|
|
|
|
// Verify the DataNode contains the correct file
|
|
exists, err := dn.Exists("foo.txt")
|
|
if err != nil {
|
|
t.Fatalf("Exists failed: %v", err)
|
|
}
|
|
if !exists {
|
|
t.Errorf("Expected to find file foo.txt in DataNode, but it was not found")
|
|
}
|
|
}
|