diff --git a/pkg/builder/util.go b/pkg/builder/util.go index eb75f7fd7..c92093f0d 100644 --- a/pkg/builder/util.go +++ b/pkg/builder/util.go @@ -321,18 +321,23 @@ func resolveExporterDest(exporter, dest string) (func(map[string]string) (io.Wri func ParseFromStr(fromStr string) (string, string, error) { filename := defaultFile funcname := defaultFunc - if strings.Contains(fromStr, ":") { - fromArr := strings.Split(fromStr, ":") - - if len(fromArr) != 2 { - return "", "", errors.New("invalid from format, expected `file:func`") - } - if fromArr[0] != "" { - filename = fromArr[0] - } - if fromArr[1] != "" { - funcname = fromArr[1] + if !strings.Contains(fromStr, ":") { + if len(fromStr) > 0 { + filename = fromStr } + return filename, funcname, nil + } + + fromArr := strings.Split(fromStr, ":") + + if len(fromArr) != 2 { + return "", "", errors.New("invalid from format, expected `file:func`") + } + if fromArr[0] != "" { + filename = fromArr[0] + } + if fromArr[1] != "" { + funcname = fromArr[1] } return filename, funcname, nil } diff --git a/pkg/builder/util_test.go b/pkg/builder/util_test.go index 740bb8d4e..b3137db50 100644 --- a/pkg/builder/util_test.go +++ b/pkg/builder/util_test.go @@ -193,3 +193,68 @@ func TestParseOutput(t *testing.T) { }) } } + +func TestParseFromStr(t *testing.T) { + type testCase struct { + name string + from string + expectFile string + expectFunc string + expectError bool + } + + testCases := []testCase{ + { + "empty", + "", + "build.envd", + "build", + false, + }, + { + "without func", + "main.envd", + "main.envd", + "build", + false, + }, + { + "without func but has :", + "test.envd:", + "test.envd", + "build", + false, + }, + { + "without file", + ":test", + "build.envd", + "test", + false, + }, + { + "all", + "hello.envd:run", + "hello.envd", + "run", + false, + }, + { + "more than 2 :", + "test.envd:run:foo", + "", + "", + true, + }, + } + + for _, tc := range testCases { + file, function, err := ParseFromStr(tc.from) + if tc.expectError { + require.Error(t, err) + } else { + require.Equal(t, file, tc.expectFile, tc.name) + require.Equal(t, function, tc.expectFunc, tc.name) + } + } +}