Skip to content

Commit

Permalink
Add -template option for testify. (#109)
Browse files Browse the repository at this point in the history
* Add function.tmpl for testify

* Add templates package

* Add LoadCustomTemplatesName

* Add -template option

* Add tests for template

* testify: Add tt.name to non-subtest cases

* Fix test cases name

* testify: Add more test cases

Covering more patterns both with and without subtests.

* testify: Add testcase for printInputs

* testify: Handle case only with printInputs

* testify: Refactor function template

* render: Fix hardcoded template name

* Add more test cases for -template

- Test with empty directory

- Test with invalid template (copied from testify and removed a
  bracket)

* Simplify -template test case

For testing invalid template, we only need one template that is
invalid and the others can be empty files.
  • Loading branch information
shihanng authored and cweill committed Nov 9, 2019
1 parent 17e4e80 commit 12fe222
Show file tree
Hide file tree
Showing 30 changed files with 1,018 additions and 12 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ Available options:
-w write output to (test) files instead of stdout
-template_dir Path to a directory containing custom test code templates
-template_dir Path to a directory containing custom test code templates. Takes
precedence over -template
-template Specify custom test code templates, e.g. testify
-template_params_file read external parameters to template by json with file
Expand Down
2 changes: 2 additions & 0 deletions gotests.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Options struct {
PrintInputs bool // Print function parameters in error messages
Subtests bool // Print tests using Go 1.7 subtests
Importer func() types.Importer // A custom importer.
Template string // Name of custom template set
TemplateDir string // Path to custom template set
TemplateParams map[string]interface{} // Custom external parameters
}
Expand Down Expand Up @@ -118,6 +119,7 @@ func generateTest(src models.Path, files []models.Path, opt *Options) (*Generate
b, err := output.Process(h, funcs, &output.Options{
PrintInputs: opt.PrintInputs,
Subtests: opt.Subtests,
Template: opt.Template,
TemplateDir: opt.TemplateDir,
TemplateParams: opt.TemplateParams,
})
Expand Down
9 changes: 7 additions & 2 deletions gotests/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
//
// -w write output to (test) files instead of stdout
//
// -template_dir Path to a directory containing custom test code templates
// -template_dir Path to a directory containing custom test code templates. Takes
// precedence over -template
//
// -template Specify custom test code templates, e.g. testify
//
// -template_params_file read external parameters to template by json with file
//
Expand All @@ -47,7 +50,8 @@ var (
allFuncs = flag.Bool("all", false, "generate tests for all functions and methods")
printInputs = flag.Bool("i", false, "print test inputs in error messages")
writeOutput = flag.Bool("w", false, "write output to (test) files instead of stdout")
templateDir = flag.String("template_dir", "", `optional. Path to a directory containing custom test code templates`)
templateDir = flag.String("template_dir", "", `optional. Path to a directory containing custom test code templates. Takes precedence over -template`)
template = flag.String("template", "", `optional. Specify custom test code templates, e.g. testify`)
templateParamsPath = flag.String("template_params_file", "", "read external parameters to template by json with file")
templateParams = flag.String("template_params", "", "read external parameters to template by json with stdin")
)
Expand All @@ -69,6 +73,7 @@ func main() {
PrintInputs: *printInputs,
Subtests: !nosubtests,
WriteOutput: *writeOutput,
Template: *template,
TemplateDir: *templateDir,
TemplateParamsPath: *templateParamsPath,
})
Expand Down
2 changes: 2 additions & 0 deletions gotests/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Options struct {
PrintInputs bool // Print function parameters as part of error messages.
Subtests bool // Print tests using Go 1.7 subtests
WriteOutput bool // Write output to test file(s).
Template string // Name of custom template set
TemplateDir string // Path to custom template set
TemplateParamsPath string // Path to custom paramters json file(s).
}
Expand Down Expand Up @@ -92,6 +93,7 @@ func parseOptions(out io.Writer, opt *Options) *gotests.Options {
Exported: opt.ExportedFuncs,
PrintInputs: opt.PrintInputs,
Subtests: opt.Subtests,
Template: opt.Template,
TemplateDir: opt.TemplateDir,
TemplateParams: templateParams,
}
Expand Down
74 changes: 74 additions & 0 deletions gotests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestGenerateTests(t *testing.T) {
subtests bool
importer types.Importer
templateDir string
template string
templateParamsPath string
}
tests := []struct {
Expand Down Expand Up @@ -634,6 +635,78 @@ func TestGenerateTests(t *testing.T) {
wantErr: false,
want: mustReadAndFormatGoFile(t, "testdata/goldens/use_template_params_test.go"),
},
{
name: "With template=testify",
args: args{
srcPath: `testdata/test201.go`,
template: "testify",
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/template_testify.go"),
},
{
name: "With template=testify and subtests",
args: args{
srcPath: `testdata/test201.go`,
template: "testify",
subtests: true,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/template_testify_subtests.go"),
},
{
name: "With template=testify and printInputs",
args: args{
srcPath: `testdata/test201.go`,
template: "testify",
printInputs: true,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/template_testify_printinputs.go"),
},
{
name: "With template=testify and subtests and printInputs",
args: args{
srcPath: `testdata/test201.go`,
template: "testify",
printInputs: true,
subtests: true,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/template_testify_subtests_printinputs.go"),
},
{
name: "With template=unknown",
args: args{
srcPath: `testdata/test004.go`,
template: "unknown",
},
wantNoTests: true,
wantErr: true,
},
{
name: "With template=testify templateDir=testdata/customtemplates",
args: args{
srcPath: `testdata/test004.go`,
template: "testify",
templateDir: `testdata/customtemplates`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/function_with_return_value_custom_template.go"),
},
{
name: "With template=test_empty (empty directory)",
args: args{
srcPath: `testdata/test004.go`,
template: "test_empty",
},
wantNoTests: true,
wantErr: true,
},
{
name: "With template=test (invalid template)",
args: args{
srcPath: `testdata/test004.go`,
template: "test",
},
wantNoTests: true,
wantErr: true,
},
}
tmp, err := ioutil.TempDir("", "gotests_test")
if err != nil {
Expand All @@ -658,6 +731,7 @@ func TestGenerateTests(t *testing.T) {
Subtests: tt.args.subtests,
Importer: func() types.Importer { return tt.args.importer },
TemplateDir: tt.args.templateDir,
Template: tt.args.template,
TemplateParams: params,
})
if (err != nil) != tt.wantErr {
Expand Down
6 changes: 6 additions & 0 deletions internal/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
type Options struct {
PrintInputs bool
Subtests bool
Template string
TemplateDir string
TemplateParams map[string]interface{}
}
Expand All @@ -27,6 +28,11 @@ func Process(head *models.Header, funcs []*models.Function, opt *Options) ([]byt
if err != nil {
return nil, fmt.Errorf("loading custom templates: %v", err)
}
} else if opt != nil && opt.Template != "" {
err := render.LoadCustomTemplatesName(opt.Template)
if err != nil {
return nil, fmt.Errorf("loading custom templates of name: %v", err)
}
}

tf, err := ioutil.TempFile("", "gotests_")
Expand Down
16 changes: 8 additions & 8 deletions internal/render/bindata/esc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 32 additions & 1 deletion internal/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ import (

"github.com/cweill/gotests/internal/models"
"github.com/cweill/gotests/internal/render/bindata"
"github.com/cweill/gotests/templates"
)

const name = "name"
const (
name = "name"
nFile = 7
)

var (
tmpls *template.Template
Expand Down Expand Up @@ -46,6 +50,33 @@ func LoadCustomTemplates(dir string) error {
return nil
}

// LoadCustomTemplatesName allows to load in custom templates of a specified name from the templates directory.
func LoadCustomTemplatesName(name string) error {
f, err := templates.Dir(false, "/").Open(name)
if err != nil {
return fmt.Errorf("templates.Open: %v", err)
}

files, err := f.Readdir(nFile)
if err != nil {
return fmt.Errorf("f.Readdir: %v", err)
}

for _, f := range files {
text, err := templates.FSString(false, path.Join("/", name, f.Name()))
if err != nil {
return fmt.Errorf("templates.FSString: %v", err)
}

tmpls, err = tmpls.Parse(text)
if err != nil {
return fmt.Errorf("tmpls.Parse: %v", err)
}
}

return nil
}

func initEmptyTmpls() {
tmpls = template.New("render").Funcs(map[string]interface{}{
"Field": fieldName,
Expand Down
2 changes: 2 additions & 0 deletions templates/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//go:generate esc -include=.*\.tmpl -o=tmpl.go -pkg=templates ./
package templates
Empty file added templates/test/call.tmpl
Empty file.
1 change: 1 addition & 0 deletions templates/test/function.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{/* a comment */}
Empty file added templates/test/header.tmpl
Empty file.
Empty file added templates/test/inline.tmpl
Empty file.
Empty file added templates/test/inputs.tmpl
Empty file.
Empty file added templates/test/message.tmpl
Empty file.
Empty file added templates/test/results.tmpl
Empty file.
Empty file added templates/test_empty/.gitkeep
Empty file.
1 change: 1 addition & 0 deletions templates/testify/call.tmpl
Loading

0 comments on commit 12fe222

Please sign in to comment.