golang.org/x/build@v0.0.0-20240506185731-218518f32b70/internal/envutil/envutil_test.go (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package envutil
     6  
     7  import (
     8  	"fmt"
     9  	"reflect"
    10  	"testing"
    11  )
    12  
    13  func TestDedup(t *testing.T) {
    14  	tests := []struct {
    15  		in   []string
    16  		want map[string][]string // keyed by GOOS
    17  	}{
    18  		{
    19  			in: []string{"k1=v1", "k2=v2", "K1=v3"},
    20  			want: map[string][]string{
    21  				"windows": {"k2=v2", "K1=v3"},
    22  				"linux":   {"k1=v1", "k2=v2", "K1=v3"},
    23  			},
    24  		},
    25  		{
    26  			in: []string{"k1=v1", "K1=V2", "k1=v3"},
    27  			want: map[string][]string{
    28  				"windows": {"k1=v3"},
    29  				"linux":   {"K1=V2", "k1=v3"},
    30  			},
    31  		},
    32  	}
    33  	for i, tt := range tests {
    34  		t.Run(fmt.Sprint(i), func(t *testing.T) {
    35  			for goos, want := range tt.want {
    36  				t.Run(goos, func(t *testing.T) {
    37  					got := Dedup(goos, tt.in)
    38  					if !reflect.DeepEqual(got, want) {
    39  						t.Errorf("Dedup(%q, %q) = %q; want %q", goos, tt.in, got, want)
    40  					}
    41  				})
    42  			}
    43  		})
    44  	}
    45  }
    46  
    47  func TestGet(t *testing.T) {
    48  	tests := []struct {
    49  		env  []string
    50  		want map[string]map[string]string // GOOS → key → value
    51  	}{
    52  		{
    53  			env: []string{"k1=v1", "k2=v2", "K1=v3"},
    54  			want: map[string]map[string]string{
    55  				"windows": {"k1": "v3", "k2": "v2", "K1": "v3", "K2": "v2"},
    56  				"linux":   {"k1": "v1", "k2": "v2", "K1": "v3", "K2": ""},
    57  			},
    58  		},
    59  		{
    60  			env: []string{"k1=v1", "K1=V2", "k1=v3"},
    61  			want: map[string]map[string]string{
    62  				"windows": {"k1": "v3", "K1": "v3"},
    63  				"linux":   {"k1": "v3", "K1": "V2"},
    64  			},
    65  		},
    66  	}
    67  
    68  	for i, tt := range tests {
    69  		t.Run(fmt.Sprint(i), func(t *testing.T) {
    70  			for goos, m := range tt.want {
    71  				t.Run(goos, func(t *testing.T) {
    72  					for k, want := range m {
    73  						got := Get(goos, tt.env, k)
    74  						if got != want {
    75  							t.Errorf("Get(%q, %q, %q) = %q; want %q", goos, tt.env, k, got, want)
    76  						}
    77  					}
    78  				})
    79  			}
    80  		})
    81  	}
    82  }