Skip to content

Commit

Permalink
Expand test suite, fix a race in command output handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
cortesi committed Jul 22, 2018
1 parent 8a65da3 commit b1b950a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
16 changes: 5 additions & 11 deletions modd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/cortesi/termlog"
)

const timeout = 2 * time.Second
const timeout = 5 * time.Second

func touch(p string) {
p = filepath.FromSlash(p)
Expand Down Expand Up @@ -51,7 +51,7 @@ func events(p string) []string {
return parts
}

func _testWatch(t *testing.T, modfunc func(), trigger string, expected []string) {
func _testWatch(t *testing.T, modfunc func(), expected []string) {
defer utils.WithTempDir(t)()

err := os.MkdirAll("a/inner", 0777)
Expand Down Expand Up @@ -99,16 +99,17 @@ func _testWatch(t *testing.T, modfunc func(), trigger string, expected []string)
}

lt := termlog.NewLogTest()

modchan := make(chan *moddwatch.Mod, 1024)
cback := func() {
start := time.Now()
modfunc()
for {
if strings.Contains(lt.String(), trigger) {
ret := events(lt.String())
if reflect.DeepEqual(ret, expected) {
break
}
if time.Now().Sub(start) > timeout {
t.Errorf("Expected\n%#v\nGot\n%#v", expected, ret)
break
}
time.Sleep(50 * time.Millisecond)
Expand All @@ -125,9 +126,7 @@ func _testWatch(t *testing.T, modfunc func(), trigger string, expected []string)
if err != nil {
t.Fatalf("runOnChan: %s", err)
}

ret := events(lt.String())

if !reflect.DeepEqual(ret, expected) {
t.Errorf("Expected\n%#v\nGot\n%#v", expected, ret)
}
Expand All @@ -140,7 +139,6 @@ func TestWatch(t *testing.T) {
_testWatch(
t,
func() { touch("a/touched") },
"touched",
[]string{
":all: ./a/initial",
":a: ./a/initial",
Expand All @@ -160,7 +158,6 @@ func TestWatch(t *testing.T) {
touch("a/touched")
touch("b/touched")
},
"touched",
[]string{
":all: ./a/initial",
":a: ./a/initial",
Expand All @@ -180,7 +177,6 @@ func TestWatch(t *testing.T) {
func() {
touch("a/inner/touched.xxx")
},
"touched",
[]string{
":all: ./a/initial",
":a: ./a/initial",
Expand All @@ -199,7 +195,6 @@ func TestWatch(t *testing.T) {
func() {
touch("a/direct")
},
"direct",
[]string{
":all: ./a/initial",
":a: ./a/initial",
Expand All @@ -219,7 +214,6 @@ func TestWatch(t *testing.T) {
func() {
touch("direct")
},
"direct",
[]string{
":all: ./a/initial",
":a: ./a/initial",
Expand Down
5 changes: 4 additions & 1 deletion shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,12 @@ func (e *Executor) Run(log termlog.Stream, bufferr bool) (error, *ExecState) {
if err != nil {
return err, nil
}
eret := cmd.Wait()

// Order is important here. We MUST wait for the readers to exit before we wait
// on the command itself.
wg.Wait()

eret := cmd.Wait()
estate := &ExecState{
Error: eret,
ErrOutput: buff.String(),
Expand Down
10 changes: 8 additions & 2 deletions shell/shell_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package shell

import (
"fmt"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -98,7 +99,12 @@ func TestBash(t *testing.T) {
t.Skip("skipping bash tests")
return
}
for _, tc := range bashTests {
testCmd(t, "bash", tc)
for i, tc := range bashTests {
t.Run(
fmt.Sprintf("%d", i),
func(t *testing.T) {
testCmd(t, "bash", tc)
},
)
}
}
2 changes: 1 addition & 1 deletion vendor/github.com/cortesi/moddwatch/watch.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b1b950a

Please sign in to comment.