Skip to content

Commit

Permalink
Merge pull request #489 from zong-zhe/mark-run-deprecated
Browse files Browse the repository at this point in the history
refactor: move some deperacted compilation methods into deperacted.go
  • Loading branch information
Peefy authored Sep 24, 2024
2 parents 090741f + 10d018c commit b7efafd
Show file tree
Hide file tree
Showing 30 changed files with 641 additions and 689 deletions.
43 changes: 43 additions & 0 deletions pkg/api/deperated.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package api

import (
"path/filepath"

"kcl-lang.io/kcl-go/pkg/kcl"
"kcl-lang.io/kpm/pkg/client"
"kcl-lang.io/kpm/pkg/opt"
)

// CompileWithOpt will compile the kcl program without kcl package.
// Deprecated: This method will not be maintained in the future. Use RunWithOpts instead.
func RunWithOpt(opts *opt.CompileOptions) (*kcl.KCLResultList, error) {
// The entries will override the entries in the settings file.
if opts.HasSettingsYaml() && len(opts.KFilenameList) > 0 && len(opts.Entries()) > 0 {
opts.KFilenameList = []string{}
}
if len(opts.Entries()) > 0 {
for _, entry := range opts.Entries() {
if filepath.IsAbs(entry) {
opts.Merge(kcl.WithKFilenames(entry))
} else {
opts.Merge(kcl.WithKFilenames(filepath.Join(opts.PkgPath(), entry)))
}
}
} else if !opts.HasSettingsYaml() && len(opts.KFilenameList) == 0 {
// If no entry, no kcl files and no settings files.
opts.Merge(kcl.WithKFilenames(opts.PkgPath()))
}
opts.Merge(kcl.WithWorkDir(opts.PkgPath()))
return kcl.RunWithOpts(*opts.Option)
}

// RunPkgWithOpt will compile the kcl package with the compile options.
// Deprecated: This method will not be maintained in the future. Use RunWithOpts instead.
func RunPkgWithOpt(opts *opt.CompileOptions) (*kcl.KCLResultList, error) {
kpmcli, err := client.NewKpmClient()
kpmcli.SetNoSumCheck(opts.NoSumCheck())
if err != nil {
return nil, err
}
return run(kpmcli, opts)
}
118 changes: 118 additions & 0 deletions pkg/api/deperated_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package api

import (
"bytes"
"fmt"
"os"
"path/filepath"
"testing"

"gotest.tools/v3/assert"
"kcl-lang.io/kcl-go/pkg/kcl"
"kcl-lang.io/kpm/pkg/opt"
"kcl-lang.io/kpm/pkg/utils"
)

func TestGetEntries(t *testing.T) {
testPath := getTestDir("test_get_entries")
pkgPath := filepath.Join(testPath, "no_entries")
pkg, err := GetKclPackage(pkgPath)
assert.Equal(t, err, nil)
assert.Equal(t, len(pkg.GetPkgProfile().GetEntries()), 0)

pkgPath = filepath.Join(testPath, "with_path_entries")
pkg, err = GetKclPackage(pkgPath)
assert.Equal(t, err, nil)
assert.Equal(t, len(pkg.GetPkgProfile().GetEntries()), 1)

res, err := RunWithOpts(
opt.WithEntries(pkg.GetPkgProfile().GetEntries()),
opt.WithKclOption(kcl.WithWorkDir(pkgPath)),
)

assert.Equal(t, err, nil)
assert.Equal(t, res.GetRawYamlResult(), "sub: test in sub")
assert.Equal(t, res.GetRawJsonResult(), "{\"sub\": \"test in sub\"}")
}

func TestRunWithOpts(t *testing.T) {
pkgPath := getTestDir("test_run_pkg_in_path")
opts := opt.DefaultCompileOptions()
opts.AddEntry(filepath.Join(pkgPath, "test_kcl", "main.k"))
opts.SetPkgPath(filepath.Join(pkgPath, "test_kcl"))
result, err := RunPkgWithOpt(opts)
fmt.Printf("err: %v\n", err)
assert.Equal(t, err, nil)
expected, _ := os.ReadFile(filepath.Join(pkgPath, "expected"))
assert.Equal(t, utils.RmNewline(string(result.GetRawYamlResult())), utils.RmNewline(string(expected)))
expectedJson, _ := os.ReadFile(filepath.Join(pkgPath, "expected.json"))
assert.Equal(t, utils.RmNewline(string(result.GetRawJsonResult())), utils.RmNewline(string(expectedJson)))
}

