go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/buildbucket/cli/prop_test.go (about)

     1  // Copyright 2019 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cli
    16  
    17  import (
    18  	"io/ioutil"
    19  	"testing"
    20  
    21  	"google.golang.org/protobuf/types/known/structpb"
    22  
    23  	. "github.com/smartystreets/goconvey/convey"
    24  
    25  	. "go.chromium.org/luci/common/testing/assertions"
    26  )
    27  
    28  func mustStruct(data map[string]any) *structpb.Struct {
    29  	ret, err := structpb.NewStruct(data)
    30  	if err != nil {
    31  		panic(err)
    32  	}
    33  	return ret
    34  }
    35  
    36  func TestPropertiesFlag(t *testing.T) {
    37  	t.Parallel()
    38  
    39  	Convey("PropertieFlag", t, func() {
    40  		props := &structpb.Struct{}
    41  		f := PropertiesFlag(props)
    42  
    43  		Convey("File", func() {
    44  			file, err := ioutil.TempFile("", "")
    45  			So(err, ShouldBeNil)
    46  			defer file.Close()
    47  
    48  			_, err = file.WriteString(`{
    49  				"in-file-1": "orig",
    50  				"in-file-2": "orig"
    51  			}`)
    52  			So(err, ShouldBeNil)
    53  
    54  			So(f.Set("@"+file.Name()), ShouldBeNil)
    55  
    56  			So(props, ShouldResembleProto, mustStruct(map[string]any{
    57  				"in-file-1": "orig",
    58  				"in-file-2": "orig",
    59  			}))
    60  
    61  			Convey("Override", func() {
    62  				So(f.Set("in-file-2=override"), ShouldBeNil)
    63  				So(props, ShouldResembleProto, mustStruct(map[string]any{
    64  					"in-file-1": "orig",
    65  					"in-file-2": "override",
    66  				}))
    67  
    68  				So(f.Set("a=b"), ShouldBeNil)
    69  				So(props, ShouldResembleProto, mustStruct(map[string]any{
    70  					"in-file-1": "orig",
    71  					"in-file-2": "override",
    72  					"a":         "b",
    73  				}))
    74  			})
    75  		})
    76  
    77  		Convey("Name=Value", func() {
    78  			So(f.Set("foo=bar"), ShouldBeNil)
    79  			So(props, ShouldResembleProto, mustStruct(map[string]any{
    80  				"foo": "bar",
    81  			}))
    82  
    83  			Convey("JSON", func() {
    84  				So(f.Set("array=[1]"), ShouldBeNil)
    85  				So(props, ShouldResembleProto, mustStruct(map[string]any{
    86  					"foo":   "bar",
    87  					"array": []any{1},
    88  				}))
    89  			})
    90  
    91  			Convey("Trims spaces", func() {
    92  				So(f.Set("array = [1]"), ShouldBeNil)
    93  				So(props, ShouldResembleProto, mustStruct(map[string]any{
    94  					"foo":   "bar",
    95  					"array": []any{1},
    96  				}))
    97  			})
    98  
    99  			Convey("Dup", func() {
   100  				So(f.Set("foo=bar"), ShouldErrLike, `duplicate property "foo`)
   101  			})
   102  		})
   103  	})
   104  }