From 82a7b113edae15670efc0660ea2a7bca82cb20ad Mon Sep 17 00:00:00 2001 From: Staffan Selander Date: Mon, 1 Aug 2022 11:06:16 +0200 Subject: [PATCH] Template converter, don't skip .yaml extension. (#3242) --- plugin/converter/template.go | 5 ++-- plugin/converter/template_test.go | 43 +++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/plugin/converter/template.go b/plugin/converter/template.go index ca7642a99..3ea60fb0f 100644 --- a/plugin/converter/template.go +++ b/plugin/converter/template.go @@ -23,7 +23,6 @@ import ( "errors" "path/filepath" "regexp" - "strings" templating "text/template" "github.com/drone/drone/core" @@ -56,7 +55,9 @@ type templatePlugin struct { func (p *templatePlugin) Convert(ctx context.Context, req *core.ConvertArgs) (*core.Config, error) { // check type is yaml - if strings.HasSuffix(req.Repo.Config, ".yml") == false { + configExt := filepath.Ext(req.Repo.Config) + + if configExt != ".yml" && configExt != ".yaml" { return nil, nil } diff --git a/plugin/converter/template_test.go b/plugin/converter/template_test.go index 63937aad5..5103184e8 100644 --- a/plugin/converter/template_test.go +++ b/plugin/converter/template_test.go @@ -16,6 +16,7 @@ package converter import ( "encoding/json" + "errors" "io/ioutil" "runtime" "strings" @@ -141,6 +142,48 @@ func TestTemplatePluginConvertDroneFileTypePipeline(t *testing.T) { } } +// Test makes sure that we don't skip templating for neither the "yml" or "yaml" extension. +func TestTemplatePluginConvertDroneFileYamlExtensions(t *testing.T) { + extensions := []string{"yml", "yaml"} + dummyErr := errors.New("dummy-error") + + for _, extension := range extensions { + t.Run(extension, func(t *testing.T) { + args, err := ioutil.ReadFile("testdata/yaml.template.yml") + if err != nil { + t.Error(err) + return + } + + controller := gomock.NewController(t) + defer controller.Finish() + + templates := mock.NewMockTemplateStore(controller) + templates.EXPECT().FindName(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, dummyErr) + + plugin := Template(templates, 0) + req := &core.ConvertArgs{ + Build: &core.Build{ + After: "3d21ec53a331a6f037a91c368710b99387d012c1", + }, + Repo: &core.Repository{ + Slug: "octocat/hello-world", + Config: ".drone." + extension, + }, + Config: &core.Config{Data: string(args)}, + } + + _, err = plugin.Convert(noContext, req) + if err != nil && err != dummyErr { + t.Error(err) + } + if err == nil { + t.Errorf("Templating was skipped") + } + }) + } +} + func TestTemplatePluginConvertTemplateNotFound(t *testing.T) { templateArgs, err := ioutil.ReadFile("testdata/starlark.template.yml") if err != nil {