src: better errors when custom runtime not found

This commit is contained in:
Luke Kingland 2021-05-06 16:11:32 +09:00
parent 11164bf8d9
commit a31a6f67a1
No known key found for this signature in database
GPG Key ID: 4896F75BAF2E1966
2 changed files with 37 additions and 5 deletions

View File

@ -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

View File

@ -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 {