Skip to content

[feature request] support exporting start_line of region in JSON report #2110

Description

@silverzhaojr

Is your feature request related to a problem? Please describe.

In the index page of "Functions" or "Classes" HTML report, if I click a function or class, it will go to the start line of the function or class in the related source file html page, which is quite convenient.

Details
# https://github.com/coveragepy/coveragepy/blob/7.13.0/coverage/html.py#L604
602                    page_data.summaries.append(
603                        IndexItem(
604                            url=f"{ftr.html_filename}#t{region.start}",
605                            file=escape(ftr.fr.relative_filename()),
606                            description=(
607                                f"<data value='{escape(sorting_name)}'>"
608                                + escape(region.name)
609                                + "</data>"
610                            ),
611                            nums=analysis.numbers,
612                        )

We want this (region.start) in the JSON report also, it can be very helpful.

Describe the solution you'd like

# cat test.py
1 # test.py
2 def add(a, b):
3   return a + b
4 
5 print(add(2, 7))

We can add a field start_line in the "functions" part, like:

sample JSON report
            "functions": {
                "add": {
                    "start_line": 2,        // <== HERE
                    "executed_lines": [
                        3
                    ],
                    "summary": {
                        "covered_lines": 1,
                        "num_statements": 1,
                        "percent_covered": 100.0,
                        "percent_covered_display": "100",
                        "missing_lines": 0,
                        "excluded_lines": 0,
                        "percent_statements_covered": 100.0,
                        "percent_statements_covered_display": "100"
                    },
                    "missing_lines": [],
                    "excluded_lines": []
                },

Describe alternatives you've considered

I tried to make a diff like this and it works in my local env:

Details
diff --git a/coverage/jsonreport.py b/coverage/jsonreport.py
index 1aad2e15..77fdc4e3 100644
--- a/coverage/jsonreport.py
+++ b/coverage/jsonreport.py
@@ -151,19 +151,22 @@ class JsonReporter:
                 region_data[region.name] = self.make_region_data(
                     coverage_data,
                     narrower.narrow(region.lines),
+                    region.start,
                 )
 
             region_data[""] = self.make_region_data(
                 coverage_data,
                 narrower.narrow(outside_lines),
+                min(outside_lines) if outside_lines else 0,
             )
         return reported_file
 
-    def make_region_data(self, coverage_data: CoverageData, narrowed_analysis: Analysis) -> JsonObj:
+    def make_region_data(self, coverage_data: CoverageData, narrowed_analysis: Analysis, start_line: int) -> JsonObj:
         """Create the data object for one region of a file."""
         narrowed_nums = narrowed_analysis.numbers
         narrowed_summary = self.make_summary(narrowed_nums)
         this_region = {
+            "start_line": start_line,
             "executed_lines": sorted(narrowed_analysis.executed),
             "summary": narrowed_summary,
             "missing_lines": sorted(narrowed_analysis.missing),

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions