github.com/stevenmatthewt/agent@v3.5.4+incompatible/env/export_test.go (about)

     1  package env
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestFromExportHandlesNewlines(t *testing.T) {
    11  	var lines = []string{
    12  		`declare -x USER="keithpitt"`,
    13  		`declare -x VAR1="boom\nboom\nshake\nthe\nroom"`,
    14  		`declare -x VAR2="hello`,
    15  		`friends"`,
    16  		`declare -x VAR3="hello`,
    17  		`friends`,
    18  		`OMG=foo`,
    19  		`test"`,
    20  		`declare -x SOMETHING="0"`,
    21  		`declare -x VAR4="ends with a space "`,
    22  		`declare -x VAR5="ends with`,
    23  		`another space "`,
    24  		`declare -x VAR6="ends with a quote \"`,
    25  		`and a new line \""`,
    26  		`declare -x _="/usr/local/bin/watch"`,
    27  	}
    28  
    29  	env := FromExport(strings.Join(lines, "\n"))
    30  
    31  	assertEqualEnv(t, `SOMETHING`, `0`, env)
    32  	assertEqualEnv(t, `USER`, `keithpitt`, env)
    33  	assertEqualEnv(t, `VAR1`, "boom\\nboom\\nshake\\nthe\\nroom", env)
    34  	assertEqualEnv(t, `VAR2`, "hello\nfriends", env)
    35  	assertEqualEnv(t, `VAR3`, "hello\nfriends\nOMG=foo\ntest", env)
    36  	assertEqualEnv(t, `VAR4`, `ends with a space `, env)
    37  	assertEqualEnv(t, `VAR5`, "ends with\nanother space ", env)
    38  	assertEqualEnv(t, `VAR6`, "ends with a quote \"\nand a new line \"", env)
    39  	assertEqualEnv(t, `_`, `/usr/local/bin/watch`, env)
    40  }
    41  
    42  func TestFromExportHandlesEscapedCharacters(t *testing.T) {
    43  	var lines = []string{
    44  		`declare -x DOLLARS="i love \$money"`,
    45  		`declare -x WITH_NEW_LINE="i have a \\n new line"`,
    46  		`declare -x CARRIAGE_RETURN="i have a \\r carriage"`,
    47  		`declare -x TOTES="with a \" quote"`,
    48  	}
    49  
    50  	env := FromExport(strings.Join(lines, "\n"))
    51  
    52  	assertEqualEnv(t, `DOLLARS`, `i love $money`, env)
    53  	assertEqualEnv(t, `WITH_NEW_LINE`, `i have a \n new line`, env)
    54  	assertEqualEnv(t, `CARRIAGE_RETURN`, `i have a \r carriage`, env)
    55  	assertEqualEnv(t, `TOTES`, `with a " quote`, env)
    56  }
    57  
    58  func TestFromExportWithVariablesWithoutEquals(t *testing.T) {
    59  	var lines = []string{
    60  		`declare -x THING_TOTES`,
    61  		`declare -x HTTP_PROXY="http://proxy.example.com:1234/"`,
    62  		`declare -x LANG="en_US.UTF-8"`,
    63  		`declare -x LOGNAME="buildkite-agent"`,
    64  		`declare -x SOME_VALUE="this is my value"`,
    65  		`declare -x OLDPWD`,
    66  		`declare -x SOME_OTHER_VALUE="this is my value across`,
    67  		`new`,
    68  		`lines"`,
    69  		`declare -x OLDPWD2`,
    70  		`declare -x GONNA_TRICK_YOU="the next line is a string`,
    71  		`declare -x WITH_A_STRING="I'm a string!!`,
    72  		`"`,
    73  		`declare -x PATH="/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"`,
    74  		`declare -x PWD="/"`,
    75  	}
    76  
    77  	env := FromExport(strings.Join(lines, "\n"))
    78  
    79  	assertEqualEnv(t, "LANG", "en_US.UTF-8", env)
    80  	assertEqualEnv(t, "LOGNAME", `buildkite-agent`, env)
    81  	assertEqualEnv(t, "SOME_VALUE", `this is my value`, env)
    82  	assertEqualEnv(t, "OLDPWD", ``, env)
    83  	assertEqualEnv(t, "SOME_OTHER_VALUE", "this is my value across\nnew\nlines", env)
    84  	assertEqualEnv(t, "OLDPWD2", ``, env)
    85  	assertEqualEnv(t, "GONNA_TRICK_YOU", "the next line is a string\ndeclare -x WITH_A_STRING=\"I'm a string!!\n", env)
    86  	assertEqualEnv(t, "PATH", `/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin`, env)
    87  	assertEqualEnv(t, "PWD", `/`, env)
    88  }
    89  
    90  func TestFromExportWithLeadingAndTrailingWhitespace(t *testing.T) {
    91  	var lines = []string{
    92  		``,
    93  		``,
    94  		``,
    95  		`declare -x DOLLARS="i love \$money"`,
    96  		`declare -x WITH_NEW_LINE="i have a \\n new line"`,
    97  		`declare -x CARRIAGE_RETURN="i have a \\r carriage"`,
    98  		`declare -x TOTES="with a \" quote"`,
    99  		``,
   100  		``,
   101  		``,
   102  		``,
   103  	}
   104  	env := FromExport(strings.Join(lines, "\n"))
   105  
   106  	if env.Length() != 4 {
   107  		t.Fatalf("Expected length of 4, got %d", env.Length())
   108  	}
   109  
   110  	assertEqualEnv(t, `DOLLARS`, "i love $money", env)
   111  	assertEqualEnv(t, `WITH_NEW_LINE`, `i have a \n new line`, env)
   112  	assertEqualEnv(t, `CARRIAGE_RETURN`, `i have a \r carriage`, env)
   113  	assertEqualEnv(t, `TOTES`, `with a " quote`, env)
   114  }
   115  
   116  func TestFromExportJSONInside(t *testing.T) {
   117  	var lines = []string{
   118  		`declare -x FOO="{`,
   119  		`	\"key\": \"test\",`,
   120  		`	\"hello\": [`,
   121  		`	  1,`,
   122  		`	  2,`,
   123  		`	  3`,
   124  		`	]`,
   125  		`  }"`,
   126  	}
   127  
   128  	var expected = []string{
   129  		`{`,
   130  		`	"key": "test",`,
   131  		`	"hello": [`,
   132  		`	  1,`,
   133  		`	  2,`,
   134  		`	  3`,
   135  		`	]`,
   136  		`  }`,
   137  	}
   138  
   139  	env := FromExport(strings.Join(lines, "\n"))
   140  
   141  	assertEqualEnv(t, `FOO`, strings.Join(expected, "\n"), env)
   142  }
   143  
   144  func TestFromExportFromWindows(t *testing.T) {
   145  	var lines = []string{
   146  		`SESSIONNAME=Console`,
   147  		`SystemDrive=C:`,
   148  		`SystemRoot=C:\Windows`,
   149  		`TEMP=C:\Users\IEUser\AppData\Local\Temp`,
   150  		`TMP=C:\Users\IEUser\AppData\Local\Temp`,
   151  		`USERDOMAIN=IE11WIN10`,
   152  	}
   153  
   154  	env := FromExport(strings.Join(lines, "\r\n"))
   155  
   156  	if !assert.Equal(t, env.Length(), 6) {
   157  		t.FailNow()
   158  	}
   159  
   160  	assertEqualEnv(t, `SESSIONNAME`, "Console", env)
   161  	assertEqualEnv(t, `SystemDrive`, "C:", env)
   162  	assertEqualEnv(t, `SystemRoot`, "C:\\Windows", env)
   163  	assertEqualEnv(t, `TEMP`, "C:\\Users\\IEUser\\AppData\\Local\\Temp", env)
   164  	assertEqualEnv(t, `TMP`, "C:\\Users\\IEUser\\AppData\\Local\\Temp", env)
   165  	assertEqualEnv(t, `USERDOMAIN`, "IE11WIN10", env)
   166  }
   167  
   168  func TestFromExportFromWindowsWithLeadingAndTrailingSpaces(t *testing.T) {
   169  	var lines = []string{
   170  		``,
   171  		``,
   172  		``,
   173  		`SESSIONNAME=Console`,
   174  		`SystemDrive=C:`,
   175  		`SystemRoot=C:\Windows`,
   176  		`TEMP=C:\Users\IEUser\AppData\Local\Temp`,
   177  		`TMP=C:\Users\IEUser\AppData\Local\Temp`,
   178  		`USERDOMAIN=IE11WIN10`,
   179  		``,
   180  		``,
   181  		``,
   182  	}
   183  
   184  	env := FromExport(strings.Join(lines, "\r\n"))
   185  
   186  	if !assert.Equal(t, env.Length(), 6) {
   187  		t.FailNow()
   188  	}
   189  
   190  	assertEqualEnv(t, `SESSIONNAME`, "Console", env)
   191  	assertEqualEnv(t, `SystemDrive`, "C:", env)
   192  	assertEqualEnv(t, `SystemRoot`, "C:\\Windows", env)
   193  	assertEqualEnv(t, `TEMP`, "C:\\Users\\IEUser\\AppData\\Local\\Temp", env)
   194  	assertEqualEnv(t, `TMP`, "C:\\Users\\IEUser\\AppData\\Local\\Temp", env)
   195  	assertEqualEnv(t, `USERDOMAIN`, "IE11WIN10", env)
   196  }
   197  
   198  func assertEqualEnv(t *testing.T, key string, expected string, env *Environment) {
   199  	t.Helper()
   200  	v, _ := env.Get(key)
   201  	if !assert.Equal(t, expected, v) {
   202  		t.FailNow()
   203  	}
   204  }