Skip to content

Commit

Permalink
Merge pull request #401 from tonyhallett/fix-include-test-assembly
Browse files Browse the repository at this point in the history
Fix include test assembly
  • Loading branch information
tonyhallett authored Mar 8, 2024
2 parents 6a6fce2 + 146dbcb commit 7324da1
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public void Should_Set_The_TestAdapter()
new TestUserRunSettingsProjectDetails
{
CoverageOutputFolder = "",
TestDllFile = "",
Settings = new TestMsCodeCoverageOptions{ IncludeTestAssembly = true},
ExcludedReferencedProjects = new List<string>(),
IncludedReferencedProjects = new List<string>(),
Expand Down Expand Up @@ -116,6 +117,7 @@ public void Should_Set_The_ResultsDirectory_To_The_First_OutputFolder(string out
new TestUserRunSettingsProjectDetails
{
CoverageOutputFolder = outputFolder1,
TestDllFile = "",
Settings = new TestMsCodeCoverageOptions{ IncludeTestAssembly = true},
ExcludedReferencedProjects = new List<string>(),
IncludedReferencedProjects = new List<string>(),
Expand All @@ -126,6 +128,7 @@ public void Should_Set_The_ResultsDirectory_To_The_First_OutputFolder(string out
new TestUserRunSettingsProjectDetails
{
CoverageOutputFolder = outputFolder2,
TestDllFile = "",
Settings = new TestMsCodeCoverageOptions{ IncludeTestAssembly = true},
ExcludedReferencedProjects = new List<string>(),
IncludedReferencedProjects = new List<string>(),
Expand Down Expand Up @@ -157,7 +160,6 @@ TestMsCodeCoverageOptions CreateSettings(string id)
return new TestMsCodeCoverageOptions
{
IncludeTestAssembly = true,

AttributesExclude = new string[] { $"AttributeExclude{id}" },
AttributesInclude = new string[] { $"AttributeInclude{id}" },
CompanyNamesExclude = new string[] { $"CompanyNameExclude{id}" },
Expand All @@ -178,6 +180,7 @@ TestMsCodeCoverageOptions CreateSettings(string id)
new TestUserRunSettingsProjectDetails
{
CoverageOutputFolder = "",
TestDllFile = "",
Settings = CreateSettings("1"),
ExcludedReferencedProjects = new List<string>(),
IncludedReferencedProjects = new List<string>(),
Expand All @@ -188,6 +191,7 @@ TestMsCodeCoverageOptions CreateSettings(string id)
new TestUserRunSettingsProjectDetails
{
CoverageOutputFolder = "",
TestDllFile = "",
Settings = CreateSettings("2"),
ExcludedReferencedProjects = new List<string>(),
IncludedReferencedProjects = new List<string>(),
Expand Down Expand Up @@ -276,8 +280,9 @@ string GetModulePathExcludeWhenExcludingTestAssembly(bool first)
Assert.AreEqual(expectedModulePathExcludes, replacements.ModulePathsExclude);
}

[Test]
public void Should_Add_Regexed_IncludedExcluded_Referenced_Projects_To_ModulePaths()
[TestCase(true)]
[TestCase(false)]
public void Should_Add_Regexed_IncludedExcluded_Referenced_Projects_To_ModulePaths(bool included)
{
var testContainers = new List<ITestContainer>()
{
Expand All @@ -290,48 +295,47 @@ public void Should_Add_Regexed_IncludedExcluded_Referenced_Projects_To_ModulePat
{
"Source1",
new TestUserRunSettingsProjectDetails
{
CoverageOutputFolder = "",
Settings = new TestMsCodeCoverageOptions{
IncludeTestAssembly = true,
ModulePathsExclude = new string[]{ "ModulePathExclude"},
ModulePathsInclude = new string[]{ "ModulePathInclude"}
},
ExcludedReferencedProjects = new List<string>{ "ExcludedReferenced1"},
IncludedReferencedProjects = new List<string>{ "IncludedReferenced1" },
}
{
CoverageOutputFolder = "",
TestDllFile = "",
Settings = new TestMsCodeCoverageOptions
{
IncludeTestAssembly = !included,
ModulePathsExclude = new string[] { "ModulePathExclude" },
ModulePathsInclude = new string[] { "ModulePathInclude" }
},
ExcludedReferencedProjects = new List<string> { "ExcludedReferenced1" },
IncludedReferencedProjects = new List<string> { "IncludedReferenced1" },
}
},
{
"Source2",
new TestUserRunSettingsProjectDetails
{
CoverageOutputFolder = "",
Settings = new TestMsCodeCoverageOptions{ IncludeTestAssembly = true},
ExcludedReferencedProjects = new List<string>{ "ExcludedReferenced2"},
IncludedReferencedProjects = new List<string>{ "IncludedReferenced2" },
TestDllFile = "",
Settings = new TestMsCodeCoverageOptions { IncludeTestAssembly = !included },
ExcludedReferencedProjects = new List<string> { "ExcludedReferenced2" },
IncludedReferencedProjects = new List<string> { "IncludedReferenced2" },
}
},
};

var projectDetails = userRunSettingsProjectDetailsLookup.Select(kvp => kvp.Value).ToList();
var allExcludedReferencesProjects = projectDetails.SelectMany(pd => pd.ExcludedReferencedProjects);
var allIncludedReferencesProjects = projectDetails.SelectMany(pd => pd.IncludedReferencedProjects);
IEnumerable<string> allReferencedProjects = projectDetails.SelectMany(pd => included ? pd.IncludedReferencedProjects : pd.ExcludedReferencedProjects);

string GetExpectedExcludedOrIncludedEscaped(IEnumerable<string> excludedOrIncludedReferenced)
{
return string.Join("", excludedOrIncludedReferenced.Select(referenced => ModulePathElement(MsCodeCoverageRegex.RegexModuleName(referenced))));
}

var expectedModulePathExcludes = GetExpectedExcludedOrIncludedEscaped(allExcludedReferencesProjects) + ModulePathElement("ModulePathExclude");
var expectedModulePathIncludes = GetExpectedExcludedOrIncludedEscaped(allIncludedReferencesProjects) + ModulePathElement("ModulePathInclude");
var expectedExcludes = GetExpectedExcludedOrIncludedEscaped(allReferencedProjects) + ModulePathElement(included ? "ModulePathInclude" : "ModulePathExclude");

var replacements = runSettingsTemplateReplacementsFactory.Create(testContainers, userRunSettingsProjectDetailsLookup, null);
Assert.AreEqual(expectedModulePathExcludes, replacements.ModulePathsExclude);
Assert.AreEqual(expectedModulePathIncludes, replacements.ModulePathsInclude);
Assert.AreEqual(expectedExcludes, included ? replacements.ModulePathsInclude : replacements.ModulePathsExclude);
}

[Test]
public void Should_Be_Empty_String_Replacement_When_Null()
public void Should_Be_Null_TestAdapter_Replacement_When_Null()
{
var testContainers = new List<ITestContainer>()
{
Expand All @@ -346,6 +350,7 @@ public void Should_Be_Empty_String_Replacement_When_Null()
new TestUserRunSettingsProjectDetails
{
CoverageOutputFolder = "",
TestDllFile = "",
Settings = new TestMsCodeCoverageOptions{ IncludeTestAssembly = true},
ExcludedReferencedProjects = new List<string>(),
IncludedReferencedProjects = new List<string>(),
Expand All @@ -356,14 +361,15 @@ public void Should_Be_Empty_String_Replacement_When_Null()
new TestUserRunSettingsProjectDetails
{
CoverageOutputFolder = "",
TestDllFile = "",
Settings = new TestMsCodeCoverageOptions{ IncludeTestAssembly = true},
ExcludedReferencedProjects = new List<string>(),
IncludedReferencedProjects = new List<string>(),
}
}
};
var replacements = runSettingsTemplateReplacementsFactory.Create(testContainers, userRunSettingsProjectDetailsLookup, null);
ReplacementsAssertions.AssertAllEmpty(replacements);
Assert.That(replacements.TestAdapter, Is.Null);
}

[TestCase(true,true,"true")]
Expand All @@ -386,6 +392,7 @@ public void Should_Be_Disabled_When_All_Projects_Are_Disabled(bool project1Enabl
new TestUserRunSettingsProjectDetails
{
CoverageOutputFolder = "",
TestDllFile = "",
Settings = new TestMsCodeCoverageOptions{ Enabled = project1Enabled, IncludeTestAssembly = true},
ExcludedReferencedProjects = new List<string>(),
IncludedReferencedProjects = new List<string>(),
Expand All @@ -396,6 +403,7 @@ public void Should_Be_Disabled_When_All_Projects_Are_Disabled(bool project1Enabl
new TestUserRunSettingsProjectDetails
{
CoverageOutputFolder = "",
TestDllFile = "",
Settings = new TestMsCodeCoverageOptions{ Enabled = project2Enabled, IncludeTestAssembly = true},
ExcludedReferencedProjects = new List<string>(),
IncludedReferencedProjects = new List<string>(),
Expand Down Expand Up @@ -438,13 +446,14 @@ public void Should_Set_The_TestAdapter()
Assert.AreEqual("MsTestAdapterPath", replacements.TestAdapter);
}

private ICoverageProject CreateCoverageProject(Action<Mock<ICoverageProject>> furtherSetup = null)
private ICoverageProject CreateCoverageProject(Action<Mock<ICoverageProject>> furtherSetup = null, bool includeTestAssembly = true)
{
var mockSettings = new Mock<IAppOptions>();
mockSettings.Setup(settings => settings.IncludeTestAssembly).Returns(true);
mockSettings.Setup(settings => settings.IncludeTestAssembly).Returns(includeTestAssembly);
var mockCoverageProject = new Mock<ICoverageProject>();
mockCoverageProject.Setup(cp => cp.ExcludedReferencedProjects).Returns(new List<string>());
mockCoverageProject.Setup(cp => cp.IncludedReferencedProjects).Returns(new List<string>());
mockCoverageProject.Setup(cp => cp.TestDllFile).Returns("");
mockCoverageProject.Setup(cp => cp.Settings).Returns(mockSettings.Object);
furtherSetup?.Invoke(mockCoverageProject);
return mockCoverageProject.Object;
Expand Down Expand Up @@ -498,7 +507,6 @@ void AssertReplacement(string replacement, string replacementProperty, bool isIn
}

AssertReplacement(replacements.ModulePathsExclude, "ModulePath", false);
AssertReplacement(replacements.ModulePathsInclude, "ModulePath", true);
AssertReplacement(replacements.FunctionsExclude, "Function", false);
AssertReplacement(replacements.FunctionsInclude, "Function", true);
AssertReplacement(replacements.CompanyNamesExclude, "CompanyName", false);
Expand Down Expand Up @@ -541,7 +549,6 @@ void AssertReplacement(string replacement, string replacementProperty, bool isIn
}

AssertReplacement(replacements.ModulePathsExclude, "ModulePath", false);
AssertReplacement(replacements.ModulePathsInclude, "ModulePath", true);

AssertReplacement(replacements.FunctionsExclude, "Function", false);
AssertReplacement(replacements.FunctionsInclude, "Function", true);
Expand All @@ -556,7 +563,7 @@ void AssertReplacement(string replacement, string replacementProperty, bool isIn
}

[Test]
public void Should_Be_Empty_String_Replacement_When_Null()
public void Should_Be_Null_TestAdapter_Replacement_When_Null()
{
var msCodeCoverageOptions = new TestCoverageProjectOptions
{
Expand All @@ -565,9 +572,7 @@ public void Should_Be_Empty_String_Replacement_When_Null()

var coverageProject = CreateCoverageProject(mock => mock.Setup(cp => cp.Settings).Returns(msCodeCoverageOptions));
var replacements = runSettingsTemplateReplacementsFactory.Create(coverageProject, null);


ReplacementsAssertions.AssertAllEmpty(replacements);
Assert.That(replacements.TestAdapter, Is.Null);
}

[Test]
Expand Down Expand Up @@ -595,7 +600,7 @@ public void Should_Have_ModulePathsExclude_Replacements_From_ExcludedReferencedP
}

[Test]
public void Should_Have_ModulePathsInclude_Replacements_From_IncludedReferencedProjects_And_Settings()
public void Should_Have_ModulePathsInclude_Replacements_From_IncludedReferencedProjects_Settings_And_Included_Test_Assembly()
{
var msCodeCoverageOptions = new TestCoverageProjectOptions
{
Expand All @@ -610,10 +615,11 @@ public void Should_Have_ModulePathsInclude_Replacements_From_IncludedReferencedP
{
"ModuleName"
});
mock.Setup(cp => cp.TestDllFile).Returns(@"Path\To\Test.dll");
});

var replacements = runSettingsTemplateReplacementsFactory.Create(coverageProject, null);
var expectedModulePathsInclude = $"{ModulePathElement(MsCodeCoverageRegex.RegexModuleName("ModuleName"))}{ModulePathElement("FromSettings")}";
var expectedModulePathsInclude = $"{ModulePathElement(MsCodeCoverageRegex.RegexModuleName("ModuleName"))}{ModulePathElement(MsCodeCoverageRegex.RegexEscapePath(@"Path\To\Test.dll"))}{ModulePathElement("FromSettings")}";
Assert.AreEqual(expectedModulePathsInclude, replacements.ModulePathsInclude);
}

Expand Down
13 changes: 13 additions & 0 deletions FineCodeCoverageTests/OpenCoverExeArgumentsProvider_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ public void Should_Safely_Include_The_TestDLLFile_In_The_TargetArgs()
AssertHasEscapedSetting(arguments, $"-targetargs:{CommandLineArguments.AddEscapeQuotes("testDllFile")}");
}

[Test]
public void Should_Include_The_Test_Assembly_In_The_Filter_When_AppOptions_IncludeTestAssembly()
{
var openCoverExeArgumentsProvider = new OpenCoverExeArgumentsProvider();
var mockCoverageProject = SafeMockCoverageProject();
mockCoverageProject.SetupGet(coverageProject => coverageProject.Settings.IncludeTestAssembly).Returns(true);
mockCoverageProject.SetupGet(coverageProject => coverageProject.ProjectName).Returns("TheTestName");

var arguments = openCoverExeArgumentsProvider.Provide(mockCoverageProject.Object, "");

AssertHasEscapedSetting(arguments, "-filter:+[TheTestName]*");
}

[Test]
public void Should_Safely_Include_The_Project_RunSettingsFile_In_The_TargetArgs_When_Present()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FineCodeCoverage.Engine.Model;
using FineCodeCoverage.Core.Model;
using FineCodeCoverage.Engine.Model;
using FineCodeCoverage.Options;
using Microsoft.VisualStudio.TestWindow.Extensibility;
using System;
Expand Down Expand Up @@ -151,33 +152,46 @@ public IRunSettingsTemplateReplacements Create(


var additionalModulePathsExclude = allProjectDetails.SelectMany(pd =>
{
var additional = pd.ExcludedReferencedProjects.Select(rp => MsCodeCoverageRegex.RegexModuleName(rp)).ToList();
if (!pd.Settings.IncludeTestAssembly)
{
additional.Add(MsCodeCoverageRegex.RegexEscapePath(pd.TestDllFile));
}
return additional;
});

var additionalModulePathsInclude = allProjectDetails.SelectMany(projectDetails => projectDetails.IncludedReferencedProjects.Select(rp => MsCodeCoverageRegex.RegexModuleName(rp)));
GetAdditionalModulePaths(pd.ExcludedReferencedProjects, pd.TestDllFile, pd.Settings.IncludeTestAssembly, false));
var additionalModulePathsInclude = allProjectDetails.SelectMany(pd =>
GetAdditionalModulePaths(pd.IncludedReferencedProjects, pd.TestDllFile, pd.Settings.IncludeTestAssembly, true));
var settings = new CombinedIncludesExcludesOptions(mergedSettings, additionalModulePathsInclude, additionalModulePathsExclude);
return new RunSettingsTemplateReplacements(settings, resultsDirectory, (!allProjectsDisabled).ToString().ToLower(), testAdapter);
}

public IRunSettingsTemplateReplacements Create(ICoverageProject coverageProject, string testAdapter)
private IEnumerable<string> GetAdditionalModulePaths(
IEnumerable<string> referencedProjects,
string testDllFile,
bool includeTestAssembly,
bool isInclude
)
{
var projectSettings = coverageProject.Settings;
var additionalModulePathsExclude = coverageProject.ExcludedReferencedProjects.Select(
rp => MsCodeCoverageRegex.RegexModuleName(rp)).ToList();

if (!projectSettings.IncludeTestAssembly)
var additionalReferenced = referencedProjects.Select(
rp => MsCodeCoverageRegex.RegexModuleName(rp));
if(includeTestAssembly == isInclude)
{
additionalModulePathsExclude.Add(MsCodeCoverageRegex.RegexEscapePath(coverageProject.TestDllFile));
additionalReferenced = additionalReferenced.Append(MsCodeCoverageRegex.RegexEscapePath(testDllFile));
}
return additionalReferenced;

}

var additionalModulePathsInclude = coverageProject.IncludedReferencedProjects.Select(rp => MsCodeCoverageRegex.RegexModuleName(rp)).ToList();
public IRunSettingsTemplateReplacements Create(ICoverageProject coverageProject, string testAdapter)
{
var projectSettings = coverageProject.Settings;
var additionalModulePathsExclude = GetAdditionalModulePaths(
coverageProject.ExcludedReferencedProjects,
coverageProject.TestDllFile,
projectSettings.IncludeTestAssembly,
false);


var additionalModulePathsInclude = GetAdditionalModulePaths(
coverageProject.IncludedReferencedProjects,
coverageProject.TestDllFile,
projectSettings.IncludeTestAssembly,
true);

var settings = new CombinedIncludesExcludesOptions(projectSettings, additionalModulePathsInclude, additionalModulePathsExclude);
return new RunSettingsTemplateReplacements(settings, coverageProject.CoverageOutputFolder, projectSettings.Enabled.ToString(), testAdapter);
}
Expand Down
Loading

0 comments on commit 7324da1

Please sign in to comment.