func TestRunPkgWithOpts(t *testing.T) {
pkgPath := getTestDir("test_run_pkg_in_path")

result, err := RunWithOpts(
opt.WithNoSumCheck(false),
opt.WithEntries([]string{filepath.Join(pkgPath, "test_kcl", "main.k")}),
opt.WithKclOption(kcl.WithWorkDir(filepath.Join(pkgPath, "test_kcl"))),
)

assert.Equal(t, err, nil)
expected, _ := os.ReadFile(filepath.Join(pkgPath, "expected"))
assert.Equal(t, utils.RmNewline(string(result.GetRawYamlResult())), utils.RmNewline(string(expected)))
expectedJson, _ := os.ReadFile(filepath.Join(pkgPath, "expected.json"))
assert.Equal(t, utils.RmNewline(string(result.GetRawJsonResult())), utils.RmNewline(string(expectedJson)))
}

func TestRunWithOptsAndNoSumCheck(t *testing.T) {
pkgPath := filepath.Join(getTestDir("test_run_pkg_in_path"), "test_run_no_sum_check")
testCases := []string{"dep_git_commit", "dep_git_tag", "dep_oci"}

for _, testCase := range testCases {

pathMainK := filepath.Join(pkgPath, testCase, "main.k")
workDir := filepath.Join(pkgPath, testCase)
modLock := filepath.Join(workDir, "kcl.mod.lock")
expected, err := os.ReadFile(filepath.Join(pkgPath, testCase, "expected"))
assert.Equal(t, err, nil)
fmt.Printf("testCase: %v\n", testCase)
res, err := RunWithOpts(
opt.WithNoSumCheck(true),
opt.WithEntries([]string{pathMainK}),
opt.WithKclOption(kcl.WithWorkDir(workDir)),
)
assert.Equal(t, err, nil)
assert.Equal(t, utils.DirExists(modLock), false)
assert.Equal(t, utils.RmNewline(res.GetRawYamlResult()), utils.RmNewline(string(expected)))
assert.Equal(t, err, nil)
}
}

func TestRunWithOptsWithNoLog(t *testing.T) {
pkgPath := filepath.Join(getTestDir("test_run_pkg_in_path"), "test_run_with_no_log")

defer func() {
_ = os.Remove(filepath.Join(pkgPath, "kcl.mod.lock"))
}()

old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

pathMainK := filepath.Join(pkgPath, "main.k")

_, err := RunWithOpts(
opt.WithLogWriter(nil),
opt.WithEntries([]string{pathMainK}),
opt.WithKclOption(kcl.WithWorkDir(pkgPath)),
)
assert.Equal(t, err, nil)
os.Stdout = old
w.Close()
var buf bytes.Buffer
_, err = buf.ReadFrom(r)
assert.Equal(t, err, nil)

assert.Equal(t, buf.String(), "")
}
26 changes: 1 addition & 25 deletions pkg/api/kpm_pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import (
"testing"

"gotest.tools/v3/assert"
"kcl-lang.io/kcl-go/pkg/kcl"
"kcl-lang.io/kpm/pkg/client"
"kcl-lang.io/kpm/pkg/opt"
)

