github.com/grubernaut/docker@v1.6.0-rc2/integration-cli/docker_cli_help_test.go (about) 1 package main 2 3 import ( 4 "os" 5 "os/exec" 6 "runtime" 7 "strings" 8 "testing" 9 "unicode" 10 11 "github.com/docker/docker/pkg/homedir" 12 ) 13 14 func TestHelpTextVerify(t *testing.T) { 15 // Make sure main help text fits within 80 chars and that 16 // on non-windows system we use ~ when possible (to shorten things). 17 // Test for HOME set to its default value and set to "/" on linux 18 // Yes on windows setting up an array and looping (right now) isn't 19 // necessary because we just have one value, but we'll need the 20 // array/loop on linux so we might as well set it up so that we can 21 // test any number of home dirs later on and all we need to do is 22 // modify the array - the rest of the testing infrastructure should work 23 homes := []string{homedir.Get()} 24 25 // Non-Windows machines need to test for this special case of $HOME 26 if runtime.GOOS != "windows" { 27 homes = append(homes, "/") 28 } 29 30 homeKey := homedir.Key() 31 baseEnvs := os.Environ() 32 33 // Remove HOME env var from list so we can add a new value later. 34 for i, env := range baseEnvs { 35 if strings.HasPrefix(env, homeKey+"=") { 36 baseEnvs = append(baseEnvs[:i], baseEnvs[i+1:]...) 37 break 38 } 39 } 40 41 for _, home := range homes { 42 // Dup baseEnvs and add our new HOME value 43 newEnvs := make([]string, len(baseEnvs)+1) 44 copy(newEnvs, baseEnvs) 45 newEnvs[len(newEnvs)-1] = homeKey + "=" + home 46 47 scanForHome := runtime.GOOS != "windows" && home != "/" 48 49 // Check main help text to make sure its not over 80 chars 50 helpCmd := exec.Command(dockerBinary, "help") 51 helpCmd.Env = newEnvs 52 out, ec, err := runCommandWithOutput(helpCmd) 53 if err != nil || ec != 0 { 54 t.Fatalf("docker help should have worked\nout:%s\nec:%d", out, ec) 55 } 56 lines := strings.Split(out, "\n") 57 for _, line := range lines { 58 if len(line) > 80 { 59 t.Fatalf("Line is too long(%d chars):\n%s", len(line), line) 60 } 61 62 // All lines should not end with a space 63 if strings.HasSuffix(line, " ") { 64 t.Fatalf("Line should not end with a space: %s", line) 65 } 66 67 if scanForHome && strings.Contains(line, `=`+home) { 68 t.Fatalf("Line should use '%q' instead of %q:\n%s", homedir.GetShortcutString(), home, line) 69 } 70 if runtime.GOOS != "windows" { 71 i := strings.Index(line, homedir.GetShortcutString()) 72 if i >= 0 && i != len(line)-1 && line[i+1] != '/' { 73 t.Fatalf("Main help should not have used home shortcut:\n%s", line) 74 } 75 } 76 } 77 78 // Make sure each cmd's help text fits within 80 chars and that 79 // on non-windows system we use ~ when possible (to shorten things). 80 // Pull the list of commands from the "Commands:" section of docker help 81 helpCmd = exec.Command(dockerBinary, "help") 82 helpCmd.Env = newEnvs 83 out, ec, err = runCommandWithOutput(helpCmd) 84 if err != nil || ec != 0 { 85 t.Fatalf("docker help should have worked\nout:%s\nec:%d", out, ec) 86 } 87 i := strings.Index(out, "Commands:") 88 if i < 0 { 89 t.Fatalf("Missing 'Commands:' in:\n%s", out) 90 } 91 92 // Grab all chars starting at "Commands:" 93 // Skip first line, its "Commands:" 94 cmds := []string{} 95 for _, cmd := range strings.Split(out[i:], "\n")[1:] { 96 // Stop on blank line or non-idented line 97 if cmd == "" || !unicode.IsSpace(rune(cmd[0])) { 98 break 99 } 100 101 // Grab just the first word of each line 102 cmd = strings.Split(strings.TrimSpace(cmd), " ")[0] 103 cmds = append(cmds, cmd) 104 105 helpCmd := exec.Command(dockerBinary, cmd, "--help") 106 helpCmd.Env = newEnvs 107 out, ec, err := runCommandWithOutput(helpCmd) 108 if err != nil || ec != 0 { 109 t.Fatalf("Error on %q help: %s\nexit code:%d", cmd, out, ec) 110 } 111 lines := strings.Split(out, "\n") 112 for _, line := range lines { 113 if len(line) > 80 { 114 t.Fatalf("Help for %q is too long(%d chars):\n%s", cmd, 115 len(line), line) 116 } 117 118 if scanForHome && strings.Contains(line, `"`+home) { 119 t.Fatalf("Help for %q should use ~ instead of %q on:\n%s", 120 cmd, home, line) 121 } 122 i := strings.Index(line, "~") 123 if i >= 0 && i != len(line)-1 && line[i+1] != '/' { 124 t.Fatalf("Help for %q should not have used ~:\n%s", cmd, line) 125 } 126 127 // If a line starts with 4 spaces then assume someone 128 // added a multi-line description for an option and we need 129 // to flag it 130 if strings.HasPrefix(line, " ") { 131 t.Fatalf("Help for %q should not have a multi-line option: %s", cmd, line) 132 } 133 134 // Options should NOT end with a period 135 if strings.HasPrefix(line, " -") && strings.HasSuffix(line, ".") { 136 t.Fatalf("Help for %q should not end with a period: %s", cmd, line) 137 } 138 139 // Options should NOT end with a space 140 if strings.HasSuffix(line, " ") { 141 t.Fatalf("Help for %q should not end with a space: %s", cmd, line) 142 } 143 144 } 145 } 146 147 expected := 39 148 if len(cmds) != expected { 149 t.Fatalf("Wrong # of cmds(%d), it should be: %d\nThe list:\n%q", 150 len(cmds), expected, cmds) 151 } 152 } 153 154 logDone("help - verify text") 155 }