From 7562910e4e3334bb8091d3e92c2072ab6b763436 Mon Sep 17 00:00:00 2001
From: "github-actions[bot]" <github-actions[bot]`@users`.noreply.github.com>
Date: Fri, 17 Apr 2026 13:17:24 +0000
Subject: [PATCH] test: add normalizeArch tests and expand SanitizeInstance
table-driven tests
- Add Test_normalizeArch in internal/host/host_test.go (11 cases):
all valid arch strings (x86_64/amd64/aarch64/arm64), uppercase variants,
and error cases for unsupported arch and empty input
- Expand TestSanitizeInstance in internal/autostart/sanitize_test.go to
table-driven with 15 cases: basic valid, special-char replacement,
consecutive-special collapse, leading/trailing strip, and error cases
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---
internal/autostart/sanitize_test.go | 53 +++++++++++++++++++++++------
internal/host/host_test.go | 43 +++++++++++++++++++++++
2 files changed, 86 insertions(+), 10 deletions(-)
diff --git a/internal/autostart/sanitize_test.go b/internal/autostart/sanitize_test.go
index 2ef4243..d4dd840 100644
--- a/internal/autostart/sanitize_test.go
+++ b/internal/autostart/sanitize_test.go
@@ -4,16 +4,49 @@ import "testing"
func TestSanitizeInstance(t *testing.T) {
t.Parallel()
- got, err := SanitizeInstance("ci-1")
- if err != nil || got != "ci-1" {
- t.Fatalf("ci-1: got %q err %v", got, err)
+ cases := []struct {
+ input string
+ want string
+ wantErr bool
+ }{
+ // Basic valid inputs pass through unchanged
+ {"ci-1", "ci-1", false},
+ {"runner", "runner", false},
+ {"UPPER", "UPPER", false},
+ {"123", "123", false},
+ // Special characters replaced with dashes
+ {"my_runner-2", "my-runner-2", false},
+ {"hello world", "hello-world", false},
+ {"a@b", "a-b", false},
+ // Consecutive special chars collapse to a single dash
+ {"a___b", "a-b", false},
+ {"a..b", "a-b", false},
+ {"a__b__c", "a-b-c", false},
+ // Leading/trailing special chars are stripped
+ {"_runner_", "runner", false},
+ {".ci.", "ci", false},
+ // Error cases: all chars sanitize away to empty
+ {"---", "", true},
+ {"___", "", true},
+ {"", "", true},
}
- got, err = SanitizeInstance("my_runner-2")
- if err != nil || got != "my-runner-2" {
- t.Fatalf("my_runner-2: got %q err %v", got, err)
- }
- _, err = SanitizeInstance("---")
- if err == nil {
- t.Fatal("expected error for empty after trim")
+ for _, tc := range cases {
+ t.Run(tc.input, func(t *testing.T) {
+ t.Parallel()
+ got, err := SanitizeInstance(tc.input)
+ if tc.wantErr {
+ if err == nil {
+ t.Errorf("SanitizeInstance(%q): expected error, got %q", tc.input, got)
+ }
+ return
+ }
+ if err != nil {
+ t.Errorf("SanitizeInstance(%q): unexpected error: %v", tc.input, err)
+ return
+ }
+ if got != tc.want {
+ t.Errorf("SanitizeInstance(%q) = %q, want %q", tc.input, got, tc.want)
+ }
+ })
}
}
diff --git a/internal/host/host_test.go b/internal/host/host_test.go
index da80c38..d0c7737 100644
--- a/internal/host/host_test.go
+++ b/internal/host/host_test.go
@@ -9,6 +9,49 @@ import (
"github.com/an-lee/gh-sr/internal/config"
)
+func Test_normalizeArch(t *testing.T) {
+ t.Parallel()
+ cases := []struct {
+ input string
+ want string
+ wantErr bool
+ }{
+ // Valid amd64 inputs
+ {"x86_64", "amd64", false},
+ {"amd64", "amd64", false},
+ {"X86_64", "amd64", false},
+ {"AMD64", "amd64", false},
+ // Valid arm64 inputs
+ {"aarch64", "arm64", false},
+ {"arm64", "arm64", false},
+ {"AARCH64", "arm64", false},
+ {"ARM64", "arm64", false},
+ // Error cases
+ {"i386", "", true},
+ {"UNKNOWN", "", true},
+ {"", "", true},
+ }
+ for _, tc := range cases {
+ t.Run(tc.input, func(t *testing.T) {
+ t.Parallel()
+ got, err := normalizeArch(tc.input)
+ if tc.wantErr {
+ if err == nil {
+ t.Errorf("normalizeArch(%q): expected error, got %q", tc.input, got)
+ }
+ return
+ }
+ if err != nil {
+ t.Errorf("normalizeArch(%q): unexpected error: %v", tc.input, err)
+ return
+ }
+ if got != tc.want {
+ t.Errorf("normalizeArch(%q) = %q, want %q", tc.input, got, tc.want)
+ }
+ })
+ }
+}
+
func Test_parseAddr(t *testing.T) {
t.Parallel()
u, a := parseAddr("user@host.example:2222")
--
2.53.0
🤖 This was created by Test Improver, an automated AI assistant focused on improving tests for this repository.
Goal and Rationale
Two pure functions still lacked thorough direct test coverage:
normalizeArch(internal/host/detect.go): maps rawuname -m/ PowerShell arch strings to"amd64"or"arm64". Incorrect normalization silently causes wrong runner binaries — a critical path. Previously zero direct tests.SanitizeInstance(internal/autostart/sanitize.go): sanitizes runner names for systemd/launchd/Windows scheduled task names. Existing test had only 3 cases; edge conditions (consecutive-special collapse, leading/trailing stripping, empty-result errors) were uncovered.Approach
internal/host/host_test.go— addsTest_normalizeArch(11 cases):x86_64,amd64,aarch64,arm64X86_64,AMD64,AARCH64,ARM64i386,UNKNOWN, empty stringinternal/autostart/sanitize_test.go— rewritesTestSanitizeInstance(15 cases):@→ dashesa___b→a-b)_runner_→runner)---,___, empty stringReproducibility
To create a pull request with the changes:
Show patch (141 lines)