Skip to content

Commit 5186c1c

Browse files
committed
move getting MsCodeCoverageRunSettingsService solution directory path to MsCodeCoverageRunSettingsService
1 parent 94593dc commit 5186c1c

File tree

8 files changed

+57
-61
lines changed

8 files changed

+57
-61
lines changed

SharedProject/Core/FCCEngine.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ internal class FCCEngine : IFCCEngine,IDisposable
2828
public string AppDataFolderPath { get; private set; }
2929
public List<CoverageLine> CoverageLines { get; internal set; }
3030
private bool IsVsShutdown => disposeAwareTaskRunner.DisposalToken.IsCancellationRequested;
31-
public string SolutionPath { get; set; }
32-
3331

3432
private readonly ICoverageUtilManager coverageUtilManager;
3533
private readonly ICoberturaUtil coberturaUtil;
@@ -372,12 +370,6 @@ protected virtual void Dispose(bool disposing)
372370
disposed = true;
373371
}
374372
}
375-
376-
public void PrepareTestRun(ITestOperation testOperation)
377-
{
378-
msCodeCoverageRunSettingsService.PrepareRunSettings(SolutionPath, testOperation);
379-
}
380-
381373
}
382374

383375
}

SharedProject/Core/IFCCEngine.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ internal interface IFCCEngine
1515

1616
void ClearUI();
1717
List<CoverageLine> CoverageLines { get; }
18-
string SolutionPath { get; set; }
19-
20-
void PrepareTestRun(ITestOperation testOperation);
2118
}
2219

2320
}

