From 75d7a26fcdb6008dd0d679490a1fcacd906bf2eb Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 10 Nov 2024 23:05:36 +0900 Subject: [PATCH] internal/shaderlister: output [] instead of null when no shaders are found Updates #3157 --- internal/shaderlister/main.go | 6 ++++-- internal/shaderlister/shaderlister_test.go | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/internal/shaderlister/main.go b/internal/shaderlister/main.go index f6937c743..60c10523a 100644 --- a/internal/shaderlister/main.go +++ b/internal/shaderlister/main.go @@ -56,13 +56,15 @@ func xmain() error { } pkgs, err := packages.Load(&packages.Config{ - Mode: packages.NeedName | packages.NeedTypes | packages.NeedImports | packages.NeedDeps | packages.NeedSyntax | packages.NeedTypesInfo, + Mode: packages.NeedName | packages.NeedImports | packages.NeedDeps | packages.NeedTypes | packages.NeedSyntax | packages.NeedTypesInfo, }, flag.Args()...) if err != nil { return err } - var shaders []Shader + // Collect shader information. + // Even if no shader is found, the output should be a JSON array. Start with an empty slice, not nil. + shaders := []Shader{} packages.Visit(pkgs, func(pkg *packages.Package) bool { path := pkg.PkgPath diff --git a/internal/shaderlister/shaderlister_test.go b/internal/shaderlister/shaderlister_test.go index a2a189c00..86e4d3ac0 100644 --- a/internal/shaderlister/shaderlister_test.go +++ b/internal/shaderlister/shaderlister_test.go @@ -20,6 +20,7 @@ import ( "fmt" "os/exec" "slices" + "strings" "testing" ) @@ -63,3 +64,19 @@ func TestRun(t *testing.T) { } } } + +func TestEmpty(t *testing.T) { + cmd := exec.Command("go", "run", "github.com/hajimehoshi/ebiten/v2/internal/shaderlister", "github.com/ebitengine/purego") + out, err := cmd.Output() + if err != nil { + if err, ok := err.(*exec.ExitError); ok { + t.Fatalf("Error: %v\n%s", err, err.Stderr) + } + t.Fatal(err) + } + + // Check the output is `[]`, not `null`. + if got, want := strings.TrimSpace(string(out)), "[]"; got != want { + t.Errorf("output: got: %q, want: %q", got, want) + } +}