github.com/lovishpuri/go-40569/src@v0.0.0-20230519171745-f8623e7c56cf/os/env_unix_test.go (about)

     1  // Copyright 2013 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  //go:build unix
     6  
     7  package os_test
     8  
     9  import (
    10  	"fmt"
    11  	. "os"
    12  	"testing"
    13  )
    14  
    15  var setenvEinvalTests = []struct {
    16  	k, v string
    17  }{
    18  	{"", ""},      // empty key
    19  	{"k=v", ""},   // '=' in key
    20  	{"\x00", ""},  // '\x00' in key
    21  	{"k", "\x00"}, // '\x00' in value
    22  }
    23  
    24  func TestSetenvUnixEinval(t *testing.T) {
    25  	for _, tt := range setenvEinvalTests {
    26  		err := Setenv(tt.k, tt.v)
    27  		if err == nil {
    28  			t.Errorf(`Setenv(%q, %q) == nil, want error`, tt.k, tt.v)
    29  		}
    30  	}
    31  }
    32  
    33  var shellSpecialVarTests = []struct {
    34  	k, v string
    35  }{
    36  	{"*", "asterisk"},
    37  	{"#", "pound"},
    38  	{"$", "dollar"},
    39  	{"@", "at"},
    40  	{"!", "exclamation mark"},
    41  	{"?", "question mark"},
    42  	{"-", "dash"},
    43  }
    44  
    45  func TestExpandEnvShellSpecialVar(t *testing.T) {
    46  	for _, tt := range shellSpecialVarTests {
    47  		Setenv(tt.k, tt.v)
    48  		defer Unsetenv(tt.k)
    49  
    50  		argRaw := fmt.Sprintf("$%s", tt.k)
    51  		argWithBrace := fmt.Sprintf("${%s}", tt.k)
    52  		if gotRaw, gotBrace := ExpandEnv(argRaw), ExpandEnv(argWithBrace); gotRaw != gotBrace {
    53  			t.Errorf("ExpandEnv(%q) = %q, ExpandEnv(%q) = %q; expect them to be equal", argRaw, gotRaw, argWithBrace, gotBrace)
    54  		}
    55  	}
    56  }