Title: test_git_work_tree_env renders os.environ inert in unpatching attempt · Issue #1671 · gitpython-developers/GitPython · GitHub
Open Graph Title: test_git_work_tree_env renders os.environ inert in unpatching attempt · Issue #1671 · gitpython-developers/GitPython
X Title: test_git_work_tree_env renders os.environ inert in unpatching attempt · Issue #1671 · gitpython-developers/GitPython
Description: TestRepo.test_git_work_tree_env contains this code, which is intended to patch and unpatch two environment variables: GitPython/test/test_repo.py Lines 1341 to 1350 in a5a6464 oldenv = os.environ.copy() os.environ["GIT_DIR"] = new_git_di...
Open Graph Description: TestRepo.test_git_work_tree_env contains this code, which is intended to patch and unpatch two environment variables: GitPython/test/test_repo.py Lines 1341 to 1350 in a5a6464 oldenv = os.environ.c...
X Description: TestRepo.test_git_work_tree_env contains this code, which is intended to patch and unpatch two environment variables: GitPython/test/test_repo.py Lines 1341 to 1350 in a5a6464 oldenv = os.environ.c...
Opengraph URL: https://github.com/gitpython-developers/GitPython/issues/1671
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"test_git_work_tree_env renders os.environ inert in unpatching attempt","articleBody":"`TestRepo.test_git_work_tree_env` contains this code, which is intended to patch and unpatch two environment variables:\r\n\r\nhttps://github.com/gitpython-developers/GitPython/blob/a5a646494393478c65f26cd3a921f3505219d3e1/test/test_repo.py#L1341-L1350\r\n\r\nHowever, that does not unpatch the variables, and more importantly, it prevents writes through `os.environ` from ever actually setting environment variables (to be inherited automatically by subprocesses) for the rest of the process's lifetime.\r\n\r\n```text\r\n\u003e\u003e\u003e import os\r\n\u003e\u003e\u003e type(os.environ)\r\n\u003cclass 'os._Environ'\u003e\r\n\u003e\u003e\u003e oldenv = os.environ.copy()\r\n\u003e\u003e\u003e type(oldenv)\r\n\u003cclass 'dict'\u003e\r\n\u003e\u003e\u003e os.system(\"printenv FOO\")\r\n256\r\n\u003e\u003e\u003e os.environ[\"FOO\"] = \"bar\"\r\n\u003e\u003e\u003e os.system(\"printenv FOO\")\r\nbar\r\n0\r\n\u003e\u003e\u003e os.environ = oldenv\r\n\u003e\u003e\u003e os.system(\"printenv FOO\")\r\nbar\r\n0\r\n\u003e\u003e\u003e os.environ[\"BAZ\"] = \"quux\"\r\n\u003e\u003e\u003e os.system(\"printenv BAZ\")\r\n256\r\n```\r\n\r\nThis happens because, all types in Python being reference types, the `environ` attribute of `os` merely *refers to* the object of a special mutable mapping type that updates the process's environment variables when mutated. Calling its `copy` method constructs a `dict` object with the same items. Assigning that `dict` back to `os.environ` does not modify the original `os.environ` object in any way, but instead causes the `environ` attribute of `os` to point to the `dict` object from that point forward. From then on, writes to `os.environ` have no effect on the running process's environment variables.\r\n\r\nYet, this does not cause any other tests to fail, nor do any currently passing tests fail as a result of fixing it. I believe this is for three reasons, which I list in descending order of what I *guess* to be their significance:\r\n\r\n1. When GitPython runs `git`, it doesn't rely on automatically passing its own environment variables to the `git` subprocess. Instead, it builds a modified environment based on its own and passes that explicitly. The way it finds out about its own environment is by consulting `os.environ`, which doesn't actually have to work for setting environment variables, because `Popen` is doing it:\r\n\r\n https://github.com/gitpython-developers/GitPython/blob/a5a646494393478c65f26cd3a921f3505219d3e1/git/cmd.py#L948-L955\r\n\r\n https://github.com/gitpython-developers/GitPython/blob/a5a646494393478c65f26cd3a921f3505219d3e1/git/cmd.py#L987-L989\r\n\r\n2. When GitPython, or any library it uses if that library is written in Python, or the testing framework, accesses and changes environment variables for use within the process, that is also nearly always via `os.environ`, so again, it's okay if the real environment is not used.\r\n\r\n3. The test is in `test_submodules.py`, and test modules are usually exercised in alphabetical order, with only `test_tree.py` and `test_util.py` coming after it.\r\n\r\nThis only affects the tests, and it can be solved in any way appropriate for tests. Specifically, it can be solved--and also that test code significantly simplified--by patching `os.environ` with `unittest.mock.patch.dict`. We cannot use this in the code of the `git` module, because it upcases environment variable names (#1646). But it is not a problem to use it in tests (besides the test in #1650 that #1646 is fixed), and it is already being used in a few places in the test suite.\r\n\r\n---\r\n\r\nThe way I found out about this was that `flake8` reported it once I removed `test/` from its excluded directories:\r\n\r\n```text\r\ntest/test_repo.py:1350:13: B003 Assigning to `os.environ` doesn't clear the environment. Subprocesses are going to see outdated variables, in disagreement with the current process. Use `os.environ.clear()` or the `env=` argument to Popen.\r\n os.environ = oldenv\r\n ^\r\n```\r\n\r\nThe specific suggestions aren't quite applicable here (we do need to mutate `os.environ`, and to do so less drastically than with `clear`), but that message nonetheless accurately identifies the problem, which seems previously to have gone undetected. I think it is a good idea to enable `flake8` for `test/` as well as `git/` (though perhaps sometime soon we might manage to replace `flake8` with [`ruff`](https://docs.astral.sh/ruff/)). I plan to include a fix for this, and also for less serious, mostly merely stylistic issues found while examining `test/` with the help of `flake8`, in the same PR that fixes #1670.","author":{"url":"https://github.com/EliahKagan","@type":"Person","name":"EliahKagan"},"datePublished":"2023-09-21T09:46:18.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/1671/GitPython/issues/1671"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:6998b9fc-83bc-dee2-5375-d3c6528f9fdb |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D7F8:24D534:4B8D78:6953E5:696897F0 |
| html-safe-nonce | 5f079cee5d5815c5e67fe95854295462b65454eb8bea579393b47d520ae402e7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEN0Y4OjI0RDUzNDo0QjhENzg6Njk1M0U1OjY5Njg5N0YwIiwidmlzaXRvcl9pZCI6Ijg4MTc0NjM1NDQyOTA3NzcwNzIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | b3ae7bdc7e437e8d2fafe70b743a53629dc188473de2d697f0ad2ec07ed267a4 |
| hovercard-subject-tag | issue:1906563191 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/gitpython-developers/GitPython/1671/issue_layout |
| twitter:image | https://opengraph.githubassets.com/38ff1f116c1111f31c14c23750e9f31fcc7a6740fa0b7a95d5c0780611ca3b98/gitpython-developers/GitPython/issues/1671 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/38ff1f116c1111f31c14c23750e9f31fcc7a6740fa0b7a95d5c0780611ca3b98/gitpython-developers/GitPython/issues/1671 |
| og:image:alt | TestRepo.test_git_work_tree_env contains this code, which is intended to patch and unpatch two environment variables: GitPython/test/test_repo.py Lines 1341 to 1350 in a5a6464 oldenv = os.environ.c... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | EliahKagan |
| hostname | github.com |
| expected-hostname | github.com |
| None | 50f46dc2d6192249fd8ebf20e76c800f4f2596d4a5f3ab63dd63a754df154f54 |
| turbo-cache-control | no-preview |
| go-import | github.com/gitpython-developers/GitPython git https://github.com/gitpython-developers/GitPython.git |
| octolytics-dimension-user_id | 503709 |
| octolytics-dimension-user_login | gitpython-developers |
| octolytics-dimension-repository_id | 1126087 |
| octolytics-dimension-repository_nwo | gitpython-developers/GitPython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1126087 |
| octolytics-dimension-repository_network_root_nwo | gitpython-developers/GitPython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | fef287f17234b4529a4b112a3d47fe8551e32ddd |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width