Black Lives Matter. Support the Equal Justice Initiative.

Source file src/os/env_unix_test.go

Documentation: os

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

View as plain text