aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorStefan Hoffmann <stefan991@gmail.com>2014-03-03 20:02:32 +0100
committerThiago de Arruda <tpadilha84@gmail.com>2014-03-06 13:38:45 -0300
commitf2433aedc86db171d5616410605cf0d398d8fdc2 (patch)
tree2df40baf1dce2b9e6e6dc175f66cfff41d7bd6ad /test
parentfc8686640250561156913387c62924d2bdb5e1ac (diff)
downloadrneovim-f2433aedc86db171d5616410605cf0d398d8fdc2.tar.gz
rneovim-f2433aedc86db171d5616410605cf0d398d8fdc2.tar.bz2
rneovim-f2433aedc86db171d5616410605cf0d398d8fdc2.zip
cleanup environment variable handling + unit tests
* removed a putenv() implementation which isn't needed anymore * mch_getenv() and mch_setenv() are now functions in src/os/env.c * removes direct calls to getenv() and setenv() outside of src/os/env.c * refactored the logic of get_env_name into mch_getenvname_at_index * added unittests for the functions in os/env.c
Diffstat (limited to 'test')
-rw-r--r--test/unit/os/env.moon95
1 files changed, 95 insertions, 0 deletions
diff --git a/test/unit/os/env.moon b/test/unit/os/env.moon
new file mode 100644
index 0000000000..b83fcac5f1
--- /dev/null
+++ b/test/unit/os/env.moon
@@ -0,0 +1,95 @@
+{:cimport, :internalize, :eq, :ffi, :lib, :cstr} = require 'test.unit.helpers'
+require 'lfs'
+
+-- fs = cimport './src/os/os.h'
+-- remove these statements once 'cimport' is working properly for misc1.h
+env = lib
+ffi.cdef [[
+const char *mch_getenv(const char *name);
+int mch_setenv(const char *name, const char *value, int override);
+char *mch_getenvname_at_index(size_t index);
+]]
+
+str_to_charp = (str) ->
+ cstr (string.len str), str
+
+NULL = ffi.cast 'void*', 0
+
+describe 'env function', ->
+
+ mch_setenv = (name, value, override) ->
+ env.mch_setenv (str_to_charp name), (str_to_charp value), override
+
+ mch_getenv = (name) ->
+ rval = env.mch_getenv (str_to_charp name)
+ if rval != NULL
+ ffi.string rval
+ else
+ NULL
+
+ describe 'mch_setenv', ->
+
+ OK = 0
+
+ it 'sets an env variable and returns OK', ->
+ name = 'NEOVIM_UNIT_TEST_SETENV_1N'
+ value = 'NEOVIM_UNIT_TEST_SETENV_1V'
+ eq nil, os.getenv name
+ eq OK, (mch_setenv name, value, 1)
+ eq value, os.getenv name
+
+ it "dosn't overwrite an env variable if overwrite is 0", ->
+ name = 'NEOVIM_UNIT_TEST_SETENV_2N'
+ value = 'NEOVIM_UNIT_TEST_SETENV_2V'
+ value_updated = 'NEOVIM_UNIT_TEST_SETENV_2V_UPDATED'
+ eq OK, (mch_setenv name, value, 0)
+ eq value, os.getenv name
+ eq OK, (mch_setenv name, value_updated, 0)
+ eq value, os.getenv name
+
+ describe 'mch_getenv', ->
+
+ it 'reads an env variable', ->
+ name = 'NEOVIM_UNIT_TEST_GETENV_1N'
+ value = 'NEOVIM_UNIT_TEST_GETENV_1V'
+ eq NULL, mch_getenv name
+ -- need to use mch_setenv, because lua dosn't have a setenv function
+ mch_setenv name, value, 1
+ eq value, mch_getenv name
+
+ it 'returns NULL if the env variable is not found', ->
+ name = 'NEOVIM_UNIT_TEST_GETENV_NOTFOUND'
+ eq NULL, mch_getenv name
+
+ describe 'mch_getenvname_at_index', ->
+
+ it 'returns names of environment variables', ->
+ test_name = 'NEOVIM_UNIT_TEST_GETENVNAME_AT_INDEX_1N'
+ test_value = 'NEOVIM_UNIT_TEST_GETENVNAME_AT_INDEX_1V'
+ mch_setenv test_name, test_value, 1
+ i = 0
+ names = {}
+ found_name = false
+ name = env.mch_getenvname_at_index i
+ while name != NULL
+ table.insert names, ffi.string name
+ if (ffi.string name) == test_name
+ found_name = true
+ i += 1
+ name = env.mch_getenvname_at_index i
+
+ eq true, (table.getn names) > 0
+ eq true, found_name
+
+ it 'returns NULL if the index is out of bounds', ->
+ huge = ffi.new 'size_t', 10000
+ maxuint32 = ffi.new 'size_t', 4294967295
+ eq NULL, env.mch_getenvname_at_index huge
+ eq NULL, env.mch_getenvname_at_index maxuint32
+ if ffi.abi '64bit'
+ -- couldn't use a bigger number because it gets converted to
+ -- double somewere, should be big enough anyway
+ -- maxuint64 = ffi.new 'size_t', 18446744073709551615
+ maxuint64 = ffi.new 'size_t', 18446744073709000000
+ eq NULL, env.mch_getenvname_at_index maxuint64
+