Skip to content

Commit d88aee4

Browse files
committed
Add integration test
1 parent ef15572 commit d88aee4

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build/

cmd/integrationtest/pi_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package integration_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"os/exec"
8+
"testing"
9+
10+
"github.com/tygern/pi/internal/assert"
11+
)
12+
13+
func TestIntegration(t *testing.T) {
14+
prepareBuildDirectory(t)
15+
build(t, "pi")
16+
17+
testCtx, cancelCtx := context.WithCancel(t.Context())
18+
defer cancelCtx()
19+
20+
output := runCommand(t, testCtx, "./build/pi", "-d", "1", "-n", "2")
21+
22+
assert.ContainsSubstring(t, output, "Running for 1")
23+
assert.ContainsSubstring(t, output, "with 2 workers")
24+
assert.ContainsSubstring(t, output, "π ≈ 3.14")
25+
}
26+
27+
func prepareBuildDirectory(t *testing.T) {
28+
err := os.RemoveAll("../../build")
29+
assert.NoError(t, err)
30+
err = os.MkdirAll("./build", os.ModePerm)
31+
assert.NoError(t, err)
32+
}
33+
34+
func build(t *testing.T, name string) {
35+
err := exec.Command("go", "build", "-o", fmt.Sprintf("./build/%s", name), fmt.Sprintf("../../cmd/%s", name)).Run()
36+
assert.NoError(t, err)
37+
}
38+
39+
func runCommand(t *testing.T, ctx context.Context, command string, arguments ...string) string {
40+
output, err := exec.CommandContext(ctx, command, arguments...).Output()
41+
assert.NoError(t, err)
42+
return string(output)
43+
}

internal/assert/assert.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package assert
22

33
import (
44
"cmp"
5+
"strings"
56
"testing"
67
)
78

89
func Equal[V comparable](t testing.TB, expected, actual V) {
910
t.Helper()
1011

1112
if expected != actual {
12-
t.Errorf(`Test assertion failed.
13+
t.Fatalf(`Test assertion failed.
1314
expected: %v
1415
actual: %v`, expected, actual)
1516
}
@@ -19,7 +20,25 @@ func GreaterThanOrEqualTo[V cmp.Ordered](t testing.TB, first, second V) {
1920
t.Helper()
2021

2122
if first < second {
22-
t.Errorf(`Test assertion failed.
23+
t.Fatalf(`Test assertion failed.
2324
expected %v to be greater than or equal to %v`, first, second)
2425
}
2526
}
27+
28+
func NoError(t testing.TB, err error) {
29+
t.Helper()
30+
31+
if err != nil {
32+
t.Fatalf(`Test assertion failed.
33+
expected error '%v' to be nil`, err)
34+
}
35+
}
36+
37+
func ContainsSubstring(t testing.TB, subject, substring string) {
38+
t.Helper()
39+
40+
if !strings.Contains(subject, substring) {
41+
t.Fatalf(`Test assertion failed.
42+
expected '%v' to contain '%v'`, subject, substring)
43+
}
44+
}

0 commit comments

Comments
 (0)