github.com/KusionStack/kpm@v0.8.4-0.20240326033734-dc72298a30e5/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 TestRunTarPkg(t *testing.T) {
   135  	pkgPath := getTestDir("test_run_tar_in_path")
   136  	tarPath, _ := filepath.Abs(filepath.Join(pkgPath, "test.tar"))
   137  	untarPath := filepath.Join(pkgPath, "test")
   138  	expectPath := filepath.Join(pkgPath, "expected")
   139  	expectPathJson := filepath.Join(pkgPath, "expected.json")
   140  
   141  	if utils.DirExists(untarPath) {
   142  		os.RemoveAll(untarPath)
   143  	}
   144  
   145  	expectedResult, _ := os.ReadFile(expectPath)
   146  	expectedResultJson, _ := os.ReadFile(expectPathJson)
   147  	opts := opt.DefaultCompileOptions()
   148  	opts.SetVendor(true)
   149  	gotResult, err := RunTarPkg(tarPath, opts)
   150  	assert.Equal(t, err, nil)
   151  	assert.Equal(t, utils.RmNewline(string(expectedResult)), utils.RmNewline(gotResult.GetRawYamlResult()))
   152  	assert.Equal(t, utils.RmNewline(string(expectedResultJson)), utils.RmNewline(gotResult.GetRawJsonResult()))
   153  	assert.Equal(t, utils.DirExists(untarPath), true)
   154  
   155  	if utils.DirExists(untarPath) {
   156  		os.RemoveAll(untarPath)
   157  	}
   158  }
   159  
   160  func TestRunWithNoSumCheck(t *testing.T) {
   161  	pkgPath := getTestDir("test_run_with_nosumcheck")
   162  	opts := opt.DefaultCompileOptions()
   163  	opts.SetPkgPath(pkgPath)
   164  	opts.SetNoSumCheck(true)
   165  	_, err := RunPkgInPath(opts)
   166  	assert.Equal(t, err, nil)
   167  	assert.Equal(t, utils.DirExists(filepath.Join(pkgPath, "kcl.mod.lock")), false)
   168  
   169  	opts = opt.DefaultCompileOptions()
   170  	opts.SetPkgPath(pkgPath)
   171  	opts.SetNoSumCheck(false)
   172  	_, err = RunPkgInPath(opts)
   173  	assert.Equal(t, err, nil)
   174  	assert.Equal(t, utils.DirExists(filepath.Join(pkgPath, "kcl.mod.lock")), true)
   175  	defer func() {
   176  		_ = os.Remove(filepath.Join(pkgPath, "kcl.mod.lock"))
   177  	}()
   178  }
   179  
   180  func TestRunPkgWithOpts(t *testing.T) {
   181  	pkgPath := getTestDir("test_run_pkg_in_path")
   182  
   183  	result, err := RunWithOpts(
   184  		opt.WithNoSumCheck(false),
   185  		opt.WithEntries([]string{filepath.Join(pkgPath, "test_kcl", "main.k")}),
   186  		opt.WithKclOption(kcl.WithWorkDir(filepath.Join(pkgPath, "test_kcl"))),
   187  	)
   188  
   189  	assert.Equal(t, err, nil)
   190  	expected, _ := os.ReadFile(filepath.Join(pkgPath, "expected"))
   191  	assert.Equal(t, utils.RmNewline(string(result.GetRawYamlResult())), utils.RmNewline(string(expected)))
   192  	expectedJson, _ := os.ReadFile(filepath.Join(pkgPath, "expected.json"))
   193  	assert.Equal(t, utils.RmNewline(string(result.GetRawJsonResult())), utils.RmNewline(string(expectedJson)))
   194  }
   195  
   196  func TestRunWithOptsAndNoSumCheck(t *testing.T) {
   197  	pkgPath := filepath.Join(getTestDir("test_run_pkg_in_path"), "test_run_no_sum_check")
   198  	testCases := []string{"dep_git_commit", "dep_git_tag", "dep_oci"}
   199  
   200  	for _, testCase := range testCases {
   201  
   202  		pathMainK := filepath.Join(pkgPath, testCase, "main.k")
   203  		workDir := filepath.Join(pkgPath, testCase)
   204  		modLock := filepath.Join(workDir, "kcl.mod.lock")
   205  		expected, err := os.ReadFile(filepath.Join(pkgPath, testCase, "expected"))
   206  		assert.Equal(t, err, nil)
   207  		fmt.Printf("testCase: %v\n", testCase)
   208  		res, err := RunWithOpts(
   209  			opt.WithNoSumCheck(true),
   210  			opt.WithEntries([]string{pathMainK}),
   211  			opt.WithKclOption(kcl.WithWorkDir(workDir)),
   212  		)
   213  		assert.Equal(t, err, nil)
   214  		assert.Equal(t, utils.DirExists(modLock), false)
   215  		assert.Equal(t, utils.RmNewline(res.GetRawYamlResult()), utils.RmNewline(string(expected)))
   216  		assert.Equal(t, err, nil)
   217  	}
   218  }
   219  
   220  func TestRunWithOptsWithNoLog(t *testing.T) {
   221  	pkgPath := filepath.Join(getTestDir("test_run_pkg_in_path"), "test_run_with_no_log")
   222  
   223  	defer func() {
   224  		_ = os.Remove(filepath.Join(pkgPath, "kcl.mod.lock"))
   225  	}()
   226  
   227  	old := os.Stdout
   228  	r, w, _ := os.Pipe()
   229  	os.Stdout = w
   230  
   231  	pathMainK := filepath.Join(pkgPath, "main.k")
   232  
   233  	_, err := RunWithOpts(
   234  		opt.WithLogWriter(nil),
   235  		opt.WithEntries([]string{pathMainK}),
   236  		opt.WithKclOption(kcl.WithWorkDir(pkgPath)),
   237  	)
   238  	assert.Equal(t, err, nil)
   239  	os.Stdout = old
   240  	w.Close()
   241  	var buf bytes.Buffer
   242  	_, err = buf.ReadFrom(r)
   243  	assert.Equal(t, err, nil)
   244  
   245  	assert.Equal(t, buf.String(), "")
   246  }