File: runner.go

package info (click to toggle)
golang-github-hashicorp-hcl-v2 2.14.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 3,120 kB
  • sloc: ruby: 205; makefile: 72; python: 43; sh: 11
file content (524 lines) | stat: -rw-r--r-- 14,732 bytes parent folder | download
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"sort"
	"strings"

	"github.com/zclconf/go-cty-debug/ctydebug"
	"github.com/zclconf/go-cty/cty"
	"github.com/zclconf/go-cty/cty/convert"
	ctyjson "github.com/zclconf/go-cty/cty/json"

	"github.com/hashicorp/hcl/v2"
	"github.com/hashicorp/hcl/v2/ext/typeexpr"
	"github.com/hashicorp/hcl/v2/hclparse"
)

type Runner struct {
	parser      *hclparse.Parser
	hcldecPath  string
	baseDir     string
	logBegin    LogBeginCallback
	logProblems LogProblemsCallback
}

func (r *Runner) Run() hcl.Diagnostics {
	return r.runDir(r.baseDir)
}

func (r *Runner) runDir(dir string) hcl.Diagnostics {
	var diags hcl.Diagnostics

	infos, err := ioutil.ReadDir(dir)
	if err != nil {
		diags = append(diags, &hcl.Diagnostic{
			Severity: hcl.DiagError,
			Summary:  "Failed to read test directory",
			Detail:   fmt.Sprintf("The directory %q could not be opened: %s.", dir, err),
		})
		return diags
	}

	var tests []string
	var subDirs []string
	for _, info := range infos {
		name := info.Name()
		if strings.HasPrefix(name, ".") {
			continue
		}

		if info.IsDir() {
			subDirs = append(subDirs, name)
		}
		if strings.HasSuffix(name, ".t") {
			tests = append(tests, name)
		}
	}
	sort.Strings(tests)
	sort.Strings(subDirs)

	for _, filename := range tests {
		filename = filepath.Join(dir, filename)
		testDiags := r.runTest(filename)
		diags = append(diags, testDiags...)
	}

	for _, dirName := range subDirs {
		dir := filepath.Join(dir, dirName)
		dirDiags := r.runDir(dir)
		diags = append(diags, dirDiags...)
	}

	return diags
}

func (r *Runner) runTest(filename string) hcl.Diagnostics {
	prettyName := r.prettyTestName(filename)
	tf, diags := r.LoadTestFile(filename)
	if diags.HasErrors() {
		// We'll still log, so it's clearer which test the diagnostics belong to.
		if r.logBegin != nil {
			r.logBegin(prettyName, nil)
		}
		if r.logProblems != nil {
			r.logProblems(prettyName, nil, diags)
			return nil // don't duplicate the diagnostics we already reported
		}
		return diags
	}

	if r.logBegin != nil {
		r.logBegin(prettyName, tf)
	}

	basePath := filename[:len(filename)-2]
	specFilename := basePath + ".hcldec"
	nativeFilename := basePath + ".hcl"
	jsonFilename := basePath + ".hcl.json"

	// We'll add the source code of the spec file to our own parser, even
	// though it'll actually be parsed by the hcldec child process, since that
	// way we can produce nice diagnostic messages if hcldec fails to process
	// the spec file.
	src, err := ioutil.ReadFile(specFilename)
	if err == nil {
		r.parser.AddFile(specFilename, &hcl.File{
			Bytes: src,
		})
	}

	if _, err := os.Stat(specFilename); err != nil {
		diags = append(diags, &hcl.Diagnostic{
			Severity: hcl.DiagError,
			Summary:  "Missing .hcldec file",
			Detail:   fmt.Sprintf("No specification file for test %s: %s.", prettyName, err),
		})
		return diags
	}

	if _, err := os.Stat(nativeFilename); err == nil {
		moreDiags := r.runTestInput(specFilename, nativeFilename, tf)
		diags = append(diags, moreDiags...)
	}

	if _, err := os.Stat(jsonFilename); err == nil {
		moreDiags := r.runTestInput(specFilename, jsonFilename, tf)
		diags = append(diags, moreDiags...)
	}

	if r.logProblems != nil {
		r.logProblems(prettyName, nil, diags)
		return nil // don't duplicate the diagnostics we already reported
	}

	return diags
}

