Skip to content

Commit

Permalink
improve tests: add tests, add js, vm testing
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Jul 27, 2021
1 parent 9ef299d commit 9b4d4fd
Showing 1 changed file with 54 additions and 38 deletions.
92 changes: 54 additions & 38 deletions tests/stdlib/tosenv.nim
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")

0 comments on commit 9b4d4fd

Please sign in to comment.