docker_cli_help_test.go 2.5 KB

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