-
Notifications
You must be signed in to change notification settings - Fork 541
/
client.go
157 lines (133 loc) · 3.89 KB
/
client.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package envtest
import (
"bytes"
"os"
"os/exec"
"path/filepath"
"runtime"
"github.com/stretchr/testify/require"
chainconfig "github.com/ignite/cli/v28/ignite/config/chain"
"github.com/ignite/cli/v28/ignite/pkg/cmdrunner"
"github.com/ignite/cli/v28/ignite/pkg/cmdrunner/step"
)
type clientOptions struct {
env map[string]string
testName, testFilePath string
}
// ClientOption defines options for the TS client test runner.
type ClientOption func(*clientOptions)
// ClientEnv option defines environment values for the tests.
func ClientEnv(env map[string]string) ClientOption {
return func(o *clientOptions) {
for k, v := range env {
o.env[k] = v
}
}
}
// ClientTestName option defines a pattern to match the test names that should be run.
func ClientTestName(pattern string) ClientOption {
return func(o *clientOptions) {
o.testName = pattern
}
}
// ClientTestFile option defines the name of the file where to look for tests.
func ClientTestFile(filePath string) ClientOption {
return func(o *clientOptions) {
o.testFilePath = filePath
}
}
// RunClientTests runs the Typescript client tests.
func (a App) RunClientTests(options ...ClientOption) bool {
npm, err := exec.LookPath("npm")
require.NoError(a.env.t, err, "npm binary not found")
// The root dir for the tests must be an absolute path.
// It is used as the start search point to find test files.
rootDir, err := os.Getwd()
require.NoError(a.env.t, err)
// The filename of this module is required to be able to define the location
// of the TS client test runner package to be used as working directory when
// running the tests.
_, filename, _, ok := runtime.Caller(0)
if !ok {
a.env.t.Fatal("failed to read file name")
}
opts := clientOptions{
env: map[string]string{
// Absolute path to the blockchain app directory
"TEST_CHAIN_PATH": a.path,
// Absolute path to the TS client directory
"TEST_TSCLIENT_DIR": filepath.Join(a.path, chainconfig.DefaultTSClientPath),
},
}
for _, o := range options {
o(&opts)
}
var (
output bytes.Buffer
env []string
)
// Install the dependencies needed to run TS client tests
ok = a.env.Exec("install client dependencies", step.NewSteps(
step.New(
step.Workdir(filepath.Join(a.path, chainconfig.DefaultTSClientPath)),
step.Stdout(&output),
step.Exec(npm, "install"),
step.PostExec(func(err error) error {
// Print the npm output when there is an error
if err != nil {
a.env.t.Log("\n", output.String())
}
return err
}),
),
))
if !ok {
return false
}
output.Reset()
args := []string{"run", "test", "--", "--dir", rootDir}
if opts.testName != "" {
args = append(args, "-t", opts.testName)
}
if opts.testFilePath != "" {
args = append(args, opts.testFilePath)
}
for k, v := range opts.env {
env = append(env, cmdrunner.Env(k, v))
}
// The tests are run from the TS client test runner package directory
runnerDir := filepath.Join(filepath.Dir(filename), "testdata/tstestrunner")
// TODO: Ignore stderr ? Errors are already displayed with traceback in the stdout
return a.env.Exec("run client tests", step.NewSteps(
// Make sure the test runner dependencies are installed
step.New(
step.Workdir(runnerDir),
step.Stdout(&output),
step.Exec(npm, "install"),
step.PostExec(func(err error) error {
// Print the npm output when there is an error
if err != nil {
a.env.t.Log("\n", output.String())
}
return err
}),
),
// Run the TS client tests
step.New(
step.Workdir(runnerDir),
step.Stdout(&output),
step.Env(env...),
step.PreExec(func() error {
// Clear the output from the previous step
output.Reset()
return nil
}),
step.Exec(npm, args...),
step.PostExec(func(err error) error {
// Always print tests output to be available on errors or when verbose is enabled
a.env.t.Log("\n", output.String())
return err
}),
),
))
}