generated from oracle/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: use partial clone to reduce clone time (#389)
Signed-off-by: Nathan Nguyen <[email protected]> Signed-off-by: Trong Nhan Mai <[email protected]> Co-authored-by: Trong Nhan Mai <[email protected]>
- Loading branch information
Showing
4 changed files
with
143 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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 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,45 @@ | ||
# Copyright (c) 2023 - 2023, Oracle and/or its affiliates. All rights reserved. | ||
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. | ||
|
||
"""Helper functions related to environment variables.""" | ||
|
||
import os | ||
from collections.abc import Mapping | ||
|
||
|
||
def get_patched_env( | ||
patch: Mapping[str, str | None], | ||
_env: dict[str, str] | None = None, | ||
) -> dict[str, str]: | ||
"""Return a dictionary whose elements copied from ``os.environ`` and are updated according to ``patch``. | ||
This function does not modify ``os.environ``. | ||
Parameters | ||
---------- | ||
patch : Mapping[str, str | None] | ||
A mapping (immutable) in which: | ||
- each key is an environment variable. | ||
- each value is the value to set to the corresponding environment variable. | ||
If value is ``None``, the environment variable is "unset". | ||
_env : dict[str, str] | None | ||
The environment being updated. | ||
This is ``None`` by default, in which case ``os.environ`` is being updated. | ||
Returns | ||
------- | ||
dict[str, str] | ||
The the dictionary contains the patched env variables. | ||
""" | ||
env = os.environ if _env is None else _env | ||
|
||
# Make a copy of the environment. | ||
copied_env = dict(env) | ||
|
||
for var, value in patch.items(): | ||
if value is None: | ||
copied_env.pop(var, None) | ||
else: | ||
copied_env[var] = value | ||
|
||
return copied_env |
This file contains 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 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,51 @@ | ||
# Copyright (c) 2023 - 2023, Oracle and/or its affiliates. All rights reserved. | ||
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. | ||
|
||
"""Tests for helper functions related to environment variables.""" | ||
|
||
import pytest | ||
|
||
from macaron.environment_variables import get_patched_env | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("before", "patch", "expect"), | ||
[ | ||
pytest.param( | ||
{"FOO": "some-value"}, | ||
{}, | ||
{"FOO": "some-value"}, | ||
id="patch is empty", | ||
), | ||
pytest.param( | ||
{"FOO": "some-value"}, | ||
{"GIT_TERMINAL_PROMPT": "0"}, | ||
{ | ||
"FOO": "some-value", | ||
"GIT_TERMINAL_PROMPT": "0", | ||
}, | ||
id="patch adding a variable", | ||
), | ||
pytest.param( | ||
{"GIT_TERMINAL_PROMPT": "1"}, | ||
{"GIT_TERMINAL_PROMPT": "0"}, | ||
{"GIT_TERMINAL_PROMPT": "0"}, | ||
id="patch overriding a variable", | ||
), | ||
pytest.param( | ||
{"GIT_TERMINAL_PROMPT": "0"}, | ||
{"GIT_TERMINAL_PROMPT": None}, | ||
{}, | ||
id="patch removing a variable", | ||
), | ||
], | ||
) | ||
def test_patched_env( | ||
before: dict[str, str], | ||
patch: dict[str, str | None], | ||
expect: dict[str, str], | ||
) -> None: | ||
"""Tests for the ``get_patched_env`` helper function.""" | ||
env = dict(before) | ||
|
||
assert get_patched_env(patch, env) == expect |