Skip to content

Commit cba4914

Browse files
committed
add tests for lockfile
1 parent 5130727 commit cba4914

File tree

1 file changed

+49
-33
lines changed

1 file changed

+49
-33
lines changed

guard_test.go

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
package main
22

33
import (
4+
"fmt"
45
"io/ioutil"
56
"os"
7+
"strings"
68
"testing"
9+
"time"
710

811
"github.com/google/go-cmp/cmp"
912
)
1013

1114
type (
1215
tCase struct {
13-
command string
14-
content string
15-
}
16-
17-
tQuietCase struct {
18-
quiet string
19-
command string
20-
content string
16+
command string
17+
additionalArgs []string
18+
content string
2119
}
2220
)
2321

2422
func guard(t *testing.T, additionalArgs []string, command string, want string) (err error) {
23+
fmt.Printf("running %s\n", command)
2524
tempFile, err := ioutil.TempFile("", "guard")
2625
if err != nil {
27-
t.Errorf("unable to create tmp file (%s): %s", tempFile.Name(), err)
26+
return fmt.Errorf("unable to create tmp file (%s): %s", tempFile.Name(), err)
2827
}
2928

3029
os.Args = []string{
@@ -43,8 +42,11 @@ func guard(t *testing.T, additionalArgs []string, command string, want string) (
4342
return
4443
}
4544

45+
if strings.ContainsRune(string(got), '\x00') {
46+
return fmt.Errorf("Nullbyte")
47+
}
4648
if diff := cmp.Diff(want, string(got)); diff != "" {
47-
t.Errorf("mismatch (-want +got):\n%s", diff)
49+
return fmt.Errorf("mismatch (-want +got):\n%s", diff)
4850
}
4951

5052
tempFile.Close()
@@ -53,7 +55,7 @@ func guard(t *testing.T, additionalArgs []string, command string, want string) (
5355
return nil
5456
}
5557

56-
return
58+
return err
5759
}
5860

5961
func TestOutput(t *testing.T) {
@@ -62,45 +64,59 @@ func TestOutput(t *testing.T) {
6264
// test normal
6365
cases := []tCase{
6466
// check exit statuss
65-
{"true", ""},
66-
{"false", "// error: exit status 1\n"},
67-
{"exit 2", "// error: exit status 2\n"},
67+
{"true", []string{}, ""},
68+
{"false", []string{}, "// error: exit status 1\n"},
69+
{"exit 2", []string{}, "// error: exit status 2\n"},
6870

6971
// check output
70-
{"echo fail", "fail\n// error: bad keyword in command output: fail\n"},
71-
{"echo failure", "failure\n// error: bad keyword in command output: failure\n"},
72-
{"echo ERR", "ERR\n// error: bad keyword in command output: ERR\n"},
73-
{"echo ERROR", "ERROR\n// error: bad keyword in command output: ERROR\n"},
74-
{"echo Crit", "Crit\n// error: bad keyword in command output: Crit\n"},
75-
{"echo Critical", "Critical\n// error: bad keyword in command output: Critical\n"},
72+
{"echo fail", []string{}, "fail\n// error: bad keyword in command output: fail\n"},
73+
{"echo failure", []string{}, "failure\n// error: bad keyword in command output: failure\n"},
74+
{"echo ERR", []string{}, "ERR\n// error: bad keyword in command output: ERR\n"},
75+
{"echo ERROR", []string{}, "ERROR\n// error: bad keyword in command output: ERROR\n"},
76+
{"echo Crit", []string{}, "Crit\n// error: bad keyword in command output: Crit\n"},
77+
{"echo Critical", []string{}, "Critical\n// error: bad keyword in command output: Critical\n"},
7678

7779
// check err output
78-
{"echo Hi there 1>&2", "Hi there\n// error: stderr is not empty\n"},
80+
{"echo Hi there 1>&2", []string{}, "Hi there\n// error: stderr is not empty\n"},
7981

8082
// check asci boundaries
81-
{"echo transferred", ""},
82-
{"echo transferred error", "transferred error\n// error: bad keyword in command output: transferred error\n"},
83+
{"echo transferred", []string{}, ""},
84+
{"echo transferred error", []string{}, "transferred error\n// error: bad keyword in command output: transferred error\n"},
85+
86+
// quiet tests
87+
{"false", []string{"-quiet-times", "0 * * * *:1h"}, ""},
88+
{"false", []string{"-quiet-times", "0 0 * * *:0s"}, "// error: exit status 1\n"},
89+
90+
// timeout tests
91+
{"sleep 1", []string{"-timeout", "2s"}, ""},
92+
{"sleep 2", []string{"-timeout", "500ms"}, "// error: context deadline exceeded\n"},
8393
}
8494
for _, c := range cases {
85-
err = guard(t, []string{}, c.command, c.content)
95+
err = guard(t, c.additionalArgs, c.command, c.content)
8696
if err != nil {
8797
t.Error(err)
8898
break
8999
}
90100
}
91101

92-
// test with quiet
93-
qCases := []tQuietCase{
94-
{"0 * * * *:1h", "false", ""},
95-
{"0 0 * * *:0s", "false", "// error: exit status 1\n"},
102+
// paralell tests
103+
cases = []tCase{
104+
// lockfile tests
105+
{"sleep 2; echo failed", []string{"-lockfile", "/tmp/guard.lock"}, "failed\n// error: bad keyword in command output: failed\n"},
106+
{"echo failed this should not run; sleep 3", []string{"-lockfile", "/tmp/guard.lock"}, ""},
96107
}
97-
98-
99-
for _, c := range qCases {
100-
err = guard(t, []string{"-quiet-times", c.quiet}, c.command, c.content)
108+
errChan := make(chan error)
109+
for _, c := range cases {
110+
<-time.After(1 * time.Second)
111+
go func(c tCase) {
112+
err := guard(t, c.additionalArgs, c.command, c.content)
113+
errChan <- err
114+
}(c)
115+
}
116+
for i := 0; i < len(cases); i++ {
117+
err = <-errChan
101118
if err != nil {
102119
t.Error(err)
103-
break
104120
}
105121
}
106122
}

0 commit comments

Comments
 (0)