-
Notifications
You must be signed in to change notification settings - Fork 0
/
endtoend_test.go
76 lines (68 loc) · 1.56 KB
/
endtoend_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
// (c) Copyright 2021, Gorror Authors.
//
// Licensed under the terms of the GNU GPL License version 3.
package main
import (
"io"
"os"
"os/exec"
"path/filepath"
"testing"
)
func TestEndToEnd(t *testing.T) {
tmpdir, exePath := buildGorror(t)
entries, err := os.ReadDir("testdata")
if err != nil {
t.Fatal(err)
}
errorsSource := filepath.Join(tmpdir, "errors.go")
for _, entry := range entries {
t.Logf("run: %s %s\n", exePath, entry.Name())
source := filepath.Join(tmpdir, entry.Name())
err = copyFile(source, filepath.Join("testdata", entry.Name()))
if err != nil {
t.Fatalf("copying file to temporary directory: %s", err)
}
// Run gorror in temporary directory.
err = run(exePath, "-type", "Err", "-output", errorsSource, source)
if err != nil {
t.Fatal(err)
}
// Run the binary in the temporary directory.
err = run("go", "run", errorsSource, source)
if err != nil {
t.Fatal(err)
}
}
}
func buildGorror(t *testing.T) (string, string) {
t.Helper()
dir := t.TempDir()
file := filepath.Join(dir, "gorror.exe")
err := run("go", "build", "-o", file)
if err != nil {
t.Fatalf("building gorror: %v", err)
}
return dir, file
}
func run(name string, arg ...string) error {
cmd := exec.Command(name, arg...)
cmd.Dir = "."
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func copyFile(to, from string) error {
toFd, err := os.Create(to)
if err != nil {
return err
}
defer toFd.Close()
fromFd, err := os.Open(from)
if err != nil {
return err
}
defer fromFd.Close()
_, err = io.Copy(toFd, fromFd)
return err
}