e2d4214405
ci / vet, staticcheck, test, build (push) Failing after 3m50s
- Add Hugo + Geekdoc documentation site (docs/): installation, CLI reference, configuration, CI integration, and changelog pages; explicit menu bundle nav so all pages appear in the sidebar on every page - Add Gitea CI workflow (.gitea/workflows/docs.yml): builds on push to main when docs/** changes, deploys minified site to gh-pages branch - Add docs:setup / docs:serve / docs:build Taskfile tasks; theme bundle downloaded at build time (not committed) - Add FuzzUpdate to internal/changelog and FuzzWriteVersion to internal/node to complete fuzz coverage for all file-rewriting packages - Add fuzzing completeness guidelines to CLAUDE.md: authoritative table, exempt-package rationale, seed corpus rules Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
115 lines
3.0 KiB
Go
115 lines
3.0 KiB
Go
package node
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func writeJSON(t *testing.T, content string) string {
|
|
t.Helper()
|
|
path := filepath.Join(t.TempDir(), "package.json")
|
|
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return path
|
|
}
|
|
|
|
const simplePackage = `{
|
|
"name": "my-app",
|
|
"version": "1.2.3",
|
|
"description": "test"
|
|
}`
|
|
|
|
func TestReadVersion(t *testing.T) {
|
|
got, err := ReadVersion(writeJSON(t, simplePackage))
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if got != "1.2.3" {
|
|
t.Errorf("got %q, want 1.2.3", got)
|
|
}
|
|
}
|
|
|
|
func TestReadVersionMissingFile(t *testing.T) {
|
|
_, err := ReadVersion(filepath.Join(t.TempDir(), "package.json"))
|
|
if err == nil {
|
|
t.Error("expected error for missing file")
|
|
}
|
|
}
|
|
|
|
func TestReadVersionInvalidJSON(t *testing.T) {
|
|
_, err := ReadVersion(writeJSON(t, `{not valid json`))
|
|
if err == nil {
|
|
t.Error("expected error for invalid JSON")
|
|
}
|
|
}
|
|
|
|
func TestReadVersionNoVersionField(t *testing.T) {
|
|
_, err := ReadVersion(writeJSON(t, `{"name":"app"}`))
|
|
if err == nil {
|
|
t.Error("expected error when version field is absent")
|
|
}
|
|
}
|
|
|
|
func TestWriteVersion(t *testing.T) {
|
|
path := writeJSON(t, simplePackage)
|
|
if err := WriteVersion(path, "1.2.3", "1.2.4"); err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
data, _ := os.ReadFile(path)
|
|
if !strings.Contains(string(data), `"version": "1.2.4"`) {
|
|
t.Errorf("expected updated version in file; got:\n%s", data)
|
|
}
|
|
// name and description must be preserved
|
|
if !strings.Contains(string(data), `"name": "my-app"`) {
|
|
t.Error("name field was lost")
|
|
}
|
|
}
|
|
|
|
func TestWriteVersionMissingFile(t *testing.T) {
|
|
err := WriteVersion(filepath.Join(t.TempDir(), "package.json"), "1.0.0", "1.0.1")
|
|
if err == nil {
|
|
t.Error("expected error for missing file")
|
|
}
|
|
}
|
|
|
|
func TestWriteVersionNotFound(t *testing.T) {
|
|
err := WriteVersion(writeJSON(t, simplePackage), "9.9.9", "9.9.10")
|
|
if err == nil {
|
|
t.Error("expected error when old version not found")
|
|
}
|
|
}
|
|
|
|
// FuzzWriteVersion verifies WriteVersion never panics on arbitrary content or version strings.
|
|
func FuzzWriteVersion(f *testing.F) {
|
|
f.Add(simplePackage, "1.2.3", "1.2.4")
|
|
f.Add(`{"version":"0.0.1"}`, "0.0.1", "0.0.2")
|
|
f.Add(`{}`, "1.0.0", "1.0.1")
|
|
f.Add("", "1.0.0", "1.0.1")
|
|
f.Add(`{"name":"app","version":"1.0.0","version":"dup"}`, "1.0.0", "1.0.1")
|
|
|
|
f.Fuzz(func(t *testing.T, content, oldVersion, newVersion string) {
|
|
path := filepath.Join(t.TempDir(), "package.json")
|
|
os.WriteFile(path, []byte(content), 0644) //nolint:errcheck
|
|
WriteVersion(path, oldVersion, newVersion) //nolint:errcheck
|
|
})
|
|
}
|
|
|
|
// FuzzReadVersion verifies ReadVersion never panics on arbitrary content.
|
|
func FuzzReadVersion(f *testing.F) {
|
|
f.Add(simplePackage)
|
|
f.Add(`{}`)
|
|
f.Add(`{"version":"1.0.0"}`)
|
|
f.Add(`not json at all`)
|
|
f.Add(``)
|
|
f.Add("\x00\xff")
|
|
|
|
f.Fuzz(func(t *testing.T, content string) {
|
|
path := filepath.Join(t.TempDir(), "package.json")
|
|
os.WriteFile(path, []byte(content), 0644) //nolint:errcheck
|
|
ReadVersion(path) //nolint:errcheck
|
|
})
|
|
}
|