summaryrefslogtreecommitdiffstats
path: root/cmd/mattermost/commands/cmdtestlib.go
blob: 93dcc956602926bb3d41e4c2f6c5ed2fb3296a95 (plain)
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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package commands

import (
	"flag"
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"testing"

	"github.com/stretchr/testify/require"
)

var coverprofileCounters map[string]int = make(map[string]int)

func execArgs(t *testing.T, args []string) []string {
	ret := []string{"-test.run", "ExecCommand"}
	if coverprofile := flag.Lookup("test.coverprofile").Value.String(); coverprofile != "" {
		dir := filepath.Dir(coverprofile)
		base := filepath.Base(coverprofile)
		baseParts := strings.SplitN(base, ".", 2)
		coverprofileCounters[t.Name()] = coverprofileCounters[t.Name()] + 1
		baseParts[0] = fmt.Sprintf("%v-%v-%v", baseParts[0], t.Name(), coverprofileCounters[t.Name()])
		ret = append(ret, "-test.coverprofile", filepath.Join(dir, strings.Join(baseParts, ".")))
	}
	return append(append(ret, "--", "--disableconfigwatch"), args...)
}

func CheckCommand(t *testing.T, args ...string) string {
	path, err := os.Executable()
	require.NoError(t, err)
	output, err := exec.Command(path, execArgs(t, args)...).CombinedOutput()
	require.NoError(t, err, string(output))
	return strings.TrimSpace(strings.TrimSuffix(strings.TrimSpace(string(output)), "PASS"))
}

func RunCommand(t *testing.T, args ...string) error {
	path, err := os.Executable()
	require.NoError(t, err)
	return exec.Command(path, execArgs(t, args)...).Run()
}