Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions conda_build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -3483,6 +3483,19 @@ def test(
if metadata.config.remove_work_dir:
env["SRC_DIR"] = metadata.config.test_dir

for var in utils.ensure_list(metadata.get_value("test/script_env")):
if "=" in var:
val = var.split("=", 1)[1]
var = var.split("=", 1)[0]
env[var] = val
elif var not in os.environ:
warnings.warn(
f"The environment variable '{var}' specified in test/script_env is undefined.",
UserWarning,
)
else:
env[var] = os.environ[var]

test_script, _ = write_test_scripts(
metadata, env, py_files, pl_files, lua_files, r_files, shell_files, trace
)
Expand Down
1 change: 1 addition & 0 deletions conda_build/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,7 @@ def parse(data, config, path=None):
"files": list,
"imports": list,
"source_files": list,
"script_env": list,
"downstreams": list,
},
"about": {
Expand Down
36 changes: 36 additions & 0 deletions docs/source/resources/define-metadata.rst
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,8 @@ disable this with:
include_recipe: False


.. _script-env:

Use environment variables
-------------------------

Expand Down Expand Up @@ -1282,6 +1284,40 @@ Commands that are run as part of the test.
- bspatch4 -h


.. _test-script-env:

Test environment variables
---------------------------

Pass environment variables through to the test environment.
This works identically to :ref:`build/script_env <script-env>`.

.. code-block:: yaml

test:
script_env:
- MYVAR
- ANOTHER_VAR

If a listed environment variable is missing from the environment
seen by the conda-build process itself, a UserWarning is
emitted during the test phase and the variable remains
undefined.

Values can also be set inline with ``=`` syntax:

.. code-block:: yaml

test:
script_env:
- MY_VAR=some value

.. note::
Environment variables inherited via ``test/script_env`` are only
visible during the test phase, not the build phase. Use
:ref:`build/script_env <script-env>` for build-time variables.


Python imports
--------------

Expand Down
10 changes: 10 additions & 0 deletions docs/source/user-guide/environment-variables.rst
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,16 @@ Additionally, values can be set by including ``=`` followed by the desired value
this feature with caution or explicitly set values using the ``=``
syntax.

This feature can also be used in the :ref:`test section <test-script-env>` of
``meta.yaml`` to pass environment variables to the test phase:

.. code-block:: yaml

test:
script_env:
- TMPDIR
- MY_VAR=some value

.. note::
If you split your build and test phases with ``--no-test`` and ``--test``,
you need to ensure that the environment variables present at build time and test
Expand Down
7 changes: 7 additions & 0 deletions news/4940-test-script-env
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### Enhancements

* Allow setting environment variables in the test section via `test/script_env`, matching the existing `build/script_env` feature. (#4940)

### Docs

* Document `test/script_env` in the test section reference.
47 changes: 47 additions & 0 deletions tests/test_api_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -2008,6 +2008,53 @@ def assert_keyword(keyword):
os.environ.pop(token)


@pytest.mark.sanity
def test_test_script_env_warnings(testing_config, testing_workdir, recwarn):
os.makedirs("recipe")
with open("recipe/meta.yaml", "w") as f:
f.write("""
package:
name: test-script-env-test
version: "1.0"

build:
number: 0

test:
script_env:
- TEST_VAR
commands:
- echo "test"
""")
for empty in ("build.sh", "bld.bat"):
open(f"recipe/{empty}", "w").close()

token = "TEST_VAR"

def assert_undefined_warning_present():
messages = [str(w.message) for w in recwarn.list]
assert any(
token in m and "test/script_env" in m and "undefined" in m for m in messages
)

def assert_undefined_warning_absent():
messages = [str(w.message) for w in recwarn.list]
assert not any(
token in m and "test/script_env" in m and "undefined" in m for m in messages
)

api.build("recipe", config=testing_config)
assert_undefined_warning_present()
recwarn.clear()

os.environ[token] = "value"
try:
api.build("recipe", config=testing_config)
assert_undefined_warning_absent()
finally:
os.environ.pop(token)


@pytest.mark.slow
def test_activated_prefixes_in_actual_path(testing_metadata):
"""
Expand Down
Loading