Skip to content

Commit 9cbc013

Browse files
committed
E2E tests working.
1 parent 091fd8e commit 9cbc013

8 files changed

Lines changed: 244 additions & 31 deletions

File tree

Makefile

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Variables
2-
BINARY_NAME=e2e
2+
BINARY_NAME=re2e
33
SRC_DIR=./cmd/e2e
4+
SRCS := $(shell find ./ -name '*.go')
45
BUILD_DIR=bin
56
DIST_DIR = dist
67
MODULE_PATH = github.com/go-rivet/e2e/internal/version
@@ -39,7 +40,8 @@ endif
3940
all: help
4041

4142
## build: Build the binary for the current OS/Architecture
42-
build:
43+
build: $(BUILD_DIR)/$(BINARY_NAME)
44+
$(BUILD_DIR)/$(BINARY_NAME): $(SRCS)
4345
@echo "Building $(BINARY_NAME)..."
4446
@mkdir -p $(BUILD_DIR)
4547
CGO_ENABLED=0 go build \
@@ -50,6 +52,13 @@ build:
5052
@chmod +x $(BUILD_DIR)/$(BINARY_NAME)
5153
@echo "Binary built at $(BUILD_DIR)/$(BINARY_NAME)"
5254

55+
## install: Install the binary to the standard user Go bin directory
56+
install: build
57+
@echo "Installing $(BINARY_NAME) to $(GOBIN)..."
58+
@mkdir -p $(GOBIN)
59+
@cp $(BUILD_DIR)/$(BINARY_NAME) $(GOBIN)/$(BINARY_NAME)
60+
@echo "Installation successful."
61+
5362
## run: Build and run the local binary immediately
5463
run: build
5564
./$(BUILD_DIR)/$(BINARY_NAME)
@@ -78,13 +87,6 @@ clean:
7887
rm -rf $(BUILD_DIR)
7988
rm -rf $(DIST_DIR)
8089

81-
## install: Install the binary to the standard user Go bin directory
82-
install: build
83-
@echo "Installing $(BINARY_NAME) to $(GOBIN)..."
84-
@mkdir -p $(GOBIN)
85-
@cp $(BUILD_DIR)/$(BINARY_NAME) $(GOBIN)/$(BINARY_NAME)
86-
@echo "Installation successful."
87-
8890
## help: Show this help screen
8991
help:
9092
@echo "Usage: make [target]"

catalog/cli/cli.flags.init.txtar

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Test: init
2+
rivet --init
3+
exists Taskfile.yml
4+
stdout 'Taskfile created: Taskfile.yml'
5+
filecontains Taskfile.yml expect
6+
rivet
7+
stdout 'Hello, world!'
8+
9+
-- expect --
10+
version: '3'
11+
12+
vars:
13+
GREETING: Hello, world!
14+
15+
tasks:
16+
default:
17+
desc: Print a greeting message
18+
cmds:
19+
- echo "{{.GREETING}}"
20+
silent: true
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Test: version
2+
rivet --version
3+
stdout 'Rivet: v'
4+
stdout 'Commit: '
5+
stdout 'Built: '

catalog/watch/watch.interval.txtar

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
skip
2+
3+
mkdir src
4+
5+
# Test with extended interval, change should not be detected.
6+
task &
7+
sleep 500ms
8+
touch src/a
9+
sleep 500ms
10+
kill -INT
11+
wait
12+
cmp stdout expect_stdout
13+
cmp stderr expect_stderr
14+
15+
16+
-- Taskfile.yml --
17+
version: '3'
18+
interval: 1200ms
19+
tasks:
20+
default:
21+
watch: true
22+
sources:
23+
- "src/*"
24+
cmds:
25+
- echo "Task running!"
26+
- sleep 2
27+
28+
-- expect_stdout --
29+
Task running!
30+
task: Signal received: "interrupt"
31+
-- expect_stderr --
32+
task: Started watching for tasks: default
33+
task: [default] echo "Task running!"
34+
task: [default] sleep 2

