From 865f9f83a7cf2f9eb1fe2735b6bd498f56486f50 Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 13 Nov 2017 13:59:51 -0600 Subject: Improve test coverage reporting / accuracy (#7819) * improve test coverage reporting / accuracy * handle absolute coverpaths * move tests into multiple files * rename codecov.yml (https://github.com/codecov/support/issues/426) --- cmd/platform/platform_test.go | 53 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 cmd/platform/platform_test.go (limited to 'cmd/platform/platform_test.go') diff --git a/cmd/platform/platform_test.go b/cmd/platform/platform_test.go new file mode 100644 index 000000000..8a2839c7e --- /dev/null +++ b/cmd/platform/platform_test.go @@ -0,0 +1,53 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package main + +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, "--"), 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 string(output) +} + +func runCommand(t *testing.T, args ...string) error { + path, err := os.Executable() + require.NoError(t, err) + return exec.Command(path, execArgs(t, args)...).Run() +} + +func TestExecCommand(t *testing.T) { + if filter := flag.Lookup("test.run").Value.String(); filter != "ExecCommand" { + t.Skip("use -run ExecCommand to execute a command via the test executable") + } + rootCmd.SetArgs(flag.Args()) + require.NoError(t, rootCmd.Execute()) +} -- cgit v1.2.3-1-g7c22