Permalink
Browse files

Don't write template code for funcs w/o return value.

fixes #26
  • Loading branch information...
natefinch committed Nov 5, 2016
1 parent 48ac3e1 commit febe198f375fd7c40d84beef237393e0266bfa31
Showing with 58 additions and 4 deletions.
  1. +7 −0 cli/parse.go
  2. +5 −2 run/command.go
  3. +37 −0 run/command_test.go
  4. +9 −2 run/template.go
@@ -7,6 +7,7 @@ import (
"fmt"
"io"
"io/ioutil"
"os/exec"
"strings"

"npf.io/gorram/run"
@@ -43,6 +44,12 @@ func ParseAndRun(env OSEnv) int {
return 2
}
if err := run.Run(c); err != nil {
if _, ok := err.(*exec.ExitError); ok {
// if there's a problem running os/Exec commands, we'll have alreday
// printed out stdout and stderr, so we can just be silent on this
// one
return 1
}
fmt.Fprintln(env.Stderr, err.Error())
return 1
}
@@ -23,7 +23,7 @@ import (
// doesn't matter, as long as it's different from earlier versions, but it's
// nice to keep it in <semver> <timestamp> format so that it has some human
// meaning.
const version = "0.9.0 2016-10-12 16:25:09.416829333"
const version = "0.9.1 2016-11-04 16:13:09.14020166"

// Used for type comparison.
// These are ok to keep global since they're static.
@@ -264,6 +264,7 @@ func gen(path string, data templateData) error {
type templateData struct {
Version string
Results string
HasRetVal bool
Args string
NumCLIArgs int
PkgName string
@@ -308,7 +309,6 @@ func (c *Command) compileData() (templateData, error) {
c.Package: {},
"log": {},
"os": {},
"fmt": {},
},
cmd: c,
}
@@ -611,6 +611,9 @@ func (data *templateData) parseResults(results *types.Tuple) error {
func (data *templateData) setReturnType(t types.Type) {
h := data.cmd.retHandler(t)
data.PrintVal = h.Code(t)
data.HasRetVal = true
data.Imports["text/template"] = struct{}{}
data.Imports["fmt"] = struct{}{}
for _, imp := range h.Imports {
data.Imports[imp] = struct{}{}
}
@@ -255,6 +255,43 @@ func TestBase64EncodeToStringFromFilename(t *testing.T) {
}
}

// func Stdout.Write(b []byte) (n int, err error)
// Tests stdin to []byte argument.
// Tests a function with no printable return value.
func TestStdoutWrite(t *testing.T) {
t.Parallel()
dir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
defer os.Remove(dir)
stderr := &bytes.Buffer{}
stdout := &bytes.Buffer{}
stdin := strings.NewReader(`hi!`)
env := Env{
Stderr: stderr,
Stdout: stdout,
Stdin: stdin,
}
c := &Command{
Package: "os",
GlobalVar: "Stdout",
Function: "Write",
Cache: dir,
Env: env,
}
err = Run(c)
checkRunErr(err, c.script(), t)
out := stdout.String()
expected := `hi!`
if out != expected {
t.Errorf("Expected %q but got %q", expected, out)
}
if msg := stderr.String(); msg != "" {
t.Errorf("Expected no stderr output but got %q", msg)
}
}

func TestVersionKeep(t *testing.T) {
t.Parallel()
dir, err := ioutil.TempDir("", "")
@@ -9,14 +9,19 @@ import (
{{range $import, $ignored := .Imports -}}
"{{$import}}"
{{end}}
{{if eq .DstIdx -1}}"text/template"{{end}}
)
const version = "{{.Version}}"
func main() {
log.SetFlags(0)
{{if not .HasRetVal}}
if os.Getenv("GORRAM_TEMPLATE") != "" {
log.Fatalf("No return value to use with templates.")
}
{{end}}
{{.SrcInit}}
{{if gt .NumCLIArgs 0}}
@@ -49,6 +54,7 @@ func main() {
{{if ne .DstIdx -1}}
{{.DstToStdout}}
{{else}}
{{if .HasRetVal}}
t := os.Getenv("GORRAM_TEMPLATE")
if t != "" {
tmpl, err := template.New("").Parse(t)
@@ -62,6 +68,7 @@ func main() {
fmt.Println("")
os.Exit(0)
}
{{end}}
{{.PrintVal}}
{{end}}
}

0 comments on commit febe198

Please sign in to comment.