From 540babdf9c1b45df50850225587e2e29c4355599 Mon Sep 17 00:00:00 2001 From: "Artem V. Navrotskiy" Date: Tue, 27 Feb 2024 11:35:34 +0300 Subject: [PATCH] Add `BuildProgram` and `ExecArtifact` to native client (#243) Signed-off-by: Artem V. Navrotskiy --- pkg/native/api_test.go | 2 +- pkg/native/client.go | 20 +++++++++++++++ pkg/native/client_test.go | 54 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 74 insertions(+), 2 deletions(-) diff --git a/pkg/native/api_test.go b/pkg/native/api_test.go index 0473f564..3d0a6a5e 100644 --- a/pkg/native/api_test.go +++ b/pkg/native/api_test.go @@ -15,7 +15,7 @@ import ( func TestNativeRun(t *testing.T) { // TODO: windows support if runtime.GOOS != "windows" { - yaml := MustRun("main.k", kcl.WithCode(code)).GetRawYamlResult() + yaml := MustRun("main.k", kcl.WithCode(code), kcl.WithOptions("a=1", "b=2")).GetRawYamlResult() fmt.Println(yaml) } } diff --git a/pkg/native/client.go b/pkg/native/client.go index 9e26085f..546f1fdd 100644 --- a/pkg/native/client.go +++ b/pkg/native/client.go @@ -107,3 +107,23 @@ func (c *NativeServiceClient) ExecProgram(in *gpyrpc.ExecProgram_Args) (*gpyrpc. return out, err } + +func (c *NativeServiceClient) BuildProgram(in *gpyrpc.BuildProgram_Args) (*gpyrpc.BuildProgram_Result, error) { + if in == nil { + in = new(gpyrpc.BuildProgram_Args) + } + out := new(gpyrpc.BuildProgram_Result) + err := c.cApiCall("KclvmService.BuildProgram", in, out) + + return out, err +} + +func (c *NativeServiceClient) ExecArtifact(in *gpyrpc.ExecArtifact_Args) (*gpyrpc.ExecProgram_Result, error) { + if in == nil { + in = new(gpyrpc.ExecArtifact_Args) + } + out := new(gpyrpc.ExecProgram_Result) + err := c.cApiCall("KclvmService.ExecArtifact", in, out) + + return out, err +} diff --git a/pkg/native/client_test.go b/pkg/native/client_test.go index 9b381e95..0d597666 100644 --- a/pkg/native/client_test.go +++ b/pkg/native/client_test.go @@ -4,6 +4,7 @@ package native import ( + "path" "runtime" "testing" @@ -15,7 +16,7 @@ const code = ` import kcl_plugin.hello name = "kcl" -three = hello.add(1,2) +sum = hello.add(option("a"), option("b")) ` func TestExecProgramWithPlugin(t *testing.T) { @@ -25,6 +26,16 @@ func TestExecProgramWithPlugin(t *testing.T) { result, err := client.ExecProgram(&gpyrpc.ExecProgram_Args{ KFilenameList: []string{"main.k"}, KCodeList: []string{code}, + Args: []*gpyrpc.CmdArgSpec{ + { + Name: "a", + Value: "1", + }, + { + Name: "b", + Value: "2", + }, + }, }) if err != nil { t.Fatal(err) @@ -34,3 +45,44 @@ func TestExecProgramWithPlugin(t *testing.T) { } } } + +func TestExecArtifactWithPlugin(t *testing.T) { + // TODO: windows support + if runtime.GOOS != "windows" { + output := path.Join(t.TempDir(), "example") + client := NewNativeServiceClient() + // BuildProgram + buildResult, err := client.BuildProgram(&gpyrpc.BuildProgram_Args{ + ExecArgs: &gpyrpc.ExecProgram_Args{ + KFilenameList: []string{"main.k"}, + KCodeList: []string{code}, + }, + Output: output, + }) + if err != nil { + t.Fatal(err) + } + // ExecArtifact + execResult, err := client.ExecArtifact(&gpyrpc.ExecArtifact_Args{ + ExecArgs: &gpyrpc.ExecProgram_Args{ + Args: []*gpyrpc.CmdArgSpec{ + { + Name: "a", + Value: "1", + }, + { + Name: "b", + Value: "2", + }, + }, + }, + Path: buildResult.Path, + }) + if err != nil { + t.Fatal(err) + } + if execResult.ErrMessage != "" { + t.Fatal("error message must be empty") + } + } +}