Skip to content
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

Provide the ability to exclude a code block from coverage #1803

Closed
xqt opened this issue Jun 24, 2024 · 5 comments
Closed

Provide the ability to exclude a code block from coverage #1803

xqt opened this issue Jun 24, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@xqt
Copy link

xqt commented Jun 24, 2024

Excluding a code block
Having a greater code block it would be great to have aq change to disable a code block from coverage with a start end end token. Currently only a single line can be excluded or code blocks within a clause. For example either the whole debug clause can be excluded but not the last two statements

This excludes the the whole if debug clause

a = my_function1()
if debug:   # pragma: no cover
    msg = "blah blah"
    log_message(msg, a)
    b = my_function2()

but I want to exclude the latest two statements (in practice these can be a lot of statemens). This can be done by excluding every line like

a = my_function1()
if debug: 
    msg = "blah blah"
    log_message(msg, a)   # pragma: no cover
    b = my_function2()   # pragma: no cover

but this may lead to a lot of such pragma: no cover command or a senseless clause must be used like

a = my_function1()
if debug: 
    msg = "blah blah"
    if True:   # pragma: no cover
        log_message(msg, a)
        b = my_function2()

start/end command as proposal
A start/end command can be introduced to solve this issue like pragma: no cover start and pragma: no cover stop or shorter no cover: start and no cover: stop or something like that. Instead of no cover: stop command a no cover: end command can be implemented as stop command. The code can then be excluded with

a = my_function1()
if debug: 
    msg = "blah blah"
    # no cover: start
    log_message(msg, a)
    b = my_function2()
    # no cover: stop

