{"id":15387291,"url":"https://github.com/rhysd/go-tmpenv","last_synced_at":"2025-04-15T20:15:28.050Z","repository":{"id":57494359,"uuid":"157089904","full_name":"rhysd/go-tmpenv","owner":"rhysd","description":"Small Go library for modifying environment variables temporarily","archived":false,"fork":false,"pushed_at":"2018-11-21T21:33:51.000Z","size":40,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-15T20:15:20.929Z","etag":null,"topics":["environment-variables","golang","temporary"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rhysd.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-11-11T14:52:25.000Z","updated_at":"2025-02-06T14:35:59.000Z","dependencies_parsed_at":"2022-09-03T03:41:51.175Z","dependency_job_id":null,"html_url":"https://github.com/rhysd/go-tmpenv","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rhysd%2Fgo-tmpenv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rhysd%2Fgo-tmpenv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rhysd%2Fgo-tmpenv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rhysd%2Fgo-tmpenv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rhysd","download_url":"https://codeload.github.com/rhysd/go-tmpenv/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249145420,"owners_count":21219966,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["environment-variables","golang","temporary"],"created_at":"2024-10-01T14:53:29.805Z","updated_at":"2025-04-15T20:15:28.020Z","avatar_url":"https://github.com/rhysd.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"Go package `tmpenv`\n===================\n[![Linux/Mac Build Status][travisci-badge]][travisci]\n[![Windows Build Status][appveyor-badge]][appveyor]\n[![Coverage Report][codecov-badge]][codecov]\n[![Documentation][doc-badge]][doc]\n\n[`tmpenv`](doc) is a small Go package for replacing environment variables temporarily.\n\nSetting temporary environment variables and restoring them are common pattern on testing.\nHowever, they have a pitfall since setting empty value to environment variable is different from\nunsetting environment variable.\n\n`tmpenv` helps you to do the pattern correctly with less lines.\n\n## Installation\n\n```\n$ go get -u github.com/rhysd/go-tmpenv\n```\n\n## Usage\n\nYou can find some examples at [example tests](example_test.go).\n\nTo ensure to restore all existing environment variables and to remove all temporary environment variables,\n`tmpenv.All()` is useful. Even if environment variables are set via `os.Setenv()`, `Restore()` method\ncan resture the original values.\n\n```go\nimport (\n\t\"github.com/rhysd/go-tmpenv\"\n\t\"testing\"\n)\n\nfunc TestFoo(t *testing.T) {\n\t// Captures all environment variables\n\tguard := tmpenv.All()\n\n\t// Ensure to restore the captured variables with 'defer'\n\tdefer guard.Restore()\n\n\t// Modify existing environment variable\n\tos.Setenv(\"LANG\", \"C\")\n\n\t// Set new environment variable\n\tos.Setenv(\"FOO_ANSWER\", \"42\")\n\n\t// Do some tests...\n\n\t// $LANG will be restored to original value and $FOO_ANSWER will be removed.\n\t// And other all environment variables will be restored to original values.\n}\n```\n\nIf you're interested in specific environment variables:\n\n```go\nimport (\n\t\"github.com/rhysd/go-tmpenv\"\n\t\"testing\"\n)\n\nfunc TestFoo(t *testing.T) {\n\t// $CONFIG_FOO and $CONFIG_BAR are set to \"aaa\" and \"bbb\" temporarily.\n\t// They will be restored to original values when calling Restore() method.\n\tguard, err := tmpenv.Setenvs(map[string]string{\n\t\t\"CONFIG_FOO\": \"aaa\",\n\t\t\"CONFIG_BAR\": \"bbb\",\n\t})\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Ensure to restore the captured variables with 'defer'\n\tdefer guard.Restore()\n\n\t// Do some tests...\n\n\t// $CONFIG_FOO and $CONFIG_BAR will be restored. When they were already existing,\n\t// they will be restored to original values. If they were not existing, they will\n\t// will be removed.\n}\n```\n\nFollowing is the same as above, but adding variables one by one:\n\n```go\nimport (\n\t\"github.com/rhysd/go-tmpenv\"\n\t\"testing\"\n)\n\nfunc TestFoo(t *testing.T) {\n\tguard := tmpenv.New()\n\n\t// Ensure to restore the captured variables with 'defer'\n\tdefer guard.Restore()\n\n\t// $CONFIG_FOO and $CONFIG_BAR are set to \"aaa\" and \"bbb\" temporarily.\n\t// They will be restored to original values when calling Restore() method.\n\t// You need to use guard.Setenv instead of os.Setenv to notify which environment variable\n\t// was temporarily set to `guard` object.\n\tguard.Setenv(\"CONFIG_FOO\", \"aaa\")\n\tguard.Setenv(\"CONFIG_BAR\", \"bbb\")\n\n\t// Do some tests...\n\n\t// $CONFIG_FOO and $CONFIG_BAR will be restored. When they were already existing,\n\t// they will be restored to original values. If they were not existing, they will\n\t// will be removed.\n}\n```\n\nYou can also declare the environment variables which should be restored at `tmpenv.New()`.\n\n```go\nimport (\n\t\"github.com/rhysd/go-tmpenv\"\n\t\"testing\"\n)\n\nfunc TestFoo(t *testing.T) {\n\t// Let's say $CONFIG_FOO is already existing, and $CONFIG_BAR does not exist\n\n\tguard := tmpenv.New(\"CONFIG_FOO\", \"CONFIG_BAR\")\n\n\t// Ensure to restore the captured variables with 'defer'\n\tdefer guard.Restore()\n\n\tos.Setenv(\"CONFIG_FOO\", \"some value\")\n\tos.Setenv(\"CONFIG_BAR\", \"some value\")\n\n\t// Do some tests...\n\n\t// $CONFIG_FOO will be restored to original value and $CONFIG_BAR will be unset.\n}\n```\n\nWhen you want to clear environment variables temporarily, `tmpenv.Unset` function or `Unsetenv` method\nare available.\n\n```go\nimport (\n\t\"github.com/rhysd/go-tmpenv\"\n\t\"testing\"\n)\n\nfunc TestFoo(t *testing.T) {\n\t// Let's say $CONFIG_FOO and $CONFIG_BAR are already existing\n\n\t// Remember $CONFIG_FOO value and unset it\n\tguard := tmpenv.Unset(\"CONFIG_FOO\")\n\n\t// Ensure to restore the captured variables with 'defer'\n\tdefer guard.Restore()\n\n\t// Unsetenv method also unset the environment variable with remembering the original value\n\tguard.Unsetenv(\"CONFIG_BAR\")\n\n\t// Here, $CONFIG_FOO and $CONFIG_BAR are not set\n\n\t// Do some tests...\n\n\t// $CONFIG_FOO and $CONFIG_BAR will be restored to original values.\n}\n```\n\nPlease read [the documentation][doc] for more details.\n\n## Repository\n\nThis library is developed at [GitHub repository](https://github.com/rhysd/go-tmpenv). If you're facing\nsome error or have some feature request, please create a new issue at the page.\n\n## License\n\n[MIT License](LICENSE.txt)\n\n\n[doc-badge]: https://godoc.org/github.com/rhysd/go-tmpenv?status.svg\n[doc]: http://godoc.org/github.com/rhysd/go-tmpenv\n[travisci-badge]: https://travis-ci.org/rhysd/go-tmpenv.svg?branch=master\n[travisci]: https://travis-ci.org/rhysd/go-tmpenv\n[appveyor-badge]: https://ci.appveyor.com/api/projects/status/5pbcku1buw8gnqu9/branch/master?svg=true\n[appveyor]: https://ci.appveyor.com/project/rhysd/go-tmpenv\n[codecov-badge]: https://codecov.io/gh/rhysd/go-tmpenv/branch/master/graph/badge.svg\n[codecov]: https://codecov.io/gh/rhysd/go-tmpenv\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frhysd%2Fgo-tmpenv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frhysd%2Fgo-tmpenv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frhysd%2Fgo-tmpenv/lists"}