github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/fly/commands/internal/flaghelpers/pipeline_flag_test.go (about) 1 package flaghelpers_test 2 3 import ( 4 "encoding/json" 5 6 . "github.com/onsi/ginkgo" 7 . "github.com/onsi/gomega" 8 9 "github.com/pf-qiu/concourse/v6/atc" 10 "github.com/pf-qiu/concourse/v6/fly/commands/internal/flaghelpers" 11 ) 12 13 var _ = Describe("PipelineFlag", func() { 14 Describe("UnmarshalFlag", func() { 15 16 var pipelineFlag *flaghelpers.PipelineFlag 17 18 BeforeEach(func() { 19 pipelineFlag = &flaghelpers.PipelineFlag{} 20 }) 21 22 for _, tt := range []struct { 23 desc string 24 flag string 25 name string 26 instanceVars atc.InstanceVars 27 err string 28 }{ 29 { 30 desc: "name", 31 flag: "some-pipeline", 32 name: "some-pipeline", 33 }, 34 { 35 desc: "instance var", 36 flag: "some-pipeline/branch:master", 37 name: "some-pipeline", 38 instanceVars: atc.InstanceVars{"branch": "master"}, 39 }, 40 { 41 desc: "multiple instance vars", 42 flag: `some-pipeline/branch:master,list:[1, "2"],other:{foo:bar: 123}`, 43 name: "some-pipeline", 44 instanceVars: atc.InstanceVars{ 45 "branch": "master", 46 "list": []interface{}{json.Number("1"), "2"}, 47 "other": map[string]interface{}{"foo:bar": json.Number("123")}, 48 }, 49 }, 50 { 51 desc: "quoted yaml brackets/braces with \"", 52 flag: `some-pipeline/field1:{a: "{", b: "["},field2:hello`, 53 name: "some-pipeline", 54 instanceVars: atc.InstanceVars{ 55 "field1": map[string]interface{}{ 56 "a": "{", 57 "b": "[", 58 }, 59 "field2": "hello", 60 }, 61 }, 62 { 63 desc: "quoted yaml brackets/braces with '", 64 flag: `some-pipeline/field1:{a: '{', b: '['},field2:hello`, 65 name: "some-pipeline", 66 instanceVars: atc.InstanceVars{ 67 "field1": map[string]interface{}{ 68 "a": "{", 69 "b": "[", 70 }, 71 "field2": "hello", 72 }, 73 }, 74 { 75 desc: "empty values", 76 flag: `some-pipeline/field1:,field2:"",field3:null`, 77 name: "some-pipeline", 78 instanceVars: atc.InstanceVars{ 79 "field1": nil, 80 "field2": "", 81 "field3": nil, 82 }, 83 }, 84 { 85 desc: "yaml list", 86 flag: `some-pipeline/field:[{a: '{', b: '['}, 1]`, 87 name: "some-pipeline", 88 instanceVars: atc.InstanceVars{ 89 "field": []interface{}{ 90 map[string]interface{}{ 91 "a": "{", 92 "b": "[", 93 }, 94 json.Number("1"), 95 }, 96 }, 97 }, 98 { 99 desc: "indexing by numerical field still uses map", 100 flag: `some-pipeline/field.0:0,field.1:1`, 101 name: "some-pipeline", 102 instanceVars: atc.InstanceVars{ 103 "field": map[string]interface{}{ 104 "0": json.Number("0"), 105 "1": json.Number("1"), 106 }, 107 }, 108 }, 109 { 110 desc: "whitespace trimmed from path/values", 111 flag: `some-pipeline/branch: master, other: 123`, 112 name: "some-pipeline", 113 instanceVars: atc.InstanceVars{ 114 "branch": "master", 115 "other": json.Number("123"), 116 }, 117 }, 118 { 119 desc: "quoted fields can contain special characters", 120 flag: `some-pipeline/"some.field:here":abc`, 121 name: "some-pipeline", 122 instanceVars: atc.InstanceVars{ 123 "some.field:here": "abc", 124 }, 125 }, 126 { 127 desc: "special characters in quoted yaml", 128 flag: `some-pipeline/field1:'foo,bar',field2:"value1:value2"`, 129 name: "some-pipeline", 130 instanceVars: atc.InstanceVars{ 131 "field1": "foo,bar", 132 "field2": "value1:value2", 133 }, 134 }, 135 { 136 desc: "supports dot notation", 137 flag: `some-pipeline/"my.field".subkey1."subkey:2":"my-value","my.field".other:'other-value'`, 138 name: "some-pipeline", 139 instanceVars: atc.InstanceVars{ 140 "my.field": map[string]interface{}{ 141 "subkey1": map[string]interface{}{ 142 "subkey:2": "my-value", 143 }, 144 "other": "other-value", 145 }, 146 }, 147 }, 148 { 149 desc: "errors if invalid ref is passed", 150 flag: `some-pipeline/"my.field".:bad`, 151 err: `invalid var '"my.field".': empty field`, 152 }, 153 { 154 desc: "errors if invalid YAML is passed as the value", 155 flag: `some-pipeline/hello:{bad: yaml`, 156 err: `invalid value for key 'hello': error converting YAML to JSON: yaml: line 1: did not find expected ',' or '}'`, 157 }, 158 } { 159 tt := tt 160 It(tt.desc, func() { 161 err := pipelineFlag.UnmarshalFlag(tt.flag) 162 if tt.err == "" { 163 Expect(err).ToNot(HaveOccurred()) 164 Expect(pipelineFlag.Name).To(Equal(tt.name)) 165 Expect(pipelineFlag.InstanceVars).To(Equal(tt.instanceVars)) 166 } else { 167 Expect(err).To(MatchError(tt.err)) 168 } 169 }) 170 } 171 }) 172 })