From 6701b4533144a9f94ab0b4ae3c24f90b29e6b5f8 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Fri, 17 Apr 2026 23:13:47 +0200 Subject: refactor(vimfn): full-Lua impl of vim.fn.environ() --- test/functional/vimscript/environ_spec.lua | 32 ++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) (limited to 'test/functional/vimscript') diff --git a/test/functional/vimscript/environ_spec.lua b/test/functional/vimscript/environ_spec.lua index abb093a6c8..9046d2d696 100644 --- a/test/functional/vimscript/environ_spec.lua +++ b/test/functional/vimscript/environ_spec.lua @@ -11,17 +11,37 @@ local command = n.command local eval = n.eval local setenv = n.fn.setenv -describe('environment variables', function() - it('environ() handles empty env variable', function() +describe('vim.fn.environ()', function() + 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('exists() handles empty env variable', function() - clear({ env = { EMPTY_VAR = '' } }) - eq(1, exists('$EMPTY_VAR')) - eq(0, exists('$DOES_NOT_EXIST')) + 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 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) -- cgit