Skip to content

Commit

Permalink
internal/shaderlister: output [] instead of null when no shaders are …
Browse files Browse the repository at this point in the history
…found

Updates #3157
  • Loading branch information
hajimehoshi committed Nov 10, 2024
1 parent 9b84981 commit 75d7a26
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
6 changes: 4 additions & 2 deletions internal/shaderlister/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions internal/shaderlister/shaderlister_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"os/exec"
"slices"
"strings"
"testing"
)

Expand Down Expand Up @@ -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)
}
}

0 comments on commit 75d7a26

Please sign in to comment.