bazel-skylib icon indicating copy to clipboard operation
bazel-skylib copied to clipboard

Analysis tests cannot see TestEnvironment added environment variables in actions

Open rickeylev opened this issue 4 years ago • 2 comments

Test rules can return testing.TestEnvironment to include custom environment variables for when the test is run. This works, however, they are not visible in the action object that an analysis test sees.

(incomplete) repro:

def foo_test_impl(ctx):
  return [testing.TestEnvironment({"FOO": "BAR"})]

def verify_foo_test(ctx):
  env = analysistest.begin(ctx)
  actions = analysistest.target_actions(env)
  for a in actions:
    print(a.env)
  return analysistest.end(env)

Expected: The "Testing ..." action should have FOO=BAR as one of its environment variable.

Actual: The FOO=BAR env var is not shown in the action's env values.

rickeylev avatar Nov 20 '21 03:11 rickeylev

@rickeylev I briefly looked into this, but already failed to get the actual test action in Starlark. All I'm seeing is:

[action 'Writing script test/undertest', action 'Creating source manifest for //test:undertest', action 'Creating runfiles tree bazel-out/k8-fastbuild/bin/test/undertest.runfiles', runfiles for //test:undertest]

If I were able to get the Testing ... action, I think I know how to fix this in Bazel and write a test for it. Could you give me some pointers?

fmeum avatar Jun 08 '22 13:06 fmeum

Confirmed. But I think this is a bug in Bazel, not in Skylib.

Note that in Bazel 6, testing.TestEnvironment is deprecated in favor of RunEnvironmentInfo, but the environment set by RunEnvironmentInfo is also not visible in analysis tests.

tetromino avatar Jul 13 '22 19:07 tetromino