|
| 1 | +/* |
| 2 | + Copyright 2023 Docker Compose CLI authors |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package mobycli |
| 18 | + |
| 19 | +import ( |
| 20 | + "os" |
| 21 | + "path/filepath" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/docker/compose-cli/api/config" |
| 25 | + |
| 26 | + "gotest.tools/v3/assert" |
| 27 | +) |
| 28 | + |
| 29 | +func TestCliHintsEnabled(t *testing.T) { |
| 30 | + testCases := []struct { |
| 31 | + name string |
| 32 | + setup func() |
| 33 | + expected bool |
| 34 | + }{ |
| 35 | + { |
| 36 | + "enabled by default", |
| 37 | + func() {}, |
| 38 | + true, |
| 39 | + }, |
| 40 | + { |
| 41 | + "enabled from environment variable", |
| 42 | + func() { |
| 43 | + t.Setenv(cliHintsEnvVarName, "true") |
| 44 | + }, |
| 45 | + true, |
| 46 | + }, |
| 47 | + { |
| 48 | + "disabled from environment variable", |
| 49 | + func() { |
| 50 | + t.Setenv(cliHintsEnvVarName, "false") |
| 51 | + }, |
| 52 | + false, |
| 53 | + }, |
| 54 | + { |
| 55 | + "unsupported value", |
| 56 | + func() { |
| 57 | + t.Setenv(cliHintsEnvVarName, "maybe") |
| 58 | + }, |
| 59 | + true, |
| 60 | + }, |
| 61 | + { |
| 62 | + "enabled in config file", |
| 63 | + func() { |
| 64 | + d := testConfigDir(t) |
| 65 | + writeSampleConfig(t, d, configEnabled) |
| 66 | + }, |
| 67 | + true, |
| 68 | + }, |
| 69 | + { |
| 70 | + "plugin defined in config file but no enabled entry", |
| 71 | + func() { |
| 72 | + d := testConfigDir(t) |
| 73 | + writeSampleConfig(t, d, configPartial) |
| 74 | + }, |
| 75 | + true, |
| 76 | + }, |
| 77 | + |
| 78 | + { |
| 79 | + "unsupported value", |
| 80 | + func() { |
| 81 | + d := testConfigDir(t) |
| 82 | + writeSampleConfig(t, d, configOnce) |
| 83 | + }, |
| 84 | + true, |
| 85 | + }, |
| 86 | + { |
| 87 | + "disabled in config file", |
| 88 | + func() { |
| 89 | + d := testConfigDir(t) |
| 90 | + writeSampleConfig(t, d, configDisabled) |
| 91 | + }, |
| 92 | + false, |
| 93 | + }, |
| 94 | + { |
| 95 | + "enabled in config file but disabled by env var", |
| 96 | + func() { |
| 97 | + d := testConfigDir(t) |
| 98 | + writeSampleConfig(t, d, configEnabled) |
| 99 | + t.Setenv(cliHintsEnvVarName, "false") |
| 100 | + }, |
| 101 | + false, |
| 102 | + }, |
| 103 | + { |
| 104 | + "disabled in config file but enabled by env var", |
| 105 | + func() { |
| 106 | + d := testConfigDir(t) |
| 107 | + writeSampleConfig(t, d, configDisabled) |
| 108 | + t.Setenv(cliHintsEnvVarName, "true") |
| 109 | + }, |
| 110 | + true, |
| 111 | + }, |
| 112 | + } |
| 113 | + |
| 114 | + for _, testCase := range testCases { |
| 115 | + tc := testCase |
| 116 | + t.Run(tc.name, func(t *testing.T) { |
| 117 | + tc.setup() |
| 118 | + assert.Equal(t, CliHintsEnabled(), tc.expected) |
| 119 | + }) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func testConfigDir(t *testing.T) string { |
| 124 | + dir := config.Dir() |
| 125 | + d, _ := os.MkdirTemp("", "") |
| 126 | + config.WithDir(d) |
| 127 | + t.Cleanup(func() { |
| 128 | + _ = os.RemoveAll(d) |
| 129 | + config.WithDir(dir) |
| 130 | + }) |
| 131 | + return d |
| 132 | +} |
| 133 | + |
| 134 | +func writeSampleConfig(t *testing.T, d string, conf []byte) { |
| 135 | + err := os.WriteFile(filepath.Join(d, config.ConfigFileName), conf, 0644) |
| 136 | + assert.NilError(t, err) |
| 137 | +} |
| 138 | + |
| 139 | +var configEnabled = []byte(`{ |
| 140 | + "plugins": { |
| 141 | + "-x-cli-hints": { |
| 142 | + "enabled": "true" |
| 143 | + } |
| 144 | + } |
| 145 | +}`) |
| 146 | + |
| 147 | +var configDisabled = []byte(`{ |
| 148 | + "plugins": { |
| 149 | + "-x-cli-hints": { |
| 150 | + "enabled": "false" |
| 151 | + } |
| 152 | + } |
| 153 | +}`) |
| 154 | + |
| 155 | +var configPartial = []byte(`{ |
| 156 | + "plugins": { |
| 157 | + "-x-cli-hints": { |
| 158 | + } |
| 159 | + } |
| 160 | +}`) |
| 161 | + |
| 162 | +var configOnce = []byte(`{ |
| 163 | + "plugins": { |
| 164 | + "-x-cli-hints": { |
| 165 | + "enabled": "maybe" |
| 166 | + } |
| 167 | + } |
| 168 | +}`) |
0 commit comments