I need to write unit test for some C ++ code that checks for the presence of an environment variable. I use MSVS 2008 and gtest as my framework. I am adding an environment variable using putenv, I am checking the environment variable using getevn, but I cannot figure out how to remove it so that no other test will see it. I understand that this is probably easy, but I can not find the answer on the Internet. Thanks
A putenv call again indicating "SOME_VAR=" because the parameter removes the SOME_VAR environment SOME_VAR . By the way, Microsoft recommends using _putenv since putenv deprecated.
putenv
"SOME_VAR="
SOME_VAR
_putenv
You can always use the fork / exec subprocess to do only putenv / getenv testing, and then when it completes, there is no random environment left.
getenv
you can use unsetenv function.
unsetenv
If vc2008 does not have this feature, you can directly access the environment using getenv_s and delete the entry manually, simulating unsetenv .
getenv_s
How to set env var to an empty string?
From cmd.exe this works:
set SOMEVAR=something echo %SOMEVAR% set SOMEVAR= echo %SOMEVAR%
If the latter indicates that it has been deleted.
Source: https://habr.com/ru/post/1315092/More articles:Specifying a return error code on application output - c #Is FlushFileBuffers as good as CloseHandle and then CreateFile when saving data to disk? - winapiWhy do I see DBI errors on the console even though I wrapped DBI calls in eval? - evalVarious SHA1 Hash results between Java and C # - javaSHA1 C # equivalent of this Java - javaConventions, standards, or MIME type restrictions? - standardsRuby: What does Errno :: EEXIST mean when installing a gem? - ruby | fooobar.comSend a simple GET request - jqueryWhat is the best and most comprehensive JavaScript graphics and graphics API? - javascriptCould not find or load registered .Net Framework Data Provider exception - subsonicAll Articles