SharedProject/Core/MsTestPlatform/IMsCodeCoverageRunSettingsService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace FineCodeCoverage.Engine.MsTestPlatform
88
{
99
interface IMsCodeCoverageRunSettingsService
1010
{
11-
void PrepareRunSettings(string solutionPath, ITestOperation testOperation);
11+
void PrepareRunSettings(ITestOperation testOperation);
1212
IList<String> GetCoverageFilesFromLastRun();
1313
void Initialize(string appDataFolder, CancellationToken cancellationToken);
1414
}

SharedProject/Core/MsTestPlatform/MsCodeCoverageRunSettingsService.cs

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
using FineCodeCoverage.Core.Utilities;
1+
using EnvDTE;
2+
using EnvDTE80;
3+
using FineCodeCoverage.Core.Utilities;
24
using FineCodeCoverage.Engine.Model;
35
using FineCodeCoverage.Impl;
6+
using Microsoft;
47
using Microsoft.VisualStudio.Shell;
58
using System;
69
using System.Collections.Generic;
@@ -10,6 +13,7 @@
1013
using System.Reflection;
1114
using System.Text;
1215
using System.Threading;
16+
using Task = System.Threading.Tasks.Task;
1317

1418
namespace FineCodeCoverage.Engine.MsTestPlatform
1519
{
@@ -28,15 +32,40 @@ internal class MsCodeCoverageRunSettingsService : IMsCodeCoverageRunSettingsServ
2832
private const string fccSettingsTemplate = "fineCodeCoverageSettings.xml";
2933
private const string fccSolutionFolder = ".fcc";
3034
private string testResultsDirectory;
35+
private string solutionDirectoryPath;
36+
private DTE2 dte;
3137

3238
[ImportingConstructor]
33-
public MsCodeCoverageRunSettingsService(IToolFolder toolFolder, IToolZipProvider toolZipProvider)
39+
public MsCodeCoverageRunSettingsService(
40+
IToolFolder toolFolder,
41+
IToolZipProvider toolZipProvider,
42+
ISolutionEvents solutionEvents,
43+
[Import(typeof(SVsServiceProvider))]
44+
IServiceProvider serviceProvider
45+
)
3446
{
3547
this.toolFolder = toolFolder;
3648
this.toolZipProvider = toolZipProvider;
3749
var extensionDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
3850
var runSettingsPath = Path.Combine(extensionDirectory, fccSettingsTemplate);
3951
runSettings = File.ReadAllText(runSettingsPath);
52+
solutionEvents.AfterOpen += SolutionEvents_AfterOpen;
53+
dte = (DTE2)serviceProvider.GetService(typeof(DTE));
54+
Assumes.Present(dte);
55+
}
56+
57+
private void SolutionEvents_AfterOpen(object sender, EventArgs e)
58+
{
59+
ThreadHelper.JoinableTaskFactory.Run(async () =>
60+
{
61+
await SetSolutionDirectoryPathAsync();
62+
});
63+
}
64+
65+
private async Task SetSolutionDirectoryPathAsync()
66+
{
67+
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
68+
solutionDirectoryPath = Path.GetDirectoryName(dte.Solution.FileName);
4069
}
4170

4271
public void Initialize(string appDataFolder, CancellationToken cancellationToken)
@@ -64,9 +93,9 @@ private void CopyShimForNetFrameworkProjects(List<ICoverageProject> coverageProj
6493
}
6594
}
6695

67-
private void CreateCleanResultsDirectory(string solutionPath)
96+
private void CreateCleanResultsDirectory()
6897
{
69-
testResultsDirectory = Path.Combine(solutionPath, fccSolutionFolder, "TestResults");
98+
testResultsDirectory = Path.Combine(solutionDirectoryPath, fccSolutionFolder, "TestResults");
7099
if (Directory.Exists(testResultsDirectory))
71100
{
72101
Directory.Delete(testResultsDirectory, true);
@@ -94,17 +123,26 @@ private string CreateRunSettings(List<ICoverageProject> coverageProjects)
94123

95124
}
96125

97-
public void PrepareRunSettings(string solutionPath, ITestOperation testOperation)
126+
private async Task EnsureSolutionDirectoryPathAsync()
127+
{
128+
if (solutionDirectoryPath == null)
129+
{
130+
await SetSolutionDirectoryPathAsync();
131+
}
132+
}
133+
134+
public void PrepareRunSettings(ITestOperation testOperation)
98135
{
99136
ThreadHelper.JoinableTaskFactory.Run(async () =>
100137
{
101-
var uiThread = ThreadHelper.CheckAccess();
138+
await EnsureSolutionDirectoryPathAsync();
102139
List<ICoverageProject> coverageProjects = await testOperation.GetCoverageProjectsAsync();
103140

104141
CopyShimForNetFrameworkProjects(coverageProjects);
105-
CreateCleanResultsDirectory(solutionPath);
106142

107-
var runsettingsFile = Path.Combine(solutionPath, fccSolutionFolder, "fcc.runsettings");
143+
CreateCleanResultsDirectory();
144+
145+
var runsettingsFile = Path.Combine(solutionDirectoryPath, fccSolutionFolder, "fcc.runsettings");
108146
File.WriteAllText(runsettingsFile, CreateRunSettings(coverageProjects));
109147
testOperation.SetRunSettings(runsettingsFile);
110148
});

SharedProject/Core/Utilities/ISolutionEvents.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ namespace FineCodeCoverage.Core.Utilities
55
interface ISolutionEvents
66
{
77
event EventHandler AfterClosing;
8+
event EventHandler AfterOpen;
89
}
910
}

SharedProject/Core/Utilities/SolutionEvents.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace FineCodeCoverage.Core.Utilities
1111
public class SolutionEvents : ISolutionEvents, IVsSolutionEvents
1212
{
1313
public event EventHandler AfterClosing;
14+
public event EventHandler AfterOpen;
1415

1516
[ImportingConstructor]
1617
public SolutionEvents(
@@ -56,6 +57,7 @@ public int OnBeforeUnloadProject(IVsHierarchy pRealHierarchy, IVsHierarchy pStub
5657

5758
public int OnAfterOpenSolution(object pUnkReserved, int fNewSolution)
5859
{
60+
AfterOpen?.Invoke(this, EventArgs.Empty);
5961
return VSConstants.S_OK;
6062
}
6163

@@ -71,7 +73,7 @@ public int OnBeforeCloseSolution(object pUnkReserved)
7173

7274
public int OnAfterCloseSolution(object pUnkReserved)
7375
{
74-
AfterClosing?.Invoke(this, new EventArgs());
76+
AfterClosing?.Invoke(this, EventArgs.Empty);
7577
return VSConstants.S_OK;
7678
}
7779
}

SharedProject/Impl/TestContainerDiscovery/TestContainerDiscoverer.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Microsoft.VisualStudio.TestWindow.Extensibility;
1414
using Task = System.Threading.Tasks.Task;
1515
using Microsoft.VisualStudio.Utilities;
16+
using FineCodeCoverage.Engine.MsTestPlatform;
1617

1718
namespace FineCodeCoverage.Impl
1819
{
@@ -30,6 +31,7 @@ internal class TestContainerDiscoverer : ITestContainerDiscoverer
3031
private readonly ILogger logger;
3132
private readonly IAppOptionsProvider appOptionsProvider;
3233
private readonly IReportGeneratorUtil reportGeneratorUtil;
34+
private readonly IMsCodeCoverageRunSettingsService msCodeCoverageRunSettingsService;
3335
private bool cancelling;
3436
internal Task initializeTask;
3537

@@ -51,12 +53,14 @@ public TestContainerDiscoverer
5153
ILogger logger,
5254
IAppOptionsProvider appOptionsProvider,
5355
IReportGeneratorUtil reportGeneratorUtil,
54-
IDisposeAwareTaskRunner disposeAwareTaskRunner
56+
IDisposeAwareTaskRunner disposeAwareTaskRunner,
57+
IMsCodeCoverageRunSettingsService msCodeCoverageRunSettingsService
5558

5659
)
5760
{
5861
this.appOptionsProvider = appOptionsProvider;
5962
this.reportGeneratorUtil = reportGeneratorUtil;
63+
this.msCodeCoverageRunSettingsService = msCodeCoverageRunSettingsService;
6064
this.fccEngine = fccEngine;
6165
this.testOperationFactory = testOperationFactory;
6266
this.logger = logger;
@@ -91,7 +95,7 @@ private void TestExecutionStarting(IOperation operation)
9195
if (settings.MsCodeCoverage)
9296
{
9397
var testOperation = testOperationFactory.Create(operation);
94-
fccEngine.PrepareTestRun(testOperation);
98+
msCodeCoverageRunSettingsService.PrepareRunSettings(testOperation);
9599
}
96100
else if (settings.RunInParallel)
97101
{

SharedProject/Output/OutputToolWindowPackage.cs

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using FineCodeCoverage.Options;
44
using Microsoft.VisualStudio.Shell;
55
using System.Runtime.InteropServices;
6-
using System.Diagnostics.CodeAnalysis;
76
using System.ComponentModel.Composition;
87
using System.Threading.Tasks;
98
using Task = System.Threading.Tasks.Task;
@@ -12,8 +11,6 @@
1211
using FineCodeCoverage.Engine;
1312
using EnvDTE80;
1413
using Microsoft.VisualStudio;
15-
using Microsoft.VisualStudio.Shell.Events;
16-
using System.Threading.Tasks;
1714
using System.IO;
1815
using FineCodeCoverage.Core.Utilities;
1916

@@ -44,7 +41,6 @@ namespace FineCodeCoverage.Output
4441
[ProvideOptionPage(typeof(AppOptions), Vsix.Name, "General", 0, 0, true)]
4542
[PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
4643
[ProvideToolWindow(typeof(OutputToolWindow), Style = VsDockStyle.Tabbed, DockedHeight = 300, Window = EnvDTE.Constants.vsWindowKindOutput)]
47-
[ProvideAutoLoad(VSConstants.UICONTEXT.SolutionOpening_string, PackageAutoLoadFlags.BackgroundLoad)]
4844
public sealed class OutputToolWindowPackage : AsyncPackage
4945
{
5046
private static Microsoft.VisualStudio.ComponentModelHost.IComponentModel componentModel;
@@ -99,42 +95,8 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke
9995

10096
await OutputToolWindowCommand.InitializeAsync(this, componentModel.GetService<ILogger>());
10197
await ClearUICommand.InitializeAsync(this, fccEngine);
102-
103-
bool isSolutionLoaded = await IsSolutionLoadedAsync();
104-
if (isSolutionLoaded)
105-
{
106-
HandleOpenSolution();
107-
}
108-
Microsoft.VisualStudio.Shell.Events.SolutionEvents.OnAfterBackgroundSolutionLoadComplete += HandleOpenSolution;
109-
11098
}
11199

112-
private void HandleOpenSolution()
113-
{
114-
ThreadHelper.JoinableTaskFactory.Run(async () =>
115-
{
116-
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
117-
var _dte2 = (DTE2)GetGlobalService(typeof(SDTE));
118-
var solPath = Path.GetDirectoryName(_dte2.Solution.FileName);
119-
fccEngine.SolutionPath = solPath;
120-
});
121-
}
122-
123-
private void HandleOpenSolution(object sender, EventArgs e)
124-
{
125-
HandleOpenSolution();
126-
}
127-
128-
private async Task<bool> IsSolutionLoadedAsync()
129-
{
130-
await JoinableTaskFactory.SwitchToMainThreadAsync();
131-
var solService = await GetServiceAsync(typeof(SVsSolution)) as IVsSolution;
132-
133-
ErrorHandler.ThrowOnFailure(solService.GetProperty((int)__VSPROPID.VSPROPID_IsSolutionOpen, out object value));
134-
135-
return value is bool isSolOpen && isSolOpen;
136-
}
137-
138100
protected override Task<object> InitializeToolWindowAsync(Type toolWindowType, int id, CancellationToken cancellationToken)
139101
{
140102
return Task.FromResult<object>(GetOutputToolWindowContext());

0 commit comments

Comments
 (0)