Skip to content

Commit af72757

Browse files
committed
add tests on reading actions registry from local file
1 parent 5e32104 commit af72757

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

scripts/generate-popular-actions/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func newGen(stdout, stderr, dbgout io.Writer) *gen {
7676
func (g *gen) registry() ([]*registry, error) {
7777
var a []*registry
7878
if err := json.Unmarshal(g.rawRegistry, &a); err != nil {
79-
return nil, err
79+
return nil, fmt.Errorf("could not parse the local action registry file as JSON: %w", err)
8080
}
8181
return a, nil
8282
}
@@ -291,7 +291,7 @@ func (g *gen) readJSONL(file string) (map[string]*actionlint.ActionMetadata, err
291291
}
292292
var j actionOutput
293293
if err := json.Unmarshal(l, &j); err != nil {
294-
return nil, fmt.Errorf("could not parse line as JSON for action metadata in file %s: %s", file, err)
294+
return nil, fmt.Errorf("could not parse line as JSON for action metadata in file %s: %w", file, err)
295295
}
296296
ret[j.Spec] = j.Meta
297297
}
@@ -432,7 +432,7 @@ Flags:`)
432432
if registry != "" {
433433
b, err := os.ReadFile(registry)
434434
if err != nil {
435-
fmt.Fprintf(g.stderr, "could not read actions registry from JSON file: %s\n", err)
435+
fmt.Fprintf(g.stderr, "could not read the file for actions registry: %s\n", err)
436436
return 1
437437
}
438438
g.rawRegistry = b

scripts/generate-popular-actions/main_test.go

+36-6
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ func TestDetectNoRelease(t *testing.T) {
292292
t.Fatal("exit status is non-zero:", status)
293293
}
294294
out := stdout.String()
295-
if out != "" {
296-
t.Fatalf("stdout is not empty: %q", out)
295+
if out != "No new release was found\n" {
296+
t.Fatalf("stdout is unexpected: %q", out)
297297
}
298298
})
299299
}
@@ -373,11 +373,10 @@ func TestWriteError(t *testing.T) {
373373
}
374374

375375
func TestCouldNotFetch(t *testing.T) {
376-
stdout := testErrorWriter{}
377376
stderr := &bytes.Buffer{}
378377
f := filepath.Join("testdata", "registry", "repo_not_found.json")
379378

380-
status := newGen(stdout, stderr, io.Discard).run([]string{"test", "-r", f})
379+
status := newGen(io.Discard, stderr, io.Discard).run([]string{"test", "-r", f})
381380
if status == 0 {
382381
t.Fatal("exit status is unexpectedly zero")
383382
}
@@ -400,10 +399,9 @@ func TestInvalidCommandArgs(t *testing.T) {
400399

401400
for _, tc := range testCases {
402401
t.Run(strings.Join(tc.args, " "), func(t *testing.T) {
403-
stdout := testErrorWriter{}
404402
stderr := &bytes.Buffer{}
405403

406-
status := newGen(stdout, stderr, io.Discard).run(tc.args)
404+
status := newGen(io.Discard, stderr, io.Discard).run(tc.args)
407405
if status == 0 {
408406
t.Fatal("exit status is unexpectedly zero")
409407
}
@@ -430,6 +428,38 @@ func TestDetectErrorBadRequest(t *testing.T) {
430428
}
431429
}
432430

431+
func TestReadActionRegistryError(t *testing.T) {
432+
tests := []struct {
433+
file string
434+
want string
435+
} {
436+
{
437+
file: "broken.json",
438+
want: "could not parse the local action registry file as JSON:",
439+
},
440+
{
441+
file: "oops-this-file-doesnt-exist.json",
442+
want: "could not read the file for actions registry:",
443+
},
444+
}
445+
446+
for _, tc := range tests {
447+
t.Run(tc.file, func(t *testing.T) {
448+
stdout := io.Discard
449+
stderr := &bytes.Buffer{}
450+
f := filepath.Join("testdata", "registry", tc.file)
451+
status := newGen(stdout, stderr, io.Discard).run([]string{"test", "-d", "-r", f})
452+
if status != 1 {
453+
t.Fatal("exit status is not 1:", status)
454+
}
455+
out := stderr.String()
456+
if !strings.Contains(out, tc.want) {
457+
t.Fatalf("wanted %q in stderr: %q", tc.want, out)
458+
}
459+
})
460+
}
461+
}
462+
433463
func TestActionBuildRawURL(t *testing.T) {
434464
a := &registry{Slug: "foo/bar"}
435465
have := a.rawURL("v1")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
{
3+
"slug": "rhysd/action-setup-vim",
4+
"tags": ["v0"],
5+
"next": "v1"

0 commit comments

Comments
 (0)