forked from bruin-data/bruin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration-test.go
120 lines (106 loc) · 2.89 KB
/
integration-test.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
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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"syscall"
jd "github.com/josephburnett/jd/lib"
"github.com/pkg/errors"
)
var currentFolder string
func main() {
var err error
currentFolder = filepath.Join(currentFolder, "integration-tests")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
expectExitCode("validate happy-path", 0)
expectExitCode("run --use-uv happy-path", 0)
expectJSONOutput("internal parse-pipeline happy-path", "happy-path/expectations/pipeline.yml.json")
expectJSONOutput(
"internal parse-asset happy-path/assets/asset.py",
"happy-path/expectations/asset.py.json",
)
expectJSONOutput(
"internal parse-asset happy-path/assets/chess_games.asset.yml",
"happy-path/expectations/chess_games.asset.yml.json",
)
expectJSONOutput(
"internal parse-asset happy-path/assets/chess_profiles.asset.yml",
"happy-path/expectations/chess_profiles.asset.yml.json",
)
expectJSONOutput(
"internal parse-asset happy-path/assets/player_summary.sql",
"happy-path/expectations/player_summary.sql.json",
)
}
func expectJSONOutput(command string, jsonFilePath string) {
output, err := runCommand(command)
if err != nil {
fmt.Println("Failed:", err)
os.Exit(1)
}
expectation, err := jd.ReadJsonFile(filepath.Join(currentFolder, jsonFilePath))
if err != nil {
fmt.Println("Failed to read expectation:", err)
os.Exit(1)
}
// normalize linebreaks
parsedOutput, err := jd.ReadJsonString(strings.ReplaceAll(output, "\\r\\n", "\\n"))
if err != nil {
fmt.Println("Failed to parse output:", err)
os.Exit(1)
}
diff := expectation.Diff(parsedOutput)
if len(diff) != 0 {
var path jd.JsonNode
for _, d := range diff {
// Paths are hell to normalize, we skip
path = d.Path[len(d.Path)-1]
if path.Json() == "\"path\"" {
continue
}
fmt.Print("Expected json: ")
fmt.Println(d.NewValues)
fmt.Print("Not Matching found json: ")
fmt.Println(d.OldValues)
os.Exit(1)
}
}
fmt.Println("Passed")
}
func expectExitCode(command string, code int) {
output, err := runCommand(command)
if err != nil {
// Try to get the exit code
if errors.As(err, exec.ExitError{}) {
exitError := err.(*exec.ExitError) //nolint: errorlint
// Get the status code
if status, ok := exitError.Sys().(syscall.WaitStatus); ok {
if status.ExitStatus() != code {
fmt.Println(strconv.Itoa(code) + " Was expected but got:" + strconv.Itoa(status.ExitStatus()))
fmt.Println(output)
os.Exit(1)
}
}
}
// Handle other errors
fmt.Printf("Error running command: %v\n", err)
fmt.Println(output)
return
}
fmt.Println("Passed")
}
func runCommand(command string) (string, error) {
fmt.Println("Running command:", command)
args := []string{"run", "../main.go"}
args = append(args, strings.Split(command, " ")...)
cmd := exec.Command("go", args...)
cmd.Dir = currentFolder
output, err := cmd.Output()
return string(output), err
}