Skip to content

Instantly share code, notes, and snippets.

@atotto
Last active December 21, 2015 04:29
Show Gist options
  • Save atotto/6250125 to your computer and use it in GitHub Desktop.
Save atotto/6250125 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
)
func main() {
fmt.Println("hello")
}
package main
import (
"testing"
)
func TestMain(t *testing.T) {
main()
}
// Copyright (C) 2013 Ato ARAKI. All rights reserved
package main_test
import (
"io"
"log"
"os"
"os/exec"
"testing"
)
// Test for invoke process.
func TestRun(t *testing.T) {
err := doCompile(".")
if err != nil {
log.Fatal(err)
}
cmd := exec.Command("./main")
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}
stderr, err := cmd.StderrPipe()
if err != nil {
log.Fatal(err)
}
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
go io.Copy(os.Stdout, stdout)
go io.Copy(os.Stdout, stderr)
if err := cmd.Wait(); err != nil {
t.Fatal(err)
}
}
func doCompile(target string) error {
cmd := exec.Command("go", "build", target)
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
if err := cmd.Start(); err != nil {
return err
}
go io.Copy(os.Stdout, stdout)
go io.Copy(os.Stdout, stderr)
if err := cmd.Wait(); err != nil {
return err
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment