Skip to content

Commit

Permalink
Enable code coverage when "Code Coverage;arg1=val1;arg2=val2" is prov…
Browse files Browse the repository at this point in the history
…ided in cli (#3172)

* Adding elements to code coverage config passed via commandline,

e.g.
Input:
--collect:"Code Coverage;CLRIEInstrumentationNetCore=true;CLRIEInstrumentationNetFramework=false"

Output:
      <DataCollector friendlyName="Code Coverage" enabled="True">
        <Configuration>
          <CLRIEInstrumentationNetCore>true</CLRIEInstrumentationNetCore>
          <CLRIEInstrumentationNetFramework>false</CLRIEInstrumentationNetFramework>
        </Configuration>
      </DataCollector>

* Adding tests,

* Update existing configuration if present,

* using correct tag for specifying output file format,

* Update dependencies from https://dev.azure.com/devdiv/DevDiv/_git/vs-code-coverage build 20211111.1

Microsoft.Internal.CodeCoverage
 From Version 17.1.0-beta.21560.1 -> To Version 17.1.0-beta.21561.1

* fixing tests,

* fixing parameter name,

* Adding test for overriding format from settings file using collect command,

* Adding testadapterpath when additional config are provided for the following command,
dotnet test --collect:"Code Coverage;arg1=val1;arg2=val2"

* updating comment,

* Adding string const,

Co-authored-by: dotnet-maestro[bot] <dotnet-maestro[bot]@users.noreply.github.com>
  • Loading branch information
fhnaseer and dotnet-maestro[bot] authored Nov 16, 2021
1 parent e6afc59 commit 011aeee
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Microsoft.TestPlatform.Build/Tasks/VSTestTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class VSTestTask : Task, ICancelableTask
private VSTestForwardingApp vsTestForwardingApp;

private const string vsTestAppName = "vstest.console.dll";
private const string CodeCovergaeString = "Code Coverage";

public string TestFileFullPath
{
Expand Down Expand Up @@ -368,7 +369,12 @@ private List<string> AddArgs()
{
foreach (var arg in this.VSTestCollect)
{
if (arg.Equals("Code Coverage", StringComparison.OrdinalIgnoreCase))
// For collecting code coverage, argument value can be either "Code Coverage" or "Code Coverage;a=b;c=d".
// Split the argument with ';' and compare first token value.
var tokens = arg.Split(';');

if (arg.Equals(CodeCovergaeString, StringComparison.OrdinalIgnoreCase) ||
tokens[0].Equals(CodeCovergaeString, StringComparison.OrdinalIgnoreCase))
{
isCollectCodeCoverageEnabled = true;
}
Expand Down
13 changes: 13 additions & 0 deletions test/Microsoft.TestPlatform.Build.UnitTests/VsTestTaskTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,19 @@ public void CreateArgumentShouldAddTraceCollectorDirectoryPathAsTestAdapterForCo
CollectionAssert.Contains(allArguments, expectedArg, $"Expected argument: '''{expectedArg}''' not present in [{string.Join(", ", allArguments)}]");
}

[TestMethod]
public void CreateArgumentShouldAddTraceCollectorDirectoryPathAsTestAdapterForCodeCoverageCollectWithExtraConfigurations()
{
const string traceDataCollectorDirectoryPath = @"c:\path\to\tracedata collector";
this.vsTestTask.VSTestTraceDataCollectorDirectoryPath = traceDataCollectorDirectoryPath;
this.vsTestTask.VSTestCollect = new string[] { "code coverage;someParameter=someValue" };

var allArguments = this.vsTestTask.CreateArgument().ToArray();

const string expectedArg = "--testAdapterPath:\"c:\\path\\to\\tracedata collector\"";
CollectionAssert.Contains(allArguments, expectedArg, $"Expected argument: '''{expectedArg}''' not present in [{string.Join(", ", allArguments)}]");
}

[TestMethod]
public void CreateArgumentShouldNotAddTraceCollectorDirectoryPathAsTestAdapterForNonCodeCoverageCollect()
{
Expand Down

0 comments on commit 011aeee

Please sign in to comment.