Delete software environment changes

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

+4
source share
4 answers

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.

+9
source

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.

+2
source

you can use unsetenv function.

If vc2008 does not have this feature, you can directly access the environment using getenv_s and delete the entry manually, simulating unsetenv .

+1
source

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.

+1
source

Source: https://habr.com/ru/post/1315092/


All Articles