Finished TestTemplates func
* TestTemplates compiles the templates generated to ensure they compile without errors.
This commit is contained in:
parent
896f10199e
commit
c8530ba7e4
4 changed files with 61 additions and 28 deletions
11
cmds/boil.go
11
cmds/boil.go
|
@ -26,13 +26,12 @@ func boilRun(cmd *cobra.Command, args []string) {
|
|||
|
||||
var out [][]byte
|
||||
var imps imports
|
||||
|
||||
imps.standard = sqlBoilerDefaultImports.standard
|
||||
imps.thirdparty = sqlBoilerDefaultImports.thirdparty
|
||||
// Loop through and generate every command template (excluding skipTemplates)
|
||||
for i, command := range commandNames {
|
||||
if i == 0 {
|
||||
imps = combineImports(sqlBoilerDefaultImports, sqlBoilerCustomImports[command])
|
||||
} else {
|
||||
imps = combineImports(imps, sqlBoilerCustomImports[command])
|
||||
}
|
||||
for _, command := range commandNames {
|
||||
imps = combineImports(imps, sqlBoilerCustomImports[command])
|
||||
out = append(out, generateTemplate(command, &data))
|
||||
}
|
||||
|
||||
|
|
|
@ -69,8 +69,9 @@ var testHarnessFileOpen = func(filename string) (io.WriteCloser, error) {
|
|||
func outHandler(outFolder string, output [][]byte, data *tplData, imps *imports) error {
|
||||
out := testHarnessStdout
|
||||
|
||||
var path string
|
||||
if len(outFolder) != 0 {
|
||||
path := outFolder + "/" + data.Table + ".go"
|
||||
path = outFolder + "/" + data.Table + ".go"
|
||||
outFile, err := testHarnessFileOpen(path)
|
||||
if err != nil {
|
||||
errorQuit(fmt.Errorf("Unable to create output file %s: %s", path, err))
|
||||
|
@ -79,6 +80,10 @@ func outHandler(outFolder string, output [][]byte, data *tplData, imps *imports)
|
|||
out = outFile
|
||||
}
|
||||
|
||||
if _, err := fmt.Fprintf(out, "package %s\n\n", cmdData.PkgName); err != nil {
|
||||
errorQuit(fmt.Errorf("Unable to write package name %s to file: %s", cmdData.PkgName, path))
|
||||
}
|
||||
|
||||
impStr := buildImportString(imps)
|
||||
if len(impStr) > 0 {
|
||||
if _, err := fmt.Fprintf(out, "%s\n", impStr); err != nil {
|
||||
|
|
|
@ -1,6 +1,11 @@
|
|||
package cmds
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"testing"
|
||||
|
||||
"github.com/pobri19/sqlboiler/dbdrivers"
|
||||
|
@ -8,10 +13,19 @@ import (
|
|||
|
||||
func init() {
|
||||
cmdData = &CmdData{
|
||||
Tables: []string{"patrick_table"},
|
||||
Tables: []string{"patrick_table", "spiderman"},
|
||||
Columns: [][]dbdrivers.DBColumn{
|
||||
[]dbdrivers.DBColumn{
|
||||
{Name: "patrick_column", IsNullable: false},
|
||||
{Name: "patrick_column", Type: "string", IsNullable: false},
|
||||
{Name: "aaron_column", Type: "null.String", IsNullable: true},
|
||||
{Name: "id", Type: "null.Int", IsNullable: true},
|
||||
{Name: "fun_id", Type: "int64", IsNullable: false},
|
||||
{Name: "time", Type: "null.Time", IsNullable: true},
|
||||
{Name: "fun_time", Type: "time.Time", IsNullable: false},
|
||||
{Name: "cool_stuff_forever", Type: "[]byte", IsNullable: false},
|
||||
},
|
||||
[]dbdrivers.DBColumn{
|
||||
{Name: "patrick", Type: "string", IsNullable: false},
|
||||
},
|
||||
},
|
||||
PkgName: "patrick",
|
||||
|
@ -20,28 +34,43 @@ func init() {
|
|||
}
|
||||
}
|
||||
|
||||
// ioutil.TempDir
|
||||
// os.TempDir
|
||||
// set the temp dir to outfolder
|
||||
// generate all the stuffs
|
||||
|
||||
// create a file in the tempdir folder named templates_test.go
|
||||
// use exec package to run go test in that folder (exec go test in that temp folder)
|
||||
|
||||
// when i use the exec theres a special thing. if i look here https://golang.org/pkg/os/exec/#Cmd
|
||||
// stderr (create bytes.buf, shove it into that) (use Command for initialization of obj)
|
||||
// use Run (not start) on the command. run the thing which will give an error
|
||||
// check that error, if its nil it completed successfully and test should pass
|
||||
// if not nil, compile failed. check stderr and pump it out and fail test.
|
||||
//
|
||||
// use Dir to set working dir of test.
|
||||
// ALWAYs REMBerR To DEFerR DleELtEE The FOoFldER
|
||||
// miGtihtr WaNnaAu leAVae around iwhen testing
|
||||
|
||||
func TestTemplates(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.SkipNow()
|
||||
}
|
||||
|
||||
// Initialize the templates
|
||||
var err error
|
||||
templates, err = initTemplates("templates")
|
||||
if err != nil {
|
||||
errorQuit(fmt.Errorf("Unable to initialize templates: %s", err))
|
||||
}
|
||||
|
||||
cmdData.OutFolder, err = ioutil.TempDir("", "templates")
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to create tempdir: %s", err)
|
||||
}
|
||||
defer os.RemoveAll(cmdData.OutFolder)
|
||||
|
||||
boilRun(sqlBoilerCommands["boil"], []string{})
|
||||
|
||||
tplFile := cmdData.OutFolder + "/templates_test.go"
|
||||
tplTestHandle, err := os.Create(tplFile)
|
||||
if err != nil {
|
||||
t.Errorf("Unable to create %s: %s", tplFile, err)
|
||||
}
|
||||
defer tplTestHandle.Close()
|
||||
|
||||
tplTestHandle.WriteString(fmt.Sprintf("package %s", cmdData.PkgName))
|
||||
|
||||
buf := bytes.Buffer{}
|
||||
cmd := exec.Command("go", "test", tplFile)
|
||||
cmd.Dir = cmdData.OutFolder
|
||||
cmd.Stderr = &buf
|
||||
|
||||
if err = cmd.Run(); err != nil {
|
||||
t.Errorf("Unable to execute command 'go test', compile failed?: %s\n\n%s", err, buf.String())
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -20,7 +20,7 @@ func generateTemplate(commandName string, data *tplData) []byte {
|
|||
|
||||
output, err := processTemplate(template, data)
|
||||
if err != nil {
|
||||
errorQuit(fmt.Errorf("Unable to process the template: %s", err))
|
||||
errorQuit(fmt.Errorf("Unable to process the template %s for table %s: %s", template.Name(), data.Table, err))
|
||||
}
|
||||
|
||||
return output
|
||||
|
|
Loading…
Add table
Reference in a new issue