mirror of https://github.com/knative/func.git
src: better error when custom template not found
This commit is contained in:
parent
a31a6f67a1
commit
6673395834
|
@ -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
|
// when the requested runtime is not found in a given custom repository
|
||||||
func TestRuntimeNotFoundCustom(t *testing.T) {
|
func TestRuntimeNotFoundCustom(t *testing.T) {
|
||||||
root := "testdata/example.com/testRuntimeNotFoundCustom"
|
root := "testdata/example.com/testRuntimeNotFoundCustom"
|
||||||
|
@ -224,7 +224,7 @@ func TestRuntimeNotFoundCustom(t *testing.T) {
|
||||||
}
|
}
|
||||||
defer os.RemoveAll(root)
|
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(
|
client := bosonFunc.New(
|
||||||
bosonFunc.WithTemplates("testdata/repositories"),
|
bosonFunc.WithTemplates("testdata/repositories"),
|
||||||
bosonFunc.WithRegistry(TestRegistry))
|
bosonFunc.WithRegistry(TestRegistry))
|
||||||
|
@ -236,7 +236,7 @@ func TestRuntimeNotFoundCustom(t *testing.T) {
|
||||||
// creating should error as runtime not found
|
// creating should error as runtime not found
|
||||||
err := client.New(context.Background(), f)
|
err := client.New(context.Background(), f)
|
||||||
if !errors.Is(err, bosonFunc.ErrRuntimeNotFound) {
|
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))
|
client := bosonFunc.New(bosonFunc.WithRegistry(TestRegistry))
|
||||||
|
|
||||||
// creating a Function with an unsupported runtime should bubble
|
// Creating a function with an invalid template shulid generate the
|
||||||
// the error generated by the unsderlying template initializer.
|
// appropriate error.
|
||||||
f := bosonFunc.Function{Root: root, Runtime: "go", Trigger: "invalid"}
|
f := bosonFunc.Function{Root: root, Runtime: "go", Trigger: "invalid"}
|
||||||
err := client.New(context.Background(), f)
|
err := client.New(context.Background(), f)
|
||||||
if !errors.Is(err, bosonFunc.ErrTemplateNotFound) {
|
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
|
// TestNamed ensures that an explicitly passed name is used in leau of the
|
||||||
// path derived name when provided, and persists through instantiations.
|
// path derived name when provided, and persists through instantiations.
|
||||||
|
|
10
templates.go
10
templates.go
|
@ -90,7 +90,7 @@ func isCustom(template string) bool {
|
||||||
return len(strings.Split(template, "/")) > 1
|
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 == "" {
|
if repositoriesPath == "" {
|
||||||
return ErrRepositoriesNotDefined
|
return ErrRepositoriesNotDefined
|
||||||
}
|
}
|
||||||
|
@ -104,11 +104,17 @@ func (t *templateWriter) writeCustom(repositoriesPath, runtime, template, dest s
|
||||||
repositoriesFS := os.DirFS(repositoriesPath)
|
repositoriesFS := os.DirFS(repositoriesPath)
|
||||||
|
|
||||||
runtimePath := cc[0] + "/" + runtime
|
runtimePath := cc[0] + "/" + runtime
|
||||||
_, err := fs.Stat(repositoriesFS, runtimePath)
|
_, err = fs.Stat(repositoriesFS, runtimePath)
|
||||||
if errors.Is(err, fs.ErrNotExist) {
|
if errors.Is(err, fs.ErrNotExist) {
|
||||||
return ErrRuntimeNotFound
|
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
|
// ex: /home/alice/.config/func/repositories/boson/go/http
|
||||||
// Note that the FS instance returned by os.DirFS uses forward slashes
|
// 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
|
// internally, so source paths do not use the os path separator due to
|
||||||
|
|
Loading…
Reference in New Issue