Skip to content

Commit

Permalink
refine test case
Browse files Browse the repository at this point in the history
  • Loading branch information
amyangfei committed May 8, 2019
1 parent 5b9fd82 commit e586d04
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
44 changes: 41 additions & 3 deletions cmd/goodbye/main_test.go
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/amyangfei/fpcov/pkg/hello"
)

func TestRunMain(t *testing.T) {
func test1(t *testing.T) {
var (
args []string
exit = make(chan int)
Expand Down Expand Up @@ -42,8 +42,46 @@ func TestRunMain(t *testing.T) {

select {
case <-waitCh:
return
case <-exit:
return
}
}

func test2(t *testing.T) {
var (
args []string
exit = make(chan int)
)
for _, arg := range os.Args {
switch {
case arg == "DEVEL":
case strings.HasPrefix(arg, "-test."):
default:
args = append(args, arg)
}
}

oldOsExit := hello.OsExit
defer func() { hello.OsExit = oldOsExit }()
hello.OsExit = func(code int) {
log.Printf("[test] os.Exit with code %d", code)
exit <- code
// sleep here to prevent following code execution in the caller routine
time.Sleep(time.Second * 60)
}

go func() {
select {
case <-exit:
time.Sleep(time.Millisecond * 100)
os.Exit(0)
}
}()

os.Args = args
main()
}

func TestRunMain(t *testing.T) {
// test1(t)
test2(t)
}
2 changes: 1 addition & 1 deletion tests/run.sh
Expand Up @@ -5,4 +5,4 @@ CUR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )

mkdir -p $TEST_DIR

$CUR/../bin/goodbye.test -test.coverprofile="$TEST_DIR/goodbye.$(date +"%s").out" DEVEL >> $TEST_DIR/goodbye.log 2>&1
$CUR/../bin/goodbye.test -test.coverprofile="$TEST_DIR/cov.goodbye.$(date +"%s").out" DEVEL >> $TEST_DIR/goodbye.log 2>&1

0 comments on commit e586d04

Please sign in to comment.