CopyGlobalDdevDir creates a temporary global config directory for DDEV using a temporary directory which is set to $XDG_CONFIG_HOME/ddev Don't forget to run ResetGlobalDdevDir(t, tmpXdgConfigHomeDir) in the test's cleanup function.
(t *testing.T)
| 234 | // Don't forget to run ResetGlobalDdevDir(t, tmpXdgConfigHomeDir) |
| 235 | // in the test's cleanup function. |
| 236 | func CopyGlobalDdevDir(t *testing.T) string { |
| 237 | // Create $XDG_CONFIG_HOME |
| 238 | tmpXdgConfigHomeDir := CreateTmpDir("Home_" + util.RandString(5)) |
| 239 | // Global DDEV config directory should be named "ddev" |
| 240 | tmpGlobalDdevDir := filepath.Join(tmpXdgConfigHomeDir, "ddev") |
| 241 | // Make sure that the tmpDir/ddev doesn't exist. |
| 242 | _, err := os.Stat(tmpGlobalDdevDir) |
| 243 | require.Error(t, err) |
| 244 | require.True(t, os.IsNotExist(err)) |
| 245 | // Original ~/.ddev dir location |
| 246 | originalGlobalDdevDir := globalconfig.GetGlobalDdevDirLocation() |
| 247 | // Make sure that the global config directory is set to ~/.ddev |
| 248 | require.Equal(t, originalGlobalDdevDir, globalconfig.GetGlobalDdevDir()) |
| 249 | // Make sure that the original global config directory exists |
| 250 | require.DirExists(t, originalGlobalDdevDir) |
| 251 | originalGlobalConfig := globalconfig.DdevGlobalConfig |
| 252 | // Stop the Mutagen daemon running in the ~/.ddev |
| 253 | ddevapp.StopMutagenDaemon("") |
| 254 | t.Logf("stopped mutagen daemon %s in MUTAGEN_DATA_DIRECTORY=%s", globalconfig.GetMutagenPath(), globalconfig.GetMutagenDataDirectory()) |
| 255 | // Set $XDG_CONFIG_HOME for tests |
| 256 | t.Setenv("XDG_CONFIG_HOME", tmpXdgConfigHomeDir) |
| 257 | // Make sure that the global config directory is set to $XDG_CONFIG_HOME/ddev |
| 258 | require.Equal(t, tmpGlobalDdevDir, globalconfig.GetGlobalDdevDir()) |
| 259 | // And it should be created by now |
| 260 | require.DirExists(t, tmpGlobalDdevDir) |
| 261 | // Create the global config in $XDG_CONFIG_HOME/ddev |
| 262 | globalconfig.EnsureGlobalConfig() |
| 263 | // Copy some settings from ~/.ddev to $XDG_CONFIG_HOME/ddev |
| 264 | globalconfig.DdevGlobalConfig.PerformanceMode = originalGlobalConfig.PerformanceMode |
| 265 | globalconfig.DdevGlobalConfig.NoBindMounts = originalGlobalConfig.NoBindMounts |
| 266 | globalconfig.DdevGlobalConfig.LastStartedVersion = originalGlobalConfig.LastStartedVersion |
| 267 | err = globalconfig.WriteGlobalConfig(globalconfig.DdevGlobalConfig) |
| 268 | require.NoError(t, err) |
| 269 | // Make sure we have the .ddev/bin dir we need for docker-compose and Mutagen |
| 270 | sourceBinDir := filepath.Join(originalGlobalDdevDir, "bin") |
| 271 | _, err = os.Stat(sourceBinDir) |
| 272 | if !os.IsNotExist(err) { |
| 273 | // Copy ~/.ddev/bin to $XDG_CONFIG_HOME/ddev/bin |
| 274 | err = copy2.Copy(sourceBinDir, filepath.Join(tmpGlobalDdevDir, "bin")) |
| 275 | require.NoError(t, err) |
| 276 | } |
| 277 | // globalconfig.GetMutagenDataDirectory sets MUTAGEN_DATA_DIRECTORY |
| 278 | _ = globalconfig.GetMutagenDataDirectory() |
| 279 | // Start mutagen daemon if it's enabled |
| 280 | if globalconfig.DdevGlobalConfig.IsMutagenEnabled() { |
| 281 | ddevapp.StartMutagenDaemon() |
| 282 | t.Logf("started mutagen daemon '%s' with MUTAGEN_DATA_DIRECTORY='%s'", globalconfig.GetMutagenPath(), globalconfig.GetMutagenDataDirectory()) |
| 283 | // Make sure that $MUTAGEN_DATA_DIRECTORY is set to the correct directory |
| 284 | require.Equal(t, os.Getenv("MUTAGEN_DATA_DIRECTORY"), globalconfig.GetMutagenDataDirectory()) |
| 285 | } |
| 286 | |
| 287 | return tmpXdgConfigHomeDir |
| 288 | } |
| 289 | |
| 290 | // ResetGlobalDdevDir removes temporary $XDG_CONFIG_HOME directory |
| 291 | func ResetGlobalDdevDir(t *testing.T, tmpXdgConfigHomeDir string) { |