From f114321322c05281fb0ed93700a7beeae253b30a Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Mon, 15 Jul 2024 09:58:58 +0200 Subject: [PATCH] Save changed user config back to disk in integration tests At the moment, the user config is only read once at startup, so there's no point in writing it back to disk. However, later in this branch we will add code that reloads the user config when switching repos, which does happen quite a bit in integration tests; this would undo the changes that a test made in its SetupConfig function, so write those changes to disk to prevent that from happening. --- pkg/app/entry_point.go | 6 ++++++ pkg/config/app_config.go | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/pkg/app/entry_point.go b/pkg/app/entry_point.go index 96f6176c7..d23519a98 100644 --- a/pkg/app/entry_point.go +++ b/pkg/app/entry_point.go @@ -136,6 +136,12 @@ func Start(buildInfo *BuildInfo, integrationTest integrationTypes.IntegrationTes if integrationTest != nil { integrationTest.SetupConfig(appConfig) + + // Preserve the changes that the test setup just made to the config, so + // they don't get lost when we reload the config while running the test + // (which happens when switching between repos, going in and out of + // submodules, etc). + appConfig.SaveGlobalUserConfig() } common, err := NewCommon(appConfig) diff --git a/pkg/config/app_config.go b/pkg/config/app_config.go index 3460c4b00..e30ff4a26 100644 --- a/pkg/config/app_config.go +++ b/pkg/config/app_config.go @@ -2,6 +2,7 @@ package config import ( "fmt" + "log" "os" "path/filepath" "strings" @@ -361,6 +362,24 @@ func loadAppState() (*AppState, error) { return appState, nil } +// SaveGlobalUserConfig saves the UserConfig back to disk. This is only used in +// integration tests, so we are a bit sloppy with error handling. +func (c *AppConfig) SaveGlobalUserConfig() { + if len(c.userConfigFiles) != 1 { + panic("expected exactly one global user config file") + } + + yamlContent, err := yaml.Marshal(c.userConfig) + if err != nil { + log.Fatalf("error marshalling user config: %v", err) + } + + err = os.WriteFile(c.userConfigFiles[0].Path, yamlContent, 0o644) + if err != nil { + log.Fatalf("error saving user config: %v", err) + } +} + // AppState stores data between runs of the app like when the last update check // was performed and which other repos have been checked out type AppState struct {