-
Notifications
You must be signed in to change notification settings - Fork 1
/
exec.go
47 lines (43 loc) · 1.05 KB
/
exec.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
package tests
import (
"os"
"strings"
"testing"
"github.com/apigear-io/cli/pkg/cmd"
"github.com/apigear-io/cli/pkg/helper"
"github.com/apigear-io/cli/pkg/log"
"github.com/stretchr/testify/assert"
)
func execute(t *testing.T, args string) string {
t.Helper()
var b strings.Builder
root := cmd.NewRootCommand()
root.SetOut(&b)
root.SetErr(&b)
root.SetArgs(strings.Split(args, " "))
log.OnReportBytes(func(s string) {
b.WriteString(s)
})
// ignore error here
_ = root.Execute()
return b.String()
}
// setup sets up the test environment
// copies testdata to a temporary directory
// changes the current working directory to the temporary directory
// restores the original working directory and removes the temporary directory
func setup(t *testing.T) string {
t.Helper()
origDir, err := os.Getwd()
assert.NoError(t, err)
tmpDir := t.TempDir()
err = helper.CopyDir("./testdata", tmpDir)
assert.NoError(t, err)
err = os.Chdir(tmpDir)
assert.NoError(t, err)
t.Cleanup(func() {
err = os.Chdir(origDir)
assert.NoError(t, err)
})
return tmpDir
}