grokkit/cmd/edit_test.go

31 lines
630 B
Go
Raw Normal View History

package cmd
import (
"os"
"strings"
"testing"
)
func TestEditCommand(t *testing.T) {
tmpfile, err := os.CreateTemp("", "test_edit_*.go")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())
original := []byte("package main\n\nfunc hello() {}\n")
os.WriteFile(tmpfile.Name(), original, 0644)
cmd := rootCmd
cmd.SetArgs([]string{"edit", tmpfile.Name(), "add a comment at the top"})
if err := cmd.Execute(); err != nil {
t.Fatal(err)
}
content, _ := os.ReadFile(tmpfile.Name())
if !strings.HasPrefix(string(content), "//") {
t.Errorf("Expected a comment at the very top. Got:\n%s", content)
}
}