Skip to content

Commit

Permalink
src: better error when custom template not found
Browse files Browse the repository at this point in the history
  • Loading branch information
lkingland committed May 7, 2021
1 parent a31a6f6 commit 6673395
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
36 changes: 29 additions & 7 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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))
Expand All @@ -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)
}
}

Expand All @@ -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.
Expand Down
10 changes: 8 additions & 2 deletions templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down

0 comments on commit 6673395

Please sign in to comment.