github.com/zaquestion/lab@v0.25.1/internal/config/config_test.go (about) 1 package config 2 3 import ( 4 "bufio" 5 "bytes" 6 "fmt" 7 "io" 8 "io/ioutil" 9 "os" 10 "path" 11 "path/filepath" 12 "strings" 13 "testing" 14 15 "github.com/spf13/viper" 16 "github.com/stretchr/testify/assert" 17 "github.com/stretchr/testify/require" 18 ) 19 20 func initMainConfig(testconf string) { 21 MainConfig = viper.New() 22 MainConfig.SetConfigName("lab") 23 MainConfig.SetConfigType("toml") 24 MainConfig.AddConfigPath(testconf) 25 } 26 27 func resetMainConfig() { 28 // *viper.Viper.Reset() does not exist so just set MainConfig to nil for testing 29 MainConfig = nil 30 } 31 32 func TestNewConfig(t *testing.T) { 33 testconf := t.TempDir() 34 35 t.Run("create config", func(t *testing.T) { 36 old := os.Stdout // keep backup of the real stdout 37 r, w, _ := os.Pipe() 38 os.Stdout = w 39 40 var buf bytes.Buffer 41 fmt.Fprintln(&buf, "https://gitlab.zaquestion.io") 42 43 oldreadPassword := readPassword 44 readPassword = func(bufio.Reader) (string, string, error) { 45 return "abcde12345", "", nil 46 } 47 defer func() { 48 readPassword = oldreadPassword 49 }() 50 51 initMainConfig(testconf) 52 err := New(testconf, &buf) 53 if err != nil { 54 t.Fatal(err) 55 } 56 57 outC := make(chan string) 58 // copy the output in a separate goroutine so printing can't block indefinitely 59 go func() { 60 var buf bytes.Buffer 61 io.Copy(&buf, r) 62 outC <- buf.String() 63 }() 64 65 // back to normal state 66 w.Close() 67 os.Stdout = old // restoring the real stdout 68 out := <-outC 69 70 assert.Contains(t, out, "Enter GitLab host (default: https://gitlab.com): ") 71 72 cfg, err := os.Open(path.Join(testconf, "lab.toml")) 73 if err != nil { 74 t.Fatal(err) 75 } 76 77 cfgData, err := ioutil.ReadAll(cfg) 78 if err != nil { 79 t.Fatal(err) 80 } 81 assert.Equal(t, ` 82 [core] 83 host = "https://gitlab.zaquestion.io" 84 token = "abcde12345" 85 `, string(cfgData)) 86 }) 87 resetMainConfig() 88 } 89 90 func TestNewConfigHostOverride(t *testing.T) { 91 testconf := t.TempDir() 92 93 os.Setenv("LAB_CORE_HOST", "https://gitlab2.zaquestion.io") 94 95 t.Run("create config", func(t *testing.T) { 96 97 initMainConfig(testconf) 98 MainConfig.SetEnvPrefix("LAB") 99 MainConfig.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) 100 MainConfig.AutomaticEnv() 101 102 require.Equal(t, "https://gitlab2.zaquestion.io", MainConfig.GetString("core.host")) 103 104 old := os.Stdout // keep backup of the real stdout 105 r, w, _ := os.Pipe() 106 os.Stdout = w 107 108 oldreadPassword := readPassword 109 readPassword = func(bufio.Reader) (string, string, error) { 110 return "abcde12345", "", nil 111 } 112 defer func() { 113 readPassword = oldreadPassword 114 }() 115 116 var buf bytes.Buffer 117 118 err := New(testconf, &buf) 119 if err != nil { 120 t.Fatal(err) 121 } 122 123 outC := make(chan string) 124 // copy the output in a separate goroutine so printing can't block indefinitely 125 go func() { 126 var buf bytes.Buffer 127 io.Copy(&buf, r) 128 outC <- buf.String() 129 }() 130 131 // back to normal state 132 w.Close() 133 os.Stdout = old // restoring the real stdout 134 out := <-outC 135 136 assert.NotContains(t, out, "Enter GitLab host") 137 138 cfg, err := os.Open(path.Join(testconf, "lab.toml")) 139 if err != nil { 140 t.Fatal(err) 141 } 142 143 cfgData, err := ioutil.ReadAll(cfg) 144 if err != nil { 145 t.Fatal(err) 146 } 147 assert.Equal(t, ` 148 [core] 149 host = "https://gitlab2.zaquestion.io" 150 token = "abcde12345" 151 `, string(cfgData)) 152 }) 153 resetMainConfig() 154 } 155 156 func TestNewLoadTokenConfig(t *testing.T) { 157 testconf := t.TempDir() 158 159 t.Run("create load_token config", func(t *testing.T) { 160 old := os.Stdout // keep backup of the real stdout 161 r, w, _ := os.Pipe() 162 os.Stdout = w 163 164 var buf bytes.Buffer 165 fmt.Fprintln(&buf, "https://gitlab.zaquestion.io") 166 167 oldreadPassword := readPassword 168 readPassword = func(bufio.Reader) (string, string, error) { 169 return "", "bash echo abcde12345", nil 170 } 171 defer func() { 172 readPassword = oldreadPassword 173 }() 174 175 initMainConfig(testconf) 176 err := New(testconf, &buf) 177 if err != nil { 178 t.Fatal(err) 179 } 180 181 outC := make(chan string) 182 // copy the output in a separate goroutine so printing can't block indefinitely 183 go func() { 184 var buf bytes.Buffer 185 io.Copy(&buf, r) 186 outC <- buf.String() 187 }() 188 189 // back to normal state 190 w.Close() 191 os.Stdout = old // restoring the real stdout 192 out := <-outC 193 194 assert.Contains(t, out, "Enter GitLab host (default: https://gitlab.com): ") 195 196 cfg, err := os.Open(path.Join(testconf, "lab.toml")) 197 if err != nil { 198 t.Fatal(err) 199 } 200 201 cfgData, err := ioutil.ReadAll(cfg) 202 if err != nil { 203 t.Fatal(err) 204 } 205 assert.Equal(t, ` 206 [core] 207 host = "https://gitlab.zaquestion.io" 208 load_token = "bash echo abcde12345" 209 `, string(cfgData)) 210 }) 211 resetMainConfig() 212 } 213 214 func TestConvertHCLtoTOML(t *testing.T) { 215 tmpDir := t.TempDir() 216 oldCnfPath := filepath.Join(tmpDir, "lab.hcl") 217 newCnfPath := filepath.Join(tmpDir, "lab.toml") 218 oldCnf, err := os.Create(oldCnfPath) 219 if err != nil { 220 t.Fatal(err) 221 } 222 oldCnf.WriteString(`"core" = { 223 "host" = "https://gitlab.com" 224 "token" = "foobar" 225 "user" = "lab-testing" 226 }`) 227 228 ConvertHCLtoTOML(tmpDir, tmpDir, "lab") 229 230 _, err = os.Stat(oldCnfPath) 231 assert.True(t, os.IsNotExist(err)) 232 233 _, err = os.Stat(newCnfPath) 234 assert.NoError(t, err) 235 236 newCnf, err := os.Open(newCnfPath) 237 if err != nil { 238 t.Fatal(err) 239 } 240 cfgData, err := ioutil.ReadAll(newCnf) 241 if err != nil { 242 t.Fatal(err) 243 } 244 245 assert.Equal(t, ` 246 [core] 247 host = "https://gitlab.com" 248 token = "foobar" 249 user = "lab-testing" 250 `, string(cfgData)) 251 } 252 253 func TestTokenTest(t *testing.T) { 254 tmpDir := t.TempDir() 255 configPath := filepath.Join(tmpDir, "lab.toml") 256 config, err := os.Create(configPath) 257 if err != nil { 258 t.Fatal(err) 259 } 260 config.WriteString(` 261 [core] 262 host = "https://gitlab.com" 263 token = "foobar" 264 user = "lab-testing" 265 `) 266 267 initMainConfig(tmpDir) 268 MainConfig.ReadInConfig() 269 token := GetToken() 270 os.Remove(configPath + "/lab.toml") 271 resetMainConfig() 272 assert.Equal(t, "foobar", token) 273 } 274 275 func TestLoadTokenTest(t *testing.T) { 276 tmpDir := t.TempDir() 277 configPath := filepath.Join(tmpDir, "lab.toml") 278 config, err := os.Create(configPath) 279 if err != nil { 280 t.Fatal(err) 281 } 282 config.WriteString(` 283 [core] 284 host = "https://gitlab.com" 285 load_token = "echo foobar" 286 user = "lab-testing" 287 `) 288 289 initMainConfig(tmpDir) 290 MainConfig.ReadInConfig() 291 token := GetToken() 292 os.Remove(configPath + "/lab.toml") 293 resetMainConfig() 294 assert.Equal(t, "foobar", token) 295 }