diff options
| author | Eugene Triguba <hello@eugenetriguba.com> | 2026-05-22 12:56:07 +0000 |
|---|---|---|
| committer | Eugene Triguba <hello@eugenetriguba.com> | 2026-05-22 12:56:07 +0000 |
| commit | cd69f2e52749403eadf035c0186692a0294c58a8 (patch) | |
| tree | 70a16b885f24a3c7f7a25da75657d9f75994eca8 | |
| parent | Add cli (diff) | |
| download | nb-cd69f2e52749403eadf035c0186692a0294c58a8.zip | |
Add env to test
| -rw-r--r-- | nb/test.py | 14 | ||||
| -rw-r--r-- | nbs/test.ipynb | 76 |
2 files changed, 86 insertions, 4 deletions
@@ -1,7 +1,7 @@ # AUTOGENERATED! DO NOT EDIT! # cell: dd03783f-252c-40f5-b92f-0f3f9278ce09 -import ast,copy,contextlib,inspect,operator,textwrap,traceback +import ast,copy,contextlib,inspect,os,operator,textwrap,traceback from typing import Any,Callable,Generator from dataclasses import dataclass @@ -85,3 +85,15 @@ def equal(a, b): def nequal(a, b): "Assert that a != b" expect(a, b, operator.ne, negstr='==', frames_up=2) + +# cell: 664f33bf +@contextlib.contextmanager +def env(**kwargs) -> Generator[None, None, None]: + "Temporarily set environment variables." + old = {k: os.environ.get(k) for k in kwargs} + os.environ.update(kwargs) + try: yield + finally: + for k, v in old.items(): + if v is None: os.environ.pop(k, None) + else: os.environ[k] = v diff --git a/nbs/test.ipynb b/nbs/test.ipynb index 102162c..5301c1a 100644 --- a/nbs/test.ipynb +++ b/nbs/test.ipynb @@ -5,7 +5,16 @@ "execution_count": null, "id": "d42a9120-8d70-4b10-8d6c-b59692fd03ad", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The autoreload extension is already loaded. To reload it, use:\n", + " %reload_ext autoreload\n" + ] + } + ], "source": [ "%load_ext autoreload\n", "%autoreload 2" @@ -29,7 +38,7 @@ "outputs": [], "source": [ "#| export\n", - "import ast,copy,contextlib,inspect,operator,textwrap,traceback\n", + "import ast,copy,contextlib,inspect,os,operator,textwrap,traceback\n", "from typing import Any,Callable,Generator\n", "from dataclasses import dataclass" ] @@ -967,9 +976,70 @@ ] }, { + "cell_type": "markdown", + "id": "33be6990", + "metadata": {}, + "source": [ + "## Environment" + ] + }, + { + "cell_type": "markdown", + "id": "07e106e0", + "metadata": {}, + "source": [ + "`env` allows us to temporarily set environment variables with a context manager. Therefore, if one of our test cases needs a few environment variables set differently, we can easily do that without affecting our broader state." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "664f33bf", + "metadata": {}, + "outputs": [], + "source": [ + "#| export\n", + "@contextlib.contextmanager\n", + "def env(**kwargs) -> Generator[None, None, None]:\n", + " \"Temporarily set environment variables.\"\n", + " old = {k: os.environ.get(k) for k in kwargs}\n", + " os.environ.update(kwargs)\n", + " try: yield\n", + " finally:\n", + " for k, v in old.items():\n", + " if v is None: os.environ.pop(k, None)\n", + " else: os.environ[k] = v" + ] + }, + { + "cell_type": "markdown", + "id": "62d5fa07", + "metadata": {}, + "source": [ + "When you use `env`, you can pass in an arbitrary number of keyword arguments to set new or override existing environment variables. If the environment variable was pre-existing, it will be reset to its previous value outside of the context manager. If it didn't exist previously, it'll be popped out of `os.environ`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "47785cc3", + "metadata": {}, + "outputs": [], + "source": [ + "os.environ['CBA'] = 'SOMEVAL'\n", + "equal(os.environ.get('ABC'), None)\n", + "with env(ABC='123', CBA='321'):\n", + " equal(os.environ.get('ABC'), '123')\n", + " equal(os.environ.get('CBA'), '321')\n", + "equal(os.environ.get('ABC'), None)\n", + "equal(os.environ.get('CBA'), 'SOMEVAL')\n", + "del os.environ['CBA']" + ] + }, + { "cell_type": "code", "execution_count": null, - "id": "7a1393a7-6a89-4c5d-a8a7-51e0fd46d190", + "id": "078e15c8", "metadata": {}, "outputs": [], "source": [ |