From 483f7f81771d15be2ee34b67812361cdbf53dd7d Mon Sep 17 00:00:00 2001 From: Victor Farazdagi Date: Thu, 22 Oct 2020 08:20:56 +0300 Subject: [PATCH] Fix InitWithReset - rely on previously updated flags (#7598) * Fix initWithReset to use previously set configuration * further cleanup Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com> --- shared/featureconfig/config.go | 8 +++++++- shared/featureconfig/config_test.go | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/shared/featureconfig/config.go b/shared/featureconfig/config.go index de781cb91..d8ce81237 100644 --- a/shared/featureconfig/config.go +++ b/shared/featureconfig/config.go @@ -94,8 +94,14 @@ func Init(c *Flags) { // InitWithReset sets the global config and returns function that is used to reset configuration. func InitWithReset(c *Flags) func() { + var prevConfig Flags + if featureConfig != nil { + prevConfig = *featureConfig + } else { + prevConfig = Flags{} + } resetFunc := func() { - Init(&Flags{}) + Init(&prevConfig) } Init(c) return resetFunc diff --git a/shared/featureconfig/config_test.go b/shared/featureconfig/config_test.go index e8909f60e..699d9efb7 100644 --- a/shared/featureconfig/config_test.go +++ b/shared/featureconfig/config_test.go @@ -9,6 +9,7 @@ import ( ) func TestInitFeatureConfig(t *testing.T) { + defer Init(&Flags{}) cfg := &Flags{ MedallaTestnet: true, } @@ -21,6 +22,27 @@ func TestInitFeatureConfig(t *testing.T) { Init(cfg) } +func TestInitWithReset(t *testing.T) { + defer Init(&Flags{}) + Init(&Flags{ + OnyxTestnet: true, + }) + assert.Equal(t, false, Get().AltonaTestnet) + assert.Equal(t, true, Get().OnyxTestnet) + + // Overwrite previously set value (value that didn't come by default). + resetCfg := InitWithReset(&Flags{ + OnyxTestnet: false, + }) + assert.Equal(t, false, Get().AltonaTestnet) + assert.Equal(t, false, Get().OnyxTestnet) + + // Reset must get to previously set configuration (not to default config values). + resetCfg() + assert.Equal(t, false, Get().AltonaTestnet) + assert.Equal(t, true, Get().OnyxTestnet) +} + func TestConfigureBeaconConfig(t *testing.T) { app := cli.App{} set := flag.NewFlagSet("test", 0)