From 6cb5eee1b2e67f2e77e62ff9e7e845aa82a62183 Mon Sep 17 00:00:00 2001 From: musou1500 Date: Thu, 27 Jun 2019 09:24:09 +0900 Subject: [PATCH] add `unsetenv`, `setenv`, `clearenv` to os module (#583) --- os/os.v | 18 ++++++++++++++++++ os/os_test.v | 26 ++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 os/os_test.v diff --git a/os/os.v b/os/os.v index 61ab53e0a9..06a7a470df 100644 --- a/os/os.v +++ b/os/os.v @@ -316,6 +316,24 @@ pub fn getenv(key string) string { return tos2(s) } + +pub fn setenv(name string, value string, overwrite bool) int { + return C.setenv(name.cstr(), value.cstr(), overwrite) +} + +pub fn unsetenv(name string) int { + return C.unsetenv(name.cstr()) +} + +pub fn clearenv() { + C.clearenv() +} + +fn exit(code int) { + C.exit(code) +} + + // `file_exists` returns true if `path` exists. pub fn file_exists(path string) bool { res := false diff --git a/os/os_test.v b/os/os_test.v new file mode 100644 index 0000000000..e382bbd875 --- /dev/null +++ b/os/os_test.v @@ -0,0 +1,26 @@ +import os + +fn test_setenv() { + os.setenv('foo', 'bar', true) + assert os.getenv('foo') == 'bar' + + // `setenv` should not set if `overwrite` is false + os.setenv('foo', 'bar2', false) + assert os.getenv('foo') == 'bar' + + // `setenv` should overwrite if `overwrite` is true + os.setenv('foo', 'bar2', true) + assert os.getenv('foo') == 'bar2' +} + +fn test_unsetenv() { + os.setenv('foo', 'bar', true) + os.unsetenv('foo') + assert os.getenv('foo') == '' +} + +fn test_clearenv() { + os.setenv('foo', 'bar', true) + os.clearenv() + assert os.getenv('foo') == '' +}