From a31a6f67a1c537cd80f88db306b7a8e649e1b40e Mon Sep 17 00:00:00 2001 From: Luke Kingland Date: Thu, 6 May 2021 16:11:32 +0900 Subject: [PATCH] src: better errors when custom runtime not found --- client_test.go | 26 +++++++++++++++++++++++++- templates.go | 16 ++++++++++++---- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/client_test.go b/client_test.go index cd37b1ac3..7f9b24ed3 100644 --- a/client_test.go +++ b/client_test.go @@ -215,6 +215,31 @@ func TestRuntimeNotFound(t *testing.T) { } } +// TODO: TestRuntimeNotFoundCustom ensures that the correct error is returned +// when the requested runtime is not found in a given custom repository +func TestRuntimeNotFoundCustom(t *testing.T) { + root := "testdata/example.com/testRuntimeNotFoundCustom" + if err := os.MkdirAll(root, 0700); err != nil { + t.Fatal(err) + } + defer os.RemoveAll(root) + + // Create a new client with path to the extensible templates + client := bosonFunc.New( + bosonFunc.WithTemplates("testdata/repositories"), + bosonFunc.WithRegistry(TestRegistry)) + + // Create a Function specifying a runtime, 'python' that does not exist + // in the custom (testdata) repository but does in the embedded. + f := bosonFunc.Function{Root: root, Runtime: "python", Trigger: "customProvider/event"} + + // creating should error as runtime not found + err := client.New(context.Background(), f) + if !errors.Is(err, bosonFunc.ErrRuntimeNotFound) { + t.Fatal(err) + } +} + // TestTemplateNotFound generates an error (embedded default repository). func TestTemplateNotFound(t *testing.T) { root := "testdata/example.com/testTemplateNotFound" @@ -234,7 +259,6 @@ func TestTemplateNotFound(t *testing.T) { } } -// TODO: TestRuntimeNotFound in custom repository // TODO: TestTemplateNotFound in custom repository // TestNamed ensures that an explicitly passed name is used in leau of the diff --git a/templates.go b/templates.go index 9137a8ad8..d6b16cee2 100644 --- a/templates.go +++ b/templates.go @@ -90,23 +90,31 @@ func isCustom(template string) bool { return len(strings.Split(template, "/")) > 1 } -func (t *templateWriter) writeCustom(repositories, runtime, template, dest string) error { - if repositories == "" { +func (t *templateWriter) writeCustom(repositoriesPath, runtime, template, dest string) error { + if repositoriesPath == "" { return ErrRepositoriesNotDefined } - if !repositoryExists(repositories, template) { + if !repositoryExists(repositoriesPath, template) { return ErrRepositoryNotFound } cc := strings.Split(template, "/") if len(cc) < 2 { return ErrTemplateMissingRepository } + repositoriesFS := os.DirFS(repositoriesPath) + + runtimePath := cc[0] + "/" + runtime + _, err := fs.Stat(repositoriesFS, runtimePath) + if errors.Is(err, fs.ErrNotExist) { + return ErrRuntimeNotFound + } + // ex: /home/alice/.config/func/repositories/boson/go/http // Note that the FS instance returned by os.DirFS uses forward slashes // internally, so source paths do not use the os path separator due to // that breaking Windows. src := cc[0] + "/" + runtime + "/" + cc[1] - return t.cp(src, dest, os.DirFS(repositories)) + return t.cp(src, dest, repositoriesFS) } func (t *templateWriter) writeEmbedded(runtime, template, dest string) error {