grokkit/cmd/root_test.go
Greg Gauthier d377e6b345
All checks were successful
CI / Test (push) Successful in 41s
CI / Lint (push) Successful in 27s
CI / Build (push) Successful in 21s
test: add tests for recipe package and disable parallelism in cmd tests
- Introduce new test suite in internal/recipe/recipe_test.go covering recipe loading, parameter overrides, safety checks, work dir resolution, file discovery, and unified patch creation.
- Remove t.Parallel() from tests in cmd/changelog_test.go, cmd/root_test.go, and cmd/scaffold_test.go that modify global state (e.g., os.Args, HOME, or use os.Chdir()) to avoid race conditions and ensure test isolation.
2026-03-08 12:53:01 +00:00

54 lines
1.2 KiB
Go

package cmd
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
)
func rootSetHomeForTest(t *testing.T, dir string) {
t.Helper()
if err := os.Setenv("HOME", dir); err != nil {
t.Fatal(err)
}
}
func TestExecute(t *testing.T) {
// Not using t.Parallel() because it modifies os.Args and environment variables (HOME)
tests := []struct {
name string
args []string
exitCode int
wantLevel string
}{
{"version", []string{"version"}, 0, ""},
{"help", []string{}, 0, ""},
{"debug flag", []string{"version", "--debug"}, 0, "debug"},
{"verbose flag", []string{"version", "-v"}, 0, "info"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
rootSetHomeForTest(t, tmpDir)
oldArgs := os.Args
os.Args = append([]string{"grokkit"}, tt.args...)
defer func() { os.Args = oldArgs }()
Execute()
if tt.wantLevel != "" {
logFile := filepath.Join(tmpDir, ".config", "grokkit", "grokkit.log")
content, err := os.ReadFile(logFile)
if err != nil {
t.Error(err)
return
}
str := string(content)
if !strings.Contains(str, fmt.Sprintf(`"log_level":"%s"`, tt.wantLevel)) {
t.Errorf("log level not %s:\n%s", tt.wantLevel, str)
}
}
})
}
}