catalog/watch/watch.service.txtar

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
skip
2+
3+
# Build the service.
4+
mkdir $WORK/.cache/go-build
5+
env GOCACHE=$WORK/.cache/go-build
6+
task build
7+
8+
# Test that the service is restarted when watched file changed.
9+
task run &
10+
sleep 0.5s
11+
touch watch.file
12+
sleep 0.5s
13+
kill -INT
14+
wait
15+
stdout 'Service received signal: interrupt'
16+
stdout 'Starting Service'
17+
stdout 'Wait for signal'
18+
grep -count=1 'Service received signal: interrupt'
19+
grep -count=2 'Starting Service'
20+
grep -count=2 'Wait for signal'
21+
22+
# Test that the service is _not_ restarted when an excluded file is changed.
23+
task -s run &
24+
sleep 0.5s
25+
touch watch.exclude
26+
sleep 0.5s
27+
kill -INT
28+
wait
29+
! stdout 'Service received signal: interrupt'
30+
stdout 'Starting Service'
31+
stdout 'Wait for signal'
32+
! grep 'Service received signal: interrupt'
33+
grep -count=1 'Starting Service'
34+
grep -count=1 'Wait for signal'
35+
36+
37+
-- Taskfile.yml --
38+
39+
version: '3'
40+
tasks:
41+
provision:
42+
cmds:
43+
- touch watch.exclude
44+
build:
45+
sources:
46+
- 'service.go'
47+
generates:
48+
- './out/service'
49+
cmds:
50+
- rm -rf ./out
51+
- task: provision
52+
- go build -o ./out/ service.go
53+
run:
54+
deps: [run:service]
55+
watch: true
56+
sources:
57+
- 'watch.file'
58+
- exclude: 'watch.exclude'
59+
method: none
60+
run:service:
61+
cmds:
62+
- './out/service'
63+
64+
65+
-- service.go --
66+
package main
67+
68+
import (
69+
"fmt"
70+
"os"
71+
"os/signal"
72+
)
73+
74+
func main() {
75+
fmt.Println("Starting Service ...")
76+
c := make (chan os.Signal, 1)
77+
signal.Notify(c)
78+
fmt.Println("Wait for signal ...")
79+
s := <-c
80+
fmt.Println("Service received signal:", s)
81+
}

