From 71487a935e658e2c193188d871a1c3a7a570dbc0 Mon Sep 17 00:00:00 2001 From: Rui Abreu Ferreira Date: Wed, 25 Feb 2015 10:33:04 +0000 Subject: [PATCH] Implement os_unsetenv() - In UNIX systems where unsetenv() is available, it is used. Otherwise the variables are set with the empty string. - New check HAVE_UNSETENV for unsetenv() - Added unit test to env_spec.lua --- config/CMakeLists.txt | 1 + config/config.h.in | 1 + src/nvim/os/env.c | 13 +++++++++++++ test/unit/os/env_spec.lua | 17 +++++++++++++++++ 4 files changed, 32 insertions(+) diff --git a/config/CMakeLists.txt b/config/CMakeLists.txt index 0d3ef62297..ed1f422070 100644 --- a/config/CMakeLists.txt +++ b/config/CMakeLists.txt @@ -58,6 +58,7 @@ check_function_exists(setenv HAVE_SETENV) if(NOT HAVE_SETENV) message(SEND_ERROR "setenv() function not found on your system.") endif() +check_function_exists(unsetenv HAVE_UNSETENV) check_function_exists(setpgid HAVE_SETPGID) check_function_exists(setsid HAVE_SETSID) check_function_exists(sigaction HAVE_SIGACTION) diff --git a/config/config.h.in b/config/config.h.in index 04db12fbe3..9b2abfa19d 100644 --- a/config/config.h.in +++ b/config/config.h.in @@ -37,6 +37,7 @@ // TODO: add proper cmake check // #define HAVE_SELINUX 1 #cmakedefine HAVE_SETENV +#cmakedefine HAVE_UNSETENV #cmakedefine HAVE_SETPGID #cmakedefine HAVE_SETSID #cmakedefine HAVE_SIGACTION diff --git a/src/nvim/os/env.c b/src/nvim/os/env.c index 37158f4d3c..be4b22de3a 100644 --- a/src/nvim/os/env.c +++ b/src/nvim/os/env.c @@ -37,6 +37,19 @@ int os_setenv(const char *name, const char *value, int overwrite) return setenv(name, value, overwrite); } +/// Unset environment variable +/// +/// For systems where unsetenv() is not available the value will be set as an +/// empty string +int os_unsetenv(const char *name) +{ +#ifdef HAVE_UNSETENV + return unsetenv(name); +#else + return os_setenv(name, "", 1); +#endif +} + char *os_getenvname_at_index(size_t index) { # if defined(HAVE__NSGETENVIRON) diff --git a/test/unit/os/env_spec.lua b/test/unit/os/env_spec.lua index 5896f5ddd4..9d936c2564 100644 --- a/test/unit/os/env_spec.lua +++ b/test/unit/os/env_spec.lua @@ -3,6 +3,7 @@ local helpers = require('test.unit.helpers') local cimport = helpers.cimport local internalize = helpers.internalize local eq = helpers.eq +local neq = helpers.neq local ffi = helpers.ffi local lib = helpers.lib local cstr = helpers.cstr @@ -21,6 +22,10 @@ describe('env function', function() return env.os_setenv((to_cstr(name)), (to_cstr(value)), override) end + function os_unsetenv(name, value, override) + return env.os_unsetenv((to_cstr(name))) + end + function os_getenv(name) local rval = env.os_getenv((to_cstr(name))) if rval ~= NULL then @@ -68,6 +73,18 @@ describe('env function', function() end) end) + describe('os_unsetenv', function() + it('unsets environment variable', function() + local name = 'TEST_UNSETENV' + local value = 'TESTVALUE' + os_setenv(name, value, 1) + os_unsetenv(name) + neq(os_getenv(name), value) + -- Depending on the platform the var might be unset or set as '' + assert.True(os_getenv(name) == nil or os_getenv(name) == '') + end) + end) + describe('os_getenvname_at_index', function() it('returns names of environment variables', function() local test_name = 'NEOVIM_UNIT_TEST_GETENVNAME_AT_INDEX_1N'