internal/shaderlister: output [] instead of null when no shaders are found

Updates #3157
This commit is contained in:
Hajime Hoshi 2024-11-10 23:05:36 +09:00
parent 9b849819f3
commit 75d7a26fcd
2 changed files with 21 additions and 2 deletions

View File

@ -56,13 +56,15 @@ func xmain() error {
} }
pkgs, err := packages.Load(&packages.Config{ 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()...) }, flag.Args()...)
if err != nil { if err != nil {
return err 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 { packages.Visit(pkgs, func(pkg *packages.Package) bool {
path := pkg.PkgPath path := pkg.PkgPath

View File

@ -20,6 +20,7 @@ import (
"fmt" "fmt"
"os/exec" "os/exec"
"slices" "slices"
"strings"
"testing" "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)
}
}