140251882e
* fix bug for setting flag only values in process setup when the code was changed to directly load values into the config structs, it was missed that some configuration is only defined through flags, but can be loaded from config files still. so, we need to propogate the settings to the flag only values. * add test for setting propagation * fix linting error
47 lines
1.0 KiB
Go
47 lines
1.0 KiB
Go
// Copyright (C) 2019 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package process
|
|
|
|
import (
|
|
"flag"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func setenv(key, value string) func() {
|
|
old := os.Getenv(key)
|
|
_ = os.Setenv(key, value)
|
|
return func() { _ = os.Setenv(key, old) }
|
|
}
|
|
|
|
func TestExec_PropagatesSettings(t *testing.T) {
|
|
// Set up a command that does nothing.
|
|
cmd := &cobra.Command{RunE: func(cmd *cobra.Command, args []string) error { return nil }}
|
|
|
|
// Define a config struct and some flags.
|
|
var config struct {
|
|
X int `default:"0"`
|
|
}
|
|
Bind(cmd, &config)
|
|
y := cmd.Flags().Int("y", 0, "y flag (command)")
|
|
z := flag.Int("z", 0, "z flag (stdlib)")
|
|
|
|
// Set some environment variables for viper.
|
|
defer setenv("STORJ_X", "1")()
|
|
defer setenv("STORJ_Y", "2")()
|
|
defer setenv("STORJ_Z", "3")()
|
|
|
|
// Run the command through the exec call.
|
|
Exec(cmd)
|
|
|
|
// Check that the variables are now bound.
|
|
require.Equal(t, 1, config.X)
|
|
require.Equal(t, 2, *y)
|
|
require.Equal(t, 3, *z)
|
|
}
|