-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_test.go
74 lines (51 loc) · 1.56 KB
/
command_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
package k6exec_test
import (
"context"
"net/http"
"net/http/httptest"
"net/url"
"runtime"
"strings"
"testing"
"github.com/grafana/k6exec"
"github.com/stretchr/testify/require"
)
func TestCommand(t *testing.T) {
t.Parallel()
if runtime.GOOS == "windows" { // TODO - Re-enable as soon as k6build supports Windows!
t.Skip("Skip because k6build doesn't work on Windows yet!")
}
srv := testWebServer(t)
defer srv.Close()
u, err := url.Parse(srv.URL + "/minimal-catalog.json")
require.NoError(t, err)
ctx := context.Background()
opts := &k6exec.Options{StateDir: t.TempDir(), CacheDir: t.TempDir(), ExtensionCatalogURL: u}
cmd, err := k6exec.Command(ctx, []string{"version"}, nil, opts)
require.NoError(t, err)
out, err := cmd.Output()
require.NoError(t, err)
require.True(t, strings.HasPrefix(string(out), "k6 "))
}
func TestCommand_errors(t *testing.T) {
t.Parallel()
srv := testWebServer(t)
defer srv.Close()
u, err := url.Parse(srv.URL + "/missing-catalog.json")
require.NoError(t, err)
ctx := context.Background()
_, err = k6exec.Command(ctx, nil, nil, &k6exec.Options{AppName: invalidAppName(t)})
require.Error(t, err)
require.ErrorIs(t, err, k6exec.ErrState)
_, err = k6exec.Command(ctx, nil, nil, &k6exec.Options{ExtensionCatalogURL: u})
require.Error(t, err)
require.ErrorIs(t, err, k6exec.ErrBuild)
}
func testWebServer(t *testing.T) *httptest.Server {
t.Helper()
return httptest.NewServer(http.FileServer(http.Dir("testdata")))
}
func invalidAppName(t *testing.T) string {
t.Helper()
return strings.Repeat("too long", 2048)
}