tempenv


Nametempenv JSON
Version 2.0.0 PyPI version JSON
download
home_pagehttps://github.com/jeking3/tempenv
SummaryEnvironment Variable Context Manager
upload_time2022-01-22 17:11:55
maintainer
docs_urlNone
authorJames E. King III
requires_python>=3.7
licenseApache License 2.0
keywords temporary environment variable context manager test testing
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            # tempenv

[![pypi](https://img.shields.io/pypi/v/tempenv.svg)](https://pypi.python.org/pypi/tempenv)
[![Build Status](https://github.com/jeking3/tempenv/actions/workflows/ci.yml/badge.svg)](https://github.com/jeking3/tempenv/actions/workflows/ci.yml)
[![codecov](https://codecov.io/gh/jeking3/tempenv/branch/main/graph/badge.svg)](https://codecov.io/gh/jeking3/tempenv)
[![Code Style](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)

Manage environment variables in a temporary scope.

Some products use environment variables as a primary means to supply
credentials.  To ensure the lifetime of exposed credentials is short,
wrap them in a TemporaryEnvironment so that they are automatically
destroyed on scope exit.

You can:

- Set or unset environment variables inside a ``with`` code block,
- Get a warning if the code block modifies one of the environment
  variables,
- Optionally bypass restoration of the original environment variable
  value if the code block modifies the environment variable.

## Install

Install the latest version of tempenv:

```
pip install tempenv
```

## Examples

Each of these examples can be found in the tests.

Set some environment variables temporarily:
(see tests/example_set_test.py):

```python
def test_set(self):
    user_before = os.environ.get("USER")
    with TemporaryEnvironment({"USER": "nobody", "OTHER": "foo"}):
        assert os.environ.get("USER") == "nobody"
        assert os.environ.get("OTHER") == "foo"
    assert os.environ.get("USER") == user_before
```

Changing the value to ``None`` will unset the environment variable during
the code block
(see tests/example_unset_test.py):

```python
def test_unset(self):
    os.environ["DEBUG"] = "1"
    with TemporaryEnvironment({"DEBUG": None}):
        assert "DEBUG" not in os.environ
    assert "DEBUG" in os.environ
```

Changing a temporary environment variable during the scope will cause a
warning
(see tests/example_overwrite_test.py):

```python
def test_overwritten_in_context(self):
    with self.assertWarnsRegex(EnvironmentVariableChangedWarning, "FOO"):
        with TemporaryEnvironment({"FOO": "BAR"}):
            os.environ["FOO"] = "SAM"
```

If you set the optional argument ``restore_if_changed=False`` then a change
during the scope of the TemporaryEnvironment will not issue a warning and will
not restore to the original value
(see tests/example_ignore_test.py):

```python
def test_ignored_overwrite_in_context(self):
    os.environ["FOO"] = "BAR"
    with TemporaryEnvironment({"FOO": "SAM"}, restore_if_changed=False):
        os.environ["FOO"] = "DEAN"
    assert os.environ["FOO"] == "DEAN"
```

You can use TemporaryEnvironment in a unittest scope as follows
(see tests/example_unittest_test.py):

```python
@TemporaryEnvironment({"USER": "Crowley"})
def test_check(self):
    assert os.environ.get("USER") == "Crowley"
```

## License

Released under the Apache Software License, Version 2.0 (see `LICENSE`):

```
   Copyright (C) 2019 - 2022 James E. King III (@jeking3) <jking@apache.org>
```

## Bugs

Please report any bugs that you find on [GitHub](https://github.com/jeking3/tempenv/issues).
Or, even better, fork the repository on [GitHub](https://github.com/jeking3/tempenv)
and create a pull request (PR). We welcome all changes, big or small, and we
will help you make the PR if you are new to `git` (just ask on the issue and/or
see `CONTRIBUTING.rst`).


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/jeking3/tempenv",
    "name": "tempenv",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "temporary,environment variable,context manager,test,testing",
    "author": "James E. King III",
    "author_email": "jking@apache.org",
    "download_url": "https://files.pythonhosted.org/packages/e7/8c/f7a6b5402f8ec07b0d3a1d2c321d01fab372a633f11b42b5b98a180be403/tempenv-2.0.0.tar.gz",
    "platform": "",
    "description": "# tempenv\n\n[![pypi](https://img.shields.io/pypi/v/tempenv.svg)](https://pypi.python.org/pypi/tempenv)\n[![Build Status](https://github.com/jeking3/tempenv/actions/workflows/ci.yml/badge.svg)](https://github.com/jeking3/tempenv/actions/workflows/ci.yml)\n[![codecov](https://codecov.io/gh/jeking3/tempenv/branch/main/graph/badge.svg)](https://codecov.io/gh/jeking3/tempenv)\n[![Code Style](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n\nManage environment variables in a temporary scope.\n\nSome products use environment variables as a primary means to supply\ncredentials.  To ensure the lifetime of exposed credentials is short,\nwrap them in a TemporaryEnvironment so that they are automatically\ndestroyed on scope exit.\n\nYou can:\n\n- Set or unset environment variables inside a ``with`` code block,\n- Get a warning if the code block modifies one of the environment\n  variables,\n- Optionally bypass restoration of the original environment variable\n  value if the code block modifies the environment variable.\n\n## Install\n\nInstall the latest version of tempenv:\n\n```\npip install tempenv\n```\n\n## Examples\n\nEach of these examples can be found in the tests.\n\nSet some environment variables temporarily:\n(see tests/example_set_test.py):\n\n```python\ndef test_set(self):\n    user_before = os.environ.get(\"USER\")\n    with TemporaryEnvironment({\"USER\": \"nobody\", \"OTHER\": \"foo\"}):\n        assert os.environ.get(\"USER\") == \"nobody\"\n        assert os.environ.get(\"OTHER\") == \"foo\"\n    assert os.environ.get(\"USER\") == user_before\n```\n\nChanging the value to ``None`` will unset the environment variable during\nthe code block\n(see tests/example_unset_test.py):\n\n```python\ndef test_unset(self):\n    os.environ[\"DEBUG\"] = \"1\"\n    with TemporaryEnvironment({\"DEBUG\": None}):\n        assert \"DEBUG\" not in os.environ\n    assert \"DEBUG\" in os.environ\n```\n\nChanging a temporary environment variable during the scope will cause a\nwarning\n(see tests/example_overwrite_test.py):\n\n```python\ndef test_overwritten_in_context(self):\n    with self.assertWarnsRegex(EnvironmentVariableChangedWarning, \"FOO\"):\n        with TemporaryEnvironment({\"FOO\": \"BAR\"}):\n            os.environ[\"FOO\"] = \"SAM\"\n```\n\nIf you set the optional argument ``restore_if_changed=False`` then a change\nduring the scope of the TemporaryEnvironment will not issue a warning and will\nnot restore to the original value\n(see tests/example_ignore_test.py):\n\n```python\ndef test_ignored_overwrite_in_context(self):\n    os.environ[\"FOO\"] = \"BAR\"\n    with TemporaryEnvironment({\"FOO\": \"SAM\"}, restore_if_changed=False):\n        os.environ[\"FOO\"] = \"DEAN\"\n    assert os.environ[\"FOO\"] == \"DEAN\"\n```\n\nYou can use TemporaryEnvironment in a unittest scope as follows\n(see tests/example_unittest_test.py):\n\n```python\n@TemporaryEnvironment({\"USER\": \"Crowley\"})\ndef test_check(self):\n    assert os.environ.get(\"USER\") == \"Crowley\"\n```\n\n## License\n\nReleased under the Apache Software License, Version 2.0 (see `LICENSE`):\n\n```\n   Copyright (C) 2019 - 2022 James E. King III (@jeking3) <jking@apache.org>\n```\n\n## Bugs\n\nPlease report any bugs that you find on [GitHub](https://github.com/jeking3/tempenv/issues).\nOr, even better, fork the repository on [GitHub](https://github.com/jeking3/tempenv)\nand create a pull request (PR). We welcome all changes, big or small, and we\nwill help you make the PR if you are new to `git` (just ask on the issue and/or\nsee `CONTRIBUTING.rst`).\n\n",
    "bugtrack_url": null,
    "license": "Apache License 2.0",
    "summary": "Environment Variable Context Manager",
    "version": "2.0.0",
    "project_urls": {
        "Download": "https://github.com/jeking3/tempenv/archive/main.zip",
        "Homepage": "https://github.com/jeking3/tempenv"
    },
    "split_keywords": [
        "temporary",
        "environment variable",
        "context manager",
        "test",
        "testing"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2c4b4fe6de3ff063f5b86ebbd0e7f0d684cbc3962a16e3d7fab8acf8674ba4d5",
                "md5": "449cb022750587531f41563cc35721bc",
                "sha256": "942766e7c1decd898c81c8a4138d236df6685df42b13e739012d62d92eef35dc"
            },
            "downloads": -1,
            "filename": "tempenv-2.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "449cb022750587531f41563cc35721bc",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 9371,
            "upload_time": "2022-01-22T17:11:54",
            "upload_time_iso_8601": "2022-01-22T17:11:54.446819Z",
            "url": "https://files.pythonhosted.org/packages/2c/4b/4fe6de3ff063f5b86ebbd0e7f0d684cbc3962a16e3d7fab8acf8674ba4d5/tempenv-2.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e78cf7a6b5402f8ec07b0d3a1d2c321d01fab372a633f11b42b5b98a180be403",
                "md5": "e2a629c4d796135f5d271b06408de678",
                "sha256": "cdd29b419d701b177e358052d4ab362b66a39fe030ca0a30cf720ea8e4f35d54"
            },
            "downloads": -1,
            "filename": "tempenv-2.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "e2a629c4d796135f5d271b06408de678",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 10008,
            "upload_time": "2022-01-22T17:11:55",
            "upload_time_iso_8601": "2022-01-22T17:11:55.545261Z",
            "url": "https://files.pythonhosted.org/packages/e7/8c/f7a6b5402f8ec07b0d3a1d2c321d01fab372a633f11b42b5b98a180be403/tempenv-2.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-01-22 17:11:55",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "jeking3",
    "github_project": "tempenv",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "tox": true,
    "lcname": "tempenv"
}
        
Elapsed time: 0.07821s