xref: /vim-8.2.3635/src/testdir/test_environ.vim (revision a5fe91e6)
1" Test for environment variables.
2
3scriptencoding utf-8
4
5source check.vim
6
7func 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'])
14endfunc
15
16func Test_getenv()
17  unlet! $TESTENV
18  call assert_equal(v:null, 'TESTENV'->getenv())
19  let $TESTENV = 'foo'
20  call assert_equal('foo', getenv('TESTENV'))
21endfunc
22
23func 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'))
29endfunc
30
31func Test_external_env()
32  call setenv('FOO', 'HelloWorld')
33  if has('win32')
34    let result = system('echo %FOO%')
35  else
36    let result = system('echo $FOO')
37  endif
38  let result = substitute(result, '[ \r\n]', '', 'g')
39  call assert_equal('HelloWorld', result)
40
41  call setenv('FOO', v:null)
42  if has('win32')
43    let result = system('set | findstr "^FOO="')
44  else
45    let result = system('env | grep ^FOO=')
46  endif
47  call assert_equal('', result)
48endfunc
49
50func Test_mac_locale()
51  CheckFeature osxdarwin
52
53  " If $LANG is not set then the system locale will be used.
54  " Run Vim after unsetting all the locale environmental vars, and capture the
55  " output of :lang.
56  let lang_results = system("unset LANG; unset LC_MESSAGES; " ..
57            \ shellescape(v:progpath) ..
58            \ " --clean -esX -c 'redir @a' -c 'lang' -c 'put a' -c 'print' -c 'qa!' ")
59
60  " Check that:
61  " 1. The locale is the form of <locale>.UTF-8.
62  " 2. Check that fourth item (LC_NUMERIC) is properly set to "C".
63  " Example match: "en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8"
64  call assert_match('"\([a-zA-Z_]\+\.UTF-8/\)\{3}C\(/[a-zA-Z_]\+\.UTF-8\)\{2}"',
65        \ lang_results,
66        \ "Default locale should have UTF-8 encoding set, and LC_NUMERIC set to 'C'")
67endfunc
68
69" vim: shiftwidth=2 sts=2 expandtab
70