kcl-lang.io/kpm@v0.8.7-0.20240520061008-9fc4c5efc8c7/pkg/api/kpm_run_test.go (about)

     1  package api
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  	"kcl-lang.io/kcl-go/pkg/kcl"
    13  	"kcl-lang.io/kpm/pkg/opt"
    14  	"kcl-lang.io/kpm/pkg/utils"
    15  )
    16  
    17  const testDataDir = "test_data"
    18  
    19  func getTestDir(subDir string) string {
    20  	pwd, _ := os.Getwd()
    21  	testDir := filepath.Join(pwd, testDataDir)
    22  	testDir = filepath.Join(testDir, subDir)
    23  
    24  	return testDir
    25  }
    26  
    27  func TestGetAbsInputPath(t *testing.T) {
    28  	pkgPath := getTestDir("test_abs_input")
    29  	path, err := getAbsInputPath(filepath.Join(pkgPath, "test_pkg_path"), "test_input")
    30  	assert.Equal(t, err, nil)
    31  	assert.Equal(t, path, filepath.Join(filepath.Join(pkgPath, "test_pkg_path"), "test_input"))
    32  
    33  	path, err = getAbsInputPath(pkgPath, filepath.Join("test_pkg_path", "test_input"))
    34  	assert.Equal(t, err, nil)
    35  	assert.Equal(t, path, filepath.Join(filepath.Join(pkgPath, "test_pkg_path"), "test_input"))
    36  
    37  	path, err = getAbsInputPath(pkgPath, "test_input_outside")
    38  	assert.Equal(t, err, nil)
    39  	assert.Equal(t, path, filepath.Join(pkgPath, "test_input_outside"))
    40  
    41  	path, err = getAbsInputPath(pkgPath, "path_not_exist")
    42  	assert.NotEqual(t, err, nil)
    43  	assert.Equal(t, path, "")
    44  }
    45  
    46  func TestRunPkgInPath(t *testing.T) {
    47  	pkgPath := getTestDir("test_run_pkg_in_path")
    48  	opts := opt.DefaultCompileOptions()
    49  	opts.AddEntry(filepath.Join(pkgPath, "test_kcl", "main.k"))
    50  	opts.SetPkgPath(filepath.Join(pkgPath, "test_kcl"))
    51  	result, err := RunPkgInPath(opts)
    52  	assert.Equal(t, err, nil)
    53  	expected, _ := os.ReadFile(filepath.Join(pkgPath, "expected"))
    54  	assert.Equal(t, utils.RmNewline(string(result)), utils.RmNewline(string(expected)))
    55  }
    56  
    57  func TestRunPkgInPathInvalidPath(t *testing.T) {
    58  	pkgPath := getTestDir("test_run_pkg_in_path")
    59  	opts := opt.DefaultCompileOptions()
    60  	opts.AddEntry(filepath.Join(pkgPath, "test_kcl", "not_exist.k"))
    61  	opts.SetPkgPath(filepath.Join(pkgPath, "test_kcl"))
    62  	result, err := RunPkgInPath(opts)
    63  	assert.NotEqual(t, err, nil)
    64  	assert.Equal(t, err.Error(), fmt.Sprintf("failed to compile the kcl package\nCannot find the kcl file, please check the file path %s\n", filepath.Join(pkgPath, "test_kcl", "not_exist.k")))
    65  	assert.Equal(t, result, "")
    66  }
    67  
    68  func TestRunPkgInPathInvalidPkg(t *testing.T) {
    69  	pkgPath := getTestDir("test_run_pkg_in_path")
    70  	opts := opt.DefaultCompileOptions()
    71  	opts.SetPkgPath(pkgPath)
    72  	opts.Merge(kcl.WithKFilenames(filepath.Join(pkgPath, "invalid_pkg", "not_exist.k")))
    73  	result, err := RunPkgInPath(opts)
    74  	assert.NotEqual(t, err, nil)
    75  	assert.Equal(t, true, strings.Contains(err.Error(), fmt.Sprintf("could not load 'kcl.mod' in '%s'\n", pkgPath)))
    76  	assert.Equal(t, result, "")
    77  }
    78  
    79  func TestRunTar(t *testing.T) {
    80  	pkgPath := getTestDir("test_run_tar_in_path")
    81  	tarPath, _ := filepath.Abs(filepath.Join(pkgPath, "test.tar"))
    82  	untarPath := filepath.Join(pkgPath, "test")
    83  	expectPath := filepath.Join(pkgPath, "expected")
    84  
    85  	if utils.DirExists(untarPath) {
    86  		os.RemoveAll(untarPath)
    87  	}
    88  
    89  	expectedResult, _ := os.ReadFile(expectPath)
    90  	opts := opt.DefaultCompileOptions()
    91  	opts.SetVendor(true)
    92  	gotResult, err := RunTar(tarPath, opts)
    93  	assert.Equal(t, err, nil)
    94  	assert.Equal(t, utils.RmNewline(string(expectedResult)), utils.RmNewline(gotResult))
    95  	assert.Equal(t, utils.DirExists(untarPath), true)
    96  
    97  	if utils.DirExists(untarPath) {
    98  		os.RemoveAll(untarPath)
    99  	}
   100  }
   101  
   102  func TestRunWithWorkdir(t *testing.T) {
   103  	pkgPath := getTestDir(filepath.Join("test_work_dir", "dev"))
   104  	opts := opt.DefaultCompileOptions()
   105  	opts.SetPkgPath(pkgPath)
   106  	result, err := RunPkgInPath(opts)
   107  	assert.Equal(t, err, nil)
   108  	assert.Equal(t, result, "base: base\nmain: main")
   109  }
   110  
   111  func TestRunWithOpts(t *testing.T) {
   112  	pkgPath := getTestDir("test_run_pkg_in_path")
   113  	opts := opt.DefaultCompileOptions()
   114  	opts.AddEntry(filepath.Join(pkgPath, "test_kcl", "main.k"))
   115  	opts.SetPkgPath(filepath.Join(pkgPath, "test_kcl"))
   116  	result, err := RunPkgWithOpt(opts)
   117  	fmt.Printf("err: %v\n", err)
   118  	assert.Equal(t, err, nil)
   119  	expected, _ := os.ReadFile(filepath.Join(pkgPath, "expected"))
   120  	assert.Equal(t, utils.RmNewline(string(result.GetRawYamlResult())), utils.RmNewline(string(expected)))
   121  	expectedJson, _ := os.ReadFile(filepath.Join(pkgPath, "expected.json"))
   122  	assert.Equal(t, utils.RmNewline(string(result.GetRawJsonResult())), utils.RmNewline(string(expectedJson)))
   123  }
   124  
   125  func TestRunWithSettingsOpts(t *testing.T) {
   126  	pkgPath := getTestDir("test_settings")
   127  	opts := opt.DefaultCompileOptions()
   128  	opts.Merge(kcl.WithSettings(filepath.Join(pkgPath, "kcl.yaml")))
   129  	opts.SetHasSettingsYaml(true)
   130  	_, err := RunWithOpt(opts)
   131  	assert.Equal(t, err, nil)
   132  }
   133  
   134  func TestRunWithSettingsOptsAndFile(t *testing.T) {
   135  	pkgPath := getTestDir("test_settings")
   136  	opts := opt.DefaultCompileOptions()
   137  	opts.Merge(kcl.WithSettings(filepath.Join(pkgPath, "kcl.yaml")))
   138  	opts.SetHasSettingsYaml(true)
   139  	opts.SetEntries([]string{filepath.Join(pkgPath, "test.k")})
   140  	_, err := RunWithOpt(opts)
   141  	assert.Equal(t, err, nil)
   142  }
   143  
   144  func TestRunTarPkg(t *testing.T) {
   145  	pkgPath := getTestDir("test_run_tar_in_path")
   146  	tarPath, _ := filepath.Abs(filepath.Join(pkgPath, "test.tar"))
   147  	untarPath := filepath.Join(pkgPath, "test")
   148  	expectPath := filepath.Join(pkgPath, "expected")
   149  	expectPathJson := filepath.Join(pkgPath, "expected.json")
   150  
   151  	if utils.DirExists(untarPath) {
   152  		os.RemoveAll(untarPath)
   153  	}
   154  
   155  	expectedResult, _ := os.ReadFile(expectPath)
   156  	expectedResultJson, _ := os.ReadFile(expectPathJson)
   157  	opts := opt.DefaultCompileOptions()
   158  	opts.SetVendor(true)
   159  	gotResult, err := RunTarPkg(tarPath, opts)
   160  	assert.Equal(t, err, nil)
   161  	assert.Equal(t, utils.RmNewline(string(expectedResult)), utils.RmNewline(gotResult.GetRawYamlResult()))
   162  	assert.Equal(t, utils.RmNewline(string(expectedResultJson)), utils.RmNewline(gotResult.GetRawJsonResult()))
   163  	assert.Equal(t, utils.DirExists(untarPath), true)
   164  
   165  	if utils.DirExists(untarPath) {
   166  		os.RemoveAll(untarPath)
   167  	}
   168  }
   169  
   170  func TestRunWithNoSumCheck(t *testing.T) {
   171  	pkgPath := getTestDir("test_run_with_nosumcheck")
   172  	opts := opt.DefaultCompileOptions()
   173  	opts.SetPkgPath(pkgPath)
   174  	opts.SetNoSumCheck(true)
   175  	_, err := RunPkgInPath(opts)
   176  	assert.Equal(t, err, nil)
   177  	assert.Equal(t, utils.DirExists(filepath.Join(pkgPath, "kcl.mod.lock")), false)
   178  
   179  	opts = opt.DefaultCompileOptions()
   180  	opts.SetPkgPath(pkgPath)
   181  	opts.SetNoSumCheck(false)
   182  	_, err = RunPkgInPath(opts)
   183  	assert.Equal(t, err, nil)
   184  	assert.Equal(t, utils.DirExists(filepath.Join(pkgPath, "kcl.mod.lock")), true)
   185  	defer func() {
   186  		_ = os.Remove(filepath.Join(pkgPath, "kcl.mod.lock"))
   187  	}()
   188  }
   189  
   190  func TestRunPkgWithOpts(t *testing.T) {
   191  	pkgPath := getTestDir("test_run_pkg_in_path")
   192  
   193  	result, err := RunWithOpts(
   194  		opt.WithNoSumCheck(false),
   195  		opt.WithEntries([]string{filepath.Join(pkgPath, "test_kcl", "main.k")}),
   196  		opt.WithKclOption(kcl.WithWorkDir(filepath.Join(pkgPath, "test_kcl"))),
   197  	)
   198  
   199  	assert.Equal(t, err, nil)
   200  	expected, _ := os.ReadFile(filepath.Join(pkgPath, "expected"))
   201  	assert.Equal(t, utils.RmNewline(string(result.GetRawYamlResult())), utils.RmNewline(string(expected)))
   202  	expectedJson, _ := os.ReadFile(filepath.Join(pkgPath, "expected.json"))
   203  	assert.Equal(t, utils.RmNewline(string(result.GetRawJsonResult())), utils.RmNewline(string(expectedJson)))
   204  }
   205  
   206  func TestRunWithOptsAndNoSumCheck(t *testing.T) {
   207  	pkgPath := filepath.Join(getTestDir("test_run_pkg_in_path"), "test_run_no_sum_check")
   208  	testCases := []string{"dep_git_commit", "dep_git_tag", "dep_oci"}
   209  
   210  	for _, testCase := range testCases {
   211  
   212  		pathMainK := filepath.Join(pkgPath, testCase, "main.k")
   213  		workDir := filepath.Join(pkgPath, testCase)
   214  		modLock := filepath.Join(workDir, "kcl.mod.lock")
   215  		expected, err := os.ReadFile(filepath.Join(pkgPath, testCase, "expected"))
   216  		assert.Equal(t, err, nil)
   217  		fmt.Printf("testCase: %v\n", testCase)
   218  		res, err := RunWithOpts(
   219  			opt.WithNoSumCheck(true),
   220  			opt.WithEntries([]string{pathMainK}),
   221  			opt.WithKclOption(kcl.WithWorkDir(workDir)),
   222  		)
   223  		assert.Equal(t, err, nil)
   224  		assert.Equal(t, utils.DirExists(modLock), false)
   225  		assert.Equal(t, utils.RmNewline(res.GetRawYamlResult()), utils.RmNewline(string(expected)))
   226  		assert.Equal(t, err, nil)
   227  	}
   228  }
   229  
   230  func TestRunWithOptsWithNoLog(t *testing.T) {
   231  	pkgPath := filepath.Join(getTestDir("test_run_pkg_in_path"), "test_run_with_no_log")
   232  
   233  	defer func() {
   234  		_ = os.Remove(filepath.Join(pkgPath, "kcl.mod.lock"))
   235  	}()
   236  
   237  	old := os.Stdout
   238  	r, w, _ := os.Pipe()
   239  	os.Stdout = w
   240  
   241  	pathMainK := filepath.Join(pkgPath, "main.k")
   242  
   243  	_, err := RunWithOpts(
   244  		opt.WithLogWriter(nil),
   245  		opt.WithEntries([]string{pathMainK}),
   246  		opt.WithKclOption(kcl.WithWorkDir(pkgPath)),
   247  	)
   248  	assert.Equal(t, err, nil)
   249  	os.Stdout = old
   250  	w.Close()
   251  	var buf bytes.Buffer
   252  	_, err = buf.ReadFrom(r)
   253  	assert.Equal(t, err, nil)
   254  
   255  	assert.Equal(t, buf.String(), "")
   256  }