Skip to content

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

@Adnn

Description

@Adnn

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:

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() 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?

  • I've read the CONTRIBUTING guide

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions