Skip to content

Commit 2278ffe

Browse files
committed
refactor MSCodeCoverageRunSettingsService
1 parent b8829f4 commit 2278ffe

File tree

1 file changed

+50
-43
lines changed

1 file changed

+50
-43
lines changed

SharedProject/Core/MsTestPlatform/MsCodeCoverageRunSettingsService.cs

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,33 @@ internal class MsCodeCoverageRunSettingsService : IMsCodeCoverageRunSettingsServ
2020

2121
public string ShimPath { get; private set; }
2222

23-
private string ExtensionDirectory;
23+
private readonly string runSettings;
2424
private readonly IToolFolder toolFolder;
2525
private readonly IToolZipProvider toolZipProvider;
2626
private const string zipPrefix = "microsoft.codecoverage";
2727
private const string zipDirectoryName = "msCodeCoverage";
2828
private const string fccSettingsTemplate = "fineCodeCoverageSettings.xml";
29+
private const string fccSolutionFolder = ".fcc";
30+
private string testResultsDirectory;
2931

3032
[ImportingConstructor]
3133
public MsCodeCoverageRunSettingsService(IToolFolder toolFolder, IToolZipProvider toolZipProvider)
3234
{
3335
this.toolFolder = toolFolder;
3436
this.toolZipProvider = toolZipProvider;
37+
var extensionDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
38+
var runSettingsPath = Path.Combine(extensionDirectory, fccSettingsTemplate);
39+
runSettings = File.ReadAllText(runSettingsPath);
3540
}
3641

3742
public void Initialize(string appDataFolder, CancellationToken cancellationToken)
3843
{
3944
var zipDestination = toolFolder.EnsureUnzipped(appDataFolder, zipDirectoryName, toolZipProvider.ProvideZip(zipPrefix), cancellationToken);
4045
MsCodeCoveragePath = Path.Combine(zipDestination, "build", "netstandard1.0");
4146
ShimPath = Path.Combine(zipDestination, "build", "netstandard1.0", "CodeCoverage", "coreclr", "Microsoft.VisualStudio.CodeCoverage.Shim.dll");
42-
ExtensionDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
4347
}
4448

45-
private void PrepareOutputFolder(string outputFolder)
49+
private void CopyShim(string outputFolder)
4650
{
4751
string destination = Path.Combine(outputFolder, Path.GetFileName(ShimPath));
4852
if (!File.Exists(destination))
@@ -51,56 +55,59 @@ private void PrepareOutputFolder(string outputFolder)
5155
}
5256
}
5357

54-
private string testResultsDirectory;
58+
private void CopyShimForNetFrameworkProjects(List<ICoverageProject> coverageProjects)
59+
{
60+
var netFrameworkCoverageProjects = coverageProjects.Where(cp => !cp.IsDotNetSdkStyle());
61+
foreach(var netFrameworkCoverageProject in netFrameworkCoverageProjects)
62+
{
63+
CopyShim(netFrameworkCoverageProject.ProjectOutputFolder);
64+
}
65+
}
5566

56-
public void PrepareRunSettings(string solutionPath, ITestOperation testOperation)
67+
private void CreateCleanResultsDirectory(string solutionPath)
5768
{
58-
ThreadHelper.JoinableTaskFactory.Run(async () =>
69+
testResultsDirectory = Path.Combine(solutionPath, fccSolutionFolder, "TestResults");
70+
if (Directory.Exists(testResultsDirectory))
5971
{
60-
List<ICoverageProject> coverageProjects = await testOperation.GetCoverageProjectsAsync();
61-
var excluded = new HashSet<string>();
72+
Directory.Delete(testResultsDirectory, true);
73+
}
74+
Directory.CreateDirectory(testResultsDirectory);
75+
}
6276

63-
foreach (var p in coverageProjects)
64-
{
65-
// TODO only call this if the project is a .net framework project
66-
// Not needed for .net core
67-
PrepareOutputFolder(p.ProjectOutputFolder);
68-
}
77+
private string CreateRunSettings(List<ICoverageProject> coverageProjects)
78+
{
79+
var excluded = new HashSet<string>();
80+
foreach (var p in coverageProjects.Where(x => !x.Settings.IncludeTestAssembly))
81+
{
82+
excluded.Add(Path.GetFileName(p.TestDllFile));
83+
}
6984

70-
foreach (var p in coverageProjects.Where(x => !x.Settings.IncludeTestAssembly))
71-
{
72-
excluded.Add(Path.GetFileName(p.TestDllFile));
73-
}
85+
string excludeXml = "";
86+
foreach (var ex in excluded)
87+
{
88+
excludeXml += $"<ModulePath>{ex}</ModulePath>";
89+
}
7490

75-
string excludeXml = "";
76-
foreach (var ex in excluded)
77-
{
78-
excludeXml += $"<ModulePath>{ex}</ModulePath>";
79-
}
91+
return runSettings.Replace("%resultsDir%", testResultsDirectory)
92+
.Replace("%testAdapter%", MsCodeCoveragePath)
93+
.Replace("%exclude%", excludeXml);
8094

81-
testResultsDirectory = Path.Combine(solutionPath, ".fcc", "TestResults");
82-
if (Directory.Exists(testResultsDirectory))
83-
{
84-
Directory.Delete(testResultsDirectory, true);
85-
}
86-
Directory.CreateDirectory(testResultsDirectory);
95+
}
8796

88-
var fccTemplate = Path.Combine(solutionPath, fccSettingsTemplate);
89-
string runSettings = "";
90-
if (!File.Exists(fccTemplate))
91-
{
92-
File.Copy(Path.Combine(ExtensionDirectory, fccSettingsTemplate), fccTemplate);
93-
}
94-
runSettings = File.ReadAllText(fccTemplate);
97+
public void PrepareRunSettings(string solutionPath, ITestOperation testOperation)
98+
{
99+
ThreadHelper.JoinableTaskFactory.Run(async () =>
100+
{
101+
var uiThread = ThreadHelper.CheckAccess();
102+
List<ICoverageProject> coverageProjects = await testOperation.GetCoverageProjectsAsync();
95103

96-
var runsettingsFile = Path.Combine(solutionPath, ".fcc", "fcc.runsettings");
97-
var preparedRunsettings = runSettings.Replace("%resultsDir%", testResultsDirectory)
98-
.Replace("%testAdapter%", MsCodeCoveragePath)
99-
.Replace("%exclude%", excludeXml);
100-
File.WriteAllText(runsettingsFile, preparedRunsettings);
104+
CopyShimForNetFrameworkProjects(coverageProjects);
105+
CreateCleanResultsDirectory(solutionPath);
106+
107+
var runsettingsFile = Path.Combine(solutionPath, fccSolutionFolder, "fcc.runsettings");
108+
File.WriteAllText(runsettingsFile, CreateRunSettings(coverageProjects));
101109
testOperation.SetRunSettings(runsettingsFile);
102-
}
103-
);
110+
});
104111
}
105112

106113
public IList<string> GetCoverageFilesFromLastRun()

0 commit comments

Comments
 (0)