github.com/spreadshirt/mattermost-server@v5.3.2-0.20180927191755-a257d501df3d+incompatible/cmd/mattermost/commands/cmdtestlib.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package commands
     5  
     6  import (
     7  	"flag"
     8  	"fmt"
     9  	"os"
    10  	"os/exec"
    11  	"path/filepath"
    12  	"strings"
    13  	"testing"
    14  
    15  	"github.com/stretchr/testify/require"
    16  )
    17  
    18  var coverprofileCounters map[string]int = make(map[string]int)
    19  
    20  func execArgs(t *testing.T, args []string) []string {
    21  	ret := []string{"-test.run", "ExecCommand"}
    22  	if coverprofile := flag.Lookup("test.coverprofile").Value.String(); coverprofile != "" {
    23  		dir := filepath.Dir(coverprofile)
    24  		base := filepath.Base(coverprofile)
    25  		baseParts := strings.SplitN(base, ".", 2)
    26  		coverprofileCounters[t.Name()] = coverprofileCounters[t.Name()] + 1
    27  		baseParts[0] = fmt.Sprintf("%v-%v-%v", baseParts[0], t.Name(), coverprofileCounters[t.Name()])
    28  		ret = append(ret, "-test.coverprofile", filepath.Join(dir, strings.Join(baseParts, ".")))
    29  	}
    30  	return append(append(ret, "--", "--disableconfigwatch"), args...)
    31  }
    32  
    33  func CheckCommand(t *testing.T, args ...string) string {
    34  	path, err := os.Executable()
    35  	require.NoError(t, err)
    36  	output, err := exec.Command(path, execArgs(t, args)...).CombinedOutput()
    37  	require.NoError(t, err, string(output))
    38  	return strings.TrimSpace(strings.TrimSuffix(strings.TrimSpace(string(output)), "PASS"))
    39  }
    40  
    41  func RunCommand(t *testing.T, args ...string) error {
    42  	path, err := os.Executable()
    43  	require.NoError(t, err)
    44  	return exec.Command(path, execArgs(t, args)...).Run()
    45  }