From af727576c2da3b263adefddcb5742feeed44d438 Mon Sep 17 00:00:00 2001 From: rhysd Date: Tue, 5 Mar 2024 19:38:49 +0900 Subject: [PATCH] add tests on reading actions registry from local file --- scripts/generate-popular-actions/main.go | 6 +-- scripts/generate-popular-actions/main_test.go | 42 ++++++++++++++++--- .../testdata/registry/broken.json | 5 +++ 3 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 scripts/generate-popular-actions/testdata/registry/broken.json diff --git a/scripts/generate-popular-actions/main.go b/scripts/generate-popular-actions/main.go index 5a06e5a1d..22175f179 100644 --- a/scripts/generate-popular-actions/main.go +++ b/scripts/generate-popular-actions/main.go @@ -76,7 +76,7 @@ func newGen(stdout, stderr, dbgout io.Writer) *gen { func (g *gen) registry() ([]*registry, error) { var a []*registry if err := json.Unmarshal(g.rawRegistry, &a); err != nil { - return nil, err + return nil, fmt.Errorf("could not parse the local action registry file as JSON: %w", err) } return a, nil } @@ -291,7 +291,7 @@ func (g *gen) readJSONL(file string) (map[string]*actionlint.ActionMetadata, err } var j actionOutput if err := json.Unmarshal(l, &j); err != nil { - return nil, fmt.Errorf("could not parse line as JSON for action metadata in file %s: %s", file, err) + return nil, fmt.Errorf("could not parse line as JSON for action metadata in file %s: %w", file, err) } ret[j.Spec] = j.Meta } @@ -432,7 +432,7 @@ Flags:`) if registry != "" { b, err := os.ReadFile(registry) if err != nil { - fmt.Fprintf(g.stderr, "could not read actions registry from JSON file: %s\n", err) + fmt.Fprintf(g.stderr, "could not read the file for actions registry: %s\n", err) return 1 } g.rawRegistry = b diff --git a/scripts/generate-popular-actions/main_test.go b/scripts/generate-popular-actions/main_test.go index ce854c4c6..9eea48597 100644 --- a/scripts/generate-popular-actions/main_test.go +++ b/scripts/generate-popular-actions/main_test.go @@ -292,8 +292,8 @@ func TestDetectNoRelease(t *testing.T) { t.Fatal("exit status is non-zero:", status) } out := stdout.String() - if out != "" { - t.Fatalf("stdout is not empty: %q", out) + if out != "No new release was found\n" { + t.Fatalf("stdout is unexpected: %q", out) } }) } @@ -373,11 +373,10 @@ func TestWriteError(t *testing.T) { } func TestCouldNotFetch(t *testing.T) { - stdout := testErrorWriter{} stderr := &bytes.Buffer{} f := filepath.Join("testdata", "registry", "repo_not_found.json") - status := newGen(stdout, stderr, io.Discard).run([]string{"test", "-r", f}) + status := newGen(io.Discard, stderr, io.Discard).run([]string{"test", "-r", f}) if status == 0 { t.Fatal("exit status is unexpectedly zero") } @@ -400,10 +399,9 @@ func TestInvalidCommandArgs(t *testing.T) { for _, tc := range testCases { t.Run(strings.Join(tc.args, " "), func(t *testing.T) { - stdout := testErrorWriter{} stderr := &bytes.Buffer{} - status := newGen(stdout, stderr, io.Discard).run(tc.args) + status := newGen(io.Discard, stderr, io.Discard).run(tc.args) if status == 0 { t.Fatal("exit status is unexpectedly zero") } @@ -430,6 +428,38 @@ func TestDetectErrorBadRequest(t *testing.T) { } } +func TestReadActionRegistryError(t *testing.T) { + tests := []struct { + file string + want string + } { + { + file: "broken.json", + want: "could not parse the local action registry file as JSON:", + }, + { + file: "oops-this-file-doesnt-exist.json", + want: "could not read the file for actions registry:", + }, + } + + for _, tc := range tests { + t.Run(tc.file, func(t *testing.T) { + stdout := io.Discard + stderr := &bytes.Buffer{} + f := filepath.Join("testdata", "registry", tc.file) + status := newGen(stdout, stderr, io.Discard).run([]string{"test", "-d", "-r", f}) + if status != 1 { + t.Fatal("exit status is not 1:", status) + } + out := stderr.String() + if !strings.Contains(out, tc.want) { + t.Fatalf("wanted %q in stderr: %q", tc.want, out) + } + }) + } +} + func TestActionBuildRawURL(t *testing.T) { a := ®istry{Slug: "foo/bar"} have := a.rawURL("v1") diff --git a/scripts/generate-popular-actions/testdata/registry/broken.json b/scripts/generate-popular-actions/testdata/registry/broken.json new file mode 100644 index 000000000..71a1de66e --- /dev/null +++ b/scripts/generate-popular-actions/testdata/registry/broken.json @@ -0,0 +1,5 @@ +[ + { + "slug": "rhysd/action-setup-vim", + "tags": ["v0"], + "next": "v1"