func (r *Runner) runTestInput(specFilename, inputFilename string, tf *TestFile) hcl.Diagnostics {
	// We'll add the source code of the input file to our own parser, even
	// though it'll actually be parsed by the hcldec child process, since that
	// way we can produce nice diagnostic messages if hcldec fails to process
	// the input file.
	src, err := ioutil.ReadFile(inputFilename)
	if err == nil {
		r.parser.AddFile(inputFilename, &hcl.File{
			Bytes: src,
		})
	}

	var diags hcl.Diagnostics

	if tf.ChecksTraversals {
		gotTraversals, moreDiags := r.hcldecVariables(specFilename, inputFilename)
		diags = append(diags, moreDiags...)
		if !moreDiags.HasErrors() {
			expected := tf.ExpectedTraversals
			for _, got := range gotTraversals {
				e := findTraversalSpec(got, expected)
				rng := got.SourceRange()
				if e == nil {
					diags = append(diags, &hcl.Diagnostic{
						Severity: hcl.DiagError,
						Summary:  "Unexpected traversal",
						Detail:   "Detected traversal that is not indicated as expected in the test file.",
						Subject:  &rng,
					})
				} else {
					moreDiags := checkTraversalsMatch(got, inputFilename, e)
					diags = append(diags, moreDiags...)
				}
			}

			// Look for any traversals that didn't show up at all.
			for _, e := range expected {
				if t := findTraversalForSpec(e, gotTraversals); t == nil {
					diags = append(diags, &hcl.Diagnostic{
						Severity: hcl.DiagError,
						Summary:  "Missing expected traversal",
						Detail:   "This expected traversal was not detected.",
						Subject:  e.Traversal.SourceRange().Ptr(),
					})
				}
			}
		}

	}

	val, transformDiags := r.hcldecTransform(specFilename, inputFilename)
	if len(tf.ExpectedDiags) == 0 {
		diags = append(diags, transformDiags...)
		if transformDiags.HasErrors() {
			// If hcldec failed then there's no point in continuing.
			return diags
		}

		if errs := val.Type().TestConformance(tf.ResultType); len(errs) > 0 {
			diags = append(diags, &hcl.Diagnostic{
				Severity: hcl.DiagError,
				Summary:  "Incorrect result type",
				Detail: fmt.Sprintf(
					"Input file %s produced %s, but was expecting %s.",
					inputFilename, typeexpr.TypeString(val.Type()), typeexpr.TypeString(tf.ResultType),
				),
			})
		}

		if tf.Result != cty.NilVal {
			cmpVal, err := convert.Convert(tf.Result, tf.ResultType)
			if err != nil {
				diags = append(diags, &hcl.Diagnostic{
					Severity: hcl.DiagError,
					Summary:  "Incorrect type for result value",
					Detail: fmt.Sprintf(
						"Result does not conform to the given result type: %s.", err,
					),
					Subject: &tf.ResultRange,
				})
			} else {
				if !val.RawEquals(cmpVal) {
					diags = append(diags, &hcl.Diagnostic{
						Severity: hcl.DiagError,
						Summary:  "Incorrect result value",
						Detail: fmt.Sprintf(
							"Input file %s produced %#v, but was expecting %#v.\n\n%s",
							inputFilename, val, tf.Result,
							ctydebug.DiffValues(tf.Result, val),
						),
					})
				}
			}
		}
	} else {
		// We're expecting diagnostics, and so we'll need to correlate the
		// severities and source ranges of our actual diagnostics against
		// what we were expecting.
		type DiagnosticEntry struct {
			Severity hcl.DiagnosticSeverity
			Range    hcl.Range
		}
		got := make(map[DiagnosticEntry]*hcl.Diagnostic)
		want := make(map[DiagnosticEntry]hcl.Range)
		for _, diag := range transformDiags {
			if diag.Subject == nil {
				// Sourceless diagnostics can never be expected, so we'll just
				// pass these through as-is and assume they are hcldec
				// operational errors.
				diags = append(diags, diag)
				continue
			}
			if diag.Subject.Filename != inputFilename {
				// If the problem is for something other than the input file
				// then it can't be expected.
				diags = append(diags, diag)
				continue
			}
			entry := DiagnosticEntry{
				Severity: diag.Severity,
				Range:    *diag.Subject,
			}
			got[entry] = diag
		}
		for _, e := range tf.ExpectedDiags {
			e.Range.Filename = inputFilename // assumed here, since we don't allow any other filename to be expected
			entry := DiagnosticEntry{
				Severity: e.Severity,
				Range:    e.Range,
			}
			want[entry] = e.DeclRange
		}

		for gotEntry, diag := range got {
			if _, wanted := want[gotEntry]; !wanted {
				// Pass through the diagnostic itself so the user can see what happened
				diags = append(diags, diag)
				diags = append(diags, &hcl.Diagnostic{
					Severity: hcl.DiagError,
					Summary:  "Unexpected diagnostic",
					Detail: fmt.Sprintf(
						"No %s diagnostic was expected %s. The unexpected diagnostic was shown above.",
						severityString(gotEntry.Severity), rangeString(gotEntry.Range),
					),
					Subject: gotEntry.Range.Ptr(),
				})
			}
		}

		for wantEntry, declRange := range want {
			if _, gotted := got[wantEntry]; !gotted {
				diags = append(diags, &hcl.Diagnostic{
					Severity: hcl.DiagError,
					Summary:  "Missing expected diagnostic",
					Detail: fmt.Sprintf(
						"No %s diagnostic was generated %s.",
						severityString(wantEntry.Severity), rangeString(wantEntry.Range),
					),
					Subject: declRange.Ptr(),
				})
			}
		}
	}

	return diags
}

