1
0
mirror of https://github.com/beego/bee.git synced 2024-11-22 10:10:53 +00:00

modify bee test for more competible with none beego project, and add func safePathAppend

This commit is contained in:
shxsun 2013-09-24 10:42:02 +08:00
parent dbab79687b
commit 8115d5e1b8
2 changed files with 42 additions and 23 deletions

50
test.go
View File

@ -2,10 +2,11 @@ package main
import (
"os"
path "path/filepath"
"os/exec"
path "path/filepath"
"time"
"bytes"
// "bytes"
)
var cmdTest = &Command{
@ -18,6 +19,20 @@ func init() {
cmdTest.Run = testApp
}
func safePathAppend(arr []string, paths ...string) []string {
for _, path := range paths {
if pathExists(path) {
arr = append(arr, path)
}
}
return arr
}
func pathExists(path string) bool {
_, err := os.Stat(path)
return err == nil || os.IsExist(err)
}
var started = make(chan bool)
func testApp(cmd *Command, args []string) {
@ -34,7 +49,7 @@ func testApp(cmd *Command, args []string) {
colorLog("[ERRO] Fail to parse bee.json[ %s ]", err)
}
var paths []string
paths = append(paths,
paths = safePathAppend(paths,
path.Join(crupath, conf.DirStruct.Controllers),
path.Join(crupath, conf.DirStruct.Models),
path.Join(crupath, "./")) // Current path.
@ -50,28 +65,33 @@ func testApp(cmd *Command, args []string) {
select {
case <-started:
runTest()
Kill()
os.Exit(0)
//Kill()
//os.Exit(0)
}
}
}
func runTest() {
colorLog("[INFO] Start testing...\n")
time.Sleep(time.Second*5)
path, _ := os.Getwd()
os.Chdir(path+"/tests")
time.Sleep(time.Second * 1)
crupwd, _ := os.Getwd()
testDir := path.Join(crupwd, "tests")
if pathExists(testDir) {
os.Chdir(testDir)
}
var err error
icmd := exec.Command("go", "test")
var out,errbuffer bytes.Buffer
icmd.Stdout = &out
icmd.Stderr = &errbuffer
colorLog("[INFO] ============== Test Begin ===================\n")
//var out,errbuffer bytes.Buffer
//icmd.Stdout = &out
//icmd.Stderr = &errbuffer
icmd.Stdout = os.Stdout
icmd.Stderr = os.Stderr
colorLog("[TRAC] ============== Test Begin ===================\n")
err = icmd.Run()
colorLog(out.String())
colorLog(errbuffer.String())
colorLog("[INFO] ============== Test End ===================\n")
//colorLog(out.String())
//colorLog(errbuffer.String())
colorLog("[TRAC] ============== Test End ===================\n")
if err != nil {
colorLog("[ERRO] ============== Test failed ===================\n")

View File

@ -3,7 +3,6 @@ package main
import (
"fmt"
"github.com/howeyc/fsnotify"
"log"
"os"
"os/exec"
"strings"
@ -51,7 +50,7 @@ func NewWatcher(paths []string) {
go Autobuild()
}
case err := <-watcher.Error:
log.Fatal("error:", err)
colorLog("[WARN] %s\n", err.Error()) // No need to exit here
}
}
}()
@ -125,7 +124,7 @@ func Kill() {
fmt.Println("Kill -> ", e)
}
}()
if cmd != nil {
if cmd != nil && cmd.Process != nil {
cmd.Process.Kill()
}
}