cweill / gotests

Automatically generate Go test boilerplate from your source code.
Apache License 2.0
4.95k stars 345 forks source link

Allow loading from externally embedded template data #139

Closed titolins closed 3 years ago

titolins commented 3 years ago

Currently, there's no way to integrate gotests programmatically with your own binary embedded templates.

Since the current structure doesn't allow for any access to the tmpls variable in internal/render, and the template functions are also not exported, what I had in mind was doing something similar to how Template, TemplateDir and TemplateParams works.

I'll just create the pull request since I've got something working already.

Tks in advance :+1:

cweill commented 3 years ago

Hi @titolins, could you please provide an example of what's the intended behavior you expect? I'm not sure what you mean by "externally embedded template data".

titolins commented 3 years ago

Hey @cweill , thanks for your time.

I've just opened a PR, but didn't know how to properly link it to this issue: https://github.com/cweill/gotests/pull/140

Sorry for that, but I can't appear to fix the link, if you could help me would be awesome :smile:

Regarding the use case, I'm sorry for not being so clear. I'm part of a code generator project for internal use and I'm trying to use gotests for generating the test files.

However, since we use another external library for embedding our templates into the go binary (https://github.com/jteeuwen/go-bindata), having our customized templates embedded is not possible currently.

That's how I'm currently using

func loadTemplateData() [][]byte {
    templates := make([][]byte, 0)
    for _, assetName := range templatesgenerated.AssetNames() {
        if strings.Contains(assetName, "tests/") {
            data := templatesgenerated.MustAsset(assetName)
            templates = append(templates, data)
        }
    }

    return templates
}

func generateTests(filename string) {
    testFilename := strings.Split(filename, ".")[0] + "_test.go"
    opts := &process.Options{
        //OnlyFuncs:          *onlyFuncs,
        //ExclFuncs:          *exclFuncs,
        //ExportedFuncs:      *exportedFuncs,
        AllFuncs: true,
        //PrintInputs:        *printInputs,
        //Subtests:           !nosubtests,
        //Parallel:           parallel,
        WriteOutput: true,
        //WriteOutput:        *writeOutput,
        //Template:           valOrGetenv(*template, "GOTESTS_TEMPLATE"),
        //TemplateDir:        valOrGetenv(*templateDir, "GOTESTS_TEMPLATE_DIR"),
        //TemplateDir: TestsTemplateDir,
        //TemplateParamsPath: *templateParamsPath,
        TemplateData: loadTemplateData(),
    }

    process.Run(os.Stdout, []string{filename}, opts)
    exec.Command("gofmt", "-w", testFilename).Run()
    exec.Command("goimports", "-w", testFilename).Run()
}

Please let me know if this is acceptable :raised_hands: :smile:

cweill commented 3 years ago

Are you using the gotests module as a library instead of the binary? Any reason you couldn't use the -template_dir flag instead? Then you could use exec.Command("gotests", "-template_dir", "/path/to/templates").Run()

titolins commented 3 years ago

Yeah, my first approach was doing exactly that. The problem is that then I wouldn't be able to embed the customized templates in my binary. Another option would be forking the repo and having the modified templates in there instead.