-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain_e2e_test.go
More file actions
93 lines (77 loc) · 2.18 KB
/
main_e2e_test.go
File metadata and controls
93 lines (77 loc) · 2.18 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
// Copyright IBM Corp. 2020, 2025
// SPDX-License-Identifier: MPL-2.0
package main
import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
"gotest.tools/v3/assert"
"gotest.tools/v3/golden"
"gotest.tools/v3/icmd"
)
var (
shouldVet = flag.Bool("vet-gen", true, "should we vet the generated code")
shouldPrint = flag.Bool("print-gen", false, "should we print the generated code")
)
func TestE2E_NoSourcesFound(t *testing.T) {
sourcepkg := "./internal/e2e/sourcepkg-empty"
// Cleanup the generated file when the test ends. The source must be a
// loadable Go package, so it can not be easily generated into a temporary
// directory.
output := "./internal/e2e/sourcepkg-empty/node_gen.go"
t.Cleanup(func() {
os.Remove(output)
})
args := []string{"mog", "-source", sourcepkg}
err := run(args)
assert.NilError(t, err)
_, err = os.Stat(output)
assert.ErrorType(t, err, os.IsNotExist)
}
func TestE2E(t *testing.T) {
if testing.Short() {
t.Skip("e2e test too slow for -short")
}
sourcepkg := "./internal/e2e/sourcepkg"
// Cleanup the generated file when the test ends. The source must be a
// loadable Go package, so it can not be easily generated into a temporary
// directory.
output := "./internal/e2e/sourcepkg/node_gen.go"
t.Cleanup(func() {
os.Remove(output)
})
args := []string{"mog", "-source", sourcepkg}
err := run(args)
assert.NilError(t, err)
if *shouldVet {
// go vet the file to check that it is valid Go syntax
icmd.RunCommand("go", "vet", sourcepkg).Assert(t, icmd.Success)
}
actual, err := ioutil.ReadFile(output)
assert.NilError(t, err)
if *shouldPrint {
t.Logf("OUTPUT\n%s\n", PrependLineNumbers(string(actual)))
}
golden.Assert(t, string(actual), t.Name()+"-expected-node_gen.go")
}
// PrependLineNumbers prepends line numbers onto the text passed in. In the
// event of some parsing error it just returns the original input, unprefixed.
func PrependLineNumbers(s string) string {
scan := bufio.NewScanner(strings.NewReader(s))
var (
next = 1
lines []string
)
for scan.Scan() {
lines = append(lines, fmt.Sprintf("%4d: %s", next, scan.Text()))
next++
}
if scan.Err() != nil {
return s
}
return strings.Join(lines, "\n")
}