-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #187 from ykakarap/prog_avail
add prog_avail_local function
- Loading branch information
Showing
4 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright (c) 2020 VMware, Inc. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package starlark | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/vladimirvivien/echo" | ||
"go.starlark.net/starlark" | ||
) | ||
|
||
// progAvailLocalFunc is a built-in starlark function that checks if a program is available locally. | ||
// It returns the path to the command if availble or else, returns an empty string. | ||
// Starlark format: prog_avail_local(prog=<prog_name>) | ||
func progAvailLocalFunc(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { | ||
var progStr string | ||
if err := starlark.UnpackArgs( | ||
identifiers.progAvailLocal, args, kwargs, | ||
"prog", &progStr, | ||
); err != nil { | ||
return starlark.None, fmt.Errorf("%s: %s", identifiers.progAvailLocal, err) | ||
} | ||
|
||
p := echo.New().Prog.Avail(progStr) | ||
return starlark.String(p), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright (c) 2020 VMware, Inc. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package starlark | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"go.starlark.net/starlark" | ||
) | ||
|
||
func TestProgAvailLocalScript(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
scriptSnippet string | ||
exists bool | ||
}{ | ||
{ | ||
name: "prog_avail_local checks for 'go' using positional args", | ||
scriptSnippet: "prog_avail_local(prog='go')", | ||
exists: true, | ||
}, | ||
{ | ||
name: "prog_avail_local checks for 'go' using keyword args", | ||
scriptSnippet: "prog_avail_local('go')", | ||
exists: true, | ||
}, | ||
{ | ||
name: "prog_avail_local checks for 'nonexistant' using positional args", | ||
scriptSnippet: "prog_avail_local(prog='nonexistant')", | ||
exists: false, | ||
}, | ||
{ | ||
name: "prog_avail_local checks for 'nonexistant' using keyword args", | ||
scriptSnippet: "prog_avail_local('nonexistant')", | ||
exists: false, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
script := fmt.Sprintf(`path=%v`, test.scriptSnippet) | ||
exe := New() | ||
if err := exe.Exec("test.star", strings.NewReader(script)); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
resultVal := exe.result["path"] | ||
if resultVal == nil { | ||
t.Fatal("prog_avail_local() should be assigned to a variable for test") | ||
} | ||
|
||
result, ok := resultVal.(starlark.String) | ||
if !ok { | ||
t.Fatal("prog_avail_local() should return a string") | ||
} | ||
|
||
if (len(string(result)) == 0) == test.exists { | ||
if test.exists { | ||
t.Fatalf("expecting prog to exists but 'prog_avail_local' didnt find it") | ||
} else { | ||
t.Fatalf("expecting prog to not exists but 'prog_avail_local' found it") | ||
|
||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters