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

[BUG] ExcludeFromCodeCoverage does code exclude method in a partial class that contains a linq expression #1548

Closed
bdurrani opened this issue Nov 6, 2023 · 3 comments · Fixed by #1542
Labels
bug Something isn't working with repro Issue with repro

Comments

@bdurrani
Copy link

bdurrani commented Nov 6, 2023

Describe the bug
Please share a clear and concise description of the problem.

To Reproduce
I have the following class under Test

public partial class StuffMcStuffy
{
    private readonly ILogger<StuffMcStuffy> _logger;

    public StuffMcStuffy(ILogger<StuffMcStuffy> logger)
    {
        _logger = logger;
    }

    [LoggerMessage(
        EventId = 0,
        Level = LogLevel.Information,
        EventName = "testname",
        Message = "tes:extraction {JobId} {ExtractionStatus} {ExtractionError}")]
    private partial void RecordExtractionEvent(Guid jobId, string extractionStatus, string extractionError);

    [ExcludeFromCodeCoverage]
    public void DoThis()
    {
        Console.Write("do this");
    }

    public void DoThat()
    {
        Console.Write("do that");
    }

    public void BlahThis()
    {
        var thislist = new List<KeyValuePair<string, object>>();
        thislist.Add(new KeyValuePair<string, object>("hey", "ho"));
        thislist.Add(new KeyValuePair<string, object>("hey1", "ho1"));
        var blah = CreateLoggerDictionary(thislist);
        blah.ToString();
    }

    [ExcludeFromCodeCoverage]
    private static Dictionary<string, string> CreateLoggerDictionary(List<KeyValuePair<string, object>> attributes)
    {
        return attributes.ToDictionary(item => item.Key,
            item => item.Value.ToString() ?? string.Empty);
    }
}

I used the following script to run the unit tests and generate the coverage reports

#!/usr/bin/env bash

set -euo pipefail

find . -type d -name TestResults -exec rm -rf {} +
rm -rf ./codeCoverageReport

PROJECTS="$(find . -type d -name "TestProject1")"
readonly PROJECTS

for project in ${PROJECTS}; do
  dotnet test -c Release "$@" "${project}" --collect:"XPlat Code Coverage" --logger "console;verbosity=normal" --logger trx
done

dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
REPORT_TYPE=Html
COMMAND=open
FILENAME=index.html
TARGETDIRECTORY="$(ls -d "${dir}"/.)/codeCoverageReport"
REPORTS="$(find "${dir}/.." -type f -name "coverage.cobertura.xml" | tr "\n" ";")"

dotnet ~/.nuget/packages/reportgenerator/*/tools/*/ReportGenerator.dll \
  -reports:"${REPORTS}" \
  -targetdir:"${TARGETDIRECTORY}" \
  -reporttypes:"${REPORT_TYPE}" && \
  "${COMMAND}" "${TARGETDIRECTORY}/${FILENAME}"

Expected behavior

The method CreateLoggerDictionary() should be excluded from test coverage

Actual behavior

The method is not excluded from coverage

image

** Workaround **

Rewrite the code using foreach()

  private static Dictionary<string, string> CreateLoggerDictionary(List<KeyValuePair<string, object>> attributes)
    {
        Dictionary<string, string> dictionary = new Dictionary<string, string>();
        foreach (var attribute in attributes)
        {
            dictionary.Add(attribute.Key, attribute.Value.ToString() ?? string.Empty);
        }
        return dictionary;
    }
image

Configuration (please complete the following information):
Please provide more information on your .NET configuration:
* Which coverlet package and version was used? v 6.0
* Which version of .NET is the code running on? .NET 6
* What OS and version, and what distro if applicable? Mac OS
* What is the architecture (x64, x86, ARM, ARM64)? ARM64
* Do you know whether it is specific to that configuration? No

Additional context
I've attached the project here
Coverlet-Test.zip

❗ Please also read Known Issues

@github-actions github-actions bot added the untriaged To be investigated label Nov 6, 2023
@bdurrani bdurrani changed the title [BUG] [BUG] ExcludeFromCodeCoverage Nov 6, 2023
@bdurrani bdurrani changed the title [BUG] ExcludeFromCodeCoverage [BUG] ExcludeFromCodeCoverage does code exclude method in a partial class that contains a linq expression Nov 6, 2023
@Kralizek
Copy link

Kralizek commented Nov 7, 2023

I'm having the same issue

image

@daveMueller daveMueller added with repro Issue with repro bug Something isn't working and removed untriaged To be investigated labels Nov 8, 2023
@daveMueller
Copy link
Collaborator

Thansk for reporting and thanks a lot for the repro 🙏. I just checked it and this will be fixed with PR #1542.

grafik

@Kralizek
Copy link

Kralizek commented Nov 9, 2023

That was fast! thanks @daveMueller

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working with repro Issue with repro
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants