github.com/upcmd/up@v0.8.1-0.20230108151705-ad8b797bf04f/tests/shared.go (about)

     1  // Ultimate Provisioner: UP cmd
     2  // Copyright (c) 2019 Stephen Cheng and contributors
     3  
     4  /* This Source Code Form is subject to the terms of the Mozilla Public
     5   * License, v. 2.0. If a copy of the MPL was not distributed with this
     6   * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
     7  
     8  package tests
     9  
    10  import (
    11  	"github.com/upcmd/up/biz/impl"
    12  	u "github.com/upcmd/up/utils"
    13  	"os/exec"
    14  	"path"
    15  	"path/filepath"
    16  	"runtime"
    17  	"strings"
    18  	"testing"
    19  )
    20  
    21  //TestHello -> Hello
    22  func GetTestName(testFullName string) string {
    23  	return strings.Replace(testFullName, "Test", "", 1)
    24  }
    25  
    26  func RunCmd(t *testing.T, cmd string) string {
    27  	t.Logf("cmd:%s", cmd)
    28  	cmdExec := exec.Command("/bin/sh", "-c", cmd)
    29  	exec.Command("bash", "-c", cmd)
    30  	cmdOutput, err := cmdExec.Output()
    31  	if err != nil {
    32  		panic(err)
    33  	}
    34  	t.Log("exec result:", string(cmdOutput))
    35  	return string(cmdOutput)
    36  }
    37  
    38  func Setup(prefix string, t *testing.T) *u.UpConfig {
    39  	cfg := u.NewUpConfig("", "").InitConfig()
    40  	cfg.SetTaskfile(GetTestName(u.Spfv("%s%s", prefix, t.Name())))
    41  	cfg.ShowCoreConfig("mocktest")
    42  
    43  	u.Pln(" :test task file:", impl.ConfigRuntime().TaskFile)
    44  	u.Pln(" :release version:", impl.ConfigRuntime().Version)
    45  	u.Pln(" :verbose level:", impl.ConfigRuntime().Verbose)
    46  	return cfg
    47  }
    48  
    49  func TestT(prefix string, t *testing.T) {
    50  	cfg := Setup(prefix, t)
    51  	tasker := impl.NewTasker("dev", "", cfg)
    52  	tasker.ListTasks()
    53  	tasker.ExecTask("task", nil, false)
    54  }
    55  
    56  //mock required settings
    57  func Setupx(filename string, cfg *u.UpConfig) {
    58  	filenameonly := path.Base(filename)
    59  	//filenoext := strings.TrimSuffix(filenameonly, filepath.Ext(filenameonly))
    60  	//cfg.SetTaskfile(GetTestName(filenoext))
    61  	cfg.SetTaskfile(filenameonly)
    62  	cfg.SetRefdir("./tests/functests")
    63  	cfg.Secure = &u.SecureSetting{Type: "default_aes", Key: "enc_key"}
    64  	cfg.ShowCoreConfig("mocktest")
    65  	u.Ppmsgvvvvhint("core config", cfg)
    66  	u.Pln(" :test task file:", cfg.TaskFile)
    67  	u.Pln(" :release version:", cfg.Version)
    68  	u.Pln(" :verbose level:", cfg.Verbose)
    69  
    70  }
    71  
    72  func GetUnitTestCollection() []string {
    73  	_, filename, _, _ := runtime.Caller(1)
    74  	dir := path.Dir(filename)
    75  
    76  	files, err := filepath.Glob(u.Spfv("%s/%s", dir, "c????.yml"))
    77  	u.LogError("list func test cases", err)
    78  
    79  	for _, f := range files {
    80  		u.Pln(f)
    81  	}
    82  
    83  	return files
    84  }
    85  
    86  func GetModuleTestCollection() []string {
    87  	//TODO: to implemene this, delete all below
    88  	_, filename, _, _ := runtime.Caller(1)
    89  	dir := path.Dir(filename)
    90  
    91  	files, err := filepath.Glob(u.Spfv("%s/????", dir))
    92  	u.LogError("list func test cases", err)
    93  
    94  	for _, f := range files {
    95  		u.Pln(f)
    96  	}
    97  
    98  	return files
    99  }