-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve tests: add tests, add js, vm testing
- Loading branch information
1 parent
9ef299d
commit 9b4d4fd
Showing
1 changed file
with
54 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,58 @@ | ||
discard """ | ||
matrix: "--threads" | ||
joinable: false | ||
targets: "c cpp" | ||
targets: "c js cpp" | ||
""" | ||
import os | ||
|
||
block: # delEnv | ||
const dummyEnvVar = "D20210720T144752" # This env var wouldn't be likely to exist to begin with | ||
doAssert not existsEnv(dummyEnvVar) | ||
putEnv(dummyEnvVar, "1") | ||
doAssert existsEnv(dummyEnvVar) | ||
delEnv(dummyEnvVar) | ||
doAssert not existsEnv(dummyEnvVar) | ||
delEnv(dummyEnvVar) # deleting an already deleted env var | ||
doAssert not existsEnv(dummyEnvVar) | ||
|
||
block: # putEnv | ||
# raises OSError on invalid input | ||
doAssertRaises(OSError, putEnv("NIM_TESTS_TOSENV_PUT=DUMMY_VALUE", "NEW_DUMMY_VALUE")) | ||
doAssertRaises(OSError, putEnv("", "NEW_DUMMY_VALUE")) | ||
doAssert not existsEnv("") | ||
doAssert not existsEnv("NIM_TESTS_TOSENV_PUT=DUMMY_VALUE") | ||
doAssert not existsEnv("NIM_TESTS_TOSENV_PUT") | ||
|
||
block: | ||
doAssert getEnv("NIM_TESTS_TOSENV_NONEXISTENT", "") == "" | ||
doAssert getEnv("NIM_TESTS_TOSENV_NONEXISTENT", " ") == " " | ||
doAssert getEnv("NIM_TESTS_TOSENV_NONEXISTENT", "Arrakis") == "Arrakis" | ||
|
||
block: # bug #18533 | ||
proc c_getenv(env: cstring): cstring {.importc: "getenv", header: "<stdlib.h>".} | ||
var thr: Thread[void] | ||
proc threadFunc {.thread.} = putEnv("foo", "fooVal2") | ||
|
||
putEnv("foo", "fooVal1") | ||
doAssert getEnv("foo") == "fooVal1" | ||
createThread(thr, threadFunc) | ||
joinThreads(thr) | ||
doAssert getEnv("foo") == $c_getenv("foo") | ||
|
||
doAssertRaises(OSError): delEnv("foo=bar") | ||
|
||
import std/os | ||
from std/sequtils import toSeq | ||
import stdtest/testutils | ||
|
||
template main = | ||
block: # delEnv, existsEnv, getEnv, envPairs | ||
for val in ["val", ""]: # ensures empty val works too | ||
const key = "NIM_TESTS_TOSENV_KEY" | ||
doAssert not existsEnv(key) | ||
putEnv(key, val) | ||
doAssert existsEnv(key) | ||
doAssert getEnv(key) == val | ||
when nimvm: discard | ||
else: | ||
doAssert (key, val) in toSeq(envPairs()) | ||
delEnv(key) | ||
when nimvm: discard | ||
else: | ||
doAssert (key, val) notin toSeq(envPairs()) | ||
doAssert not existsEnv(key) | ||
delEnv(key) # deleting an already deleted env var | ||
doAssert not existsEnv(key) | ||
|
||
block: | ||
doAssert getEnv("NIM_TESTS_TOSENV_NONEXISTENT", "") == "" | ||
doAssert getEnv("NIM_TESTS_TOSENV_NONEXISTENT", " ") == " " | ||
doAssert getEnv("NIM_TESTS_TOSENV_NONEXISTENT", "defval") == "defval" | ||
|
||
whenVMorJs: discard # xxx improve | ||
do: | ||
doAssertRaises(OSError, putEnv("NIM_TESTS_TOSENV_PUT=DUMMY_VALUE", "NEW_DUMMY_VALUE")) | ||
doAssertRaises(OSError, putEnv("", "NEW_DUMMY_VALUE")) | ||
doAssert not existsEnv("") | ||
doAssert not existsEnv("NIM_TESTS_TOSENV_PUT=DUMMY_VALUE") | ||
doAssert not existsEnv("NIM_TESTS_TOSENV_PUT") | ||
|
||
static: main() | ||
main() | ||
|
||
when not defined(js): | ||
block: # bug #18533 | ||
proc c_getenv(env: cstring): cstring {.importc: "getenv", header: "<stdlib.h>".} | ||
var thr: Thread[void] | ||
proc threadFunc {.thread.} = putEnv("foo", "fooVal2") | ||
|
||
putEnv("foo", "fooVal1") | ||
doAssert getEnv("foo") == "fooVal1" | ||
createThread(thr, threadFunc) | ||
joinThreads(thr) | ||
doAssert getEnv("foo") == $c_getenv("foo") | ||
|
||
doAssertRaises(OSError): delEnv("foo=bar") |