-
Notifications
You must be signed in to change notification settings - Fork 24
Pass globals context to env block template rendering #221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f74417b
Pass globals context to env block template rendering
anticomputer c895b04
Address PR feedback: docstring, reserved key filtering, tests
anticomputer 5fd61b2
Fix lint: use pytest.raises instead of assertRaises (PT027)
anticomputer a8fcc23
Address PR feedback: rollback partial env, fail on bad templates
anticomputer 87d5078
Merge branch 'main' into anticomputer/env-globals
anticomputer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| # SPDX-FileCopyrightText: GitHub, Inc. | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
| """Tests for env_utils: swap_env and TmpEnv with globals context.""" | ||
|
|
||
| import os | ||
| import unittest | ||
|
|
||
| import pytest | ||
|
|
||
| from seclab_taskflow_agent.env_utils import TmpEnv, swap_env | ||
|
|
||
|
|
||
| class TestSwapEnv(unittest.TestCase): | ||
| """Tests for swap_env template rendering.""" | ||
|
|
||
|
anticomputer marked this conversation as resolved.
Outdated
|
||
| def test_plain_string_unchanged(self): | ||
| assert swap_env("no templates here") == "no templates here" | ||
|
|
||
| def test_env_function_works(self): | ||
| os.environ["TEST_SWAP_ENV_VAR"] = "hello" | ||
| try: | ||
| assert swap_env('{{ env("TEST_SWAP_ENV_VAR") }}') == "hello" | ||
| finally: | ||
| del os.environ["TEST_SWAP_ENV_VAR"] | ||
|
|
||
| def test_globals_with_context(self): | ||
| result = swap_env( | ||
| "key-{{ globals.ghsa_id }}", | ||
| context={"globals": {"ghsa_id": "GHSA-1234"}}, | ||
| ) | ||
| assert result == "key-GHSA-1234" | ||
|
|
||
| def test_globals_without_context_raises(self): | ||
| with pytest.raises(LookupError): | ||
| swap_env("{{ globals.missing }}") | ||
|
|
||
| def test_context_cannot_override_env_helper(self): | ||
| """Passing an 'env' key in context must not shadow the env() function.""" | ||
| os.environ["TEST_SWAP_RESERVED"] = "works" | ||
| try: | ||
| result = swap_env( | ||
| '{{ env("TEST_SWAP_RESERVED") }}', | ||
| context={"env": "should be filtered"}, | ||
| ) | ||
| assert result == "works" | ||
| finally: | ||
| del os.environ["TEST_SWAP_RESERVED"] | ||
|
|
||
| def test_no_context_backward_compat(self): | ||
| assert swap_env("plain") == "plain" | ||
|
|
||
|
|
||
| class TestTmpEnv(unittest.TestCase): | ||
| """Tests for TmpEnv context manager with globals.""" | ||
|
|
||
| def test_globals_rendered_in_env_block(self): | ||
| env = {"MY_KEY": "pvr-{{ globals.ghsa }}"} | ||
| ctx = {"globals": {"ghsa": "GHSA-5678"}} | ||
| with TmpEnv(env, context=ctx): | ||
| assert os.environ["MY_KEY"] == "pvr-GHSA-5678" | ||
| assert "MY_KEY" not in os.environ | ||
|
|
||
| def test_env_function_still_works_in_tmpenv(self): | ||
| os.environ["SOURCE_VAR"] = "value" | ||
| try: | ||
| env = {"DEST_VAR": '{{ env("SOURCE_VAR") }}'} | ||
| with TmpEnv(env): | ||
| assert os.environ["DEST_VAR"] == "value" | ||
| finally: | ||
| del os.environ["SOURCE_VAR"] | ||
|
|
||
| def test_tmpenv_restores_original(self): | ||
| os.environ["RESTORE_TEST"] = "original" | ||
| env = {"RESTORE_TEST": "overwritten"} | ||
| with TmpEnv(env): | ||
| assert os.environ["RESTORE_TEST"] == "overwritten" | ||
| assert os.environ["RESTORE_TEST"] == "original" | ||
| del os.environ["RESTORE_TEST"] | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.