1
0

envresolver_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 compose
  14. import (
  15. "testing"
  16. "gotest.tools/assert"
  17. )
  18. func Test_EnvResolverWithCase(t *testing.T) {
  19. tests := []struct {
  20. name string
  21. environment map[string]string
  22. caseInsensitive bool
  23. search string
  24. expectedValue string
  25. expectedOk bool
  26. }{
  27. {
  28. name: "case sensitive/case match",
  29. environment: map[string]string{
  30. "Env1": "Value1",
  31. "Env2": "Value2",
  32. },
  33. caseInsensitive: false,
  34. search: "Env1",
  35. expectedValue: "Value1",
  36. expectedOk: true,
  37. },
  38. {
  39. name: "case sensitive/case unmatch",
  40. environment: map[string]string{
  41. "Env1": "Value1",
  42. "Env2": "Value2",
  43. },
  44. caseInsensitive: false,
  45. search: "ENV1",
  46. expectedValue: "",
  47. expectedOk: false,
  48. },
  49. {
  50. name: "case sensitive/nil environment",
  51. environment: nil,
  52. caseInsensitive: false,
  53. search: "Env1",
  54. expectedValue: "",
  55. expectedOk: false,
  56. },
  57. {
  58. name: "case insensitive/case match",
  59. environment: map[string]string{
  60. "Env1": "Value1",
  61. "Env2": "Value2",
  62. },
  63. caseInsensitive: true,
  64. search: "Env1",
  65. expectedValue: "Value1",
  66. expectedOk: true,
  67. },
  68. {
  69. name: "case insensitive/case unmatch",
  70. environment: map[string]string{
  71. "Env1": "Value1",
  72. "Env2": "Value2",
  73. },
  74. caseInsensitive: true,
  75. search: "ENV1",
  76. expectedValue: "Value1",
  77. expectedOk: true,
  78. },
  79. {
  80. name: "case insensitive/unmatch",
  81. environment: map[string]string{
  82. "Env1": "Value1",
  83. "Env2": "Value2",
  84. },
  85. caseInsensitive: true,
  86. search: "Env3",
  87. expectedValue: "",
  88. expectedOk: false,
  89. },
  90. {
  91. name: "case insensitive/nil environment",
  92. environment: nil,
  93. caseInsensitive: true,
  94. search: "Env1",
  95. expectedValue: "",
  96. expectedOk: false,
  97. },
  98. }
  99. for _, test := range tests {
  100. t.Run(test.name, func(t *testing.T) {
  101. f := envResolverWithCase(test.environment, test.caseInsensitive)
  102. v, ok := f(test.search)
  103. assert.Equal(t, v, test.expectedValue)
  104. assert.Equal(t, ok, test.expectedOk)
  105. })
  106. }
  107. }