Cosmos Nicolaou | 66afced | 2014-09-15 22:12:43 -0700 | [diff] [blame] | 1 | package modules |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io" |
| 6 | "path/filepath" |
| 7 | "runtime" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func init() { |
Cosmos Nicolaou | 1e78ccc | 2014-10-09 08:10:26 -0700 | [diff] [blame] | 12 | RegisterChild("echos", "[args]*", Echo) |
Cosmos Nicolaou | 66afced | 2014-09-15 22:12:43 -0700 | [diff] [blame] | 13 | } |
| 14 | |
| 15 | func Echo(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error { |
| 16 | if len(args) == 0 { |
| 17 | return fmt.Errorf("no args") |
| 18 | } |
| 19 | for _, a := range args { |
| 20 | fmt.Println(a) |
| 21 | } |
| 22 | return nil |
| 23 | } |
| 24 | |
| 25 | func assertNumHandles(t *testing.T, sh *Shell, n int) { |
| 26 | if got, want := len(sh.handles), n; got != want { |
Cosmos Nicolaou | 1e78ccc | 2014-10-09 08:10:26 -0700 | [diff] [blame] | 27 | _, file, line, _ := runtime.Caller(1) |
Cosmos Nicolaou | 66afced | 2014-09-15 22:12:43 -0700 | [diff] [blame] | 28 | t.Errorf("%s:%d: got %d, want %d", filepath.Base(file), line, got, want) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | func TestState(t *testing.T) { |
Cosmos Nicolaou | 1e78ccc | 2014-10-09 08:10:26 -0700 | [diff] [blame] | 33 | sh := NewShell("echos") |
Cosmos Nicolaou | 66afced | 2014-09-15 22:12:43 -0700 | [diff] [blame] | 34 | sh.AddSubprocess("echonotregistered", "[args]*") |
Cosmos Nicolaou | 66afced | 2014-09-15 22:12:43 -0700 | [diff] [blame] | 35 | sh.AddFunction("echof", Echo, "[args]*") |
Cosmos Nicolaou | 66afced | 2014-09-15 22:12:43 -0700 | [diff] [blame] | 36 | assertNumHandles(t, sh, 0) |
Cosmos Nicolaou | 9ca249d | 2014-09-18 15:07:12 -0700 | [diff] [blame] | 37 | |
Cosmos Nicolaou | 66afced | 2014-09-15 22:12:43 -0700 | [diff] [blame] | 38 | _, _ = sh.Start("echonotregistered") // won't start. |
| 39 | hs, _ := sh.Start("echos", "a") |
| 40 | hf, _ := sh.Start("echof", "b") |
Cosmos Nicolaou | 66afced | 2014-09-15 22:12:43 -0700 | [diff] [blame] | 41 | assertNumHandles(t, sh, 2) |
Cosmos Nicolaou | 9ca249d | 2014-09-18 15:07:12 -0700 | [diff] [blame] | 42 | |
Cosmos Nicolaou | 66afced | 2014-09-15 22:12:43 -0700 | [diff] [blame] | 43 | for i, h := range []Handle{hs, hf} { |
Cosmos Nicolaou | bbae388 | 2014-10-02 22:58:19 -0700 | [diff] [blame] | 44 | if got := h.Shutdown(nil, nil); got != nil { |
Cosmos Nicolaou | 66afced | 2014-09-15 22:12:43 -0700 | [diff] [blame] | 45 | t.Errorf("%d: got %q, want %q", i, got, nil) |
| 46 | } |
| 47 | } |
| 48 | assertNumHandles(t, sh, 0) |
Cosmos Nicolaou | 9ca249d | 2014-09-18 15:07:12 -0700 | [diff] [blame] | 49 | |
Cosmos Nicolaou | 66afced | 2014-09-15 22:12:43 -0700 | [diff] [blame] | 50 | hs, _ = sh.Start("echos", "a", "b") |
| 51 | hf, _ = sh.Start("echof", "c") |
| 52 | assertNumHandles(t, sh, 2) |
Cosmos Nicolaou | 9ca249d | 2014-09-18 15:07:12 -0700 | [diff] [blame] | 53 | |
Cosmos Nicolaou | bbae388 | 2014-10-02 22:58:19 -0700 | [diff] [blame] | 54 | sh.Cleanup(nil, nil) |
Cosmos Nicolaou | 66afced | 2014-09-15 22:12:43 -0700 | [diff] [blame] | 55 | assertNumHandles(t, sh, 0) |
| 56 | } |