-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_helper.go
74 lines (71 loc) · 2.06 KB
/
test_helper.go
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
// Copyright (c) 2021 Uber Technologies, Inc.
//
// Licensed under the Uber Non-Commercial License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at the root directory of this project.
//
// See the License for the specific language governing permissions and
// limitations under the License.
package propagate
import (
"bytes"
"go/ast"
"go/format"
"golang.org/x/tools/go/packages"
"io/ioutil"
"strings"
"testing"
)
// validateOutput compares generated output with expected output and
// type-checks expected output.
func validateOutput(t *testing.T, results map[*packages.Package]map[*ast.File]int, loadPath string, recompile bool) {
fail := false
if len(results) == 0 {
t.Log("no files have been refactored")
t.FailNow()
}
for p, nodes := range results {
for n, ind := range nodes {
expectedPath := strings.ReplaceAll(p.CompiledGoFiles[ind], "testdata/src", "testdata/src/expected")
refactoredBuf, err := ioutil.ReadFile(expectedPath)
if err != nil {
t.Log("could not read file containing expected refactored output: " + expectedPath)
t.FailNow()
}
var buf bytes.Buffer
err = format.Node(&buf, p.Fset, n)
if err != nil {
t.Log("could not format refactored AST")
t.FailNow()
}
if !bytes.Equal(refactoredBuf, buf.Bytes()) {
t.Log("refactored file and expected refactored output have different content")
t.Log("REFACTORED\n" + string(buf.Bytes()))
t.Log("EXPECTED\n" + string(refactoredBuf))
// set flag instead of failing so that we can observe more than one mismatch
fail = true
}
}
}
if fail {
t.FailNow()
}
if recompile {
cfg := &packages.Config{Mode: packages.LoadAllSyntax, Tests: true}
loaded, err := packages.Load(cfg, "expected/"+loadPath)
if err != nil {
t.Log("could not load refactored packages")
t.Log(err)
t.FailNow()
}
for _, p := range loaded {
if len(p.Errors) > 0 {
t.Log("refactored package loading errors")
for _, e := range p.Errors {
t.Log(e)
}
t.FailNow()
}
}
}
}