os: ensure that setenv creates the key on windows (#6560)

pull/6562/head
Ekopalypse 2020-10-04 17:43:28 +00:00 committed by GitHub
parent 0c174104fc
commit 2622070f14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 0 deletions

View File

@ -35,6 +35,12 @@ pub fn setenv(name string, value string, overwrite bool) int {
unsafe {
return C._putenv(format.str)
}
} else {
if getenv(name).len == 0 {
unsafe {
return C._putenv(format.str)
}
}
}
return -1
} $else {

View File

@ -1,4 +1,5 @@
import os
import time
fn test_getenv() {
// VEXE is set by the V builtin test runner
@ -34,3 +35,15 @@ fn test_environ() {
assert all['myvar2'] == 'bar2'
assert all['myvar_not_defined'] == ''
}
fn test_setenv_var_not_exists(){
key := time.new_time(time.now()).unix
os.setenv('foo$key', 'bar', false)
assert os.getenv('foo$key') == 'bar'
}
fn test_getenv_empty_var(){
key := time.new_time(time.now()).unix
os.setenv('empty$key', '""', false)
assert os.getenv('empty$key') == '""'
}