11package builtinprocessors
22
33import (
4+ "os"
5+ "path/filepath"
46 "testing"
57 "testing/fstest"
68
@@ -65,7 +67,7 @@ action:
6567 - processor: config.GetValue
6668`
6769
68- const testTplConfigGet = `
70+ const testTplConfig = `
6971action:
7072 title: test config
7173runtime:
@@ -81,7 +83,7 @@ runtime:
8183 - '{{ config "my.missing" | default "bar" }}'
8284`
8385
84- const testTplConfigGetMissing = `
86+ const testTplConfigMissing = `
8587action:
8688 title: test config
8789runtime:
@@ -91,7 +93,7 @@ runtime:
9193 - '{{ config "my.missing" }}'
9294`
9395
94- const testTplConfigGetBadArgs = `
96+ const testTplConfigBadArgs = `
9597action:
9698 title: test config
9799runtime:
110112 null: null
111113`
112114
115+ const testTplYq = `
116+ action:
117+ title: test yq
118+ options:
119+ - name: yamlPath
120+ default: "foo/bar.yaml"
121+ runtime:
122+ type: container
123+ image: alpine
124+ command:
125+ - '{{ yq .yamlPath "foo.bar" }}'
126+ - '{{ index (yq .yamlPath "foo") "bar" }}'
127+ - '{{ yq .yamlPath "foo.null" | default "foo" }}'
128+ - '{{ yq .yamlPath "my.missing" | default "bar" }}'
129+ `
130+
131+ const testTplYqMissing = `
132+ action:
133+ title: test yq
134+ options:
135+ - name: yamlPath
136+ default: "foo/bar.yaml"
137+ runtime:
138+ type: container
139+ image: alpine
140+ command:
141+ - '{{ yq .yamlPath "my.missing" }}'
142+ `
143+
144+ const testTplYqBadArgs = `
145+ action:
146+ title: test yq
147+ options:
148+ - name: yamlPath
149+ default: "foo/bar.yaml"
150+ runtime:
151+ type: container
152+ image: alpine
153+ command:
154+ - '{{ yq "1" "2" "3" }}'
155+ `
156+
157+ const testYqFileContent = `
158+ foo:
159+ bar: buz
160+ null: null
161+ `
162+
113163func testConfigFS (s string ) launchr.Config {
114164 m := fstest.MapFS {
115165 "config.yaml" : & fstest.MapFile {Data : []byte (s )},
@@ -167,15 +217,60 @@ func Test_ConfigTemplateFunc(t *testing.T) {
167217 }
168218
169219 tt := []testCase {
170- {Name : "valid" , Yaml : testTplConfigGet , Exp : []string {"my_str" , "42" , "true" , "[1 2 3]" , "2" , "foo" , "bar" }},
171- {Name : "key not found" , Yaml : testTplConfigGetMissing , Exp : []string {"<config key not found \" my.missing\" >" }},
172- {Name : "incorrect call" , Yaml : testTplConfigGetBadArgs , Err : "wrong number of args for config: want 1 got 2" },
220+ {Name : "valid" , Yaml : testTplConfig , Exp : []string {"my_str" , "42" , "true" , "[1 2 3]" , "2" , "foo" , "bar" }},
221+ {Name : "key not found" , Yaml : testTplConfigMissing , Exp : []string {"<key not found \" my.missing\" >" }},
222+ {Name : "incorrect call" , Yaml : testTplConfigBadArgs , Err : "wrong number of args for config: want 1 got 2" },
223+ }
224+ for _ , tt := range tt {
225+ tt := tt
226+ t .Run (tt .Name , func (t * testing.T ) {
227+ t .Parallel ()
228+ a := action .NewFromYAML (tt .Name , []byte (tt .Yaml ))
229+ a .SetServices (svc )
230+ err := a .EnsureLoaded ()
231+ if tt .Err != "" {
232+ require .ErrorContains (t , err , tt .Err )
233+ return
234+ }
235+ require .NoError (t , err )
236+ rdef := a .RuntimeDef ()
237+ assert .Equal (t , tt .Exp , []string (rdef .Container .Command ))
238+ })
239+ }
240+ }
241+
242+ func Test_YqTemplateFunc (t * testing.T ) {
243+ // Prepare services.
244+ tp := action .NewTemplateProcessors ()
245+ addValueProcessors (tp , nil )
246+ svc := launchr .NewServiceManager ()
247+ svc .Add (tp )
248+
249+ // Prepare test data.
250+ wd := t .TempDir ()
251+ err := os .MkdirAll (filepath .Join (wd , "foo" ), 0750 )
252+ require .NoError (t , err )
253+ err = os .WriteFile (filepath .Join (wd , "foo" , "bar.yaml" ), []byte (testYqFileContent ), 0600 )
254+ require .NoError (t , err )
255+
256+ type testCase struct {
257+ Name string
258+ Yaml string
259+ Exp []string
260+ Err string
261+ }
262+
263+ tt := []testCase {
264+ {Name : "valid" , Yaml : testTplYq , Exp : []string {"buz" , "buz" , "foo" , "bar" }},
265+ {Name : "key not found" , Yaml : testTplYqMissing , Exp : []string {"<key not found \" foo/bar.yaml:my.missing\" >" }},
266+ {Name : "incorrect call" , Yaml : testTplYqBadArgs , Err : "wrong number of args for yq: want 2 got 3" },
173267 }
174268 for _ , tt := range tt {
175269 tt := tt
176270 t .Run (tt .Name , func (t * testing.T ) {
177271 t .Parallel ()
178272 a := action .NewFromYAML (tt .Name , []byte (tt .Yaml ))
273+ a .SetWorkDir (wd )
179274 a .SetServices (svc )
180275 err := a .EnsureLoaded ()
181276 if tt .Err != "" {
0 commit comments