-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnested_block_test.go
More file actions
144 lines (132 loc) · 3.14 KB
/
Copy pathnested_block_test.go
File metadata and controls
144 lines (132 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2026 YLD Limited
// SPDX-License-Identifier: Apache-2.0
package gitlab
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/yldio/cinzel/provider"
)
// A nested map is written as an HCL block only where the schema in config.go
// declares one. Guessing from the value's shape produced HCL the parser then
// rejected, and a template body written by the generic writer turned its rule
// blocks into an attribute.
func TestNestedMapsFollowTheHCLSchema(t *testing.T) {
for _, tc := range []struct {
name string
yaml string
wantHCL []string
wantYAML []string
}{
{
name: "cache key is an attribute",
yaml: `build:
script: [make]
cache:
key:
files: [go.sum]
`,
wantHCL: []string{"key = {"},
wantYAML: []string{"key:", "- go.sum"},
},
{
name: "service variables is an attribute",
yaml: `build:
script: [make]
services:
- name: postgres:16
variables:
POSTGRES_DB: test
`,
wantHCL: []string{"variables = {"},
wantYAML: []string{"POSTGRES_DB: test"},
},
{
name: "default retry is an attribute",
yaml: `default:
retry:
max: 2
build:
script: [make]
`,
wantHCL: []string{"retry = {"},
wantYAML: []string{"max: 2"},
},
{
name: "include inputs is an attribute",
yaml: `include:
- component: gitlab.com/c/t@1
inputs:
stage: test
build:
script: [make]
`,
wantHCL: []string{"inputs = {"},
wantYAML: []string{"stage: test"},
},
{
name: "artifacts reports stays a block",
yaml: `build:
script: [make]
artifacts:
reports:
junit: report.xml
`,
wantHCL: []string{"reports {"},
wantYAML: []string{"junit: report.xml"},
},
{
name: "template rules stay blocks",
yaml: `.base:
rules:
- if: always
cache:
paths: [.cache]
build:
extends: [.base]
script: [make]
`,
wantHCL: []string{"rule {", "cache {"},
wantYAML: []string{"rules:", "- if: always"},
},
} {
t.Run(tc.name, func(t *testing.T) {
tmp := t.TempDir()
in := filepath.Join(tmp, ".gitlab-ci.yml")
outDir := filepath.Join(tmp, "hcl")
backDir := filepath.Join(tmp, "yaml")
if err := os.WriteFile(in, []byte(tc.yaml), 0o644); err != nil {
t.Fatal(err)
}
p := New()
if err := p.Unparse(provider.ProviderOps{File: in, OutputDirectory: outDir}); err != nil {
t.Fatalf("Unparse() error = %v", err)
}
hclPath := filepath.Join(outDir, ".gitlab-ci.hcl")
got, err := os.ReadFile(hclPath)
if err != nil {
t.Fatal(err)
}
for _, want := range tc.wantHCL {
if !strings.Contains(string(got), want) {
t.Errorf("HCL missing %q:\n%s", want, got)
}
}
// The HCL has to be readable by cinzel's own parser, and the
// values have to come back.
if err := p.Parse(provider.ProviderOps{File: hclPath, OutputDirectory: backDir}); err != nil {
t.Fatalf("Parse() error = %v\nHCL:\n%s", err, got)
}
back, err := os.ReadFile(filepath.Join(backDir, ".gitlab-ci.yml"))
if err != nil {
t.Fatal(err)
}
for _, want := range tc.wantYAML {
if !strings.Contains(string(back), want) {
t.Errorf("reparsed YAML missing %q:\n%s", want, back)
}
}
})
}
}