blob: d8344817f5accf9e3a35c17e9ac316db1eeb29bd [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
LemonBoy77142312022-04-15 20:50:46 +010031func 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
49endfunc
50
Bram Moolenaar691ddee2019-05-09 14:52:41 +020051func 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')
Bram Moolenaarc9740222019-06-04 08:22:53 +020063 let result = system('set | findstr "^FOO="')
Bram Moolenaar691ddee2019-05-09 14:52:41 +020064 else
65 let result = system('env | grep ^FOO=')
66 endif
67 call assert_equal('', result)
68endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020069
Bram Moolenaara5fe91e2020-09-27 16:03:15 +020070func 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; " ..
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'")
87endfunc
88
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020089" vim: shiftwidth=2 sts=2 expandtab