Browse code

Make checking of help text smarter

As I was reworking https://github.com/docker/docker/pull/9402 I realized
that the new testcase I just added that verified all help text is within
80 characters really should be smarter and ask "docker help" for the list
of commands to check instead of having a hard-coded list. This way
it will catch "docker execwait" automagically once #9402 is merged.

Signed-off-by: Doug Davis <dug@us.ibm.com>

Doug Davis authored on 2015/02/05 08:28:51
Showing 1 changed files
... ...
@@ -6,6 +6,7 @@ import (
6 6
 	"runtime"
7 7
 	"strings"
8 8
 	"testing"
9
+	"unicode"
9 10
 )
10 11
 
11 12
 func TestMainHelpWidth(t *testing.T) {
... ...
@@ -43,47 +44,33 @@ func TestCmdHelpWidth(t *testing.T) {
43 43
 		home = os.Getenv("HOME")
44 44
 	}
45 45
 
46
-	for _, command := range []string{
47
-		"attach",
48
-		"build",
49
-		"commit",
50
-		"cp",
51
-		"create",
52
-		"diff",
53
-		"events",
54
-		"exec",
55
-		"export",
56
-		"history",
57
-		"images",
58
-		"import",
59
-		"info",
60
-		"inspect",
61
-		"kill",
62
-		"load",
63
-		"login",
64
-		"logout",
65
-		"logs",
66
-		"port",
67
-		"pause",
68
-		"ps",
69
-		"pull",
70
-		"push",
71
-		"rename",
72
-		"restart",
73
-		"rm",
74
-		"rmi",
75
-		"run",
76
-		"save",
77
-		"search",
78
-		"start",
79
-		"stats",
80
-		"stop",
81
-		"tag",
82
-		"top",
83
-		"unpause",
84
-		"version",
85
-		"wait",
86
-	} {
46
+	// Pull the list of commands from the "Commands:" section of docker help
47
+	helpCmd := exec.Command(dockerBinary, "help")
48
+	out, ec, err := runCommandWithOutput(helpCmd)
49
+	if err != nil || ec != 0 {
50
+		t.Fatalf("docker help should have worked\nout:%s\nec:%d", out, ec)
51
+	}
52
+	i := strings.Index(out, "Commands:")
53
+	if i < 0 {
54
+		t.Fatalf("Missing 'Commands:' in:\n%s", out)
55
+	}
56
+
57
+	// Grab all chars starting at "Commands:"
58
+	// Skip first line, its "Commands:"
59
+	count := 0
60
+	cmds := ""
61
+	for _, command := range strings.Split(out[i:], "\n")[1:] {
62
+		// Stop on blank line or non-idented line
63
+		if command == "" || !unicode.IsSpace(rune(command[0])) {
64
+			break
65
+		}
66
+
67
+		// Grab just the first word of each line
68
+		command = strings.Split(strings.TrimSpace(command), " ")[0]
69
+
70
+		count++
71
+		cmds = cmds + "\n" + command
72
+
87 73
 		helpCmd := exec.Command(dockerBinary, command, "--help")
88 74
 		out, ec, err := runCommandWithOutput(helpCmd)
89 75
 		if err != nil || ec != 0 {
... ...
@@ -100,5 +87,11 @@ func TestCmdHelpWidth(t *testing.T) {
100 100
 		}
101 101
 	}
102 102
 
103
+	expected := 39
104
+	if count != expected {
105
+		t.Fatalf("Wrong # of commands (%d), it should be: %d\nThe list:\n%s",
106
+			len(cmds), expected, cmds)
107
+	}
108
+
103 109
 	logDone("help - cmd widths")
104 110
 }