cmd/e2e/main.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,19 @@ type Flags struct {
1818
func parseFlags() Flags {
1919
var flags Flags
2020

21-
pflag.StringVarP(&flags.TestDir, "dir", "d", "testdata/tests", "directory containing .txtar tests")
21+
pflag.StringVarP(&flags.TestDir, "dir", "d", "", "directory containing .txtar tests")
2222
pflag.BoolVarP(&flags.Version, "version", "v", false, "display the application version and build info")
23+
pflag.Usage = func() {
24+
fmt.Fprintf(os.Stderr, "Usage: %s [DIR]\n\nFlags:\n", os.Args[0])
25+
pflag.PrintDefaults()
26+
}
2327
pflag.Parse()
2428

29+
args := pflag.Args()
30+
if len(args) > 0 {
31+
flags.TestDir = args[0]
32+
}
33+
2534
return flags
2635
}
2736

@@ -32,8 +41,12 @@ func main() {
3241
fmt.Println(version.GetVersionWithBuildInfo())
3342
os.Exit(0)
3443
}
44+
if flags.TestDir == "" {
45+
pflag.Usage()
46+
os.Exit(0)
47+
}
3548

36-
fmt.Printf("🚀 Starting E2E Test Suite on: %s\n", flags.TestDir)
49+
fmt.Printf("Starting E2E Test Suite on: %s\n", flags.TestDir)
3750
if err := runner.Run(flags.TestDir); err != nil {
3851
fmt.Fprintf(os.Stderr, "❌ Error executing tests: %v\n", err)
3952
os.Exit(1)

internal/runner/clitest.go

Lines changed: 77 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,100 @@ package runner
22

33
import (
44
"fmt"
5-
"os"
5+
"runtime"
6+
"sync"
67

78
"github.com/rogpeppe/go-internal/testscript"
89
)
910

1011
type cliTest struct {
11-
failed bool
12+
mu sync.Mutex
13+
failed bool
14+
skipped bool
15+
isSub bool
16+
parent *cliTest
1217
}
1318

14-
func (c *cliTest) Fatal(args ...any) {
15-
c.failed = true
16-
_, _ = fmt.Fprint(os.Stderr, "❌ ")
17-
_, _ = fmt.Fprintln(os.Stderr, args...)
19+
func (t *cliTest) Run(name string, f func(t testscript.T)) {
20+
subTest := &cliTest{
21+
isSub: true,
22+
parent: t,
23+
}
24+
25+
fmt.Printf("Running: %s\n", name)
26+
27+
done := make(chan struct{})
28+
go func() {
29+
defer close(done)
30+
f(subTest)
31+
}()
32+
<-done
33+
34+
if subTest.failed {
35+
t.mu.Lock()
36+
t.failed = true
37+
t.mu.Unlock()
38+
}
1839
}
1940

20-
func (c *cliTest) Log(args ...any) {
21-
_, _ = fmt.Fprintln(os.Stdout, args...)
41+
func (t *cliTest) Fatal(args ...interface{}) {
42+
t.mu.Lock()
43+
t.failed = true
44+
t.mu.Unlock()
45+
fmt.Printf("🚨 Fatal error: %s\n", fmt.Sprint(args...))
46+
if t.isSub {
47+
runtime.Goexit()
48+
}
2249
}
2350

24-
func (c *cliTest) FailNow() {
25-
c.failed = true
51+
func (t *cliTest) Log(args ...interface{}) {
52+
if t.skipped {
53+
fmt.Println()
54+
} else {
55+
fmt.Println(args...)
56+
}
2657
}
2758

28-
func (c *cliTest) Skip(args ...any) {
29-
_, _ = fmt.Fprint(os.Stdout, "⏭️ Skipped: ")
30-
_, _ = fmt.Fprintln(os.Stdout, args...)
59+
func (t *cliTest) Logf(format string, args ...interface{}) {
60+
if t.skipped {
61+
fmt.Println()
62+
} else {
63+
fmt.Printf(format+"\n", args...)
64+
}
3165
}
3266

33-
func (c *cliTest) Parallel() {
34-
// No-op for CLI runner sequential runs
67+
func (t *cliTest) Skip(args ...interface{}) {
68+
t.skipped = true
69+
fmt.Printf("⚠️ Skipped: %s\n", fmt.Sprint(args...))
70+
if t.isSub {
71+
runtime.Goexit()
72+
}
3573
}
3674

37-
func (c *cliTest) Verbose() bool {
38-
return true
75+
func (t *cliTest) Skipf(format string, args ...interface{}) {
76+
t.skipped = true
77+
fmt.Printf("⚠️ Skipped: %s\n", fmt.Sprintf(format, args...))
78+
if t.isSub {
79+
runtime.Goexit()
80+
}
3981
}
4082

41-
func (c *cliTest) Run(name string, f func(testscript.T)) {
42-
f(c)
83+
func (t *cliTest) Errorf(format string, args ...interface{}) {
84+
t.mu.Lock()
85+
t.failed = true
86+
t.mu.Unlock()
87+
fmt.Printf("❌ "+format+"\n", args...)
4388
}
89+
90+
func (t *cliTest) FailNow() {
91+
t.mu.Lock()
92+
t.failed = true
93+
t.mu.Unlock()
94+
if t.isSub {
95+
runtime.Goexit()
96+
}
97+
}
98+
99+
func (t *cliTest) Parallel() {}
100+
func (t *cliTest) Helper() {}
101+
func (t *cliTest) Verbose() bool { return false }

internal/version/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ func GetVersion() string {
1515
}
1616

1717
func GetVersionWithBuildInfo() string {
18-
return fmt.Sprintf("Rivet %s\nCommit: %s\nBuilt: %s", Version, CommitSHA, BuildTime)
18+
return fmt.Sprintf("Rivet E2E: %s\nCommit: %s\nBuilt: %s", Version, CommitSHA, BuildTime)
1919
}

0 commit comments

Comments
 (0)