func TestPackageApi(t *testing.T) {
Expand All @@ -32,7 +30,7 @@ func TestPackageApi(t *testing.T) {
assert.Equal(t, dep.Version, "1.27")
assert.Equal(t, dep.Source.Registry.Oci.Reg, "ghcr.io")
assert.Equal(t, dep.Source.Registry.Oci.Repo, "kcl-lang/k8s")
assert.Equal(t, dep.Source.Registry .Oci.Tag, "1.27")
assert.Equal(t, dep.Source.Registry.Oci.Tag, "1.27")

assert.Equal(t, dep.GetLocalFullPath(""), filepath.Join(kcl_pkg_path, "k8s_1.27"))

Expand Down Expand Up @@ -187,28 +185,6 @@ func TestGetSchemaTypeUnderEmptyDir(t *testing.T) {
assert.Equal(t, schemas[filepath.Join(".")]["SchemaInMain"].SchemaName, "SchemaInMain")
}

func TestGetEntries(t *testing.T) {
testPath := getTestDir("test_get_entries")
pkgPath := filepath.Join(testPath, "no_entries")
pkg, err := GetKclPackage(pkgPath)
assert.Equal(t, err, nil)
assert.Equal(t, len(pkg.GetPkgProfile().GetEntries()), 0)

pkgPath = filepath.Join(testPath, "with_path_entries")
pkg, err = GetKclPackage(pkgPath)
assert.Equal(t, err, nil)
assert.Equal(t, len(pkg.GetPkgProfile().GetEntries()), 1)

res, err := RunWithOpts(
opt.WithEntries(pkg.GetPkgProfile().GetEntries()),
opt.WithKclOption(kcl.WithWorkDir(pkgPath)),
)

assert.Equal(t, err, nil)
assert.Equal(t, res.GetRawYamlResult(), "sub: test in sub")
assert.Equal(t, res.GetRawJsonResult(), "{\"sub\": \"test in sub\"}")
}

func TestExportSwaggerV2Spec(t *testing.T) {
pkg_path := filepath.Join(getTestDir("test_kpm_package"), "export_swagger", "aaa")
pkg, err := GetKclPackage(pkg_path)
Expand Down
34 changes: 0 additions & 34 deletions pkg/api/kpm_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,29 +65,6 @@ func RunPkgInPath(opts *opt.CompileOptions) (string, error) {
return compileResult.GetRawYamlResult(), nil
}

// CompileWithOpt will compile the kcl program without kcl package.
// Deprecated: This method will not be maintained in the future. Use RunWithOpts instead.
func RunWithOpt(opts *opt.CompileOptions) (*kcl.KCLResultList, error) {
// The entries will override the entries in the settings file.
if opts.HasSettingsYaml() && len(opts.KFilenameList) > 0 && len(opts.Entries()) > 0 {
opts.KFilenameList = []string{}
}
if len(opts.Entries()) > 0 {
for _, entry := range opts.Entries() {
if filepath.IsAbs(entry) {
opts.Merge(kcl.WithKFilenames(entry))
} else {
opts.Merge(kcl.WithKFilenames(filepath.Join(opts.PkgPath(), entry)))
}
}
} else if !opts.HasSettingsYaml() && len(opts.KFilenameList) == 0 {
// If no entry, no kcl files and no settings files.
opts.Merge(kcl.WithKFilenames(opts.PkgPath()))
}
opts.Merge(kcl.WithWorkDir(opts.PkgPath()))
return kcl.RunWithOpts(*opts.Option)
}

// RunWithOpts will compile the kcl package with the compile options.
func RunWithOpts(opts ...opt.Option) (*kcl.KCLResultList, error) {
mergedOpts := opt.DefaultCompileOptions()
Expand Down Expand Up @@ -115,17 +92,6 @@ func getAbsInputPath(pkgPath string, inputPath string) (string, error) {
return "", errors.EntryFileNotFound
}

// RunPkgWithOpt will compile the kcl package with the compile options.
// Deprecated: This method will not be maintained in the future. Use RunWithOpts instead.
func RunPkgWithOpt(opts *opt.CompileOptions) (*kcl.KCLResultList, error) {
kpmcli, err := client.NewKpmClient()
kpmcli.SetNoSumCheck(opts.NoSumCheck())
if err != nil {
return nil, err
}
return run(kpmcli, opts)
}

func runPkgWithOpt(opts *opt.CompileOptions) (*kcl.KCLResultList, error) {
kpmcli, err := client.NewKpmClient()
kpmcli.SetNoSumCheck(opts.NoSumCheck())
Expand Down
83 changes: 0 additions & 83 deletions pkg/api/kpm_run_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package api

import (
"bytes"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -111,20 +110,6 @@ func TestRunWithWorkdir(t *testing.T) {
assert.Equal(t, result, "base: base\nmain: main")
}

func TestRunWithOpts(t *testing.T) {
pkgPath := getTestDir("test_run_pkg_in_path")
opts := opt.DefaultCompileOptions()
opts.AddEntry(filepath.Join(pkgPath, "test_kcl", "main.k"))
opts.SetPkgPath(filepath.Join(pkgPath, "test_kcl"))
result, err := RunPkgWithOpt(opts)
fmt.Printf("err: %v\n", err)
assert.Equal(t, err, nil)
expected, _ := os.ReadFile(filepath.Join(pkgPath, "expected"))
assert.Equal(t, utils.RmNewline(string(result.GetRawYamlResult())), utils.RmNewline(string(expected)))
expectedJson, _ := os.ReadFile(filepath.Join(pkgPath, "expected.json"))
assert.Equal(t, utils.RmNewline(string(result.GetRawJsonResult())), utils.RmNewline(string(expectedJson)))
}

func TestRunWithSettingsOpts(t *testing.T) {
pkgPath := getTestDir("test_settings")
opts := opt.DefaultCompileOptions()
Expand Down Expand Up @@ -190,74 +175,6 @@ func TestRunWithNoSumCheck(t *testing.T) {
}()
}

func TestRunPkgWithOpts(t *testing.T) {
pkgPath := getTestDir("test_run_pkg_in_path")

result, err := RunWithOpts(
opt.WithNoSumCheck(false),
opt.WithEntries([]string{filepath.Join(pkgPath, "test_kcl", "main.k")}),
opt.WithKclOption(kcl.WithWorkDir(filepath.Join(pkgPath, "test_kcl"))),
)

assert.Equal(t, err, nil)
expected, _ := os.ReadFile(filepath.Join(pkgPath, "expected"))
assert.Equal(t, utils.RmNewline(string(result.GetRawYamlResult())), utils.RmNewline(string(expected)))
expectedJson, _ := os.ReadFile(filepath.Join(pkgPath, "expected.json"))
assert.Equal(t, utils.RmNewline(string(result.GetRawJsonResult())), utils.RmNewline(string(expectedJson)))
}

func TestRunWithOptsAndNoSumCheck(t *testing.T) {
pkgPath := filepath.Join(getTestDir("test_run_pkg_in_path"), "test_run_no_sum_check")
testCases := []string{"dep_git_commit", "dep_git_tag", "dep_oci"}

for _, testCase := range testCases {

pathMainK := filepath.Join(pkgPath, testCase, "main.k")
workDir := filepath.Join(pkgPath, testCase)
modLock := filepath.Join(workDir, "kcl.mod.lock")
expected, err := os.ReadFile(filepath.Join(pkgPath, testCase, "expected"))
assert.Equal(t, err, nil)
fmt.Printf("testCase: %v\n", testCase)
res, err := RunWithOpts(
opt.WithNoSumCheck(true),
opt.WithEntries([]string{pathMainK}),
opt.WithKclOption(kcl.WithWorkDir(workDir)),
)
assert.Equal(t, err, nil)
assert.Equal(t, utils.DirExists(modLock), false)
assert.Equal(t, utils.RmNewline(res.GetRawYamlResult()), utils.RmNewline(string(expected)))
assert.Equal(t, err, nil)
}
}

func TestRunWithOptsWithNoLog(t *testing.T) {
pkgPath := filepath.Join(getTestDir("test_run_pkg_in_path"), "test_run_with_no_log")

defer func() {
_ = os.Remove(filepath.Join(pkgPath, "kcl.mod.lock"))
}()

old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

pathMainK := filepath.Join(pkgPath, "main.k")

_, err := RunWithOpts(
opt.WithLogWriter(nil),
opt.WithEntries([]string{pathMainK}),
opt.WithKclOption(kcl.WithWorkDir(pkgPath)),
)
assert.Equal(t, err, nil)
os.Stdout = old
w.Close()
var buf bytes.Buffer
_, err = buf.ReadFrom(r)
assert.Equal(t, err, nil)

assert.Equal(t, buf.String(), "")
}

func TestStoreModAndModLockFile(t *testing.T) {
testPath := getTestDir("store_mod_and_lock")

Expand Down
Loading

0 comments on commit b7efafd

Please sign in to comment.