Skip to content

Commit

Permalink
refactor: move template filepaths map to const.go
Browse files Browse the repository at this point in the history
  • Loading branch information
tnevrlka committed Feb 19, 2024
1 parent 81d6508 commit 0bf0dd2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 25 deletions.
8 changes: 8 additions & 0 deletions magefiles/testspecs/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package testspecs

const (
TestFilePath = "templates/test_output_spec.tmpl"
FrameworkDescribePath = "templates/framework_describe_func.tmpl"

SpecsPath = "templates/specs.tmpl"
)
8 changes: 1 addition & 7 deletions magefiles/testspecs/ginkgosspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,9 @@ func generateGinkgoSpec(cwd string, destination string, dataFile string) error {
// Doing this to avoid errcheck flagging this in a defer.
// Refer to https://github.com/kisielk/errcheck
// issues 101, 77, 55
tmpl, err := GetTemplate("test-file")
if err != nil {
return err
}

fullTemplatePath := fmt.Sprintf("%s/%s", cwd, tmpl)

klog.Infof("Creating new test package directory and spec file %s.\n", destination)
_, err = ginkgoGenerateSpecCmd("--template", fullTemplatePath, "--template-data", dataFile)
_, err = ginkgoGenerateSpecCmd("--template", TestFilePath, "--template-data", dataFile)
if err != nil {
err = os.Remove(ginkgoFileName)
if err != nil {
Expand Down
19 changes: 1 addition & 18 deletions magefiles/testspecs/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ import (
"k8s.io/klog/v2"
)

var templates = map[string]string{
"test-file": "templates/test_output_spec.tmpl",
"framework-describe": "templates/framework_describe_func.tmpl",
}

func NewTemplateData(specOutline TestOutline, destination string) *TemplateData {

// This regex will find all the words that start with capital letter
Expand Down Expand Up @@ -47,22 +42,10 @@ func NewTemplateData(specOutline TestOutline, destination string) *TemplateData
return &TemplateData{Outline: specOutline, PackageName: dirName, FrameworkDescribeString: newSpecName}
}

func GetTemplate(name string) (string, error) {
if s, ok := templates[name]; ok {
return s, nil
}
return "", fmt.Errorf("no Template found for %q", name)
}

func RenderFrameworkDescribeGoFile(t TemplateData) error {

templatePath, err := GetTemplate("framework-describe")
if err != nil {
return err
}
var describeFile = "pkg/framework/describe.go"

err = renderTemplate(describeFile, templatePath, t, true)
err := renderTemplate(describeFile, FrameworkDescribePath, t, true)
if err != nil {
klog.Errorf("failed to append to pkg/framework/describe.go with : %s", err)
return err
Expand Down

0 comments on commit 0bf0dd2

Please sign in to comment.