From ddeebcf1725cd09cc4962705b6e35fef20c9d27e Mon Sep 17 00:00:00 2001 From: ktr Date: Sun, 29 Dec 2019 19:13:51 +0900 Subject: [PATCH] fix to parse "=" of a header correctly (#212) --- app/flag.go | 2 +- app/flag_test.go | 42 ++++++++++++++----- ...and_local_config_are_not_found.golden.toml | 2 +- e2e/cli_test.go | 9 ++-- e2e/old_cli_test.go | 8 ++-- meta/meta.go | 2 +- 6 files changed, 41 insertions(+), 24 deletions(-) diff --git a/app/flag.go b/app/flag.go index d56a5688..9e42d036 100644 --- a/app/flag.go +++ b/app/flag.go @@ -111,7 +111,7 @@ func (s *stringToStringSliceValue) Set(val string) error { out := make(map[string][]string, len(ss)) for _, pair := range ss { - kv := strings.Split(pair, "=") + kv := strings.SplitN(pair, "=", 2) if len(kv) != 2 { return fmt.Errorf("%s must be formatted as key=value", pair) } diff --git a/app/flag_test.go b/app/flag_test.go index f43daa0c..850fc5c0 100644 --- a/app/flag_test.go +++ b/app/flag_test.go @@ -5,16 +5,38 @@ import ( ) func Test_stringToStringSliceValue(t *testing.T) { - m := make(map[string][]string) - v := newStringToStringValue(map[string][]string{ - "ogiso": []string{"setsuna"}, - }, &m) - if err := v.Set("touma=kazusa,touma=youko"); err != nil { - t.Fatalf("Set must not return an error, but got '%s'", err) + cases := []struct { + in string + expected string + hasErr bool + }{ + {in: "touma=kazusa,touma=youko", expected: `["touma=kazusa,youko"]`, hasErr: false}, + {in: "sawamura='spencer=eriri'", expected: `[sawamura='spencer=eriri']`, hasErr: false}, + {in: "sawamura=spencer=eriri", expected: `[sawamura=spencer=eriri]`, hasErr: false}, + {in: "megumi=kato", expected: `[megumi=kato]`, hasErr: false}, + {in: "yuki=asuna,alice", expected: `["yuki=asuna,alice"]`, hasErr: false}, + {in: "alice", hasErr: true}, } - const expected = `["touma=kazusa,youko"]` - actual := v.String() - if expected != actual { - t.Errorf("expected '%s', but got '%s'", expected, actual) + for _, c := range cases { + c := c + t.Run(c.in, func(t *testing.T) { + m := make(map[string][]string) + v := newStringToStringValue(map[string][]string{ + "ogiso": []string{"setsuna"}, + }, &m) + err := v.Set(c.in) + if c.hasErr { + if err == nil { + t.Fatalf("Set must return an error, but got nil") + } + return + } else if err != nil { + t.Fatalf("Set must not return an error, but got '%s'", err) + } + actual := v.String() + if c.expected != actual { + t.Errorf("expected '%s', but got '%s'", c.expected, actual) + } + }) } } diff --git a/config/testdata/fixtures/create_a_default_global_config_if_both_of_global_and_local_config_are_not_found.golden.toml b/config/testdata/fixtures/create_a_default_global_config_if_both_of_global_and_local_config_are_not_found.golden.toml index fb6aa0dd..385ea06a 100644 --- a/config/testdata/fixtures/create_a_default_global_config_if_both_of_global_and_local_config_are_not_found.golden.toml +++ b/config/testdata/fixtures/create_a_default_global_config_if_both_of_global_and_local_config_are_not_found.golden.toml @@ -10,7 +10,7 @@ [meta] autoupdate = false - configversion = "0.8.4" + configversion = "0.8.5" updatelevel = "patch" [repl] diff --git a/e2e/cli_test.go b/e2e/cli_test.go index 49ad2d82..b11d7fb9 100644 --- a/e2e/cli_test.go +++ b/e2e/cli_test.go @@ -129,11 +129,6 @@ func TestE2E_CLI(t *testing.T) { args: "--call Unary --file testdata/unary_call.in testdata/test.proto", expectedCode: 1, }, - "cannot launch CLI mode because --header has an invalid form": { - commonFlags: "--header foo=bar=baz --package api --service Example", - args: "--call Unary --file testdata/unary_call.in testdata/test.proto", - expectedCode: 1, - }, "call unary RPC with an input file by CLI mode": { commonFlags: "--package api --service Example", args: "--call Unary --file testdata/unary_call.in testdata/test.proto", @@ -188,7 +183,7 @@ func TestE2E_CLI(t *testing.T) { }, }, "call unary RPC with an input file and custom headers by CLI mode": { - commonFlags: "--header ogiso=setsuna --header touma=kazusa,youko --package api --service Example", + commonFlags: "--header ogiso=setsuna --header touma=kazusa,youko --header sound=of=destiny --package api --service Example", args: "--call UnaryHeader --file testdata/unary_header.in testdata/test.proto", assertTest: func(t *testing.T, output string) { expectedStrings := []string{ @@ -196,6 +191,8 @@ func TestE2E_CLI(t *testing.T) { "val = setsuna", "key = touma", "val = kazusa, youko", + "key = sound", + "val = of=destiny", } for _, s := range expectedStrings { if !strings.Contains(output, s) { diff --git a/e2e/old_cli_test.go b/e2e/old_cli_test.go index a1610edc..f4acf7be 100644 --- a/e2e/old_cli_test.go +++ b/e2e/old_cli_test.go @@ -110,10 +110,6 @@ func TestE2E_OldCLI(t *testing.T) { args: "--header foo --package api --service Example --call Unary --file testdata/unary_call.in testdata/test.proto", expectedCode: 1, }, - "cannot launch CLI mode because --header has an invalid form": { - args: "--header foo=bar=baz --package api --service Example --call Unary --file testdata/unary_call.in testdata/test.proto", - expectedCode: 1, - }, "call unary RPC with an input file by CLI mode": { args: "--package api --service Example --call Unary --file testdata/unary_call.in testdata/test.proto", expectedOut: `{ "message": "hello, oumae" }`, @@ -162,13 +158,15 @@ func TestE2E_OldCLI(t *testing.T) { }, }, "call unary RPC with an input file and custom headers by CLI mode": { - args: "--header ogiso=setsuna --header touma=kazusa,youko --package api --service Example --call UnaryHeader --file testdata/unary_header.in testdata/test.proto", + args: "--header ogiso=setsuna --header touma=kazusa,youko --header sound=of=destiny --package api --service Example --call UnaryHeader --file testdata/unary_header.in testdata/test.proto", assertTest: func(t *testing.T, output string) { expectedStrings := []string{ "key = ogiso", "val = setsuna", "key = touma", "val = kazusa, youko", + "key = sound", + "val = of=destiny", } for _, s := range expectedStrings { if !strings.Contains(output, s) { diff --git a/meta/meta.go b/meta/meta.go index b312cd2f..2c3c79e4 100644 --- a/meta/meta.go +++ b/meta/meta.go @@ -5,5 +5,5 @@ import version "github.com/hashicorp/go-version" const AppName = "evans" var ( - Version = version.Must(version.NewSemver("0.8.4")) + Version = version.Must(version.NewSemver("0.8.5")) )