cmd_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. Copyright 2019 The Kubernetes 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 main
  14. import (
  15. "os"
  16. "reflect"
  17. "testing"
  18. )
  19. func TestGetCmd(t *testing.T) {
  20. testCases := []struct {
  21. desc string
  22. env Getenver
  23. expectArgs []string
  24. }{
  25. {
  26. desc: "Default",
  27. env: &explicitEnv{
  28. vals: map[string]string{
  29. ginkgoEnvKey: "ginkgobin",
  30. testBinEnvKey: "testbin",
  31. },
  32. },
  33. expectArgs: []string{
  34. "ginkgobin",
  35. "--focus=", "--skip=",
  36. "--noColor=true", "testbin", "--",
  37. "--disable-log-dump", "--repo-root=/kubernetes",
  38. "--provider=", "--report-dir=", "--kubeconfig=",
  39. },
  40. }, {
  41. desc: "Filling in defaults",
  42. env: &explicitEnv{
  43. vals: map[string]string{
  44. ginkgoEnvKey: "ginkgobin",
  45. testBinEnvKey: "testbin",
  46. focusEnvKey: "focus",
  47. skipEnvKey: "skip",
  48. providerEnvKey: "provider",
  49. resultsDirEnvKey: "results",
  50. kubeconfigEnvKey: "kubeconfig",
  51. },
  52. },
  53. expectArgs: []string{
  54. "ginkgobin",
  55. "--focus=focus", "--skip=skip",
  56. "--noColor=true", "testbin", "--",
  57. "--disable-log-dump", "--repo-root=/kubernetes",
  58. "--provider=provider", "--report-dir=results", "--kubeconfig=kubeconfig",
  59. },
  60. }, {
  61. desc: "Parallel gets set and skips serial",
  62. env: &explicitEnv{
  63. vals: map[string]string{
  64. ginkgoEnvKey: "ginkgobin",
  65. testBinEnvKey: "testbin",
  66. parallelEnvKey: "true",
  67. },
  68. },
  69. expectArgs: []string{
  70. "ginkgobin", "--p",
  71. "--focus=", "--skip=[Serial]",
  72. "--noColor=true", "testbin", "--",
  73. "--disable-log-dump", "--repo-root=/kubernetes",
  74. "--provider=", "--report-dir=", "--kubeconfig=",
  75. },
  76. }, {
  77. desc: "Arbitrary options before and after double dash split by space",
  78. env: &explicitEnv{
  79. vals: map[string]string{
  80. ginkgoEnvKey: "ginkgobin",
  81. testBinEnvKey: "testbin",
  82. extraArgsEnvKey: "--extra=1 --extra=2",
  83. extraGinkgoArgsEnvKey: "--ginkgo1 --ginkgo2",
  84. },
  85. },
  86. expectArgs: []string{
  87. "ginkgobin", "--focus=", "--skip=",
  88. "--noColor=true", "--ginkgo1", "--ginkgo2",
  89. "testbin", "--",
  90. "--disable-log-dump", "--repo-root=/kubernetes",
  91. "--provider=", "--report-dir=", "--kubeconfig=",
  92. "--extra=1", "--extra=2",
  93. },
  94. }, {
  95. desc: "Arbitrary options can be split by other tokens",
  96. env: &explicitEnv{
  97. vals: map[string]string{
  98. ginkgoEnvKey: "ginkgobin",
  99. testBinEnvKey: "testbin",
  100. extraArgsEnvKey: "--extra=value with spaces:--extra=value with % anything!$$",
  101. extraGinkgoArgsEnvKey: `--ginkgo='with "quotes" and ':--ginkgo2=true$(foo)`,
  102. extraArgsSeparaterEnvKey: ":",
  103. },
  104. },
  105. expectArgs: []string{
  106. "ginkgobin", "--focus=", "--skip=",
  107. "--noColor=true", `--ginkgo='with "quotes" and '`, "--ginkgo2=true$(foo)",
  108. "testbin", "--",
  109. "--disable-log-dump", "--repo-root=/kubernetes",
  110. "--provider=", "--report-dir=", "--kubeconfig=",
  111. "--extra=value with spaces", "--extra=value with % anything!$$",
  112. },
  113. },
  114. }
  115. for _, tc := range testCases {
  116. t.Run(tc.desc, func(t *testing.T) {
  117. c := getCmd(tc.env, os.Stdout)
  118. if !reflect.DeepEqual(c.Args, tc.expectArgs) {
  119. t.Errorf("Expected args %q but got %q", tc.expectArgs, c.Args)
  120. }
  121. })
  122. }
  123. }