File: spec_test.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 (100 lines) | stat: -rw-r--r-- 2,133 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
package spectests

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"strings"
	"testing"
)

func TestMain(m *testing.M) {
	// The test harness is an external program that also expects to have
	// hcldec built as an external program, so we'll build both into
	// temporary files in our working directory before running our tests
	// here, to ensure that we're always running a build of the latest code.
	err := build()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", err.Error())
		os.Exit(1)
	}

	// Now we can run the tests
	os.Exit(m.Run())
}

func build() error {
	err := goBuild("github.com/hashicorp/hcl/v2/cmd/hcldec", "tmp_hcldec")
	if err != nil {
		return fmt.Errorf("error building hcldec: %s", err)
	}

	err = goBuild("github.com/hashicorp/hcl/v2/cmd/hclspecsuite", "tmp_hclspecsuite")
	if err != nil {
		return fmt.Errorf("error building hcldec: %s", err)
	}

	return nil
}

func TestSpec(t *testing.T) {
	suiteDir := filepath.Clean("../specsuite/tests")
	harness := "./tmp_hclspecsuite"
	hcldec := "./tmp_hcldec"

	cmd := exec.Command(harness, suiteDir, hcldec)
	out, err := cmd.CombinedOutput()
	if _, isExit := err.(*exec.ExitError); err != nil && !isExit {
		t.Errorf("failed to run harness: %s", err)
	}
	failed := err != nil

	sc := bufio.NewScanner(bytes.NewReader(out))
	var lines []string
	for sc.Scan() {
		lines = append(lines, sc.Text())
	}

	i := 0
	for i < len(lines) {
		cur := lines[i]
		if strings.HasPrefix(cur, "- ") {
			testName := cur[2:]
			t.Run(testName, func(t *testing.T) {
				i++
				for i < len(lines) {
					cur := lines[i]
					if strings.HasPrefix(cur, "- ") || strings.HasPrefix(cur, "==") {
						return
					}
					t.Error(cur)
					i++
				}
			})
		} else {
			if !strings.HasPrefix(cur, "==") { // not the "test harness problems" report, then
				t.Log(cur)
			}
			i++
		}
	}

	if failed {
		t.Error("specsuite failed")
	}
}

func goBuild(pkg, outFile string) error {
	if runtime.GOOS == "windows" {
		outFile += ".exe"
	}

	cmd := exec.Command("go", "build", "-o", outFile, pkg)
	cmd.Stderr = os.Stderr
	cmd.Stdout = os.Stdout
	return cmd.Run()
}