Skip to content

Commit

Permalink
Template converter, don't skip .yaml extension. (harness#3242)
Browse files Browse the repository at this point in the history
  • Loading branch information
staffanselander authored and ivanvc committed Dec 14, 2023
1 parent 4ca98e8 commit 054e695
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
5 changes: 3 additions & 2 deletions plugin/converter/template.go
Expand Up @@ -23,7 +23,6 @@ import (
"errors"
"path/filepath"
"regexp"
"strings"
templating "text/template"

"github.com/drone/drone/core"
Expand Down Expand Up @@ -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
}

Expand Down
43 changes: 43 additions & 0 deletions plugin/converter/template_test.go
Expand Up @@ -16,6 +16,7 @@ package converter

import (
"encoding/json"
"errors"
"io/ioutil"
"runtime"
"strings"
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 054e695

Please sign in to comment.