Skip to content

Commit

Permalink
WIP: rules/python: Add PYTHON_COVERAGE_TARGET
Browse files Browse the repository at this point in the history
This resolves #14436 by adding a new environment variable that will
perform the coverage label resolution inside the python_stub_template
directly, which resolves the python coverage tool by label rather than
path.

Currently this resolves the path in the runfiles directory by guessing
the path the label should resolve to - this of course does not work in
general, even just defining an alias breaks it. Since labels appear to
only be resolved in the analysis phase, there does not seem to be an
easy way around this, however.

This is a draft, showing the behavior I would like - suggestions on
how to correctly implement this would be appreciated.

Co-authored-by: Bradley Burns <bradley.burns@codethink.co.uk>
  • Loading branch information
TLATER and bradb423 committed Feb 2, 2022
1 parent 72a0f9e commit f4f1c23
Showing 1 changed file with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,17 @@ def Deduplicate(items):
seen.add(it)
yield it

def target_to_filepath(build_target, module_space):
# TODO (tlater): Find a way to properly resolve this at execution
# time, rather than relying on label syntax matching up to a
# specific location in runfiles.
build_target = build_target.replace("@", "/")
build_target = build_target.replace("//", "/")
build_target = build_target.replace(":", "/")

coverage_entry_point = os.path.join(module_space, build_target.lstrip("/") + ".py")
return coverage_entry_point

def ExecuteFile(python_program, main_filename, args, env, module_space,
coverage_tool=None, workspace=None):
"""Executes the given python file using the various environment settings.
Expand Down Expand Up @@ -400,12 +411,14 @@ def Main():
# COVERAGE_DIR is set iff the instrumentation is configured for the
# file and coverage is enabled.
if os.environ.get('COVERAGE_DIR'):
if 'PYTHON_COVERAGE' in os.environ:
if 'PYTHON_COVERAGE_TARGET' in os.environ:
cov_tool = target_to_filepath(os.environ.get('PYTHON_COVERAGE_TARGET'), module_space)
elif 'PYTHON_COVERAGE' in os.environ:
cov_tool = os.environ.get('PYTHON_COVERAGE')
else:
raise EnvironmentError(
'No python coverage tool set, '
'set PYTHON_COVERAGE '
'set PYTHON_COVERAGE or PYTHON_COVERAGE_TARGET '
'to configure the coverage tool'
)

Expand Down

0 comments on commit f4f1c23

Please sign in to comment.