Source file src/os/exec/env_test.go

     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  	"slices"
     9  	"testing"
    10  )
    11  
    12  func TestDedupEnv(t *testing.T) {
    13  	t.Parallel()
    14  
    15  	tests := []struct {
    16  		noCase  bool
    17  		nulOK   bool
    18  		in      []string
    19  		want    []string
    20  		wantErr bool
    21  	}{
    22  		{
    23  			noCase: true,
    24  			in:     []string{"k1=v1", "k2=v2", "K1=v3"},
    25  			want:   []string{"k2=v2", "K1=v3"},
    26  		},
    27  		{
    28  			noCase: false,
    29  			in:     []string{"k1=v1", "K1=V2", "k1=v3"},
    30  			want:   []string{"K1=V2", "k1=v3"},
    31  		},
    32  		{
    33  			in:   []string{"=a", "=b", "foo", "bar"},
    34  			want: []string{"=b", "foo", "bar"},
    35  		},
    36  		{
    37  			// #49886: preserve weird Windows keys with leading "=" signs.
    38  			noCase: true,
    39  			in:     []string{`=C:=C:\golang`, `=D:=D:\tmp`, `=D:=D:\`},
    40  			want:   []string{`=C:=C:\golang`, `=D:=D:\`},
    41  		},
    42  		{
    43  			// #52436: preserve invalid key-value entries (for now).
    44  			// (Maybe filter them out or error out on them at some point.)
    45  			in:   []string{"dodgy", "entries"},
    46  			want: []string{"dodgy", "entries"},
    47  		},
    48  		{
    49  			// Filter out entries containing NULs.
    50  			in:      []string{"A=a\x00b", "B=b", "C\x00C=c"},
    51  			want:    []string{"B=b"},
    52  			wantErr: true,
    53  		},
    54  		{
    55  			// Plan 9 needs to preserve environment variables with NUL (#56544).
    56  			nulOK: true,
    57  			in:    []string{"path=one\x00two"},
    58  			want:  []string{"path=one\x00two"},
    59  		},
    60  	}
    61  	for _, tt := range tests {
    62  		got, err := dedupEnvCase(tt.noCase, tt.nulOK, tt.in)
    63  		if !slices.Equal(got, tt.want) || (err != nil) != tt.wantErr {
    64  			t.Errorf("Dedup(%v, %q) = %q, %v; want %q, error:%v", tt.noCase, tt.in, got, err, tt.want, tt.wantErr)
    65  		}
    66  	}
    67  }
    68  

View as plain text