-
Notifications
You must be signed in to change notification settings - Fork 973
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix core config flag parsing where, in specific circumstances, the va…
…lues from config.toml would be overwritten by defaults if any core.ip was provided as a cli flag
- Loading branch information
Showing
4 changed files
with
161 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package core | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestParseFlags(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
args []string | ||
inputCfg Config // config that could be read from ctx | ||
expectedCfg Config | ||
expectError bool | ||
}{ | ||
{ | ||
name: "no flags", | ||
args: []string{}, | ||
inputCfg: Config{}, | ||
expectedCfg: Config{ | ||
IP: "", | ||
RPCPort: "", | ||
GRPCPort: "", | ||
}, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "only core.ip", | ||
args: []string{"--core.ip=127.0.0.1"}, | ||
inputCfg: Config{ | ||
RPCPort: DefaultRPCPort, | ||
GRPCPort: DefaultGRPCPort, | ||
}, | ||
expectedCfg: Config{ | ||
IP: "127.0.0.1", | ||
RPCPort: DefaultRPCPort, | ||
GRPCPort: DefaultGRPCPort, | ||
}, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "no flags, values from input config.toml ", | ||
args: []string{}, | ||
inputCfg: Config{ | ||
IP: "127.162.36.1", | ||
RPCPort: "1234", | ||
GRPCPort: "5678", | ||
}, | ||
expectedCfg: Config{ | ||
IP: "127.162.36.1", | ||
RPCPort: "1234", | ||
GRPCPort: "5678", | ||
}, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "only core.ip, with config.toml overridden defaults for ports", | ||
args: []string{"--core.ip=127.0.0.1"}, | ||
inputCfg: Config{ | ||
RPCPort: "1234", | ||
GRPCPort: "5678", | ||
}, | ||
expectedCfg: Config{ | ||
IP: "127.0.0.1", | ||
RPCPort: "1234", | ||
GRPCPort: "5678", | ||
}, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "core.ip and core.rpc.port", | ||
args: []string{"--core.ip=127.0.0.1", "--core.rpc.port=12345"}, | ||
inputCfg: Config{ | ||
RPCPort: DefaultRPCPort, | ||
GRPCPort: DefaultGRPCPort, | ||
}, | ||
expectedCfg: Config{ | ||
IP: "127.0.0.1", | ||
RPCPort: "12345", | ||
GRPCPort: DefaultGRPCPort, | ||
}, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "core.ip and core.grpc.port", | ||
args: []string{"--core.ip=127.0.0.1", "--core.grpc.port=54321"}, | ||
inputCfg: Config{ | ||
RPCPort: DefaultRPCPort, | ||
GRPCPort: DefaultGRPCPort, | ||
}, | ||
expectedCfg: Config{ | ||
IP: "127.0.0.1", | ||
RPCPort: DefaultRPCPort, | ||
GRPCPort: "54321", | ||
}, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "core.ip, core.rpc.port, and core.grpc.port", | ||
args: []string{"--core.ip=127.0.0.1", "--core.rpc.port=12345", "--core.grpc.port=54321"}, | ||
expectedCfg: Config{ | ||
IP: "127.0.0.1", | ||
RPCPort: "12345", | ||
GRPCPort: "54321", | ||
}, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "core.rpc.port without core.ip", | ||
args: []string{"--core.rpc.port=12345"}, | ||
expectedCfg: Config{}, | ||
expectError: true, | ||
}, | ||
{ | ||
name: "core.grpc.port without core.ip", | ||
args: []string{"--core.grpc.port=54321"}, | ||
expectedCfg: Config{}, | ||
expectError: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
cmd := &cobra.Command{} | ||
flags := Flags() | ||
cmd.Flags().AddFlagSet(flags) | ||
cmd.SetArgs(tt.args) | ||
|
||
err := cmd.Execute() | ||
require.NoError(t, err) | ||
|
||
err = ParseFlags(cmd, &tt.inputCfg) | ||
if tt.expectError { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
require.Equal(t, tt.expectedCfg, tt.inputCfg) | ||
} | ||
}) | ||
} | ||
} |