diff --git a/config/config.go b/config/config.go index e722a5cb..b382d2ef 100644 --- a/config/config.go +++ b/config/config.go @@ -401,15 +401,22 @@ func initConfig(fs *pflag.FlagSet) (cfg *Config, err error) { } func setupConfig(c *Config) { - // To show protofile and protopath field, set slice which has empty string + // To show protofile and protopath field in a config file, set slice which has empty string // if these are nil. (please see default values.) // Conversely, trim the empty string element when config loading. - if (c.Default.ProtoFile == nil) || (len(c.Default.ProtoFile) == 1 && c.Default.ProtoFile[0] == "") { + if c.Default.ProtoFile == nil { c.Default.ProtoFile = []string{} } - if (c.Default.ProtoPath == nil) || (len(c.Default.ProtoPath) == 1 && c.Default.ProtoPath[0] == "") { + if len(c.Default.ProtoFile) >= 1 && c.Default.ProtoFile[0] == "" { + c.Default.ProtoFile = c.Default.ProtoFile[1:] + } + + if c.Default.ProtoPath == nil { c.Default.ProtoPath = []string{} } + if len(c.Default.ProtoPath) >= 1 && c.Default.ProtoPath[0] == "" { + c.Default.ProtoPath = c.Default.ProtoPath[1:] + } } // Edit opens the project local config file with an editor. diff --git a/config/config_test.go b/config/config_test.go index 26941984..55540b7a 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -163,14 +163,14 @@ func TestMain(m *testing.M) { func TestLoad(t *testing.T) { checkValues := func(t *testing.T, c *Config) { - if len(c.Default.ProtoFile) == 1 { + if len(c.Default.ProtoFile) >= 1 { if len(c.Default.ProtoFile[0]) == 0 { - t.Fatalf("Default.ProtoFile must not empty") + t.Fatalf("Default.ProtoFile must not be empty") } } - if len(c.Default.ProtoPath) == 1 { + if len(c.Default.ProtoPath) >= 1 { if len(c.Default.ProtoPath[0]) == 0 { - t.Fatalf("Default.ProtoPath must not empty") + t.Fatalf("Default.ProtoPath must not be empty") } } } @@ -294,6 +294,21 @@ func TestLoad(t *testing.T) { return cfg }) + + assertWithGolden(t, "apply some proto files and paths", func(t *testing.T) *Config { + _, _, cleanup := setupEnv(t) + defer cleanup() + + fs := pflag.NewFlagSet("test", pflag.ExitOnError) + fs.StringSlice("path", []string{"foo", "bar"}, "") + fs.StringSlice("proto", []string{"hoge", "fuga"}, "") + + cfg := mustGet(t, fs) + + checkValues(t, cfg) + + return cfg + }) } func TestEdit(t *testing.T) { diff --git a/config/testdata/fixtures/apply_some_proto_files_and_paths.golden.toml b/config/testdata/fixtures/apply_some_proto_files_and_paths.golden.toml new file mode 100644 index 00000000..c5ba5beb --- /dev/null +++ b/config/testdata/fixtures/apply_some_proto_files_and_paths.golden.toml @@ -0,0 +1,38 @@ + +[default] + package = "" + protofile = [] + protopath = ["foo","bar"] + service = "" + +[log] + prefix = "evans: " + +[meta] + autoupdate = false + configversion = "0.6.10" + updatelevel = "patch" + +[repl] + coloredoutput = true + historysize = 100 + inputpromptformat = "{ancestor}{name} ({type}) => " + promptformat = "{package}.{service}@{addr}:{port}" + silent = false + splashtextpath = "" + +[request] + cacertfile = "" + certfile = "" + certkeyfile = "" + web = false + + [request.header] + grpc-client = ["evans"] + +[server] + host = "127.0.0.1" + name = "" + port = "50051" + reflection = false + tls = false