aboutsummaryrefslogtreecommitdiff
path: root/test/functional/vimscript/environ_spec.lua
blob: 4bad445ea3e028a09edcbaf44a834464647fe3ed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
local t = require('test.testutil')
local n = require('test.functional.testnvim')()

local describe, it, before_each, after_each = t.describe, t.it, t.before_each, t.after_each
local clear = n.clear
local eq = t.eq
local environ = n.fn.environ
local exists = n.fn.exists
local system = n.fn.system
local nvim_prog = n.nvim_prog
local command = n.command
local eval = n.eval
local setenv = n.fn.setenv

describe('vim.fn.environ()', function()
  it("$VAR reads (multibyte) envvar, irrespective of 'isident'", function()
    -- Decoy envvar "\195" (the first byte of "é"): $é must not read it. (Windows can't set it.)
    clear({ env = { ['é'] = 'x', ['aあ'] = 'y', ['\195'] = not t.is_os('win') and 'bad' or nil } })
    command('set isident=') -- Does not matter.
    eq('x', eval('$é'))
    eq('y', eval('$aあ'))
    command("cgetexpr ['$é/$aあ-z:1:text']") -- Quickfix expands $VAR in filenames.
    eq('x/y-z', n.fn.bufname(n.fn.getqflist()[1].bufnr))
  end)

  it('exists() handles empty env variable', function()
    clear({ env = { EMPTY_VAR = '' } })
    eq(1, exists('$EMPTY_VAR'))
    eq(0, exists('$DOES_NOT_EXIST'))
  end)

  it('handles empty env variable', function()
    clear({ env = { EMPTY_VAR = '' } })
    eq('', environ()['EMPTY_VAR'])
    -- vim.env returns nil if the value is empty string. 🤷
    eq(vim.NIL, n.exec_lua('return vim.env.EMPTY_VAR'))
    eq(nil, environ()['DOES_NOT_EXIST'])
    eq(vim.NIL, n.exec_lua('return vim.env.DOES_NOT_EXIST'))
  end)

  it('forces uppercase keys on Windows #39443', function()
    if not t.is_os('win') then
      return
    end
    clear({ env = { mixed_Case_Var = 'val' } })
    eq('val', environ()['MIXED_CASE_VAR'])
    eq(nil, environ()['mixed_Case_Var'])
  end)

  it('results match getenv()', function()
    clear()
    eq(
      true,
      n.exec_lua([[
        local env = vim.fn.environ()
        assert(vim.tbl_count(env) > 10, 'environ() should have some env vars!')
        for k, v in pairs(env) do
          if v ~= '' and vim.fn.getenv(k) ~= v and vim.fn.getenv(k) ~= v:gsub('\\', '/') then
            error(('environ()[%q] = %q, but vim.fn.getenv(%q) = %q'):format(k, v, k, vim.fn.getenv(k)))
          end
        end
        return true
      ]])
    )
  end)
end)

describe('empty $HOME', function()
  local original_home = os.getenv('HOME')

  before_each(clear)

  -- recover $HOME after each test
  after_each(function()
    if original_home ~= nil then
      setenv('HOME', original_home)
    end
    os.remove('test_empty_home')
    os.remove('./~')
  end)

  local function tilde_in_cwd()
    -- get files in cwd
    command("let test_empty_home_cwd_files = split(globpath('.', '*'), '\n')")
    -- get the index of the file named '~'
    command('let test_empty_home_tilde_index = index(test_empty_home_cwd_files, "./~")')
    return eval('test_empty_home_tilde_index') ~= -1
  end

  local function write_and_test_tilde()
    system({
      nvim_prog,
      '-u',
      'NONE',
      '-i',
      'NONE',
      '--headless',
      '-c',
      'write test_empty_home',
      '+q',
    })
    eq(false, tilde_in_cwd())
  end

  it("'~' folder not created in cwd if $HOME and related env not defined", function()
    command('unlet $HOME')
    write_and_test_tilde()

    command("let $HOMEDRIVE='C:'")
    command("let $USERPROFILE='C:\\'")
    write_and_test_tilde()

    command('unlet $HOMEDRIVE')
    write_and_test_tilde()

    command('unlet $USERPROFILE')
    write_and_test_tilde()

    command("let $HOME='%USERPROFILE%'")
    command("let $USERPROFILE='C:\\'")
    write_and_test_tilde()
  end)

  it("'~' folder not created in cwd if writing a file with invalid $HOME", function()
    setenv('HOME', '/path/does/not/exist')
    write_and_test_tilde()
  end)

  it("'~' folder not created in cwd if writing a file with $HOME=''", function()
    command("let $HOME=''")
    write_and_test_tilde()
  end)
end)