Skip to content

Commit

Permalink
feat: main index page links to other index pages
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Apr 23, 2024
1 parent 3d47c9f commit e016967
Show file tree
Hide file tree
Showing 57 changed files with 744 additions and 366 deletions.
243 changes: 138 additions & 105 deletions coverage/html.py

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions coverage/htmlfiles/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ <h1>{{ title|escape }}:
</div>
</form>

<h2>
{% for ibtn in index_buttons %}
<a class="button{% if ibtn.current %} current{% endif %}"{% if ibtn.url %} href="{{ ibtn.url }}"{% endif %}>{{ ibtn.label }}</a>{#-#}
{% endfor %}
</h2>

<p class="text">
<a class="nav" href="{{__url__}}">coverage.py v{{__version__}}</a>,
created at {{ time_stamp }}
Expand Down
16 changes: 15 additions & 1 deletion coverage/htmlfiles/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ header .content { padding: 1rem 3.5rem; }

header h2 { margin-top: .5em; font-size: 1em; }

header h2 a.button { font-family: inherit; font-size: inherit; border: 1px solid; border-radius: .2em; background: #eee; color: inherit; text-decoration: none; padding: .1em .5em; margin: 1px calc(.1em + 1px); cursor: pointer; border-color: #ccc; }

@media (prefers-color-scheme: dark) { header h2 a.button { background: #333; } }

@media (prefers-color-scheme: dark) { header h2 a.button { border-color: #444; } }

header h2 a.button.current { border: 2px solid; background: #fff; border-color: #999; cursor: default; }

@media (prefers-color-scheme: dark) { header h2 a.button.current { background: #1e1e1e; } }

@media (prefers-color-scheme: dark) { header h2 a.button.current { border-color: #777; } }

header p.text { margin: .5em 0 -.5em; color: #666; font-style: italic; }

@media (prefers-color-scheme: dark) { header p.text { color: #aaa; } }
Expand Down Expand Up @@ -88,7 +100,9 @@ h1 { font-size: 1.25em; display: inline-block; }

@media (prefers-color-scheme: dark) { #filter_container label { color: #aaa; } }

header button { font-family: inherit; font-size: inherit; border: 1px solid; border-radius: .2em; color: inherit; padding: .1em .5em; margin: 1px calc(.1em + 1px); cursor: pointer; border-color: #ccc; }
header button { font-family: inherit; font-size: inherit; border: 1px solid; border-radius: .2em; background: #eee; color: inherit; text-decoration: none; padding: .1em .5em; margin: 1px calc(.1em + 1px); cursor: pointer; border-color: #ccc; }

@media (prefers-color-scheme: dark) { header button { background: #333; } }

@media (prefers-color-scheme: dark) { header button { border-color: #444; } }

Expand Down
39 changes: 29 additions & 10 deletions coverage/htmlfiles/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,22 @@ $dark-context-bg-color: #056;
}
}

@mixin button-shape {
font-family: inherit;
font-size: inherit;
border: 1px solid;
border-radius: .2em;
background: $light-gray2;
@include background-dark($dark-gray2);
color: inherit;
text-decoration: none;
padding: .1em .5em;
margin: 1px calc(.1em + 1px);
cursor: pointer;
border-color: $light-gray3;
@include border-color-dark($dark-gray3);
}

// Page-wide styles
html, body, h1, h2, h3, p, table, td, th {
margin: 0;
Expand Down Expand Up @@ -177,6 +193,18 @@ header {
h2 {
margin-top: .5em;
font-size: 1em;

a.button {
@include button-shape;
&.current {
border: 2px solid;
background: $light-bg;
@include background-dark($dark-bg);
border-color: $light-gray4;
@include border-color-dark($dark-gray4);
cursor: default;
}
}
}

p.text {
Expand Down Expand Up @@ -274,16 +302,7 @@ h1 {
}

header button {
font-family: inherit;
font-size: inherit;
border: 1px solid;
border-radius: .2em;
color: inherit;
padding: .1em .5em;
margin: 1px calc(.1em + 1px);
cursor: pointer;
border-color: $light-gray3;
@include border-color-dark($dark-gray3);
@include button-shape;
@include focus-border;

&.run {
Expand Down
26 changes: 9 additions & 17 deletions coverage/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,16 @@ def coverage_init(reg, options):

from __future__ import annotations

import dataclasses
import functools

from types import FrameType
from typing import Any, Iterable

from coverage import files
from coverage.misc import _needs_to_implement
from coverage.types import TArc, TConfigurable, TLineNo, TSourceTokenLines
from coverage.types import (
CodeRegion, TArc, TConfigurable, TLineNo, TSourceTokenLines,
)


class CoveragePlugin:
Expand Down Expand Up @@ -345,19 +346,6 @@ def line_number_range(self, frame: FrameType) -> tuple[TLineNo, TLineNo]:
return lineno, lineno


@dataclasses.dataclass
class CodeRegion:
"""Data for each region found by FileReporter.code_regions.
`name`: the description of the regions.
`start`: the first line of the named region.
`lines`: a super-set of the executable source lines in the region.
"""
name: str
start: int
lines: set[int]


@functools.total_ordering
class FileReporter(CoveragePluginBase):
"""Support needed for files during the analysis and reporting phases.
Expand Down Expand Up @@ -557,9 +545,13 @@ def source_token_lines(self) -> TSourceTokenLines:
for line in self.source().splitlines():
yield [("txt", line)]

def code_regions(self) -> dict[str, list[CodeRegion]]:
def code_regions(self) -> Iterable[CodeRegion]:
"""TODO XXX"""
return {}
return []

def code_region_kinds(self) -> Iterable[tuple[str, str]]:
"""TODO XXX"""
return []

def __eq__(self, other: Any) -> bool:
return isinstance(other, FileReporter) and self.filename == other.filename
Expand Down
8 changes: 7 additions & 1 deletion coverage/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,5 +253,11 @@ def should_be_python(self) -> bool:
def source_token_lines(self) -> TSourceTokenLines:
return source_token_lines(self.source())

def code_regions(self) -> dict[str, list[CodeRegion]]:
def code_regions(self) -> Iterable[CodeRegion]:
return code_regions(self.source())

def code_region_kinds(self) -> Iterable[tuple[str, str]]:
return [
("function", "functions"),
("class", "classes"),
]
29 changes: 19 additions & 10 deletions coverage/regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from typing import cast

from coverage.plugin import CodeRegion
from coverage.types import CodeRegion


@dataclasses.dataclass
Expand All @@ -29,10 +29,7 @@ class RegionFinder(ast.NodeVisitor):
"""
def __init__(self) -> None:
self.regions: dict[str, list[CodeRegion]] = {
"function": [],
"class": [],
}
self.regions: list[CodeRegion] = []
self.context: list[Context] = []

def parse_source(self, source: str) -> None:
Expand All @@ -55,8 +52,13 @@ def visit_FunctionDef(self, node: ast.FunctionDef) -> None:
ancestor.lines -= lines
break
self.context.append(Context(node.name, "function", lines))
self.regions["function"].append(
CodeRegion(name=self.fq_node_name(), start=node.lineno, lines=lines)
self.regions.append(
CodeRegion(
kind="function",
name=self.fq_node_name(),
start=node.lineno,
lines=lines,
)
)
self.generic_visit(node)
self.context.pop()
Expand All @@ -70,8 +72,13 @@ def visit_ClassDef(self, node: ast.ClassDef) -> None:
# finds.
lines: set[int] = set()
self.context.append(Context(node.name, "class", lines))
self.regions["class"].append(
CodeRegion(name=self.fq_node_name(), start=node.lineno, lines=lines)
self.regions.append(
CodeRegion(
kind="class",
name=self.fq_node_name(),
start=node.lineno,
lines=lines,
)
)
self.generic_visit(node)
self.context.pop()
Expand All @@ -81,9 +88,11 @@ def visit_ClassDef(self, node: ast.ClassDef) -> None:
ancestor.lines -= lines


def code_regions(source: str) -> dict[str, list[CodeRegion]]:
def code_regions(source: str) -> list[CodeRegion]:
"""Find function and class regions in source code.
TODO: Fix this description.
Takes the program `source`, and returns a dict: the keys are "function" and
"class". Each has a value which is a dict: the keys are fully qualified
names, the values are sets of line numbers included in that region::
Expand Down
23 changes: 23 additions & 0 deletions coverage/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from __future__ import annotations

import dataclasses
import os
import pathlib

Expand Down Expand Up @@ -161,6 +162,28 @@ def get_plugin_options(self, plugin: str) -> TConfigSectionOut:

TSourceTokenLines = Iterable[List[Tuple[str, str]]]


@dataclasses.dataclass
class CodeRegion:
"""Data for each region found by FileReporter.code_regions.
`kind`: the kind of region.
`name`: the description of the region.
`start`: the first line of the named region.
`lines`: a super-set of the executable source lines in the region.
"""
kind: str
name: str
start: int
lines: set[int]

def __lt__(self, other: CodeRegion) -> bool:
"""To support sorting to make test-writing easier."""
if self.name == other.name:
return min(self.lines) < min(other.lines)
return self.name < other.name


## Plugins

class TPlugin(Protocol):
Expand Down
13 changes: 9 additions & 4 deletions tests/gold/html/a/class_index.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,14 @@ <h1>Coverage report:
<label for="hide100">hide covered</label>
</div>
</form>
<h2>
<a class="button" href="index.html">Files</a>
<a class="button" href="function_index.html">Functions</a>
<a class="button current">Classes</a>
</h2>
<p class="text">
<a class="nav" href="https://coverage.readthedocs.io/en/7.5.0a1.dev1">coverage.py v7.5.0a1.dev1</a>,
created at 2024-04-19 13:27 -0400
created at 2024-04-21 10:14 -0400
</p>
</div>
</header>
Expand Down Expand Up @@ -95,12 +100,12 @@ <h1>Coverage report:
<div class="content">
<p>
<a class="nav" href="https://coverage.readthedocs.io/en/7.5.0a1.dev1">coverage.py v7.5.0a1.dev1</a>,
created at 2024-04-19 13:27 -0400
created at 2024-04-21 10:14 -0400
</p>
</div>
<aside class="hidden">
<a id="prevFileLink" class="nav" href="index.html"/>
<a id="nextFileLink" class="nav" href="index.html"/>
<a id="prevFileLink" class="nav" href=""></a>
<a id="nextFileLink" class="nav" href=""></a>
<button type="button" class="button_prev_file" data-shortcut="["/>
<button type="button" class="button_next_file" data-shortcut="]"/>
<button type="button" class="button_show_hide_help" data-shortcut="?"/>
Expand Down
13 changes: 9 additions & 4 deletions tests/gold/html/a/function_index.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,14 @@ <h1>Coverage report:
<label for="hide100">hide covered</label>
</div>
</form>
<h2>
<a class="button" href="index.html">Files</a>
<a class="button current">Functions</a>
<a class="button" href="class_index.html">Classes</a>
</h2>
<p class="text">
<a class="nav" href="https://coverage.readthedocs.io/en/7.5.0a1.dev1">coverage.py v7.5.0a1.dev1</a>,
created at 2024-04-19 13:27 -0400
created at 2024-04-21 10:14 -0400
</p>
</div>
</header>
Expand Down Expand Up @@ -95,12 +100,12 @@ <h1>Coverage report:
<div class="content">
<p>
<a class="nav" href="https://coverage.readthedocs.io/en/7.5.0a1.dev1">coverage.py v7.5.0a1.dev1</a>,
created at 2024-04-19 13:27 -0400
created at 2024-04-21 10:14 -0400
</p>
</div>
<aside class="hidden">
<a id="prevFileLink" class="nav" href="index.html"/>
<a id="nextFileLink" class="nav" href="index.html"/>
<a id="prevFileLink" class="nav" href=""></a>
<a id="nextFileLink" class="nav" href=""></a>
<button type="button" class="button_prev_file" data-shortcut="["/>
<button type="button" class="button_next_file" data-shortcut="]"/>
<button type="button" class="button_show_hide_help" data-shortcut="?"/>
Expand Down
13 changes: 9 additions & 4 deletions tests/gold/html/a/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,14 @@ <h1>Coverage report:
<label for="hide100">hide covered</label>
</div>
</form>
<h2>
<a class="button current">Files</a>
<a class="button" href="function_index.html">Functions</a>
<a class="button" href="class_index.html">Classes</a>
</h2>
<p class="text">
<a class="nav" href="https://coverage.readthedocs.io/en/7.5.0a1.dev1">coverage.py v7.5.0a1.dev1</a>,
created at 2024-04-19 13:27 -0400
created at 2024-04-21 10:14 -0400
</p>
</div>
</header>
Expand Down Expand Up @@ -91,12 +96,12 @@ <h1>Coverage report:
<div class="content">
<p>
<a class="nav" href="https://coverage.readthedocs.io/en/7.5.0a1.dev1">coverage.py v7.5.0a1.dev1</a>,
created at 2024-04-19 13:27 -0400
created at 2024-04-21 10:14 -0400
</p>
</div>
<aside class="hidden">
<a id="prevFileLink" class="nav" href="a_py.html"/>
<a id="nextFileLink" class="nav" href="a_py.html"/>
<a id="prevFileLink" class="nav" href="a_py.html"></a>
<a id="nextFileLink" class="nav" href="a_py.html"></a>
<button type="button" class="button_prev_file" data-shortcut="["/>
<button type="button" class="button_next_file" data-shortcut="]"/>
<button type="button" class="button_show_hide_help" data-shortcut="?"/>
Expand Down
Loading

0 comments on commit e016967

Please sign in to comment.