Other implementations as a sample
Coverage for Dart language support such block exclusion: (https://github.com/dart-lang/coverage)

@nedbat
Copy link
Owner

nedbat commented Jun 28, 2024

Help me understand your situation. In this code:

a = my_function1()
if debug: 
    msg = "blah blah"
    # no cover: start
    log_message(msg, a)
    b = my_function2()
    # no cover: stop

How would the msg = line be executed, but the log_message() line would not? Why do we need to exclude a block of lines adjacent to other executed lines?

@devdanzin
Copy link
Contributor

Maybe in cases like:

a = my_function1()
if debug: 
   msg = "blah blah"
   if a is None:  # common path
     return my_function3()
   # no cover: start
   log_message(msg, a)
   b = my_function2()
   return b # no cover: stop

Or even:

def main():
  if not DEBUG:
    sys.exit()

if __name__ == "__main__":
  main()
  from debug import show_info  # no cover: start
  show_info()  # no cover: stop

So it may make sense after some way of exiting the normal flow of execution.

This means the end marker wouldn't be strictly necessary if the exclusion ended automatically on block end. But it might be useful to, e.g., temporarily mark a couple of blocks for exclusion.

@boxed
Copy link

boxed commented Jun 29, 2024

You can do this like this:

if True:  # pragma: no cover
    a()
    b()
    c()

@nedbat
Copy link
Owner

nedbat commented Jul 4, 2024

As of commit 1fe897d, you can do this with a multi-line exclusion regex:

[report]
exclude_also =
    # no cover: start(?s:.)*?# no cover: stop

@nedbat nedbat closed this as completed Jul 4, 2024
@nedbat
Copy link
Owner

nedbat commented Jul 11, 2024

This is now released as part of coverage 7.6.0.

github-merge-queue bot pushed a commit to rustymotors/server-old that referenced this issue Jul 11, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [coverage](https://togithub.com/nedbat/coveragepy) | `==7.5.4` ->
`==7.6.0` |
[![age](https://developer.mend.io/api/mc/badges/age/pypi/coverage/7.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/pypi/coverage/7.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/pypi/coverage/7.5.4/7.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/pypi/coverage/7.5.4/7.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>nedbat/coveragepy (coverage)</summary>

###
[`v7.6.0`](https://togithub.com/nedbat/coveragepy/blob/HEAD/CHANGES.rst#Version-760--2024-07-11)

[Compare
Source](https://togithub.com/nedbat/coveragepy/compare/7.5.4...7.6.0)

- Exclusion patterns can now be multi-line, thanks to `Daniel Diniz
<pull
1807_>`*. This enables many interesting exclusion use-cases, including
those
requested in issues `118 <issue 118_>`* (entire files), `996 <issue
996_>`\_ (multiple lines only when appearing together), `1741 <issue
1741_>`\_ (remainder of a function), and `1803 <issue 1803_>`\_
(arbitrary sequence of marked lines). See the :ref:`multi_line_exclude`
    section of the docs for more details and examples.

- The JSON report now includes per-function and per-class coverage
information.
Thanks to `Daniel Diniz <pull 1809_>`\_ for getting the work started.
This
    closes `issue 1793`\_ and `issue 1532`\_.

- Fixed an incorrect calculation of "(no class)" lines in the HTML
classes
    report.

-   Python 3.13.0b3 is supported.

.. \_issue
118:[nedbat/coveragepy#118
.. \_issue
996[nedbat/coveragepy#996
.. \_issue
153[nedbat/coveragepy#1532
.. \_issue
17[nedbat/coveragepy#1741
.. \_issue
1[nedbat/coveragepy#1793
.. \_issue
[nedbat/coveragepy#1803
..
\_pull[nedbat/coveragepy#1807
..
\_pul[nedbat/coveragepy#1809

.. \_changes\_7-5-4:

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/rustymotors/server).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy40MjUuMSIsInVwZGF0ZWRJblZlciI6IjM3LjQyNS4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->
renovate bot added a commit to allenporter/flux-local that referenced this issue Jul 11, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [coverage](https://togithub.com/nedbat/coveragepy) | `==7.5.4` ->
`==7.6.0` |
[![age](https://developer.mend.io/api/mc/badges/age/pypi/coverage/7.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/pypi/coverage/7.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/pypi/coverage/7.5.4/7.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/pypi/coverage/7.5.4/7.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>nedbat/coveragepy (coverage)</summary>

###
[`v7.6.0`](https://togithub.com/nedbat/coveragepy/blob/HEAD/CHANGES.rst#Version-760--2024-07-11)

[Compare
Source](https://togithub.com/nedbat/coveragepy/compare/7.5.4...7.6.0)

- Exclusion patterns can now be multi-line, thanks to `Daniel Diniz
<pull
1807_>`*. This enables many interesting exclusion use-cases, including
those
requested in issues `118 <issue 118_>`* (entire files), `996 <issue
996_>`\_ (multiple lines only when appearing together), `1741 <issue
1741_>`\_ (remainder of a function), and `1803 <issue 1803_>`\_
(arbitrary sequence of marked lines). See the :ref:`multi_line_exclude`
    section of the docs for more details and examples.

- The JSON report now includes per-function and per-class coverage
information.
Thanks to `Daniel Diniz <pull 1809_>`\_ for getting the work started.
This
    closes `issue 1793`\_ and `issue 1532`\_.

- Fixed an incorrect calculation of "(no class)" lines in the HTML
classes
    report.

-   Python 3.13.0b3 is supported.

.. \_issue
118:[nedbat/coveragepy#118
.. \_issue
996[nedbat/coveragepy#996
.. \_issue
153[nedbat/coveragepy#1532
.. \_issue
17[nedbat/coveragepy#1741
.. \_issue
1[nedbat/coveragepy#1793
.. \_issue
[nedbat/coveragepy#1803
..
\_pull[nedbat/coveragepy#1807
..
\_pul[nedbat/coveragepy#1809

.. \_changes\_7-5-4:

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/allenporter/flux-local).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy40MjUuMSIsInVwZGF0ZWRJblZlciI6IjM3LjQyNS4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
jooola pushed a commit to hetznercloud/hcloud-python that referenced this issue Jul 12, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [coverage](https://togithub.com/nedbat/coveragepy) | `>=7.5,<7.6` ->
`>=7.6,<7.7` |
[![age](https://developer.mend.io/api/mc/badges/age/pypi/coverage/7.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/pypi/coverage/7.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/pypi/coverage/7.5.4/7.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/pypi/coverage/7.5.4/7.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>nedbat/coveragepy (coverage)</summary>

###
[`v7.6.0`](https://togithub.com/nedbat/coveragepy/blob/HEAD/CHANGES.rst#Version-760--2024-07-11)

[Compare
Source](https://togithub.com/nedbat/coveragepy/compare/7.5.4...7.6.0)

- Exclusion patterns can now be multi-line, thanks to `Daniel Diniz
<pull
1807_>`*. This enables many interesting exclusion use-cases, including
those
requested in issues `118 <issue 118_>`* (entire files), `996 <issue
996_>`\_ (multiple lines only when appearing together), `1741 <issue
1741_>`\_ (remainder of a function), and `1803 <issue 1803_>`\_
(arbitrary sequence of marked lines). See the :ref:`multi_line_exclude`
    section of the docs for more details and examples.

- The JSON report now includes per-function and per-class coverage
information.
Thanks to `Daniel Diniz <pull 1809_>`\_ for getting the work started.
This
    closes `issue 1793`\_ and `issue 1532`\_.

- Fixed an incorrect calculation of "(no class)" lines in the HTML
classes
    report.

-   Python 3.13.0b3 is supported.

.. \_issue
118:[nedbat/coveragepy#118
.. \_issue
996[nedbat/coveragepy#996
.. \_issue
153[nedbat/coveragepy#1532
.. \_issue
17[nedbat/coveragepy#1741
.. \_issue
1[nedbat/coveragepy#1793
.. \_issue
[nedbat/coveragepy#1803
..
\_pull[nedbat/coveragepy#1807
..
\_pul[nedbat/coveragepy#1809

.. \_changes\_7-5-4:

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/hetznercloud/hcloud-python).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy40MjUuMSIsInVwZGF0ZWRJblZlciI6IjM3LjQyNS4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants