Update deps and boost test coverage from 70.8% to 90%

Update go-git v6, gopsutil v4.26.1, x/term v0.39.0, and transitive deps.
Refactor main() into testable run() function, add injectable processLister
and termWidthFunc for test isolation, and add tests covering detached HEAD,
dirty/clean worktree, empty repo, process listing errors, and terminal
width fallback.
This commit is contained in:
2026-02-08 22:58:08 +01:00
parent 99ad5b9d7f
commit ed312a3ed0
4 changed files with 333 additions and 37 deletions

36
main.go
View File

@@ -99,13 +99,14 @@ func formatOutput(left, right string, padding int) string {
return fmt.Sprintf("%s%s%s", left, strings.Repeat(" ", padding), right)
}
func main() {
jsonStr := readInputFromStdin(bufio.NewReader(os.Stdin))
// run reads JSON from r, builds the statusline, and writes it to w.
// Returns an error if the input cannot be parsed.
func run(r *bufio.Reader, w *strings.Builder) error {
jsonStr := readInputFromStdin(r)
data, err := parseStatusInput(jsonStr)
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing JSON: %v\n", err)
os.Exit(1)
return fmt.Errorf("error parsing JSON: %w", err)
}
left, right := buildStatusLine(data)
@@ -121,7 +122,17 @@ func main() {
padding := calculatePadding(leftVisible, rightVisible, termWidth)
output := formatOutput(left, right, padding)
fmt.Print(output)
w.WriteString(output)
return nil
}
func main() {
var out strings.Builder
if err := run(bufio.NewReader(os.Stdin), &out); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
fmt.Print(out.String())
}
func formatContextInfo(contextSize int, usage *TokenUsage) string {
@@ -136,9 +147,13 @@ func formatContextInfo(contextSize int, usage *TokenUsage) string {
return fmt.Sprintf("%dk/%dk", currentK, totalK)
}
// processLister returns the list of running processes.
// Replaced in tests to avoid depending on real process state.
var processLister = process.Processes
func getGiteaStatus() string {
// Check if gitea process is running using gopsutil (cross-platform)
procs, err := process.Processes()
procs, err := processLister()
if err != nil {
return red + "●" + reset
}
@@ -196,8 +211,15 @@ func getGitInfo(cwd string) string {
return fmt.Sprintf(" git:(%s)", branch)
}
func getTerminalWidth() int {
// termWidthFunc returns the terminal width.
// Replaced in tests to avoid depending on a real TTY.
var termWidthFunc = func() (int, error) {
width, _, err := term.GetSize(int(os.Stdout.Fd()))
return width, err
}
func getTerminalWidth() int {
width, err := termWidthFunc()
if err != nil {
return 80
}