Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
October 17, 2017 How to modify process environment variables | ||||
---|---|---|---|---|
| ||||
Hi, Is it possible to change the current process's environment variables? I have looked at `std/process.d` source code, and there is only a private method `createEnv` used when new (sub)process is created. In C `putEnv` the answer is positive: http://man7.org/linux/man-pages/man3/putenv.3.html (FIXME) I come to this question as I want to set some custom variables for my unittests. My program reads some tokens from system environments, and it's convenient if I can simulate different cases for testings. Thanks for your reading and support. |
October 17, 2017 Re: How to modify process environment variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ky-Anh Huynh | Ky-Anh Huynh wrote:
> Hi,
>
> Is it possible to change the current process's environment variables?
>
> I have looked at `std/process.d` source code, and there is only a private method `createEnv` used when new (sub)process is created.
>
> In C `putEnv` the answer is positive: http://man7.org/linux/man-pages/man3/putenv.3.html (FIXME)
>
> I come to this question as I want to set some custom variables for my unittests. My program reads some tokens from system environments, and it's convenient if I can simulate different cases for testings.
>
> Thanks for your reading and support.
you can use libc's `putenv()` in D too, it is ok. just import `core.sys.posix.stdlib`, it is there. D is not antagonistic to C, and doesn't try to replace the whole libc with it's own libraries. so if you see something that libc has and you'd like to use -- just do it! ;-)
|
October 17, 2017 Re: How to modify process environment variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | On Tuesday, 17 October 2017 at 04:56:23 UTC, ketmar wrote:
>
> you can use libc's `putenv()` in D too, it is ok. just import `core.sys.posix.stdlib`, it is there. D is not antagonistic to C, and doesn't try to replace the whole libc with it's own libraries. so if you see something that libc has and you'd like to use -- just do it! ;-)
I see :) I have always tried to avoid C if possible :D
|
October 17, 2017 Re: How to modify process environment variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ky-Anh Huynh | On Tuesday, 17 October 2017 at 05:57:50 UTC, Ky-Anh Huynh wrote: > On Tuesday, 17 October 2017 at 04:56:23 UTC, ketmar wrote: >> >> you can use libc's `putenv()` in D too, it is ok. just import `core.sys.posix.stdlib`, it is there. D is not antagonistic to C, and doesn't try to replace the whole libc with it's own libraries. so if you see something that libc has and you'd like to use -- just do it! ;-) > > I see :) I have always tried to avoid C if possible :D As an alternative, a search on code.dlang.org turned up this lib: http://code.dlang.org/packages/dotenv |
October 17, 2017 Re: How to modify process environment variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ky-Anh Huynh | On 2017-10-17 06:51, Ky-Anh Huynh wrote: > Hi, > > Is it possible to change the current process's environment variables? > > I have looked at `std/process.d` source code, and there is only a private method `createEnv` used when new (sub)process is created. > > In C `putEnv` the answer is positive: http://man7.org/linux/man-pages/man3/putenv.3.html (FIXME) > > I come to this question as I want to set some custom variables for my unittests. My program reads some tokens from system environments, and it's convenient if I can simulate different cases for testings. > > Thanks for your reading and support. Use std.process.environment [1] and assign to it like an associative array. [1] https://dlang.org/phobos/std_process.html#.environment.opIndexAssign -- /Jacob Carlborg |
October 18, 2017 Re: How to modify process environment variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On Tuesday, 17 October 2017 at 11:49:32 UTC, Jacob Carlborg wrote: > On 2017-10-17 06:51, Ky-Anh Huynh wrote: >> Hi, >> >> Is it possible to change the current process's environment variables? >> >> I have looked at `std/process.d` source code, and there is only a private method `createEnv` used when new (sub)process is created. >> >> In C `putEnv` the answer is positive: http://man7.org/linux/man-pages/man3/putenv.3.html (FIXME) >> >> I come to this question as I want to set some custom variables for my unittests. My program reads some tokens from system environments, and it's convenient if I can simulate different cases for testings. >> >> Thanks for your reading and support. > > Use std.process.environment [1] and assign to it like an associative array. > > [1] https://dlang.org/phobos/std_process.html#.environment.opIndexAssign Oh thanks a lot for your pointing out, Jacob. That's the thing I'm looking for. The C version is not so bad though ``` import core.sys.posix.stdlib; import std.string: toStringz; string jenkinsToken = "TEST_TOKEN="; putenv(cast(char*)jenkinsToken); ``` |
October 18, 2017 Re: How to modify process environment variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rene Zwanenburg | On Tuesday, 17 October 2017 at 08:42:09 UTC, Rene Zwanenburg wrote:
> On Tuesday, 17 October 2017 at 05:57:50 UTC, Ky-Anh Huynh wrote:
>> On Tuesday, 17 October 2017 at 04:56:23 UTC, ketmar wrote:
>>>
>>> you can use libc's `putenv()` in D too, it is ok. just import `core.sys.posix.stdlib`, it is there. D is not antagonistic to C, and doesn't try to replace the whole libc with it's own libraries. so if you see something that libc has and you'd like to use -- just do it! ;-)
>>
>> I see :) I have always tried to avoid C if possible :D
>
> As an alternative, a search on code.dlang.org turned up this lib:
>
> http://code.dlang.org/packages/dotenv
Awesome. I will take a look definitely. I have a similar way in my NodeJS team :D
|
Copyright © 1999-2021 by the D Language Foundation