convert_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compatibility
  14. import (
  15. "errors"
  16. "os"
  17. "os/exec"
  18. "testing"
  19. "gotest.tools/v3/assert"
  20. )
  21. func Test_convert(t *testing.T) {
  22. tests := []struct {
  23. name string
  24. args []string
  25. want []string
  26. wantErr bool
  27. }{
  28. {
  29. name: "compose only",
  30. args: []string{"up"},
  31. want: []string{"compose", "up"},
  32. },
  33. {
  34. name: "with context",
  35. args: []string{"--context", "foo", "-f", "compose.yaml", "up"},
  36. want: []string{"--context", "foo", "compose", "-f", "compose.yaml", "up"},
  37. },
  38. {
  39. name: "with context arg",
  40. args: []string{"--context=foo", "-f", "compose.yaml", "up"},
  41. want: []string{"--context", "foo", "compose", "-f", "compose.yaml", "up"},
  42. },
  43. {
  44. name: "with host",
  45. args: []string{"--host", "tcp://1.2.3.4", "up"},
  46. want: []string{"--host", "tcp://1.2.3.4", "compose", "up"},
  47. },
  48. {
  49. name: "compose --verbose",
  50. args: []string{"--verbose"},
  51. want: []string{"--debug", "compose"},
  52. },
  53. {
  54. name: "compose --version",
  55. args: []string{"--version"},
  56. want: []string{"compose", "version"},
  57. },
  58. {
  59. name: "compose -v",
  60. args: []string{"-v"},
  61. want: []string{"compose", "version"},
  62. },
  63. {
  64. name: "help",
  65. args: []string{"-h"},
  66. want: []string{"compose", "--help"},
  67. },
  68. {
  69. name: "issues/1962",
  70. args: []string{"psql", "-h", "postgres"},
  71. want: []string{"compose", "psql", "-h", "postgres"}, // -h should not be converted to --help
  72. },
  73. {
  74. name: "issues/8648",
  75. args: []string{"exec", "mongo", "mongo", "--host", "mongo"},
  76. want: []string{"compose", "exec", "mongo", "mongo", "--host", "mongo"}, // --host is passed to exec
  77. },
  78. {
  79. name: "issues/12",
  80. args: []string{"--log-level", "INFO", "up"},
  81. want: []string{"--log-level", "INFO", "compose", "up"},
  82. },
  83. {
  84. name: "empty string argument",
  85. args: []string{"--project-directory", "", "ps"},
  86. want: []string{"compose", "--project-directory", "", "ps"},
  87. },
  88. {
  89. name: "compose as project name",
  90. args: []string{"--project-name", "compose", "down", "--remove-orphans"},
  91. want: []string{"compose", "--project-name", "compose", "down", "--remove-orphans"},
  92. },
  93. {
  94. name: "completion command",
  95. args: []string{"__complete", "up"},
  96. want: []string{"__complete", "compose", "up"},
  97. },
  98. {
  99. name: "string flag without argument",
  100. args: []string{"--log-level"},
  101. wantErr: true,
  102. },
  103. }
  104. for _, tt := range tests {
  105. t.Run(tt.name, func(t *testing.T) {
  106. if tt.wantErr {
  107. if os.Getenv("BE_CRASHER") == "1" {
  108. Convert(tt.args)
  109. return
  110. }
  111. cmd := exec.Command(os.Args[0], "-test.run=^"+t.Name()+"$")
  112. cmd.Env = append(os.Environ(), "BE_CRASHER=1")
  113. err := cmd.Run()
  114. var e *exec.ExitError
  115. if errors.As(err, &e) && !e.Success() {
  116. return
  117. }
  118. t.Fatalf("process ran with err %v, want exit status 1", err)
  119. } else {
  120. got := Convert(tt.args)
  121. assert.DeepEqual(t, tt.want, got)
  122. }
  123. })
  124. }
  125. }