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