github.com/mtsmfm/go/src@v0.0.0-20221020090648-44bdcb9f8fde/os/exec/env_test.go (about)

     1  // Copyright 2017 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 exec
     6  
     7  import (
     8  	"reflect"
     9  	"testing"
    10  )
    11  
    12  func TestDedupEnv(t *testing.T) {
    13  	t.Parallel()
    14  
    15  	tests := []struct {
    16  		noCase bool
    17  		in     []string
    18  		want   []string
    19  	}{
    20  		{
    21  			noCase: true,
    22  			in:     []string{"k1=v1", "k2=v2", "K1=v3"},
    23  			want:   []string{"k2=v2", "K1=v3"},
    24  		},
    25  		{
    26  			noCase: false,
    27  			in:     []string{"k1=v1", "K1=V2", "k1=v3"},
    28  			want:   []string{"K1=V2", "k1=v3"},
    29  		},
    30  		{
    31  			in:   []string{"=a", "=b", "foo", "bar"},
    32  			want: []string{"=b", "foo", "bar"},
    33  		},
    34  		{
    35  			// #49886: preserve weird Windows keys with leading "=" signs.
    36  			noCase: true,
    37  			in:     []string{`=C:=C:\golang`, `=D:=D:\tmp`, `=D:=D:\`},
    38  			want:   []string{`=C:=C:\golang`, `=D:=D:\`},
    39  		},
    40  		{
    41  			// #52436: preserve invalid key-value entries (for now).
    42  			// (Maybe filter them out or error out on them at some point.)
    43  			in:   []string{"dodgy", "entries"},
    44  			want: []string{"dodgy", "entries"},
    45  		},
    46  	}
    47  	for _, tt := range tests {
    48  		got := dedupEnvCase(tt.noCase, tt.in)
    49  		if !reflect.DeepEqual(got, tt.want) {
    50  			t.Errorf("Dedup(%v, %q) = %q; want %q", tt.noCase, tt.in, got, tt.want)
    51  		}
    52  	}
    53  }