test: expand routing and compact coverage

This commit is contained in:
Jordan Hubbard
2025-12-29 00:55:44 -04:00
committed by Steve Yegge
parent cb280b0fad
commit 12b797781d
4 changed files with 168 additions and 40 deletions

View File

@@ -1,21 +1,20 @@
package compact
import (
"bytes"
"os/exec"
"strings"
)
var gitExec = func(name string, args ...string) ([]byte, error) {
return exec.Command(name, args...).Output()
}
// GetCurrentCommitHash returns the current git HEAD commit hash.
// Returns empty string if not in a git repository or if git command fails.
func GetCurrentCommitHash() string {
cmd := exec.Command("git", "rev-parse", "HEAD")
var out bytes.Buffer
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
output, err := gitExec("git", "rev-parse", "HEAD")
if err != nil {
return ""
}
return strings.TrimSpace(out.String())
return strings.TrimSpace(string(output))
}

View File

@@ -0,0 +1,30 @@
package compact
import (
"errors"
"testing"
)
func TestGetCurrentCommitHashSuccess(t *testing.T) {
orig := gitExec
gitExec = func(string, ...string) ([]byte, error) {
return []byte("abc123\n"), nil
}
t.Cleanup(func() { gitExec = orig })
if got := GetCurrentCommitHash(); got != "abc123" {
t.Fatalf("expected trimmed hash, got %q", got)
}
}
func TestGetCurrentCommitHashError(t *testing.T) {
orig := gitExec
gitExec = func(string, ...string) ([]byte, error) {
return nil, errors.New("boom")
}
t.Cleanup(func() { gitExec = orig })
if got := GetCurrentCommitHash(); got != "" {
t.Fatalf("expected empty string on error, got %q", got)
}
}