github.com/hashicorp/packer@v1.14.3/command/flag-kv/flag_json_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package kvflag
     5  
     6  import (
     7  	"flag"
     8  	"path/filepath"
     9  	"reflect"
    10  	"testing"
    11  )
    12  
    13  func TestFlagJSON_impl(t *testing.T) {
    14  	var _ flag.Value = new(FlagJSON)
    15  }
    16  
    17  func TestFlagJSON(t *testing.T) {
    18  	cases := []struct {
    19  		Input   string
    20  		Initial map[string]string
    21  		Output  map[string]string
    22  		Error   bool
    23  	}{
    24  		{
    25  			"basic.json",
    26  			nil,
    27  			map[string]string{"key": "value"},
    28  			false,
    29  		},
    30  
    31  		{
    32  			"basic.json",
    33  			map[string]string{"foo": "bar"},
    34  			map[string]string{"foo": "bar", "key": "value"},
    35  			false,
    36  		},
    37  
    38  		{
    39  			"basic.json",
    40  			map[string]string{"key": "bar"},
    41  			map[string]string{"key": "value"},
    42  			false,
    43  		},
    44  	}
    45  
    46  	for _, tc := range cases {
    47  		f := new(FlagJSON)
    48  		if tc.Initial != nil {
    49  			f = (*FlagJSON)(&tc.Initial)
    50  		}
    51  
    52  		err := f.Set(filepath.Join("./test-fixtures", tc.Input))
    53  		if (err != nil) != tc.Error {
    54  			t.Fatalf("bad error. Input: %#v\n\n%s", tc.Input, err)
    55  		}
    56  
    57  		actual := map[string]string(*f)
    58  		if !reflect.DeepEqual(actual, tc.Output) {
    59  			t.Fatalf("bad: %#v", actual)
    60  		}
    61  	}
    62  }