conan-io / conan-io/docs

[question] How to get environment variable from the tested reference to the test_package test()?

Open
#3,876 4 comments 0 reactions 1 assignee Claimed by @memsharded View on GitHub
Dominant language
C++
Stars
125
Forks
382
Avg merge
22h 39m
Merged PRs (30d)
21

Description

### What is your question?

Hi!

We are porting a recipe which is publishing an env var (in its `package_info()`), and this variable is tested by the test_package `test()` method.

We read several pages from the documentation:
* https://docs.conan.io/2/reference/tools/env/virtualrunenv.html
* https://docs.conan.io/2/reference/conanfile/methods/package_info.html
* https://docs.conan.io/2/reference/conanfile/attributes.html#runenv-info
* https://docs.conan.io/2/reference/conanfile/methods/test.html
* https://docs.conan.io/2/reference/conanfile/running_and_output.html#running-commands
* https://docs.conan.io/2/tutorial/creating_packages/test_conan_packages.html

But none seems to indicate any limitation regarding the run environment not being propagated to the `test()` method, nor any specific requirement to retrieve environment variables.

(side note: the doc for [`conanfile.run()`](https://docs.conan.io/2/reference/conanfile/running_and_output.html#running-commands) mentions an `env` parameter, but does not describe it).

Due to this lack of guidance, we tried several approaches in the upstream to try and forward the env var:

def package_info(self):
self.runenv_info.define_path("WIX", self.package_folder)

bindir = os.path.join(self.package_folder, "bin")
self.cpp_info.bindirs = [bindir] # By the doc, this should be automatically appended to path for both Run and Build envs
self.env_info.PATH.append(bindir) # as found in CMake recipe

And made several attempts to retrieve `WIX` or `PATH` environment variables:

def test(self):
w = os.environ.get("PATH", "")
self.output.info(f"Path: {w}")

w = os.environ.get("WIX", "")
self.output.info(f"Wix: {w}")

self.run("echo %WIX%")

self.run("echo %WIX%", env="conanrun")

Sadly, the `WIX` variable remains undefined, and the `PATH` variable is not appended with the value of `bindir`.

Accessing the package environment from its test package would seems like a common requirement, yet as you could probably infer it generated a lot of frustration and confusion.

### Have you read the CONTRIBUTING guide?

- [X] I've read the CONTRIBUTING guide

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.