Skip to content

Commit 926b312

Browse files
committed
[e2e] use absolute binary path
1 parent ddeb33e commit 926b312

6 files changed

Lines changed: 68 additions & 18 deletions

File tree

‎tests/e2e/general_test.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@ package e2e
22

33
import (
44
"bytes"
5+
"e2e/testutils"
56
"os/exec"
67
"testing"
78

89
"github.com/stretchr/testify/assert"
910
)
1011

1112
func TestHelpFlag(t *testing.T) {
13+
binPath := testutils.RequireBinPath(t)
14+
1215
// Run the CLI binary with the --help flag
1316
var stdout, stderr bytes.Buffer
1417

15-
cmd := exec.Command("./smsgate", "--help")
18+
cmd := exec.Command(binPath, "--help")
1619
cmd.Stdout = &stdout
1720
cmd.Stderr = &stderr
1821

‎tests/e2e/logs_test.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import (
1717
)
1818

1919
func TestLogs(t *testing.T) {
20+
binPath := testutils.RequireBinPath(t)
21+
2022
from := time.Date(2025, 1, 10, 11, 0, 0, 0, time.UTC)
2123
to := time.Date(2025, 1, 10, 12, 0, 0, 0, time.UTC)
2224

@@ -47,7 +49,7 @@ func TestLogs(t *testing.T) {
4749

4850
var stdout, stderr bytes.Buffer
4951
cmd := exec.Command(
50-
"./smsgate",
52+
binPath,
5153
"--format", "json",
5254
"logs",
5355
"--from", from.Format(time.RFC3339),
@@ -72,7 +74,7 @@ func TestLogs(t *testing.T) {
7274
t.Run("invalid range", func(t *testing.T) {
7375
var stdout, stderr bytes.Buffer
7476
cmd := exec.Command(
75-
"./smsgate",
77+
binPath,
7678
"logs",
7779
"--from", to.Format(time.RFC3339),
7880
"--to", from.Format(time.RFC3339),

‎tests/e2e/main_test.go‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,35 @@
11
package e2e
22

33
import (
4+
"fmt"
45
"os"
56
"os/exec"
7+
"path/filepath"
68
"testing"
79
)
810

911
func TestMain(m *testing.M) {
12+
// Compute absolute path for the binary
13+
binPath, err := filepath.Abs("smsgate")
14+
if err != nil {
15+
panic(fmt.Sprintf("failed to get absolute path: %v", err))
16+
}
17+
1018
// Build the CLI binary
11-
cmd := exec.Command("go", "build", "-o", "tests/e2e/smsgate", "cmd/smsgate/smsgate.go")
19+
cmd := exec.Command("go", "build", "-o", binPath, "cmd/smsgate/smsgate.go")
1220
cmd.Dir = "../../"
13-
err := cmd.Run()
21+
err = cmd.Run()
1422
if err != nil {
15-
panic(err)
23+
panic(fmt.Sprintf("failed to build binary: %v", err))
1624
}
1725

26+
// Export the binary path for tests to use
27+
os.Setenv("SMSGATE_BIN", binPath)
28+
1829
code := m.Run()
1930

20-
_ = os.Remove("smsgate")
31+
// Cleanup: remove the built binary
32+
_ = os.Remove(binPath)
2133

2234
os.Exit(code)
2335
}

‎tests/e2e/messages_test.go‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import (
1717
)
1818

1919
func TestMessageSendValid(t *testing.T) {
20+
binPath := testutils.RequireBinPath(t)
21+
2022
tests := []struct {
2123
name string
2224
message string
@@ -173,7 +175,7 @@ func TestMessageSendValid(t *testing.T) {
173175

174176
args = append(args, tt.message)
175177

176-
cmd := exec.Command("./smsgate", args...)
178+
cmd := exec.Command(binPath, args...)
177179
cmd.Env = append([]string{}, os.Environ()...)
178180
cmd.Env = append(cmd.Env, fmt.Sprintf("ASG_ENDPOINT=%s", mockServer.URL))
179181
cmd.Env = append(cmd.Env, "ASG_USERNAME=testuser", "ASG_PASSWORD=testpass")
@@ -194,6 +196,8 @@ func TestMessageSendValid(t *testing.T) {
194196
}
195197

196198
func TestMessageSendInvalid(t *testing.T) {
199+
binPath := testutils.RequireBinPath(t)
200+
197201
tests := []struct {
198202
name string
199203
message string
@@ -277,7 +281,7 @@ func TestMessageSendInvalid(t *testing.T) {
277281

278282
args = append(args, tt.message)
279283

280-
cmd := exec.Command("./smsgate", args...)
284+
cmd := exec.Command(binPath, args...)
281285
cmd.Env = append([]string{}, os.Environ()...)
282286
cmd.Env = append(cmd.Env, fmt.Sprintf("ASG_ENDPOINT=%s", mockServer.URL))
283287
cmd.Env = append(cmd.Env, "ASG_USERNAME=testuser", "ASG_PASSWORD=testpass")
@@ -292,6 +296,8 @@ func TestMessageSendInvalid(t *testing.T) {
292296
}
293297

294298
func TestMessageSendAuthenticationError(t *testing.T) {
299+
binPath := testutils.RequireBinPath(t)
300+
295301
mockServer := testutils.CreateMockServer(func(w http.ResponseWriter, r *http.Request) {
296302
w.WriteHeader(http.StatusUnauthorized)
297303
w.Write([]byte(`{"error": "Invalid credentials"}`))
@@ -305,7 +311,7 @@ func TestMessageSendAuthenticationError(t *testing.T) {
305311
"Hello, world!",
306312
}
307313

308-
cmd := exec.Command("./smsgate", args...)
314+
cmd := exec.Command(binPath, args...)
309315
cmd.Env = append([]string{}, os.Environ()...)
310316
cmd.Env = append(cmd.Env, fmt.Sprintf("ASG_ENDPOINT=%s", mockServer.URL))
311317
cmd.Env = append(cmd.Env, "ASG_USERNAME=wronguser", "ASG_PASSWORD=wrongpass")
@@ -318,6 +324,8 @@ func TestMessageSendAuthenticationError(t *testing.T) {
318324
}
319325

320326
func TestMessageStatusValid(t *testing.T) {
327+
binPath := testutils.RequireBinPath(t)
328+
321329
tests := []struct {
322330
name string
323331
messageID string
@@ -359,7 +367,7 @@ func TestMessageStatusValid(t *testing.T) {
359367
tt.messageID,
360368
}
361369

362-
cmd := exec.Command("./smsgate", args...)
370+
cmd := exec.Command(binPath, args...)
363371
cmd.Env = append([]string{}, os.Environ()...)
364372
cmd.Env = append(cmd.Env, fmt.Sprintf("ASG_ENDPOINT=%s", mockServer.URL))
365373
cmd.Env = append(cmd.Env, "ASG_USERNAME=testuser", "ASG_PASSWORD=testpass")
@@ -379,6 +387,8 @@ func TestMessageStatusValid(t *testing.T) {
379387
}
380388

381389
func TestMessageStatusInvalid(t *testing.T) {
390+
binPath := testutils.RequireBinPath(t)
391+
382392
tests := []struct {
383393
name string
384394
messageID string
@@ -409,7 +419,7 @@ func TestMessageStatusInvalid(t *testing.T) {
409419
args = append(args, tt.messageID)
410420
}
411421

412-
cmd := exec.Command("./smsgate", args...)
422+
cmd := exec.Command(binPath, args...)
413423
cmd.Env = append([]string{}, os.Environ()...)
414424
cmd.Env = append(cmd.Env, fmt.Sprintf("ASG_ENDPOINT=%s", mockServer.URL))
415425
cmd.Env = append(cmd.Env, "ASG_USERNAME=testuser", "ASG_PASSWORD=testpass")
@@ -424,6 +434,8 @@ func TestMessageStatusInvalid(t *testing.T) {
424434
}
425435

426436
func TestMessageCommandHelp(t *testing.T) {
437+
binPath := testutils.RequireBinPath(t)
438+
427439
tests := []struct {
428440
name string
429441
args []string
@@ -447,7 +459,7 @@ func TestMessageCommandHelp(t *testing.T) {
447459
for _, tt := range tests {
448460
t.Run(tt.name, func(t *testing.T) {
449461
var stdout, stderr bytes.Buffer
450-
cmd := exec.Command("./smsgate", tt.args...)
462+
cmd := exec.Command(binPath, tt.args...)
451463
cmd.Env = append([]string{}, os.Environ()...)
452464
cmd.Env = append(cmd.Env, "ASG_USERNAME=testuser", "ASG_PASSWORD=testpass")
453465
cmd.Stdout = &stdout

‎tests/e2e/testutils/testutils.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,19 @@ package testutils
33
import (
44
"net/http"
55
"net/http/httptest"
6+
"os"
7+
"testing"
68
)
79

810
func CreateMockServer(handler http.HandlerFunc) *httptest.Server {
911
return httptest.NewServer(handler)
1012
}
13+
14+
func RequireBinPath(t *testing.T) string {
15+
t.Helper()
16+
binPath := os.Getenv("SMSGATE_BIN")
17+
if binPath == "" {
18+
t.Fatal("SMSGATE_BIN environment variable is not set")
19+
}
20+
return binPath
21+
}

‎tests/e2e/webhooks_test.go‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
)
1616

1717
func TestWebhookRegisterValid(t *testing.T) {
18+
binPath := testutils.RequireBinPath(t)
19+
1820
tests := []struct {
1921
name string
2022
url string
@@ -88,7 +90,7 @@ func TestWebhookRegisterValid(t *testing.T) {
8890

8991
args = append(args, tt.url)
9092

91-
cmd := exec.Command("./smsgate", args...)
93+
cmd := exec.Command(binPath, args...)
9294
cmd.Env = append([]string{}, os.Environ()...)
9395
cmd.Env = append(cmd.Env, fmt.Sprintf("ASG_ENDPOINT=%s", mockServer.URL))
9496
cmd.Env = append(cmd.Env, "ASG_USERNAME=testuser", "ASG_PASSWORD=testpass")
@@ -111,6 +113,8 @@ func TestWebhookRegisterValid(t *testing.T) {
111113
}
112114

113115
func TestWebhookRegisterInvalid(t *testing.T) {
116+
binPath := testutils.RequireBinPath(t)
117+
114118
tests := []struct {
115119
name string
116120
url string
@@ -169,7 +173,7 @@ func TestWebhookRegisterInvalid(t *testing.T) {
169173
args = append(args, tt.url)
170174
}
171175

172-
cmd := exec.Command("./smsgate", args...)
176+
cmd := exec.Command(binPath, args...)
173177
cmd.Env = append([]string{}, os.Environ()...)
174178
cmd.Env = append(cmd.Env, fmt.Sprintf("ASG_ENDPOINT=%s", mockServer.URL))
175179
cmd.Env = append(cmd.Env, "ASG_USERNAME=testuser", "ASG_PASSWORD=testpass")
@@ -184,6 +188,8 @@ func TestWebhookRegisterInvalid(t *testing.T) {
184188
}
185189

186190
func TestWebhookList(t *testing.T) {
191+
binPath := testutils.RequireBinPath(t)
192+
187193
tests := []struct {
188194
name string
189195
setupResponse func() []byte
@@ -233,7 +239,7 @@ func TestWebhookList(t *testing.T) {
233239

234240
var stdout, stderr bytes.Buffer
235241
cmd := exec.Command(
236-
"./smsgate",
242+
binPath,
237243
"--format", "json",
238244
"webhooks", "list",
239245
)
@@ -257,6 +263,8 @@ func TestWebhookList(t *testing.T) {
257263
}
258264

259265
func TestWebhookDelete(t *testing.T) {
266+
binPath := testutils.RequireBinPath(t)
267+
260268
tests := []struct {
261269
name string
262270
webhookID string
@@ -315,7 +323,7 @@ func TestWebhookDelete(t *testing.T) {
315323
args = append(args, tt.webhookID)
316324
}
317325

318-
cmd := exec.Command("./smsgate", args...)
326+
cmd := exec.Command(binPath, args...)
319327
cmd.Env = append([]string{}, os.Environ()...)
320328
cmd.Env = append(cmd.Env, fmt.Sprintf("ASG_ENDPOINT=%s", mockServer.URL))
321329
cmd.Env = append(cmd.Env, "ASG_USERNAME=testuser", "ASG_PASSWORD=testpass")
@@ -335,6 +343,8 @@ func TestWebhookDelete(t *testing.T) {
335343
}
336344

337345
func TestWebhookCommandHelp(t *testing.T) {
346+
binPath := testutils.RequireBinPath(t)
347+
338348
tests := []struct {
339349
name string
340350
args []string
@@ -370,7 +380,7 @@ func TestWebhookCommandHelp(t *testing.T) {
370380
for _, tt := range tests {
371381
t.Run(tt.name, func(t *testing.T) {
372382
var stdout, stderr bytes.Buffer
373-
cmd := exec.Command("./smsgate", tt.args...)
383+
cmd := exec.Command(binPath, tt.args...)
374384
cmd.Env = append([]string{}, os.Environ()...)
375385
cmd.Env = append(cmd.Env, "ASG_USERNAME=testuser", "ASG_PASSWORD=testpass")
376386
cmd.Stdout = &stdout

0 commit comments

Comments
 (0)