src: better error when custom template not found

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

View File

@ -215,7 +215,7 @@ func TestRuntimeNotFound(t *testing.T) {
}
}
// TODO: TestRuntimeNotFoundCustom ensures that the correct error is returned
// 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"
@ -224,7 +224,7 @@ func TestRuntimeNotFoundCustom(t *testing.T) {
}
defer os.RemoveAll(root)
// Create a new client with path to the extensible templates
// Create a new client with path to extensible templates
client := bosonFunc.New(
bosonFunc.WithTemplates("testdata/repositories"),
bosonFunc.WithRegistry(TestRegistry))
@ -236,7 +236,7 @@ func TestRuntimeNotFoundCustom(t *testing.T) {
// creating should error as runtime not found
err := client.New(context.Background(), f)
if !errors.Is(err, bosonFunc.ErrRuntimeNotFound) {
t.Fatal(err)
t.Fatalf("Expected ErrRuntimeNotFound, got %v", err)
}
}
@ -250,16 +250,38 @@ func TestTemplateNotFound(t *testing.T) {
client := bosonFunc.New(bosonFunc.WithRegistry(TestRegistry))
// creating a Function with an unsupported runtime should bubble
// the error generated by the unsderlying template initializer.
// Creating a function with an invalid template shulid generate the
// appropriate error.
f := bosonFunc.Function{Root: root, Runtime: "go", Trigger: "invalid"}
err := client.New(context.Background(), f)
if !errors.Is(err, bosonFunc.ErrTemplateNotFound) {
t.Fatalf("Expected ErrRuntimeNotFound, got %T", err)
t.Fatalf("Expected ErrTemplateNotFound, got %v", err)
}
}
// TODO: TestTemplateNotFound in custom repository
// TestTemplateNotFoundCustom ensures that the correct error is returned
// when the requested template is not found in the given custom repository.
func TestTemplateNotFoundCustom(t *testing.T) {
root := "testdata/example.com/testTemplateNotFoundCustom"
if err := os.MkdirAll(root, 0700); err != nil {
t.Fatal(err)
}
defer os.RemoveAll(root)
// Create a new client with path to extensible templates
client := bosonFunc.New(
bosonFunc.WithTemplates("testdata/repositories"),
bosonFunc.WithRegistry(TestRegistry))
// An invalid template, but a valid custom provider
f := bosonFunc.Function{Root: root, Runtime: "test", Trigger: "customProvider/invalid"}
// Creation should generate the correct error of template not being found.
err := client.New(context.Background(), f)
if !errors.Is(err, bosonFunc.ErrTemplateNotFound) {
t.Fatalf("Expected ErrTemplateNotFound, got %v", err)
}
}
// TestNamed ensures that an explicitly passed name is used in leau of the
// path derived name when provided, and persists through instantiations.

View File

@ -90,7 +90,7 @@ func isCustom(template string) bool {
return len(strings.Split(template, "/")) > 1
}
func (t *templateWriter) writeCustom(repositoriesPath, runtime, template, dest string) error {
func (t *templateWriter) writeCustom(repositoriesPath, runtime, template, dest string) (err error) {
if repositoriesPath == "" {
return ErrRepositoriesNotDefined
}
@ -104,11 +104,17 @@ func (t *templateWriter) writeCustom(repositoriesPath, runtime, template, dest s
repositoriesFS := os.DirFS(repositoriesPath)
runtimePath := cc[0] + "/" + runtime
_, err := fs.Stat(repositoriesFS, runtimePath)
_, err = fs.Stat(repositoriesFS, runtimePath)
if errors.Is(err, fs.ErrNotExist) {
return ErrRuntimeNotFound
}
templatePath := runtimePath + "/" + cc[1]
_, err = fs.Stat(repositoriesFS, templatePath)
if errors.Is(err, fs.ErrNotExist) {
return ErrTemplateNotFound
}
// 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