github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/opts/env_test.go (about)

     1  package opts // import "github.com/demonoid81/moby/opts"
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"runtime"
     7  	"testing"
     8  )
     9  
    10  func TestValidateEnv(t *testing.T) {
    11  	testcase := []struct {
    12  		value    string
    13  		expected string
    14  		err      error
    15  	}{
    16  		{
    17  			value:    "a",
    18  			expected: "a",
    19  		},
    20  		{
    21  			value:    "something",
    22  			expected: "something",
    23  		},
    24  		{
    25  			value:    "_=a",
    26  			expected: "_=a",
    27  		},
    28  		{
    29  			value:    "env1=value1",
    30  			expected: "env1=value1",
    31  		},
    32  		{
    33  			value:    "_env1=value1",
    34  			expected: "_env1=value1",
    35  		},
    36  		{
    37  			value:    "env2=value2=value3",
    38  			expected: "env2=value2=value3",
    39  		},
    40  		{
    41  			value:    "env3=abc!qwe",
    42  			expected: "env3=abc!qwe",
    43  		},
    44  		{
    45  			value:    "env_4=value 4",
    46  			expected: "env_4=value 4",
    47  		},
    48  		{
    49  			value:    "PATH",
    50  			expected: fmt.Sprintf("PATH=%v", os.Getenv("PATH")),
    51  		},
    52  		{
    53  			value: "=a",
    54  			err:   fmt.Errorf(fmt.Sprintf("invalid environment variable: %s", "=a")),
    55  		},
    56  		{
    57  			value:    "PATH=something",
    58  			expected: "PATH=something",
    59  		},
    60  		{
    61  			value:    "asd!qwe",
    62  			expected: "asd!qwe",
    63  		},
    64  		{
    65  			value:    "1asd",
    66  			expected: "1asd",
    67  		},
    68  		{
    69  			value:    "123",
    70  			expected: "123",
    71  		},
    72  		{
    73  			value:    "some space",
    74  			expected: "some space",
    75  		},
    76  		{
    77  			value:    "  some space before",
    78  			expected: "  some space before",
    79  		},
    80  		{
    81  			value:    "some space after  ",
    82  			expected: "some space after  ",
    83  		},
    84  		{
    85  			value: "=",
    86  			err:   fmt.Errorf(fmt.Sprintf("invalid environment variable: %s", "=")),
    87  		},
    88  	}
    89  
    90  	// Environment variables are case in-sensitive on Windows
    91  	if runtime.GOOS == "windows" {
    92  		tmp := struct {
    93  			value    string
    94  			expected string
    95  			err      error
    96  		}{
    97  			value:    "PaTh",
    98  			expected: fmt.Sprintf("PaTh=%v", os.Getenv("PATH")),
    99  			err:      nil,
   100  		}
   101  		testcase = append(testcase, tmp)
   102  	}
   103  
   104  	for _, r := range testcase {
   105  		actual, err := ValidateEnv(r.value)
   106  
   107  		if err != nil {
   108  			if r.err == nil {
   109  				t.Fatalf("Expected err is nil, got err[%v]", err)
   110  			}
   111  			if err.Error() != r.err.Error() {
   112  				t.Fatalf("Expected err[%v], got err[%v]", r.err, err)
   113  			}
   114  		}
   115  
   116  		if err == nil && r.err != nil {
   117  			t.Fatalf("Expected err[%v], but err is nil", r.err)
   118  		}
   119  
   120  		if actual != r.expected {
   121  			t.Fatalf("Expected [%v], got [%v]", r.expected, actual)
   122  		}
   123  	}
   124  }