robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
3.3 KiB123 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each, after_each = t.describe, t.it, t.before_each, t.after_each
5local clear = n.clear
6local eq = t.eq
7local environ = n.fn.environ
8local exists = n.fn.exists
9local system = n.fn.system
10local nvim_prog = n.nvim_prog
11local command = n.command
12local eval = n.eval
13local setenv = n.fn.setenv
14
15describe('vim.fn.environ()', function()
16  it('exists() handles empty env variable', function()
17    clear({ env = { EMPTY_VAR = '' } })
18    eq(1, exists('$EMPTY_VAR'))
19    eq(0, exists('$DOES_NOT_EXIST'))
20  end)
21
22  it('handles empty env variable', function()
23    clear({ env = { EMPTY_VAR = '' } })
24    eq('', environ()['EMPTY_VAR'])
25    -- vim.env returns nil if the value is empty string. 🤷
26    eq(vim.NIL, n.exec_lua('return vim.env.EMPTY_VAR'))
27    eq(nil, environ()['DOES_NOT_EXIST'])
28    eq(vim.NIL, n.exec_lua('return vim.env.DOES_NOT_EXIST'))
29  end)
30
31  it('forces uppercase keys on Windows #39443', function()
32    if not t.is_os('win') then
33      return
34    end
35    clear({ env = { mixed_Case_Var = 'val' } })
36    eq('val', environ()['MIXED_CASE_VAR'])
37    eq(nil, environ()['mixed_Case_Var'])
38  end)
39
40  it('results match getenv()', function()
41    clear()
42    eq(
43      true,
44      n.exec_lua([[
45        local env = vim.fn.environ()
46        assert(vim.tbl_count(env) > 10, 'environ() should have some env vars!')
47        for k, v in pairs(env) do
48          if v ~= '' and vim.fn.getenv(k) ~= v and vim.fn.getenv(k) ~= v:gsub('\\', '/') then
49            error(('environ()[%q] = %q, but vim.fn.getenv(%q) = %q'):format(k, v, k, vim.fn.getenv(k)))
50          end
51        end
52        return true
53      ]])
54    )
55  end)
56end)
57
58describe('empty $HOME', function()
59  local original_home = os.getenv('HOME')
60
61  before_each(clear)
62
63  -- recover $HOME after each test
64  after_each(function()
65    if original_home ~= nil then
66      setenv('HOME', original_home)
67    end
68    os.remove('test_empty_home')
69    os.remove('./~')
70  end)
71
72  local function tilde_in_cwd()
73    -- get files in cwd
74    command("let test_empty_home_cwd_files = split(globpath('.', '*'), '\n')")
75    -- get the index of the file named '~'
76    command('let test_empty_home_tilde_index = index(test_empty_home_cwd_files, "./~")')
77    return eval('test_empty_home_tilde_index') ~= -1
78  end
79
80  local function write_and_test_tilde()
81    system({
82      nvim_prog,
83      '-u',
84      'NONE',
85      '-i',
86      'NONE',
87      '--headless',
88      '-c',
89      'write test_empty_home',
90      '+q',
91    })
92    eq(false, tilde_in_cwd())
93  end
94
95  it("'~' folder not created in cwd if $HOME and related env not defined", function()
96    command('unlet $HOME')
97    write_and_test_tilde()
98
99    command("let $HOMEDRIVE='C:'")
100    command("let $USERPROFILE='C:\\'")
101    write_and_test_tilde()
102
103    command('unlet $HOMEDRIVE')
104    write_and_test_tilde()
105
106    command('unlet $USERPROFILE')
107    write_and_test_tilde()
108
109    command("let $HOME='%USERPROFILE%'")
110    command("let $USERPROFILE='C:\\'")
111    write_and_test_tilde()
112  end)
113
114  it("'~' folder not created in cwd if writing a file with invalid $HOME", function()
115    setenv('HOME', '/path/does/not/exist')
116    write_and_test_tilde()
117  end)
118
119  it("'~' folder not created in cwd if writing a file with $HOME=''", function()
120    command("let $HOME=''")
121    write_and_test_tilde()
122  end)
123end)