github.com/tcncloud/wollemi@v0.8.1/ports/wollemi/config_test.go (about)

     1  package wollemi_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/tcncloud/wollemi/domain/optional"
    10  	"github.com/tcncloud/wollemi/ports/wollemi"
    11  )
    12  
    13  func TestConfig_UnmarshalJSON(t *testing.T) {
    14  	for _, tt := range []struct {
    15  		Title string
    16  		Data  string
    17  		Want  wollemi.Config
    18  	}{{
    19  		Title: "unmarshals simple json config",
    20  		Want: wollemi.Config{
    21  			DefaultVisibility:         "//project/service/routes/...",
    22  			AllowUnresolvedDependency: optional.BoolValue(true),
    23  			ExplicitSources:           optional.BoolValue(true),
    24  			KnownDependency: map[string]string{
    25  				"github.com/olivere/elastic": "//third_party/go/github.com/olivere/elastic:v7",
    26  			},
    27  			Gofmt: wollemi.Gofmt{
    28  				Rewrite: wollemi.Bool(true),
    29  				Create:  []string{"go_library", "go_test"},
    30  				Manage:  []string{"go_binary", "go_test"},
    31  				Mapped: map[string]string{
    32  					"go_binary":  "go_custom_binary",
    33  					"go_library": "go_library",
    34  					"go_test":    "go_custom_test",
    35  				},
    36  			},
    37  		},
    38  		Data: `{
    39        "default_visibility": "//project/service/routes/...",
    40        "allow_unresolved_dependency": true,
    41        "explicit_sources": true,
    42        "known_dependency": {
    43          "github.com/olivere/elastic": "//third_party/go/github.com/olivere/elastic:v7"
    44        },
    45        "gofmt": {
    46          "rewrite": true,
    47          "create": ["go_library", "go_test"],
    48          "manage": ["go_binary", "go_test"],
    49          "mapped": {
    50            "go_binary": "go_custom_binary",
    51            "go_test": "go_custom_test"
    52          }
    53        }
    54      }`,
    55  	}, {
    56  		Title: "unmarshals json config when gofmt create set to on",
    57  		Data:  `{"gofmt":{"create":"on"}}`,
    58  		Want: wollemi.Config{
    59  			Gofmt: wollemi.Gofmt{
    60  				Create: []string{"go_binary", "go_library", "go_test"},
    61  			},
    62  		},
    63  	}, {
    64  		Title: "unmarshals json config when gofmt create set to default",
    65  		Data:  `{"gofmt":{"create":"default"}}`,
    66  		Want: wollemi.Config{
    67  			Gofmt: wollemi.Gofmt{
    68  				Create: []string{"go_binary", "go_library", "go_test"},
    69  			},
    70  		},
    71  	}, {
    72  		Title: "unmarshals json config when gofmt create set to off",
    73  		Data:  `{"gofmt":{"create":"off"}}`,
    74  		Want: wollemi.Config{
    75  			Gofmt: wollemi.Gofmt{
    76  				Create: []string{},
    77  			},
    78  		},
    79  	}, {
    80  		Title: "unmarshals json config when gofmt manage set to on",
    81  		Data:  `{"gofmt":{"manage":"on"}}`,
    82  		Want: wollemi.Config{
    83  			Gofmt: wollemi.Gofmt{
    84  				Manage: []string{"go_binary", "go_library", "go_test"},
    85  			},
    86  		},
    87  	}, {
    88  		Title: "unmarshals json config when gofmt manage set to default",
    89  		Data:  `{"gofmt":{"manage":"default"}}`,
    90  		Want: wollemi.Config{
    91  			Gofmt: wollemi.Gofmt{
    92  				Manage: []string{"go_binary", "go_library", "go_test"},
    93  			},
    94  		},
    95  	}, {
    96  		Title: "unmarshals json config when gofmt manage set to off",
    97  		Data:  `{"gofmt":{"manage":"off"}}`,
    98  		Want: wollemi.Config{
    99  			Gofmt: wollemi.Gofmt{
   100  				Manage: []string{},
   101  			},
   102  		},
   103  	}, {
   104  		Title: "unmarshals json config when gofmt manage contains default",
   105  		Data:  `{"gofmt":{"manage":["default", "go_custom_binary"]}}`,
   106  		Want: wollemi.Config{
   107  			Gofmt: wollemi.Gofmt{
   108  				Manage: []string{"go_binary", "go_library", "go_test", "go_custom_binary"},
   109  			},
   110  		},
   111  	}, {
   112  		Title: "unmarshals json config when gofmt mapped set to none",
   113  		Data:  `{"gofmt":{"mapped":"none"}}`,
   114  		Want: wollemi.Config{
   115  			Gofmt: wollemi.Gofmt{
   116  				Mapped: map[string]string{
   117  					"go_binary":  "go_binary",
   118  					"go_library": "go_library",
   119  					"go_test":    "go_test",
   120  				},
   121  			},
   122  		},
   123  	}} {
   124  		t.Run(tt.Title, func(t *testing.T) {
   125  			have := wollemi.Config{}
   126  
   127  			err := json.Unmarshal([]byte(tt.Data), &have)
   128  			require.NoError(t, err)
   129  
   130  			require.Equal(t, tt.Want, have)
   131  		})
   132  	}
   133  }
   134  
   135  func TestConfig_Merge(t *testing.T) {
   136  	for _, tt := range []struct {
   137  		Name string
   138  		Lhs  wollemi.Config
   139  		Rhs  wollemi.Config
   140  		Want wollemi.Config
   141  	}{{
   142  		Name: "merged config is lhs when rhs empty",
   143  		Lhs: wollemi.Config{
   144  			DefaultVisibility:         "PUBLIC",
   145  			AllowUnresolvedDependency: optional.BoolValue(true),
   146  			KnownDependency: map[string]string{
   147  				"aaa": "bbb",
   148  				"ccc": "ddd",
   149  				"eee": "fff",
   150  			},
   151  		},
   152  		Want: wollemi.Config{
   153  			DefaultVisibility:         "PUBLIC",
   154  			AllowUnresolvedDependency: optional.BoolValue(true),
   155  			KnownDependency: map[string]string{
   156  				"aaa": "bbb",
   157  				"ccc": "ddd",
   158  				"eee": "fff",
   159  			},
   160  		},
   161  	}, {
   162  		Name: "merged config is rhs when lhs empty",
   163  		Rhs: wollemi.Config{
   164  			DefaultVisibility:         "PUBLIC",
   165  			AllowUnresolvedDependency: optional.BoolValue(true),
   166  			KnownDependency: map[string]string{
   167  				"aaa": "bbb",
   168  				"ccc": "ddd",
   169  				"eee": "fff",
   170  			},
   171  		},
   172  		Want: wollemi.Config{
   173  			DefaultVisibility:         "PUBLIC",
   174  			AllowUnresolvedDependency: optional.BoolValue(true),
   175  			KnownDependency: map[string]string{
   176  				"aaa": "bbb",
   177  				"ccc": "ddd",
   178  				"eee": "fff",
   179  			},
   180  		},
   181  	}, {
   182  		Name: "merged default_visibility is lhs when rhs empty",
   183  		Lhs: wollemi.Config{
   184  			DefaultVisibility: "PUBLIC",
   185  		},
   186  		Rhs: wollemi.Config{},
   187  		Want: wollemi.Config{
   188  			DefaultVisibility: "PUBLIC",
   189  		},
   190  	}, {
   191  		Name: "merged default_visibility is rhs when rhs non empty",
   192  		Lhs: wollemi.Config{
   193  			DefaultVisibility: "PUBLIC",
   194  		},
   195  		Rhs: wollemi.Config{
   196  			DefaultVisibility: "//app/...",
   197  		},
   198  		Want: wollemi.Config{
   199  			DefaultVisibility: "//app/...",
   200  		},
   201  	}, {
   202  		Name: "merged allow_unresolved_dependency is rhs when rhs set",
   203  		Lhs:  wollemi.Config{},
   204  		Rhs: wollemi.Config{
   205  			AllowUnresolvedDependency: optional.BoolValue(true),
   206  		},
   207  		Want: wollemi.Config{
   208  			AllowUnresolvedDependency: optional.BoolValue(true),
   209  		},
   210  	}, {
   211  		Name: "merged allow_unresolved_dependency is lhs when rhs unset",
   212  		Lhs: wollemi.Config{
   213  			AllowUnresolvedDependency: optional.BoolValue(true),
   214  		},
   215  		Rhs: wollemi.Config{},
   216  		Want: wollemi.Config{
   217  			AllowUnresolvedDependency: optional.BoolValue(true),
   218  		},
   219  	}, {
   220  		Name: "merged known_dependency is all key values from rhs applied to lhs",
   221  		Lhs: wollemi.Config{
   222  			KnownDependency: map[string]string{
   223  				"aaa": "bbb",
   224  				"ccc": "zzz",
   225  			},
   226  		},
   227  		Rhs: wollemi.Config{
   228  			KnownDependency: map[string]string{
   229  				"ccc": "ddd",
   230  				"eee": "fff",
   231  			},
   232  		},
   233  		Want: wollemi.Config{
   234  			KnownDependency: map[string]string{
   235  				"aaa": "bbb",
   236  				"ccc": "ddd",
   237  				"eee": "fff",
   238  			},
   239  		},
   240  	}, {
   241  		Name: "merged gofmt mapped is lhs when rhs is null",
   242  		Lhs: wollemi.Config{
   243  			Gofmt: wollemi.Gofmt{
   244  				Mapped: map[string]string{
   245  					"go_library": "go_library",
   246  				},
   247  			},
   248  		},
   249  		Rhs: wollemi.Config{},
   250  		Want: wollemi.Config{
   251  			Gofmt: wollemi.Gofmt{
   252  				Mapped: map[string]string{
   253  					"go_library": "go_library",
   254  				},
   255  			},
   256  		},
   257  	}, {
   258  		Name: "merged gofmt mapped is rhs when rhs is non null",
   259  		Lhs: wollemi.Config{
   260  			Gofmt: wollemi.Gofmt{
   261  				Mapped: map[string]string{
   262  					"go_library": "go_library",
   263  				},
   264  			},
   265  		},
   266  		Rhs: wollemi.Config{
   267  			Gofmt: wollemi.Gofmt{
   268  				Mapped: map[string]string{
   269  					"go_binary": "go_binary",
   270  				},
   271  			},
   272  		},
   273  		Want: wollemi.Config{
   274  			Gofmt: wollemi.Gofmt{
   275  				Mapped: map[string]string{
   276  					"go_binary": "go_binary",
   277  				},
   278  			},
   279  		},
   280  	}} {
   281  		t.Run(tt.Name, func(t *testing.T) {
   282  			require.Equal(t, tt.Want, tt.Lhs.Merge(tt.Rhs))
   283  		})
   284  	}
   285  }