Skip to content

Commit 5749deb

Browse files
committed
check name of action cannot be empty
1 parent 38e07b4 commit 5749deb

File tree

4 files changed

+48
-19
lines changed

4 files changed

+48
-19
lines changed

action_metadata.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,12 @@ func (c *LocalActionsCache) FindMetadata(spec string) (*ActionMetadata, bool, er
236236
meta.file = f
237237
meta.dir = dir
238238

239-
c.debug("New metadata parsed from action %s: %v", dir, &meta)
239+
if meta.Name == "" {
240+
c.writeCache(spec, nil) // Remember action was invalid
241+
return nil, false, fmt.Errorf("name is required in action metadata %q", meta.Path())
242+
}
240243

244+
c.debug("New metadata parsed from action %s: %v", dir, &meta)
241245
c.writeCache(spec, &meta)
242246
return &meta, false, nil
243247
}

action_metadata_test.go

+37-18
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func testCheckCachedFlag(t *testing.T, want, have bool) {
7474
}
7575
}
7676

77-
func TestLocalActionsFindMetadata(t *testing.T) {
77+
func TestLocalActionsFindMetadataOK(t *testing.T) {
7878
testdir := filepath.Join("testdata", "action_metadata")
7979
proj := &Project{testdir, nil}
8080
c := NewLocalActionsCache(proj, nil)
@@ -295,27 +295,46 @@ func TestLocalActionsNullCache(t *testing.T) {
295295
// Error cases
296296

297297
func TestLocalActionsBrokenMetadata(t *testing.T) {
298+
tests := []struct {
299+
spec string
300+
want string
301+
}{
302+
{
303+
spec: "./broken",
304+
want: "could not parse action metadata",
305+
},
306+
{
307+
spec: "./no_name",
308+
want: "name is required in action metadata ",
309+
},
310+
}
311+
298312
proj := &Project{filepath.Join("testdata", "action_metadata"), nil}
299313
c := NewLocalActionsCache(proj, nil)
300-
m, cached, err := c.FindMetadata("./broken")
301-
if err == nil {
302-
t.Fatal("error was not returned", m)
303-
}
304-
if !strings.Contains(err.Error(), "could not parse action metadata") {
305-
t.Fatal("unexpected error:", err)
306-
}
307-
testCheckCachedFlag(t, false, cached)
308314

309-
// Second try does not return error, but metadata is also nil not to show the same error from
310-
// multiple rules.
311-
m, cached, err = c.FindMetadata("./broken")
312-
if err != nil {
313-
t.Fatal("error was returned at second try", err)
314-
}
315-
if m != nil {
316-
t.Fatal("metadata was not nil even if it does not exist", m)
315+
for _, tc := range tests {
316+
t.Run(tc.spec, func(t *testing.T) {
317+
m, cached, err := c.FindMetadata(tc.spec)
318+
if err == nil {
319+
t.Fatal("error was not returned", m)
320+
}
321+
if !strings.Contains(err.Error(), tc.want) {
322+
t.Fatalf("expected error message %q to contain %q", err, tc.want)
323+
}
324+
testCheckCachedFlag(t, false, cached)
325+
326+
// Second try does not return error, but metadata is also nil not to show the same error from
327+
// multiple rules.
328+
m, cached, err = c.FindMetadata(tc.spec)
329+
if err != nil {
330+
t.Fatal("error was returned at second try", err)
331+
}
332+
if m != nil {
333+
t.Fatal("metadata was not nil even if it does not exist", m)
334+
}
335+
testCheckCachedFlag(t, true, cached)
336+
})
317337
}
318-
testCheckCachedFlag(t, true, cached)
319338
}
320339

321340
func TestLocalActionsDuplicateInputsOutputs(t *testing.T) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
author: 'rhysd <https://rhysd.github.io>'
2+
description: 'name is missing'
3+
4+
runs:
5+
using: 'node20'
6+
main: index.js

testdata/action_metadata/no_name/index.js

Whitespace-only changes.

0 commit comments

Comments
 (0)