Skip to content

Commit fae5ff3

Browse files
🌱 Unit tests for fileparser
Included additional tests for fileparser. #986 Signed-off-by: naveen <172697+naveensrinivasan@users.noreply.github.com>
1 parent 58865e9 commit fae5ff3

File tree

1 file changed

+207
-0
lines changed

1 file changed

+207
-0
lines changed

checks/fileparser/github_workflow_test.go

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,210 @@ func TestIsGitHubOwnedAction(t *testing.T) {
254254
})
255255
}
256256
}
257+
258+
// TestGetJobName tests the GetJobName function.
259+
func TestGetJobName(t *testing.T) {
260+
t.Parallel()
261+
type args struct {
262+
job *actionlint.Job
263+
}
264+
var name actionlint.String
265+
name.Value = "foo"
266+
tests := []struct {
267+
name string
268+
args args
269+
want string
270+
}{
271+
{
272+
name: "job name",
273+
args: args{
274+
job: &actionlint.Job{
275+
Name: &name,
276+
},
277+
},
278+
want: "foo",
279+
},
280+
{
281+
name: "job name is empty",
282+
args: args{
283+
job: &actionlint.Job{},
284+
},
285+
want: "",
286+
},
287+
{
288+
name: "job is nil",
289+
args: args{},
290+
want: "",
291+
},
292+
}
293+
for _, tt := range tests {
294+
tt := tt // Re-initializing variable so it is not changed while executing the closure below
295+
t.Run(tt.name, func(t *testing.T) {
296+
t.Parallel()
297+
if got := GetJobName(tt.args.job); got != tt.want {
298+
t.Errorf("GetJobName() = %v, want %v", got, tt.want)
299+
}
300+
})
301+
}
302+
}
303+
304+
func TestGetStepName(t *testing.T) {
305+
t.Parallel()
306+
type args struct {
307+
step *actionlint.Step
308+
}
309+
var name actionlint.String
310+
name.Value = "foo"
311+
tests := []struct {
312+
name string
313+
args args
314+
want string
315+
}{
316+
{
317+
name: "step name",
318+
args: args{
319+
step: &actionlint.Step{
320+
Name: &name,
321+
},
322+
},
323+
want: "foo",
324+
},
325+
{
326+
name: "step name is empty",
327+
args: args{
328+
step: &actionlint.Step{},
329+
},
330+
want: "",
331+
},
332+
{
333+
name: "step is nil",
334+
args: args{},
335+
want: "",
336+
},
337+
}
338+
for _, tt := range tests {
339+
tt := tt // Re-initializing variable so it is not changed while executing the closure below
340+
t.Run(tt.name, func(t *testing.T) {
341+
t.Parallel()
342+
if got := GetStepName(tt.args.step); got != tt.want {
343+
t.Errorf("GetStepName() = %v, want %v", got, tt.want)
344+
}
345+
})
346+
}
347+
}
348+
349+
func TestIsStepExecKind(t *testing.T) {
350+
t.Parallel()
351+
type args struct {
352+
step *actionlint.Step
353+
kind actionlint.ExecKind
354+
}
355+
tests := []struct {
356+
name string
357+
args args
358+
want bool
359+
}{
360+
{
361+
name: "step is nil",
362+
args: args{},
363+
want: false,
364+
},
365+
}
366+
367+
for _, tt := range tests {
368+
tt := tt // Re-initializing variable so it is not changed while executing the closure below
369+
t.Run(tt.name, func(t *testing.T) {
370+
t.Parallel()
371+
if got := IsStepExecKind(tt.args.step, tt.args.kind); got != tt.want {
372+
t.Errorf("IsStepExecKind() = %v, want %v", got, tt.want)
373+
}
374+
})
375+
}
376+
}
377+
378+
func TestGetLineNumber(t *testing.T) {
379+
t.Parallel()
380+
type args struct {
381+
pos *actionlint.Pos
382+
}
383+
//nolint
384+
tests := []struct {
385+
name string
386+
args args
387+
want uint
388+
}{
389+
{
390+
name: "line number",
391+
args: args{
392+
pos: &actionlint.Pos{
393+
Line: 1,
394+
},
395+
},
396+
want: 1,
397+
},
398+
{
399+
name: "line number is empty",
400+
args: args{
401+
pos: &actionlint.Pos{
402+
Line: 1,
403+
},
404+
},
405+
want: 1,
406+
},
407+
{
408+
name: "pos is nil",
409+
args: args{},
410+
want: 1,
411+
},
412+
}
413+
414+
for _, tt := range tests {
415+
tt := tt // Re-initializing variable so it is not changed while executing the closure below
416+
t.Run(tt.name, func(t *testing.T) {
417+
t.Parallel()
418+
if got := GetLineNumber(tt.args.pos); got != tt.want {
419+
t.Errorf("GetLineNumber() = %v, want %v for %v", got, tt.want, tt.name)
420+
}
421+
})
422+
}
423+
}
424+
425+
func TestFormatActionlintError(t *testing.T) {
426+
t.Parallel()
427+
type args struct {
428+
errs []*actionlint.Error
429+
}
430+
tests := []struct {
431+
name string
432+
args args
433+
wantErr bool
434+
}{
435+
{
436+
name: "no errors",
437+
args: args{
438+
errs: []*actionlint.Error{},
439+
},
440+
wantErr: false,
441+
},
442+
{
443+
name: "one error",
444+
args: args{
445+
errs: []*actionlint.Error{
446+
{
447+
Message: "foo",
448+
},
449+
},
450+
},
451+
wantErr: true,
452+
},
453+
}
454+
for _, tt := range tests {
455+
tt := tt // Re-initializing variable so it is not changed while executing the closure below
456+
t.Run(tt.name, func(t *testing.T) {
457+
t.Parallel()
458+
if err := FormatActionlintError(tt.args.errs); (err != nil) != tt.wantErr {
459+
t.Errorf("FormatActionlintError() error = %v, wantErr %v", err, tt.wantErr)
460+
}
461+
})
462+
}
463+
}

0 commit comments

Comments
 (0)