test_environ.vim (2579B)
1 " Test for environment variables. 2 3 scriptencoding utf-8 4 5 source check.vim 6 7 func Test_environ() 8 unlet! $TESTENV 9 call assert_equal(0, has_key(environ(), 'TESTENV')) 10 let $TESTENV = 'foo' 11 call assert_equal(1, has_key(environ(), 'TESTENV')) 12 let $TESTENV = 'こんにちわ' 13 call assert_equal('こんにちわ', environ()['TESTENV']) 14 endfunc 15 16 func Test_getenv() 17 unlet! $TESTENV 18 call assert_equal(v:null, 'TESTENV'->getenv()) 19 let $TESTENV = 'foo' 20 call assert_equal('foo', getenv('TESTENV')) 21 endfunc 22 23 func Test_setenv() 24 unlet! $TESTENV 25 eval 'foo'->setenv('TEST ENV') 26 call assert_equal('foo', getenv('TEST ENV')) 27 call setenv('TEST ENV', v:null) 28 call assert_equal(v:null, getenv('TEST ENV')) 29 endfunc 30 31 func Test_special_env() 32 " The value for $HOME is cached internally by Vim, ensure the value is up to 33 " date. 34 let orig_ENV = $HOME 35 36 let $HOME = 'foo' 37 call assert_equal('foo', expand('~')) 38 " old $HOME value is kept until a new one is set 39 unlet $HOME 40 call assert_equal('foo', expand('~')) 41 42 call setenv('HOME', 'bar') 43 call assert_equal('bar', expand('~')) 44 " old $HOME value is kept until a new one is set 45 call setenv('HOME', v:null) 46 call assert_equal('bar', expand('~')) 47 48 let $HOME = orig_ENV 49 endfunc 50 51 func Test_external_env() 52 call setenv('FOO', 'HelloWorld') 53 if has('win32') 54 let result = system('echo %FOO%') 55 else 56 let result = system('echo $FOO') 57 endif 58 let result = substitute(result, '[ \r\n]', '', 'g') 59 call assert_equal('HelloWorld', result) 60 61 call setenv('FOO', v:null) 62 if has('win32') 63 let result = system('set | findstr "^FOO="') 64 else 65 let result = system('env | grep ^FOO=') 66 endif 67 call assert_equal('', result) 68 endfunc 69 70 func Test_mac_locale() 71 CheckFeature osxdarwin 72 73 " If $LANG is not set then the system locale will be used. 74 " Run Vim after unsetting all the locale environmental vars, and capture the 75 " output of :lang. 76 let lang_results = system("unset LANG; unset LC_MESSAGES; unset LC_CTYPE; " .. 77 \ shellescape(v:progpath) .. 78 \ " --clean -esX -c 'redir @a' -c 'lang' -c 'put a' -c 'print' -c 'qa!' ") 79 80 " Check that: 81 " 1. The locale is the form of <locale>.UTF-8. 82 " 2. Check that fourth item (LC_NUMERIC) is properly set to "C". 83 " Example match: "en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8" 84 call assert_match('"\([a-zA-Z_]\+\.UTF-8/\)\{3}C\(/[a-zA-Z_]\+\.UTF-8\)\{2}"', 85 \ lang_results, 86 \ "Default locale should have UTF-8 encoding set, and LC_NUMERIC set to 'C'") 87 endfunc 88 89 " vim: shiftwidth=2 sts=2 expandtab