docker_cli_help_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package main
  2. import (
  3. "os"
  4. "os/exec"
  5. "runtime"
  6. "strings"
  7. "testing"
  8. "unicode"
  9. "github.com/docker/docker/pkg/homedir"
  10. )
  11. func TestHelpWidth(t *testing.T) {
  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. t.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. t.Fatalf("Line is too long(%d chars):\n%s", len(line), line)
  51. }
  52. if home != "" && strings.Contains(line, home) {
  53. t.Fatalf("Help for %q should use ~ instead of %q on:\n%s", command, home, line)
  54. }
  55. if runtime.GOOS != "windows" {
  56. i := strings.Index(line, homedir.GetShortcutString())
  57. if i >= 0 && i != len(line)-1 && line[i+1] != '/' {
  58. t.Fatalf("Main help should not have used home shortcut:\n%s", line)
  59. }
  60. }
  61. }
  62. // Make sure each cmd's help text fits within 80 chars and that
  63. // on non-windows system we use ~ when possible (to shorten things).
  64. // Pull the list of commands from the "Commands:" section of docker help
  65. helpCmd = exec.Command(dockerBinary, "help")
  66. helpCmd.Env = newEnvs
  67. out, ec, err = runCommandWithOutput(helpCmd)
  68. if err != nil || ec != 0 {
  69. t.Fatalf("docker help should have worked\nout:%s\nec:%d", out, ec)
  70. }
  71. i := strings.Index(out, "Commands:")
  72. if i < 0 {
  73. t.Fatalf("Missing 'Commands:' in:\n%s", out)
  74. }
  75. // Grab all chars starting at "Commands:"
  76. // Skip first line, its "Commands:"
  77. cmds := []string{}
  78. for _, cmd := range strings.Split(out[i:], "\n")[1:] {
  79. // Stop on blank line or non-idented line
  80. if cmd == "" || !unicode.IsSpace(rune(cmd[0])) {
  81. break
  82. }
  83. // Grab just the first word of each line
  84. cmd = strings.Split(strings.TrimSpace(cmd), " ")[0]
  85. cmds = append(cmds, cmd)
  86. helpCmd := exec.Command(dockerBinary, cmd, "--help")
  87. helpCmd.Env = newEnvs
  88. out, ec, err := runCommandWithOutput(helpCmd)
  89. if err != nil || ec != 0 {
  90. t.Fatalf("Error on %q help: %s\nexit code:%d", cmd, out, ec)
  91. }
  92. lines := strings.Split(out, "\n")
  93. for _, line := range lines {
  94. if len(line) > 80 {
  95. t.Fatalf("Help for %q is too long(%d chars):\n%s", cmd,
  96. len(line), line)
  97. }
  98. if scanForHome && strings.Contains(line, `"`+home) {
  99. t.Fatalf("Help for %q should use ~ instead of %q on:\n%s",
  100. cmd, home, line)
  101. }
  102. i := strings.Index(line, "~")
  103. if i >= 0 && i != len(line)-1 && line[i+1] != '/' {
  104. t.Fatalf("Help for %q should not have used ~:\n%s", cmd, line)
  105. }
  106. }
  107. }
  108. expected := 39
  109. if len(cmds) != expected {
  110. t.Fatalf("Wrong # of cmds(%d), it should be: %d\nThe list:\n%q",
  111. len(cmds), expected, cmds)
  112. }
  113. }
  114. logDone("help - widths")
  115. }