forked from ipfs/go-ipfs-cmds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_test.go
60 lines (51 loc) · 1.03 KB
/
run_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package cli
import (
"context"
"os"
"testing"
"time"
cmds "github.com/ipfs/go-ipfs-cmds"
)
var root = &cmds.Command{
Subcommands: map[string]*cmds.Command{
"test": {
Run: func(req *cmds.Request, re cmds.ResponseEmitter, e cmds.Environment) error {
err := cmds.EmitOnce(re, 42)
time.Sleep(2 * time.Second)
e.(env).ch <- struct{}{}
return err
},
},
},
}
type env struct {
ch chan struct{}
}
func TestRunWaits(t *testing.T) {
flag := make(chan struct{}, 1)
devnull, err := os.OpenFile(os.DevNull, os.O_RDWR, 0600)
if err != nil {
t.Fatal(err)
}
defer devnull.Close()
err = Run(
context.Background(),
root,
[]string{"test", "test"},
devnull, devnull, devnull,
func(ctx context.Context, req *cmds.Request) (cmds.Environment, error) {
return env{flag}, nil
},
func(req *cmds.Request, env interface{}) (cmds.Executor, error) {
return cmds.NewExecutor(req.Root), nil
},
)
if err != nil {
t.Fatal(err)
}
select {
case <-flag:
default:
t.Fatal("expected flag to be raised")
}
}