Skip to content

Commit 5313cf8

Browse files
Test for command and fix for bug found by this
1 parent 4e7f799 commit 5313cf8

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

ctlcmds.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,14 @@ func (c *ctlCmdHelp) Names() []string {
8181
func (c *ctlCmdHelp) Run(ctl *ctl, params []string) bool {
8282
if len(params) == 1 {
8383
for _, cmd := range cmdAll() {
84-
fmt.Println(cmd.Help())
84+
if match, ok := cmd.(cmdMatcher); ok {
85+
for _, name := range cmd.Names() {
86+
match.Match(name)
87+
ctl.println(cmd.Help())
88+
}
89+
continue
90+
}
91+
ctl.println(cmd.Help())
8592
}
8693
return false
8794
}

ctlcmds_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// svctl
2+
// Copyright (C) 2015 Karol 'Kenji Takahashi' Woźniak
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining
5+
// a copy of this software and associated documentation files (the "Software"),
6+
// to deal in the Software without restriction, including without limitation
7+
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8+
// and/or sell copies of the Software, and to permit persons to whom the
9+
// Software is furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16+
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17+
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19+
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
20+
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
package main
23+
24+
import (
25+
"fmt"
26+
"testing"
27+
28+
"github.com/peterh/liner"
29+
)
30+
31+
func TestHelp(t *testing.T) {
32+
defs := []struct {
33+
action string
34+
nlines int
35+
}{
36+
{"", 38},
37+
{"up", 2},
38+
{"down hup", 4},
39+
{"help", 2},
40+
{"help exit", 3},
41+
}
42+
43+
stdout := &stdout{}
44+
svctl := ctl{line: liner.NewLiner(), stdout: stdout}
45+
46+
for _, def := range defs {
47+
svctl.Ctl(fmt.Sprintf("help %s", def.action))
48+
49+
n := stdout.Len()
50+
if n != def.nlines {
51+
t.Errorf("ERROR IN NLINES: `%d` != `%d` for `%s`", n, def.nlines, def.action)
52+
}
53+
54+
stdout.Clear()
55+
}
56+
57+
svctl.Ctl("help wrongaction")
58+
output := stdout.ReadString()
59+
expected := "wrongaction: unable to find action"
60+
if output != expected {
61+
t.Errorf("ERROR IN STATUS: `%s` != `%s` for `%s`", output, expected, "wrongaction")
62+
}
63+
64+
svctl.line.Close()
65+
}

svctl_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ type stdout struct {
5858
}
5959

6060
func (s *stdout) Write(p []byte) (n int, err error) {
61-
s.value = append(s.value, string(p)[:len(p)-1])
61+
value := strings.Split(string(p), "\n")
62+
s.value = append(s.value, value[:len(value)-1]...)
6263
return len(p), nil
6364
}
6465

@@ -68,6 +69,14 @@ func (s *stdout) ReadString() string {
6869
return v
6970
}
7071

72+
func (s *stdout) Len() int {
73+
return len(s.value)
74+
}
75+
76+
func (s *stdout) Clear() {
77+
s.value = []string{}
78+
}
79+
7180
func createRunitDir() string {
7281
dir, err := ioutil.TempDir("", "svctl_tests")
7382
fatal(err)

0 commit comments

Comments
 (0)