Skip to content

Commit

Permalink
Add BuildProgram and ExecArtifact to native client (#243)
Browse files Browse the repository at this point in the history
Signed-off-by: Artem V. Navrotskiy <[email protected]>
  • Loading branch information
bozaro authored Feb 27, 2024
1 parent 82bc1e0 commit 540babd
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/native/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
20 changes: 20 additions & 0 deletions pkg/native/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
54 changes: 53 additions & 1 deletion pkg/native/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package native

import (
"path"
"runtime"
"testing"

Expand All @@ -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) {
Expand All @@ -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)
Expand All @@ -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")
}
}
}

0 comments on commit 540babd

Please sign in to comment.