Skip to content

Commit

Permalink
add tests on reading actions registry from local file
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Mar 5, 2024
1 parent 5e32104 commit af72757
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
6 changes: 3 additions & 3 deletions scripts/generate-popular-actions/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
42 changes: 36 additions & 6 deletions scripts/generate-popular-actions/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
Expand Down Expand Up @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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 := &registry{Slug: "foo/bar"}
have := a.rawURL("v1")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{
"slug": "rhysd/action-setup-vim",
"tags": ["v0"],
"next": "v1"

0 comments on commit af72757

Please sign in to comment.