Asserting generated file can successfully compile

This commit is contained in:
Dan Buch 2012-12-25 10:19:54 -05:00
parent 887919098f
commit dc4fe12631
2 changed files with 113 additions and 11 deletions

View File

@ -0,0 +1,22 @@
package smplt
var (
HTTP_500_RESPONSE = []byte(`
<!DOCTYPE html>
<html>
<head>
<title>500 Internal Server Error</title>
<style type="text/css">
body {
color: #fff;
background: #f00;
}
</style>
</head>
<body>
<h1>500 Internal Server Error</h1>
<p>Something go boom inside.</p>
</body>
</html>
`)
)

View File

@ -5,9 +5,12 @@ import (
"fmt" "fmt"
"go/parser" "go/parser"
"go/token" "go/token"
"log"
"mime" "mime"
"os" "os"
"os/exec"
"path" "path"
"strings"
"testing" "testing"
"time" "time"
@ -76,10 +79,27 @@ ctx["D"] = &Dance{
var ( var (
tmpdir = path.Join(os.TempDir(), tmpdir = path.Join(os.TempDir(),
fmt.Sprintf("smplt_test-%d", time.Now().UTC().UnixNano())) fmt.Sprintf("smplt_test-%d", time.Now().UTC().UnixNano()))
smpltgenDir = path.Join(tmpdir, "src", "smpltgen")
goCmd string
) )
func init() {
err := os.Setenv("GOPATH", strings.Join([]string{tmpdir, os.Getenv("GOPATH")}, ":"))
if err != nil {
log.Fatal(err)
}
cmd, err := exec.LookPath("go")
if err != nil {
log.Fatal(err)
}
goCmdAddr := &goCmd
*goCmdAddr = cmd
}
func mkTmpDir() { func mkTmpDir() {
err := os.MkdirAll(tmpdir, os.ModeDir|os.ModePerm) err := os.MkdirAll(smpltgenDir, os.ModeDir|os.ModePerm)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -92,6 +112,47 @@ func rmTmpDir() {
} }
} }
func writeRenderedTemplate() (string, error) {
s := SimplateFromString("basic-rendered.txt", BASIC_RENDERED_TXT_SIMPLATE)
outfileName := path.Join(smpltgenDir, s.OutputName())
outf, err := os.Create(outfileName)
if err != nil {
return outfileName, err
}
s.Execute(outf)
err = outf.Close()
if err != nil {
return outfileName, err
}
return outfileName, nil
}
func runGoCommandOnSmpltgen(command string) error {
cmd := exec.Command(goCmd, command, "smpltgen")
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &out
err := cmd.Run()
if err != nil {
return err
}
log.Println(out.String())
return nil
}
func formatRenderedTemplate() error {
return runGoCommandOnSmpltgen("fmt")
}
func buildRenderedTemplate() error {
return runGoCommandOnSmpltgen("install")
}
func TestSimplateKnowsItsFilename(t *testing.T) { func TestSimplateKnowsItsFilename(t *testing.T) {
s := SimplateFromString("hasty-decisions.txt", "herpherpderpherp") s := SimplateFromString("hasty-decisions.txt", "herpherpderpherp")
if s.Filename != "hasty-decisions.txt" { if s.Filename != "hasty-decisions.txt" {
@ -247,22 +308,41 @@ func TestRenderedSimplateOutputIsValidGoSource(t *testing.T) {
defer rmTmpDir() defer rmTmpDir()
} }
s := SimplateFromString("basic-rendered.txt", BASIC_RENDERED_TXT_SIMPLATE) outfileName, err := writeRenderedTemplate()
outfile_name := path.Join(tmpdir, s.OutputName())
outf, err := os.Create(outfile_name)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return
} }
s.Execute(outf)
err = outf.Close()
if err != nil {
t.Error(err)
}
fset := token.NewFileSet() fset := token.NewFileSet()
_, err = parser.ParseFile(fset, outfile_name, nil, parser.DeclarationErrors) _, err = parser.ParseFile(fset, outfileName, nil, parser.DeclarationErrors)
if err != nil {
t.Error(err)
return
}
}
func TestRenderedSimplateCanBeCompiled(t *testing.T) {
mkTmpDir()
if len(os.Getenv("SMPLT_TEST_NOCLEANUP")) > 0 {
fmt.Println("tmpdir =", tmpdir)
} else {
defer rmTmpDir()
}
_, err := writeRenderedTemplate()
if err != nil {
t.Error(err)
return
}
err = formatRenderedTemplate()
if err != nil {
t.Error(err)
return
}
err = buildRenderedTemplate()
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return