Skip to content

Commit c514b29

Browse files
Catch and fix some more corner cases
1 parent 5313cf8 commit c514b29

File tree

4 files changed

+56
-9
lines changed

4 files changed

+56
-9
lines changed

_testdata/afile

Whitespace-only changes.

ctlcmds.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ func (c *ctlCmdStatus) Run(ctl *ctl, params []string) bool {
5353
ctl.Status("*", true)
5454
} else {
5555
for _, dir := range params[1:] {
56+
if dir == "" {
57+
continue
58+
}
5659
ctl.Status(dir, true)
5760
}
5861
}

svctl.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -242,20 +242,24 @@ func (c *ctl) Services(pattern string, toLog bool) []string {
242242
sort.Strings(files)
243243
}
244244
}
245-
return files
245+
246+
dirs := []string{}
247+
for _, file := range files {
248+
if fi, err := os.Stat(file); err == nil && fi.IsDir() {
249+
dirs = append(dirs, file)
250+
}
251+
}
252+
return dirs
246253
}
247254

248255
// Status Prints all statuses matching id and optionally their log process statuses.
249256
func (c *ctl) Status(id string, toLog bool) {
250257
// TODO: normally (up|down) and stuff?
251-
statuses := []*status{}
252-
for _, dir := range c.Services(id, toLog) {
253-
if fi, err := os.Stat(dir); err != nil || !fi.IsDir() {
254-
continue
255-
}
256-
258+
services := c.Services(id, toLog)
259+
statuses := make([]*status, len(services))
260+
for i, dir := range services {
257261
status := newStatus(dir, c.serviceName(dir))
258-
statuses = append(statuses, status)
262+
statuses[i] = status
259263

260264
for i, offset := range status.Offsets {
261265
if statuses[0].Offsets[i] < offset {

svctl_test.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ func equal(s1, s2 []string) bool {
5353
return true
5454
}
5555

56+
func containsTrimmed(slice []string, str string) bool {
57+
for _, elem := range slice {
58+
if strings.TrimSpace(elem) == strings.TrimSpace(str) {
59+
return true
60+
}
61+
}
62+
return false
63+
}
64+
5665
type stdout struct {
5766
value []string
5867
}
@@ -120,7 +129,7 @@ func (r *runitRunner) Assert(t *testing.T, cmd *cmdDef) {
120129
for _, service := range cmd.services {
121130
stdout := r.stdout.ReadString()
122131
pieces := strings.Split(stdout, " ")
123-
if !contains(cmd.services, pieces[0]) {
132+
if !containsTrimmed(cmd.services, pieces[0]) {
124133
t.Errorf(
125134
"ERROR IN STATUS: service `%s` != `%s` for %s:%s",
126135
pieces[0], service, cmd.cmd, service,
@@ -238,6 +247,8 @@ func TestCmd(t *testing.T) {
238247
{"s", []string{"o"}, "STOPPED", 0},
239248
{"once", []string{"r1"}, "RUNNING", 0},
240249
{"s", []string{"r0", "o"}, "STOPPED", 0},
250+
{"s", []string{"r0 ", "o"}, "STOPPED", 0},
251+
{"u", []string{"r0 ", "o"}, "RUNNING", 0},
241252
}
242253
for _, cmd := range cmds {
243254
if cmd.cmd == "" {
@@ -248,6 +259,35 @@ func TestCmd(t *testing.T) {
248259
runit.Assert(t, &cmd)
249260
}
250261

262+
// Tests for wildcards and defaults.
263+
assert := func() {
264+
for runit.stdout.Len() != 0 {
265+
stdout := runit.stdout.ReadString()
266+
pieces := strings.Fields(stdout)
267+
pieces[2] = strings.Join(pieces[2:], " ")
268+
if pieces[1] == "ERROR" && pieces[2] == "unable to open supervise/ok" {
269+
if pieces[0] != "longone" && pieces[0] != "w" {
270+
t.Errorf("ERROR IN IMPLICIT *: `%s`", stdout)
271+
}
272+
continue
273+
}
274+
if !strings.HasPrefix(pieces[1], "RUNNING") {
275+
t.Errorf("ERROR IN IMPLICIT *: `%s` != `RUNNING`", pieces[1])
276+
}
277+
if pieces[0] != "o" && pieces[0] != "r0" && pieces[0] != "r1" {
278+
t.Errorf("ERROR IN IMPLICIT *: Unknown service `%s`", pieces[0])
279+
}
280+
}
281+
}
282+
svctl.Ctl("u")
283+
assert()
284+
svctl.Ctl("s")
285+
assert()
286+
svctl.Ctl("s r?")
287+
runit.Assert(t, &cmdDef{"s", []string{"r0", "r1"}, "RUNNING", 0})
288+
svctl.Ctl("s l*ne")
289+
runit.Assert(t, &cmdDef{"s", []string{"longone"}, "ERROR", 0})
290+
251291
// Tests for errors.
252292
// Should span to other actions no problem, so just check with `u`.
253293
// Incorrect action.

0 commit comments

Comments
 (0)