refactor: move test to cocatest

上级 78aee9c1
package cmd
import (
"bytes"
"github.com/mattn/go-shellwords"
"github.com/phodal/coca/cocatest"
"github.com/phodal/coca/cocatest/testcase"
"github.com/spf13/cobra"
"io"
"os"
"path/filepath"
"strings"
"testing"
)
func RunTestCmd(t *testing.T, tests []testcase.CmdTestCase) {
RunTestCaseWithCmd(t, tests, NewRootCmd)
cocatest.RunTestCaseWithCmd(t, tests, NewRootCmd)
}
func RunTestCaseWithCmd(t *testing.T, tests []testcase.CmdTestCase, rootCmd func(out io.Writer) *cobra.Command) {
t.Helper()
for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
defer ResetEnv()()
t.Log("running Cmd: ", tt.Cmd)
_, output, err := executeActionCommandC(tt.Cmd, rootCmd)
if (err != nil) != tt.WantError {
t.Errorf("expected error, got '%v'", err)
}
if tt.Golden != "" {
abs, _ := filepath.Abs(tt.Golden)
slash := filepath.FromSlash(abs)
AssertGoldenString(t, output, slash)
}
})
}
}
func executeActionCommandC(cmd string, rootCmd func(out io.Writer) *cobra.Command) (*cobra.Command, string, error) {
args, err := shellwords.Parse(cmd)
if err != nil {
return nil, "", err
}
buf := new(bytes.Buffer)
command := rootCmd(buf)
command.SetArgs(args)
c, err := command.ExecuteC()
return c, buf.String(), err
}
func ResetEnv() func() {
origEnv := os.Environ()
return func() {
os.Clearenv()
for _, pair := range origEnv {
kv := strings.SplitN(pair, "=", 2)
os.Setenv(kv[0], kv[1])
}
}
}
package cmd
package cocatest
// based on https://github.com/helm/helm
// license: apache 2.0
......
package cocatest
import (
"bytes"
"github.com/mattn/go-shellwords"
"github.com/phodal/coca/cocatest/testcase"
"github.com/spf13/cobra"
"io"
"os"
"path/filepath"
"strings"
"testing"
)
func RunTestCaseWithCmd(t *testing.T, tests []testcase.CmdTestCase, rootCmd func(out io.Writer) *cobra.Command) {
t.Helper()
for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
defer ResetEnv()()
t.Log("running Cmd: ", tt.Cmd)
_, output, err := executeActionCommandC(tt.Cmd, rootCmd)
if (err != nil) != tt.WantError {
t.Errorf("expected error, got '%v'", err)
}
if tt.Golden != "" {
abs, _ := filepath.Abs(tt.Golden)
slash := filepath.FromSlash(abs)
AssertGoldenString(t, output, slash)
}
})
}
}
func executeActionCommandC(cmd string, rootCmd func(out io.Writer) *cobra.Command) (*cobra.Command, string, error) {
args, err := shellwords.Parse(cmd)
if err != nil {
return nil, "", err
}
buf := new(bytes.Buffer)
command := rootCmd(buf)
command.SetArgs(args)
c, err := command.ExecuteC()
return c, buf.String(), err
}
func ResetEnv() func() {
origEnv := os.Environ()
return func() {
os.Clearenv()
for _, pair := range origEnv {
kv := strings.SplitN(pair, "=", 2)
os.Setenv(kv[0], kv[1])
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册