111 lines
2.9 KiB
Go
111 lines
2.9 KiB
Go
package gitops
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestLocalCommitAndDirtyStatus(t *testing.T) {
|
|
dir := t.TempDir()
|
|
runner := New()
|
|
initRepo(t, dir)
|
|
writeFile(t, filepath.Join(dir, "SKILL.md"), "# Skill\n")
|
|
runGit(t, dir, "add", "SKILL.md")
|
|
runGit(t, dir, "commit", "-m", "initial")
|
|
|
|
commit, err := runner.CurrentCommit(dir)
|
|
if err != nil {
|
|
t.Fatalf("CurrentCommit returned error: %v", err)
|
|
}
|
|
if len(commit) != 40 {
|
|
t.Fatalf("commit length = %d, want 40", len(commit))
|
|
}
|
|
|
|
dirty, err := runner.HasLocalChanges(dir)
|
|
if err != nil {
|
|
t.Fatalf("HasLocalChanges returned error: %v", err)
|
|
}
|
|
if dirty {
|
|
t.Fatal("repo should be clean")
|
|
}
|
|
|
|
writeFile(t, filepath.Join(dir, "SKILL.md"), "# Changed\n")
|
|
dirty, err = runner.HasLocalChanges(dir)
|
|
if err != nil {
|
|
t.Fatalf("HasLocalChanges returned error: %v", err)
|
|
}
|
|
if !dirty {
|
|
t.Fatal("repo should be dirty")
|
|
}
|
|
}
|
|
|
|
func TestClonePullAndRemoteCommit(t *testing.T) {
|
|
root := t.TempDir()
|
|
source := filepath.Join(root, "source")
|
|
remote := filepath.Join(root, "remote.git")
|
|
clone := filepath.Join(root, "clone")
|
|
runner := New()
|
|
|
|
initRepo(t, source)
|
|
writeFile(t, filepath.Join(source, "SKILL.md"), "# Skill\n")
|
|
runGit(t, source, "add", "SKILL.md")
|
|
runGit(t, source, "commit", "-m", "initial")
|
|
runGit(t, root, "clone", "--bare", source, remote)
|
|
|
|
if err := runner.Clone(context.Background(), remote, clone, Credentials{}); err != nil {
|
|
t.Fatalf("Clone returned error: %v", err)
|
|
}
|
|
remoteCommit, err := runner.RemoteCommit(context.Background(), clone, "main", Credentials{})
|
|
if err != nil {
|
|
t.Fatalf("RemoteCommit returned error: %v", err)
|
|
}
|
|
current, err := runner.CurrentCommit(clone)
|
|
if err != nil {
|
|
t.Fatalf("CurrentCommit returned error: %v", err)
|
|
}
|
|
if remoteCommit != current {
|
|
t.Fatalf("remote commit = %q, current = %q", remoteCommit, current)
|
|
}
|
|
|
|
writeFile(t, filepath.Join(source, "README.md"), "update\n")
|
|
runGit(t, source, "add", "README.md")
|
|
runGit(t, source, "commit", "-m", "update")
|
|
runGit(t, source, "push", remote, "main")
|
|
|
|
if err := runner.Pull(context.Background(), clone, Credentials{}); err != nil {
|
|
t.Fatalf("Pull returned error: %v", err)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(clone, "README.md")); err != nil {
|
|
t.Fatalf("pull did not bring README.md: %v", err)
|
|
}
|
|
}
|
|
|
|
func initRepo(t *testing.T, dir string) {
|
|
t.Helper()
|
|
if err := os.MkdirAll(dir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
runGit(t, dir, "-c", "init.defaultBranch=main", "init")
|
|
runGit(t, dir, "config", "user.email", "test@example.com")
|
|
runGit(t, dir, "config", "user.name", "Test")
|
|
}
|
|
|
|
func writeFile(t *testing.T, path, content string) {
|
|
t.Helper()
|
|
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func runGit(t *testing.T, dir string, args ...string) {
|
|
t.Helper()
|
|
cmd := exec.Command("git", args...)
|
|
cmd.Dir = dir
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("git %v failed: %v\n%s", args, err, string(out))
|
|
}
|
|
}
|