docker_cli_help_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package main
  2. import (
  3. "os/exec"
  4. "strings"
  5. "testing"
  6. "unicode"
  7. "github.com/docker/docker/pkg/homedir"
  8. )
  9. func TestMainHelpWidth(t *testing.T) {
  10. // Make sure main help text fits within 80 chars and that
  11. // on non-windows system we use ~ when possible (to shorten things)
  12. home := homedir.Get()
  13. helpCmd := exec.Command(dockerBinary, "help")
  14. out, ec, err := runCommandWithOutput(helpCmd)
  15. if err != nil || ec != 0 {
  16. t.Fatalf("docker help should have worked\nout:%s\nec:%d", out, ec)
  17. }
  18. lines := strings.Split(out, "\n")
  19. for _, line := range lines {
  20. if len(line) > 80 {
  21. t.Fatalf("Line is too long(%d chars):\n%s", len(line), line)
  22. }
  23. if home != "" && strings.Contains(line, home) {
  24. t.Fatalf("Line should use '%q' instead of %q:\n%s", homedir.GetShortcutString(), home, line)
  25. }
  26. }
  27. logDone("help - verify main width")
  28. }
  29. func TestCmdHelpWidth(t *testing.T) {
  30. // Make sure main help text fits within 80 chars and that
  31. // on non-windows system we use ~ when possible (to shorten things)
  32. home := homedir.Get()
  33. // Pull the list of commands from the "Commands:" section of docker help
  34. helpCmd := exec.Command(dockerBinary, "help")
  35. out, ec, err := runCommandWithOutput(helpCmd)
  36. if err != nil || ec != 0 {
  37. t.Fatalf("docker help should have worked\nout:%s\nec:%d", out, ec)
  38. }
  39. i := strings.Index(out, "Commands:")
  40. if i < 0 {
  41. t.Fatalf("Missing 'Commands:' in:\n%s", out)
  42. }
  43. // Grab all chars starting at "Commands:"
  44. // Skip first line, its "Commands:"
  45. count := 0
  46. cmds := ""
  47. for _, command := range strings.Split(out[i:], "\n")[1:] {
  48. // Stop on blank line or non-idented line
  49. if command == "" || !unicode.IsSpace(rune(command[0])) {
  50. break
  51. }
  52. // Grab just the first word of each line
  53. command = strings.Split(strings.TrimSpace(command), " ")[0]
  54. count++
  55. cmds = cmds + "\n" + command
  56. helpCmd := exec.Command(dockerBinary, command, "--help")
  57. out, ec, err := runCommandWithOutput(helpCmd)
  58. if err != nil || ec != 0 {
  59. t.Fatalf("docker help should have worked\nout:%s\nec:%d", out, ec)
  60. }
  61. lines := strings.Split(out, "\n")
  62. for _, line := range lines {
  63. if len(line) > 80 {
  64. t.Fatalf("Help for %q is too long(%d chars):\n%s", command, len(line), line)
  65. }
  66. if home != "" && strings.Contains(line, home) {
  67. t.Fatalf("Help for %q should use home shortcut instead of %q on:\n%s", command, home, line)
  68. }
  69. }
  70. }
  71. expected := 39
  72. if count != expected {
  73. t.Fatalf("Wrong # of commands (%d), it should be: %d\nThe list:\n%s",
  74. len(cmds), expected, cmds)
  75. }
  76. logDone("help - cmd widths")
  77. }