github.com/KusionStack/kpm@v0.8.4-0.20240326033734-dc72298a30e5/test/e2e/cli.go (about) 1 package e2e 2 3 import ( 4 "io" 5 "os" 6 "os/exec" 7 "path/filepath" 8 "strings" 9 "time" 10 11 "github.com/onsi/ginkgo/v2" 12 "github.com/onsi/gomega/gexec" 13 "kcl-lang.io/kpm/pkg/reporter" 14 ) 15 16 const TEST_WORKSPACE = "test_kpm_workspace" 17 18 // CreateTestWorkspace create an empty dir "($pwd)/test_kpm_workspace" for test kpm cli. 19 func CreateTestWorkspace() string { 20 workspacePath := filepath.Join(GetWorkDir(), TEST_WORKSPACE) 21 err := os.MkdirAll(workspacePath, 0755) 22 if err != nil { 23 reporter.ExitWithReport("kpm_e2e: failed to create workspace.") 24 } 25 return workspacePath 26 } 27 28 // CleanUpTestWorkspace will do 'rm -rf' to the "($pwd)/test_kpm_workspace". 29 func CleanUpTestWorkspace() string { 30 workspacePath := filepath.Join(GetWorkDir(), TEST_WORKSPACE) 31 err := os.RemoveAll(workspacePath) 32 if err != nil { 33 reporter.ExitWithReport("kpm_e2e: failed to clean up workspace.") 34 } 35 return workspacePath 36 } 37 38 // GetWorkspace return the path of test workspace. 39 func GetWorkspace() string { 40 return filepath.Join(GetWorkDir(), TEST_WORKSPACE) 41 } 42 43 // GetWrokDir return work directory 44 func GetWorkDir() string { 45 dir, err := os.Getwd() 46 if err != nil { 47 reporter.ExitWithReport("kpm_e2e: failed to load workspace.") 48 } 49 return dir 50 } 51 52 // GetKpmCLIBin return kusion binary path in e2e test 53 func GetKpmCLIBin() string { 54 dir, _ := os.Getwd() 55 binPath := filepath.Join(dir, "../..", "bin") 56 return binPath 57 } 58 59 // Exec execute common command 60 func Exec(cli string) (string, string, error) { 61 var output []byte 62 c := strings.Fields(cli) 63 command := exec.Command(c[0], c[1:]...) 64 session, err := gexec.Start(command, ginkgo.GinkgoWriter, ginkgo.GinkgoWriter) 65 if err != nil { 66 return string(output), string(output), err 67 } 68 s := session.Wait(300 * time.Second) 69 return string(s.Out.Contents()), string(s.Err.Contents()), nil 70 } 71 72 // Exec execute common command 73 func ExecWithWorkDir(cli, dir string) (string, error) { 74 var output []byte 75 c := strings.Fields(cli) 76 command := exec.Command(c[0], c[1:]...) 77 command.Dir = dir 78 session, err := gexec.Start(command, ginkgo.GinkgoWriter, ginkgo.GinkgoWriter) 79 if err != nil { 80 return string(output), err 81 } 82 s := session.Wait(300 * time.Second) 83 return string(s.Out.Contents()) + string(s.Err.Contents()), nil 84 } 85 86 // ExecKpm executes kusion command 87 func ExecKpm(cli string) (string, error) { 88 var output []byte 89 c := strings.Fields(cli) 90 commandName := filepath.Join(GetKpmCLIBin(), c[0]) 91 command := exec.Command(commandName, c[1:]...) 92 session, err := gexec.Start(command, ginkgo.GinkgoWriter, ginkgo.GinkgoWriter) 93 if err != nil { 94 return string(output), err 95 } 96 s := session.Wait(300 * time.Second) 97 return string(s.Out.Contents()) + string(s.Err.Contents()), nil 98 } 99 100 // ExecKpmWithInDirWithEnv executes kpm command in work directory with env. 101 func ExecKpmWithInDirWithEnv(cli, dir, env string) (string, string, error) { 102 var output []byte 103 c := strings.Fields(cli) 104 commandName := filepath.Join(GetKpmCLIBin(), c[0]) 105 command := exec.Command(commandName, c[1:]...) 106 command.Dir = dir 107 108 // Set the env 109 envLines := strings.FieldsFunc(env, func(c rune) bool { return c == '\n' }) 110 command.Env = append(command.Env, envLines...) 111 session, err := gexec.Start(command, ginkgo.GinkgoWriter, ginkgo.GinkgoWriter) 112 if err != nil { 113 return string(output), string(output), err 114 } 115 s := session.Wait(300 * time.Second) 116 return string(s.Out.Contents()), string(s.Err.Contents()), nil 117 } 118 119 // ExecKpmWithWorkDir executes kpm command in work directory 120 func ExecKpmWithWorkDir(cli, dir string) (string, string, error) { 121 var output []byte 122 c := SplitCommand(cli) 123 commandName := filepath.Join(GetKpmCLIBin(), c[0]) 124 command := exec.Command(commandName, c[1:]...) 125 command.Dir = dir 126 session, err := gexec.Start(command, ginkgo.GinkgoWriter, ginkgo.GinkgoWriter) 127 if err != nil { 128 return string(output), string(output), err 129 } 130 s := session.Wait(300 * time.Second) 131 return string(s.Out.Contents()), string(s.Err.Contents()), nil 132 } 133 134 // ExecKpmWithStdin executes kpm command in work directory with stdin 135 func ExecKpmWithStdin(cli, dir, input string) (string, error) { 136 var output []byte 137 c := strings.Fields(cli) 138 commandName := filepath.Join(GetKpmCLIBin(), c[0]) 139 command := exec.Command(commandName, c[1:]...) 140 command.Dir = dir 141 subStdin, err := command.StdinPipe() 142 if err != nil { 143 return string(output), err 144 } 145 _, err = io.WriteString(subStdin, input) 146 if err != nil { 147 return string(output), err 148 } 149 session, err := gexec.Start(command, ginkgo.GinkgoWriter, ginkgo.GinkgoWriter) 150 if err != nil { 151 return string(output), err 152 } 153 s := session.Wait(300 * time.Second) 154 return string(s.Out.Contents()) + string(s.Err.Contents()), nil 155 }