Skip to content
kless edited this page Jul 31, 2012 · 3 revisions

This template is for testing the output of commands.
Place the output what you are expecting between characters of general comment (at the end of the template), and add the arguments in exec.Command, if it is needed to run the command.

If you use Gowizard: gowizard -tester
Else, use this template in "<Go project>/<name>_test.go"

package main

import (
	"fmt"
	"log"
	"os"
	"os/exec"
	"path/filepath"
)

func Example() {
	out, err := exec.Command(EXEC).Output()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Print(string(out))

	// Output:
/*

*/
}

// * * *

var EXEC string

func init() {
	var err error
	log.SetFlags(0)
	log.SetPrefix("ERROR: ")

	// The executable name will be the directory name.
	if EXEC, err = os.Getwd(); err != nil {
		log.Fatal(err)
	}
	EXEC = filepath.Base(EXEC)

	if _, err = exec.LookPath(EXEC); err != nil {
		if err.(*exec.Error).Err == exec.ErrNotFound {
			if err = exec.Command("go", "install").Run(); err != nil {
				log.Fatal(err)
			}
		} else {
			log.Fatal(err)
		}
	}
}
Clone this wiki locally