docker_cli_help_test.go 4.8 KB

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