coverlet-coverage / coverlet

Cross platform code coverage for .NET
MIT License
2.93k stars 386 forks source link

[BUG] ExcludeFromCodeCoverage does not exclude F# task workflow inside of function #1547

Closed JohnDoeKyrgyz closed 7 months ago

JohnDoeKyrgyz commented 8 months ago

Describe the bug The ExcludeFromCodeCoverage attribute does not exclude code inside a task workflow in an F# function

To Reproduce Project Code

namespace src

open System.Diagnostics.CodeAnalysis
open System.Threading.Tasks

module Say =

    [<ExcludeFromCodeCoverage>]
    let hello name =

        [|for i in 0 .. 10 do
             task {
                 printfn $"Task {i} Hello %s{name}"
             } :> Task|]
        |> Task.WaitAll

        printfn $"Hello %s{name}"

    let formatHello name =
        $"Hello %s{name}!"

Unit Test

module test

open NUnit.Framework

[<Test>]
let formatMessage () =
    let helloWorld = src.Say.formatHello "World"
    Assert.That(helloWorld, Is.EqualTo("Hello World!"))

Generate Code Coverage via command line

dotnet test --collect:"XPlat Code Coverage"

Expected behavior Expect the coverage file to not include any code in the hello function.

Actual behavior Coverage file picks up the body of the task in the hello function. It's listed as method MoveNext() line 13.

coverage.cobertura.xml

<?xml version="1.0" encoding="utf-8"?>
<coverage line-rate="0.3333" branch-rate="0" version="1.9" timestamp="1698873645" lines-covered="1" lines-valid="3" branches-covered="0" branches-valid="4">
  <sources>
    <source>/CodeCoverageTest/src/Library.fs/</source>
  </sources>
  <packages>
    <package name="Source" line-rate="0.5" branch-rate="0" complexity="5">
      <classes>
        <class name="src.Say" filename="/CodeCoverageTest/src/Library.fs" line-rate="1" branch-rate="1" complexity="1">
          <methods>
            <method name="formatHello" signature="(System.String)" line-rate="1" branch-rate="1" complexity="1">
              <lines>
                <line number="20" hits="1" branch="False" />
              </lines>
            </method>
          </methods>
          <lines>
            <line number="20" hits="1" branch="False" />
          </lines>
        </class>
        <class name="src.Say/Pipe #1 input at line 11@12" filename="/CodeCoverageTest/src/Library.fs" line-rate="0" branch-rate="0" complexity="4">
          <methods>
            <method name="MoveNext" signature="()" line-rate="0" branch-rate="0" complexity="4">
              <lines>
                <line number="13" hits="0" branch="True" condition-coverage="0% (0/4)">
                  <conditions>
                    <condition number="71" type="jump" coverage="0%" />
                    <condition number="119" type="jump" coverage="0%" />
                  </conditions>
                </line>
              </lines>
            </method>
          </methods>
          <lines>
            <line number="13" hits="0" branch="True" condition-coverage="0% (0/4)">
              <conditions>
                <condition number="71" type="jump" coverage="0%" />
                <condition number="119" type="jump" coverage="0%" />
              </conditions>
            </line>
          </lines>
        </class>
      </classes>
    </package>
    <package name="src" line-rate="0" branch-rate="1" complexity="1">
      <classes>
        <class name="src.Say" filename="/CodeCoverageTest/src/Library.fs" line-rate="0" branch-rate="1" complexity="1">
          <methods>
            <method name="formatHello" signature="(System.String)" line-rate="0" branch-rate="1" complexity="1">
              <lines>
                <line number="12" hits="0" branch="False" />
              </lines>
            </method>
          </methods>
          <lines>
            <line number="12" hits="0" branch="False" />
          </lines>
        </class>
      </classes>
    </package>
  </packages>
</coverage>

Configuration (please complete the following information): Please provide more information on your .NET configuration:

daveMueller commented 8 months ago

Thanks for reporting 🙏 . I can reproduce it.

grafik

daveMueller commented 8 months ago

I just checked it again. PR https://github.com/coverlet-coverage/coverlet/pull/1542 will close this issue.

grafik

<?xml version="1.0" encoding="utf-8"?>
<coverage line-rate="1" branch-rate="0" version="1.9" timestamp="1699169476" lines-covered="1" lines-valid="1" branches-covered="0" branches-valid="4">
  <sources>
    <source>D:\</source>
  </sources>
  <packages>
    <package name="Issue1547" line-rate="1" branch-rate="0" complexity="5">
      <classes>
        <class name="Issue1547.Say" filename="Repos\Repros\Issue1547\Issue1547\Library.fs" line-rate="1" branch-rate="1" complexity="1">
          <methods>
            <method name="formatHello" signature="(System.String)" line-rate="1" branch-rate="1" complexity="1">
              <lines>
                <line number="20" hits="1" branch="False" />
              </lines>
            </method>
          </methods>
          <lines>
            <line number="20" hits="1" branch="False" />
          </lines>
        </class>
        <class name="Issue1547.Say/Pipe #1 input at line 11@12" filename="Repos\Repros\Issue1547\Issue1547\Library.fs" line-rate="1" branch-rate="0" complexity="4">
          <methods />
          <lines />
        </class>
      </classes>
    </package>
  </packages>
</coverage>