func (r *Runner) hcldecTransform(specFile, inputFile string) (cty.Value, hcl.Diagnostics) {
	var diags hcl.Diagnostics
	var outBuffer bytes.Buffer
	var errBuffer bytes.Buffer

	cmd := &exec.Cmd{
		Path: r.hcldecPath,
		Args: []string{
			r.hcldecPath,
			"--spec=" + specFile,
			"--diags=json",
			"--with-type",
			"--keep-nulls",
			inputFile,
		},
		Stdout: &outBuffer,
		Stderr: &errBuffer,
	}
	err := cmd.Run()
	if err != nil {
		if _, isExit := err.(*exec.ExitError); !isExit {
			diags = append(diags, &hcl.Diagnostic{
				Severity: hcl.DiagError,
				Summary:  "Failed to run hcldec",
				Detail:   fmt.Sprintf("Sub-program hcldec failed to start: %s.", err),
			})
			return cty.DynamicVal, diags
		}

		// If we exited unsuccessfully then we'll expect diagnostics on stderr
		moreDiags := decodeJSONDiagnostics(errBuffer.Bytes())
		diags = append(diags, moreDiags...)
		return cty.DynamicVal, diags
	} else {
		// Otherwise, we expect a JSON result value on stdout. Since we used
		// --with-type above, we can decode as DynamicPseudoType to recover
		// exactly the type that was saved, without the usual JSON lossiness.
		val, err := ctyjson.Unmarshal(outBuffer.Bytes(), cty.DynamicPseudoType)
		if err != nil {
			diags = append(diags, &hcl.Diagnostic{
				Severity: hcl.DiagError,
				Summary:  "Failed to parse hcldec result",
				Detail:   fmt.Sprintf("Sub-program hcldec produced an invalid result: %s.", err),
			})
			return cty.DynamicVal, diags
		}
		return val, diags
	}
}

func (r *Runner) hcldecVariables(specFile, inputFile string) ([]hcl.Traversal, hcl.Diagnostics) {
	var diags hcl.Diagnostics
	var outBuffer bytes.Buffer
	var errBuffer bytes.Buffer

	cmd := &exec.Cmd{
		Path: r.hcldecPath,
		Args: []string{
			r.hcldecPath,
			"--spec=" + specFile,
			"--diags=json",
			"--var-refs",
			inputFile,
		},
		Stdout: &outBuffer,
		Stderr: &errBuffer,
	}
	err := cmd.Run()
	if err != nil {
		if _, isExit := err.(*exec.ExitError); !isExit {
			diags = append(diags, &hcl.Diagnostic{
				Severity: hcl.DiagError,
				Summary:  "Failed to run hcldec",
				Detail:   fmt.Sprintf("Sub-program hcldec (evaluating input) failed to start: %s.", err),
			})
			return nil, diags
		}

		// If we exited unsuccessfully then we'll expect diagnostics on stderr
		moreDiags := decodeJSONDiagnostics(errBuffer.Bytes())
		diags = append(diags, moreDiags...)
		return nil, diags
	} else {
		// Otherwise, we expect a JSON description of the traversals on stdout.
		type PosJSON struct {
			Line   int `json:"line"`
			Column int `json:"column"`
			Byte   int `json:"byte"`
		}
		type RangeJSON struct {
			Filename string  `json:"filename"`
			Start    PosJSON `json:"start"`
			End      PosJSON `json:"end"`
		}
		type StepJSON struct {
			Kind  string          `json:"kind"`
			Name  string          `json:"name,omitempty"`
			Key   json.RawMessage `json:"key,omitempty"`
			Range RangeJSON       `json:"range"`
		}
		type TraversalJSON struct {
			Steps []StepJSON `json:"steps"`
		}

		var raw []TraversalJSON
		err := json.Unmarshal(outBuffer.Bytes(), &raw)
		if err != nil {
			diags = append(diags, &hcl.Diagnostic{
				Severity: hcl.DiagError,
				Summary:  "Failed to parse hcldec result",
				Detail:   fmt.Sprintf("Sub-program hcldec (with --var-refs) produced an invalid result: %s.", err),
			})
			return nil, diags
		}

		var ret []hcl.Traversal
		if len(raw) == 0 {
			return ret, diags
		}

		ret = make([]hcl.Traversal, 0, len(raw))
		for _, rawT := range raw {
			traversal := make(hcl.Traversal, 0, len(rawT.Steps))
			for _, rawS := range rawT.Steps {
				rng := hcl.Range{
					Filename: rawS.Range.Filename,
					Start: hcl.Pos{
						Line:   rawS.Range.Start.Line,
						Column: rawS.Range.Start.Column,
						Byte:   rawS.Range.Start.Byte,
					},
					End: hcl.Pos{
						Line:   rawS.Range.End.Line,
						Column: rawS.Range.End.Column,
						Byte:   rawS.Range.End.Byte,
					},
				}

				switch rawS.Kind {

				case "root":
					traversal = append(traversal, hcl.TraverseRoot{
						Name:     rawS.Name,
						SrcRange: rng,
					})

				case "attr":
					traversal = append(traversal, hcl.TraverseAttr{
						Name:     rawS.Name,
						SrcRange: rng,
					})

				case "index":
					ty, err := ctyjson.ImpliedType([]byte(rawS.Key))
					if err != nil {
						diags = append(diags, &hcl.Diagnostic{
							Severity: hcl.DiagError,
							Summary:  "Failed to parse hcldec result",
							Detail:   fmt.Sprintf("Sub-program hcldec (with --var-refs) produced an invalid result: traversal step has invalid index key %s.", rawS.Key),
						})
						return nil, diags
					}
					keyVal, err := ctyjson.Unmarshal([]byte(rawS.Key), ty)
					if err != nil {
						diags = append(diags, &hcl.Diagnostic{
							Severity: hcl.DiagError,
							Summary:  "Failed to parse hcldec result",
							Detail:   fmt.Sprintf("Sub-program hcldec (with --var-refs) produced a result with an invalid index key %s: %s.", rawS.Key, err),
						})
						return nil, diags
					}

					traversal = append(traversal, hcl.TraverseIndex{
						Key:      keyVal,
						SrcRange: rng,
					})

				default:
					// Should never happen since the above cases are exhaustive,
					// but we'll catch it gracefully since this is coming from
					// a possibly-buggy hcldec implementation that we're testing.
					diags = append(diags, &hcl.Diagnostic{
						Severity: hcl.DiagError,
						Summary:  "Failed to parse hcldec result",
						Detail:   fmt.Sprintf("Sub-program hcldec (with --var-refs) produced an invalid result: traversal step of unsupported kind %q.", rawS.Kind),
					})
					return nil, diags
				}
			}

			ret = append(ret, traversal)
		}
		return ret, diags
	}
}

func (r *Runner) prettyDirName(dir string) string {
	rel, err := filepath.Rel(r.baseDir, dir)
	if err != nil {
		return filepath.ToSlash(dir)
	}
	return filepath.ToSlash(rel)
}

func (r *Runner) prettyTestName(filename string) string {
	dir := filepath.Dir(filename)
	dirName := r.prettyDirName(dir)
	filename = filepath.Base(filename)
	testName := filename[:len(filename)-2]
	if dirName == "." {
		return testName
	}
	return fmt.Sprintf("%s/%s", dirName, testName)
}