blob: dd34983ee5a8a7fa37e4aa4b1eb9ee41dd215f37 [file] [log] [blame]
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001" Test for environment variables.
2
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003scriptencoding utf-8
4
Bram Moolenaara5fe91e2020-09-27 16:03:15 +02005source check.vim
6
Bram Moolenaar691ddee2019-05-09 14:52:41 +02007func 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
Bram Moolenaar4c313b12019-08-24 22:58:31 +020018 call assert_equal(v:null, 'TESTENV'->getenv())
Bram Moolenaar691ddee2019-05-09 14:52:41 +020019 let $TESTENV = 'foo'
20 call assert_equal('foo', getenv('TESTENV'))
21endfunc
22
23func Test_setenv()
24 unlet! $TESTENV
Bram Moolenaar196b4662019-09-06 21:34:30 +020025 eval 'foo'->setenv('TEST ENV')
Bram Moolenaar691ddee2019-05-09 14:52:41 +020026 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')
Bram Moolenaarc9740222019-06-04 08:22:53 +020043 let result = system('set | findstr "^FOO="')
Bram Moolenaar691ddee2019-05-09 14:52:41 +020044 else
45 let result = system('env | grep ^FOO=')
46 endif
47 call assert_equal('', result)
48endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020049
Bram Moolenaara5fe91e2020-09-27 16:03:15 +020050func 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
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020069" vim: shiftwidth=2 sts=2 expandtab