kcl-lang.io/kpm@v0.8.7-0.20240520061008-9fc4c5efc8c7/test/e2e/test_suite.go (about)

     1  package e2e
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	"kcl-lang.io/kpm/pkg/reporter"
    10  )
    11  
    12  const TEST_SUITES_DIR = "test_suites"
    13  
    14  const STDOUT_EXT = ".stdout"
    15  const STDERR_EXT = ".stderr"
    16  const INPUT_EXT = ".input"
    17  const JSON_EXT = ".json"
    18  const ENV_EXT = ".env"
    19  
    20  type TestSuite struct {
    21  	Name         string
    22  	Envs         string
    23  	Input        string
    24  	ExpectStdout string
    25  	ExpectStderr string
    26  }
    27  
    28  // / checkTestSuite Check that the file corresponding to each suffix can appear only once
    29  func CheckTestSuite(testSuitePath string, name string) {
    30  	files, err := os.ReadDir(testSuitePath)
    31  	if err != nil {
    32  		reporter.ExitWithReport("kpm_e2e: failed to check test suite.")
    33  	}
    34  
    35  	extFlags := map[string]*bool{
    36  		STDOUT_EXT: new(bool),
    37  		STDERR_EXT: new(bool),
    38  		INPUT_EXT:  new(bool),
    39  		ENV_EXT:    new(bool),
    40  	}
    41  
    42  	for _, file := range files {
    43  		if !file.IsDir() {
    44  			ext := filepath.Ext(file.Name())
    45  			if flag, ok := extFlags[ext]; ok {
    46  				if *flag {
    47  					reporter.ExitWithReport("kpm_e2e: invalid test suite, duplicate '*" + ext + "' file.")
    48  				}
    49  				*flag = true
    50  			} else {
    51  				reporter.ExitWithReport("kpm_e2e: invalid test suite, unknown file :", file.Name())
    52  			}
    53  		}
    54  	}
    55  
    56  	if !*extFlags[INPUT_EXT] {
    57  		reporter.Report("kpm_e2e: ignore test ", name)
    58  	}
    59  }
    60  
    61  // LoadTestSuite load test suite from 'getWorkDir()/test_suites/name'.
    62  func LoadTestSuite(testSuitePath, name string) TestSuite {
    63  	reporter.Report("kpm_e2e: loading '", name, "' from ", testSuitePath)
    64  	CheckTestSuite(testSuitePath, name)
    65  	return TestSuite{
    66  		Name:         name,
    67  		ExpectStdout: LoadFirstFileWithExt(testSuitePath, STDOUT_EXT),
    68  		ExpectStderr: LoadFirstFileWithExt(testSuitePath, STDERR_EXT),
    69  		Input:        LoadFirstFileWithExt(testSuitePath, INPUT_EXT),
    70  		Envs:         LoadFirstFileWithExt(testSuitePath, ENV_EXT),
    71  	}
    72  }
    73  
    74  // LoadAllTestSuites load all test suites from 'getWorkDir()/test_suites'.
    75  func LoadAllTestSuites(testSuitesDir string) []TestSuite {
    76  	testSuites := make([]TestSuite, 0)
    77  	files, err := os.ReadDir(testSuitesDir)
    78  
    79  	if err != nil {
    80  		reporter.ExitWithReport("kpm_e2e: failed to read test suites dir.")
    81  	}
    82  
    83  	for _, file := range files {
    84  		if file.IsDir() {
    85  			testSuites = append(
    86  				testSuites,
    87  				LoadTestSuite(
    88  					filepath.Join(testSuitesDir, file.Name()),
    89  					file.Name(),
    90  				),
    91  			)
    92  		}
    93  	}
    94  
    95  	return testSuites
    96  }
    97  
    98  // GetTestSuiteInfo return a info for a test suite "<name>:<info>:<env>"
    99  func (ts *TestSuite) GetTestSuiteInfo() string {
   100  	return fmt.Sprintf("%s:%s", ts.Name, strings.ReplaceAll(ts.Envs, "\n", ":"))
   101  }