1// SPDX-License-Identifier: MIT
2
3package template
4
5import (
6 "reflect"
7 "strings"
8 "testing"
9)
10
11func TestWorkflowSchemaLoads(t *testing.T) {
12 s, err := WorkflowSchema()
13 if err != nil {
14 t.Fatal(err)
15 }
16 if s.Version != "workflow-v1.0" {
17 t.Errorf("version = %q", s.Version)
18 }
19 root := s.Definition("workflow-root-strict")
20 if root == nil || !root.Properties["jobs"].Required || !root.Properties["on"].Required {
21 t.Fatalf("workflow-root-strict = %+v", root)
22 }
23 if d := s.Definition("push-string"); d == nil || d.Constant != "push" {
24 t.Errorf("push-string = %+v", d)
25 }
26}
27
28func TestAllowedContext(t *testing.T) {
29 s, err := WorkflowSchema()
30 if err != nil {
31 t.Fatal(err)
32 }
33 di := newDefinitionInfo(s, nil, s.Definition("step-if"))
34 if got, want := di.NamedContexts(), []string{"github", "inputs", "vars", "needs", "strategy", "matrix", "steps", "job", "runner", "env"}; !reflect.DeepEqual(got, want) {
35 t.Errorf("named = %v", got)
36 }
37 var fs []string
38 for _, f := range di.Functions() {
39 fs = append(fs, f.Name)
40 }
41 if got, want := strings.Join(fs, ","), "always,failure,cancelled,success,hashFiles"; got != want {
42 t.Errorf("funcs = %s", got)
43 }
44 ji := newDefinitionInfo(s, nil, s.Definition("job-if"))
45 for _, f := range ji.Functions() {
46 if f.Name == "success" && f.MaxArgs != -1 {
47 t.Errorf("job-if success max = %d", f.MaxArgs)
48 }
49 }
50}
51
52func TestParseFuncEntry(t *testing.T) {
53 for in, ok := range map[string]bool{
54 "hashFiles(1,255)": true, "always(0,0)": true, "failure(0,MAX)": true,
55 "github": false, "f(1)": false, "f(a,2)": false, "(1,2)": false, "f(1,2) ": false,
56 } {
57 if _, got := parseFuncEntry(in); got != ok {
58 t.Errorf("%q: %v", in, got)
59 }
60 }
61}
62
63func TestParseSchemaErrors(t *testing.T) {
64 for in, want := range map[string]string{
65 `{"definitions":{"a":{"sequence":{}}}}`: "'a' does not defined 'item-type'",
66 `{"definitions":{"a":{"mapping":{"loose-key-type":"string"}}}}`: "Property 'loose-key-type' is defined but 'loose-value-type' is not defined on 'a'",
67 `{"definitions":{"a":{"one-of":["zzz"]}}}`: "Schema definition 'zzz' not found",
68 `{"definitions":{"a":{"description":"x"}}}`: "Not enough information to construct definition 'a'",
69 `{"definitions":{"a b":{"string":{}}}}`: "Invalid definition name 'a b'",
70 `{"definitions":{"a":{"string":{"constant":"x","require-non-empty":true}}}}`: "cannot both be set",
71 `{"foo":1}`: "Unexpected value 'foo'",
72 } {
73 _, err := ParseSchema([]byte(in))
74 if err == nil || !strings.Contains(err.Error(), want) {
75 t.Errorf("%s: err = %v, want %q", in, err, want)
76 }
77 }
78}