~mna/snow unlisted

snow/pkg/codegen/codegen_test.go -rw-r--r-- 1.1 KiB
424066c5Martin Angers doc: v0.0.5 1 year, 7 months ago
                                                                                
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
package codegen

import (
	"bytes"
	"flag"
	"go/scanner"
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"

	"git.sr.ht/~mna/snow/pkg/internal/filetest"
)

var testUpdateCodegenTests = flag.Bool("test.update-codegen-tests", false, "If set, replace expected codegen test results with actual results.")

func TestGenerator(t *testing.T) {
	baseDir := "testdata"

	for _, fi := range filetest.SourceFiles(t, baseDir) {
		t.Run(fi.Name(), func(t *testing.T) {
			dir, err := ioutil.TempDir(baseDir, "")
			if err != nil {
				t.Fatal(err)
			}
			defer func() {
				if !t.Failed() {
					os.RemoveAll(dir)
				}
			}()

			outFiles, err := Exec(dir, filepath.Join(baseDir, fi.Name()))

			var ebuf bytes.Buffer
			scanner.PrintError(&ebuf, err)

			if len(outFiles) != 1 {
				t.Fatalf("want 1 output file, got %d\n%s", len(outFiles), ebuf.String())
			}

			got, err := ioutil.ReadFile(outFiles[0])
			if err != nil {
				t.Fatal(err)
			}

			filetest.DiffOutput(t, fi, string(got), baseDir, testUpdateCodegenTests)
			filetest.DiffErrors(t, fi, ebuf.String(), baseDir, testUpdateCodegenTests)
		})
	}
}