-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rules/python: Add a coverage_tool
attribute to py_runtime
.
#15590
Closed
adam-azarchs
wants to merge
24
commits into
bazelbuild:master
from
adam-azarchs:azarchs/py_runtime-coverage
Closed
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
72a0f9e
rules/python: Rework built-in coveragepy support
TLATER 80a6cb9
Add a coverage_tool attribute to py_runtime target.
adam-azarchs 55f1aed
Merge commit '72a0f9ed13e4b1f561aee4268e4919b84ac04395' into azarchs/…
adam-azarchs de88f81
Fix up filenames in pycoverage-generated lcov output.
adam-azarchs 437934e
Properly add runfiles for coverage target.
adam-azarchs 37b4286
Add integration test for python coverage.
adam-azarchs b472c21
Update documentation.
adam-azarchs f2c9cd2
Add requires-network to bazel_coverage_hermetic_py_test.
adam-azarchs 57d88fb
Deduplicate cov_tool path entry.
adam-azarchs e9d5582
Fix bad merge conflict resolution.
adam-azarchs 9bb352d
Use a mock coverage tool in unit test.
adam-azarchs 4d628d3
Merge remote-tracking branch 'origin/master' into azarchs/py_runtime-…
adam-azarchs 1746437
Don't use parse_intermixed_args().
adam-azarchs fed8618
Make mock_coverage.py executable.
adam-azarchs baed9a2
Remove platform constraints from test python_toolchain.
adam-azarchs 337f928
Address comments from code review.
adam-azarchs fc21d6a
Merge remote-tracking branch 'origin/master' into azarchs/py_runtime-…
adam-azarchs 7c9b34f
Ignore failures unlinking the rcfile.
adam-azarchs 4550f70
Fix docstring style.
adam-azarchs ec50345
Detail the requrements for the coverage_tool.
adam-azarchs 3b892de
Factor out VERBOSE_COVERAGE handling in stub.
adam-azarchs 1e8ee96
Merge remote-tracking branch 'origin/master' into azarchs/py_runtime-…
adam-azarchs 4242f74
Merge remote-tracking branch 'origin/master' into azarchs/py_runtime-…
adam-azarchs b95db8e
Merge branch 'bazelbuild:master' into azarchs/py_runtime-coverage
adam-azarchs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -92,12 +92,20 @@ public boolean prohibitHyphensInPackagePaths() { | |||||||||
public void collectRunfilesForBinary( | ||||||||||
RuleContext ruleContext, Runfiles.Builder builder, PyCommon common, CcInfo ccInfo) { | ||||||||||
addRuntime(ruleContext, common, builder); | ||||||||||
// select() and build configuration should ideally remove coverage as | ||||||||||
// as dependency, but guard against including it at runtime just in case. | ||||||||||
if (ruleContext.getConfiguration().isCodeCoverageEnabled()) { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
addCoverageSupport(ruleContext, common, builder); | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
@Override | ||||||||||
public void collectDefaultRunfilesForBinary( | ||||||||||
RuleContext ruleContext, PyCommon common, Runfiles.Builder builder) { | ||||||||||
addRuntime(ruleContext, common, builder); | ||||||||||
if (ruleContext.getConfiguration().isCodeCoverageEnabled()) { | ||||||||||
addCoverageSupport(ruleContext, common, builder); | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
@Override | ||||||||||
|
@@ -154,6 +162,9 @@ private static void createStubFile( | |||||||||
// first-stage. | ||||||||||
String pythonBinary = getPythonBinary(ruleContext, common, bazelConfig); | ||||||||||
|
||||||||||
// The python code coverage tool to use, if any. | ||||||||||
String coverageTool = getCoverageTool(ruleContext, common, bazelConfig); | ||||||||||
|
||||||||||
// Version information for host config diagnostic warning. | ||||||||||
PythonVersion attrVersion = PyCommon.readPythonVersionFromAttribute(ruleContext.attributes()); | ||||||||||
boolean attrVersionSpecifiedExplicitly = attrVersion != null; | ||||||||||
|
@@ -172,6 +183,7 @@ private static void createStubFile( | |||||||||
Substitution.of( | ||||||||||
"%main%", common.determineMainExecutableSource(/*withWorkspaceName=*/ true)), | ||||||||||
Substitution.of("%python_binary%", pythonBinary), | ||||||||||
Substitution.of("%coverage_tool%", coverageTool == null ? "" : coverageTool), | ||||||||||
Substitution.of("%imports%", Joiner.on(":").join(common.getImports().toList())), | ||||||||||
Substitution.of("%workspace_name%", ruleContext.getWorkspaceName()), | ||||||||||
Substitution.of("%is_zipfile%", boolToLiteral(isForZipFile)), | ||||||||||
|
@@ -461,6 +473,32 @@ private static String getPythonBinary( | |||||||||
return pythonBinary; | ||||||||||
} | ||||||||||
|
||||||||||
private static void addCoverageSupport( | ||||||||||
RuleContext ruleContext, PyCommon common, Runfiles.Builder builder) { | ||||||||||
PyRuntimeInfo provider = getRuntime(ruleContext, common); | ||||||||||
if (provider != null && provider.getCoverageTool() != null) { | ||||||||||
builder.addArtifact(provider.getCoverageTool()); | ||||||||||
builder.addTransitiveArtifacts(provider.getCoverageToolFiles()); | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
@Nullable | ||||||||||
private static String getCoverageTool( | ||||||||||
RuleContext ruleContext, PyCommon common, BazelPythonConfiguration bazelConfig) { | ||||||||||
if (!ruleContext.getConfiguration().isCodeCoverageEnabled()) { | ||||||||||
return null; | ||||||||||
} | ||||||||||
String coverageTool = null; | ||||||||||
PyRuntimeInfo provider = getRuntime(ruleContext, common); | ||||||||||
if (provider != null && provider.getCoverageTool() != null) { | ||||||||||
PathFragment workspaceName = | ||||||||||
PathFragment.create(ruleContext.getRule().getPackage().getWorkspaceName()); | ||||||||||
coverageTool = | ||||||||||
workspaceName.getRelative(provider.getCoverageTool().getRunfilesPath()).getPathString(); | ||||||||||
} | ||||||||||
return coverageTool; | ||||||||||
} | ||||||||||
|
||||||||||
private static String getStubShebang(RuleContext ruleContext, PyCommon common) { | ||||||||||
PyRuntimeInfo provider = getRuntime(ruleContext, common); | ||||||||||
if (provider != null) { | ||||||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is probably an optimization to be made somewhere in here to only have the coverage dependency when coverage is enabled. The isCodeCoverage() check in the Java code prevents it from being included at runtime, but it still causes Bazel to do various target and config resolution steps.
Coverage being enabled can be detected with a config setting, e.g.
I'm not sure how that fits into toolchain registration; can selects be used within WORKSPACE?
In any case, I'm find with figuring this out in a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Declaration of the toolchain is in a BUILD file, not the WORKSPACE. However, I am not sure either about how
select
gets evaluated in toolchain resolution. As I commented elsewhere I think eventually this bit of the doc should be just baked intopython_register_toolchains
(inrules_python
) so that most users don't have to care about it.