-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChutzpahTestContainerDiscoverer.cs
324 lines (281 loc) · 12.5 KB
/
ChutzpahTestContainerDiscoverer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using Chutzpah.Extensions;
using Chutzpah.Models;
using Chutzpah.VS.Common;
using Chutzpah.VS11.EventWatchers;
using Chutzpah.VS11.EventWatchers.EventArgs;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.TestWindow.Extensibility;
using VS11.Plugin;
using ILogger = Chutzpah.VS.Common.ILogger;
namespace Chutzpah.VS2012.TestAdapter
{
[Export(typeof (ITestContainerDiscoverer))]
public class ChutzpahTestContainerDiscoverer : ITestContainerDiscoverer
{
private readonly IServiceProvider serviceProvider;
private readonly IChutzpahSettingsMapper settingsMapper;
private readonly ILogger logger;
private readonly ITestRunner testRunner;
private readonly IFileProbe fileProbe;
private ISolutionEventsListener solutionListener;
private ITestFilesUpdateWatcher testFilesUpdateWatcher;
private ITestFileAddRemoveListener testFilesAddRemoveListener;
private bool initialContainerSearch;
private readonly List<ITestContainer> cachedContainers;
public event EventHandler TestContainersUpdated;
public Uri ExecutorUri
{
get { return Constants.ExecutorUri; }
}
public IEnumerable<ITestContainer> TestContainers
{
get { return GetTestContainers(); }
}
[ImportingConstructor]
public ChutzpahTestContainerDiscoverer(
[Import(typeof (SVsServiceProvider))] IServiceProvider serviceProvider,
IChutzpahSettingsMapper settingsMapper,
ISolutionEventsListener solutionListener,
ITestFilesUpdateWatcher testFilesUpdateWatcher,
ITestFileAddRemoveListener testFilesAddRemoveListener)
: this(
serviceProvider,
settingsMapper,
new Logger(serviceProvider),
solutionListener,
testFilesUpdateWatcher,
testFilesAddRemoveListener,
TestRunner.Create(),
ChutzpahContainer.Get<IFileProbe>())
{
}
public ChutzpahTestContainerDiscoverer(IServiceProvider serviceProvider,
IChutzpahSettingsMapper settingsMapper,
ILogger logger,
ISolutionEventsListener solutionListener,
ITestFilesUpdateWatcher testFilesUpdateWatcher,
ITestFileAddRemoveListener testFilesAddRemoveListener,
ITestRunner testRunner,
IFileProbe fileProbe)
{
initialContainerSearch = true;
cachedContainers = new List<ITestContainer>();
this.serviceProvider = serviceProvider;
this.settingsMapper = settingsMapper;
this.logger = logger;
this.testRunner = testRunner;
this.fileProbe = fileProbe;
this.solutionListener = solutionListener;
this.testFilesUpdateWatcher = testFilesUpdateWatcher;
this.testFilesAddRemoveListener = testFilesAddRemoveListener;
this.testFilesAddRemoveListener.TestFileChanged += OnProjectItemChanged;
this.testFilesAddRemoveListener.StartListeningForTestFileChanges();
this.solutionListener.SolutionUnloaded += SolutionListenerOnSolutionUnloaded;
this.solutionListener.SolutionProjectChanged += OnSolutionProjectChanged;
this.solutionListener.StartListeningForChanges();
this.testFilesUpdateWatcher.FileChangedEvent += OnProjectItemChanged;
}
/// <summary>
/// Fire Events to Notify testcontainerdiscoverer listeners that containers have changed.
/// This is the push notification VS uses to update the unit test window.
///
/// The initialContainerSearch check is meant to prevent us from notifying VS about updates
/// until it is ready
/// </summary>
private void OnTestContainersChanged()
{
if (TestContainersUpdated != null && !initialContainerSearch)
{
TestContainersUpdated(this, EventArgs.Empty);
}
}
/// <summary>
/// The solution was unloaded so we need to indicate that next time containers are requested we do a full search
/// </summary>
private void SolutionListenerOnSolutionUnloaded(object sender, EventArgs eventArgs)
{
initialContainerSearch = true;
}
/// <summary>
/// Handler to react to project load/unload events.
/// </summary>
private void OnSolutionProjectChanged(object sender, SolutionEventsListenerEventArgs e)
{
if (e != null)
{
var files = FindPotentialTestFiles(e.Project);
if (e.ChangedReason == SolutionChangedReason.Load)
{
UpdateTestContainersAndFileWatchers(files, true);
}
else if (e.ChangedReason == SolutionChangedReason.Unload)
{
UpdateTestContainersAndFileWatchers(files, false);
}
}
// Do not fire OnTestContainersChanged here.
// This will cause us to fire this event too early before the UTE is ready to process containers and will result in an exception.
// The UTE will query all the TestContainerDiscoverers once the solution is loaded.
}
/// <summary>
/// After a project is loaded or unloaded either add or remove from the file watcher
/// all test potential items inside that project
/// </summary>
private void UpdateTestContainersAndFileWatchers(IEnumerable<string> files, bool isAdd)
{
foreach (var file in files)
{
if (isAdd)
{
testFilesUpdateWatcher.AddWatch(file);
AddTestContainerIfTestFile(file);
}
else
{
testFilesUpdateWatcher.RemoveWatch(file);
RemoveTestContainer(file);
}
}
}
/// <summary>
/// Handler to react to test file Add/remove/rename andcontents changed events
/// </summary>
private void OnProjectItemChanged(object sender, TestFileChangedEventArgs e)
{
if (e != null)
{
// Don't do anything for files we are sure can't be test files
if (!HasTestFileExtension(e.File)) return;
logger.Log(string.Format("Changed detected for {0} with change type of {1}", e.File, e.ChangedReason),
"ChutzpahTestContainerDiscoverer",
LogType.Information);
switch (e.ChangedReason)
{
case TestFileChangedReason.Added:
testFilesUpdateWatcher.AddWatch(e.File);
AddTestContainerIfTestFile(e.File);
break;
case TestFileChangedReason.Removed:
testFilesUpdateWatcher.RemoveWatch(e.File);
RemoveTestContainer(e.File);
break;
case TestFileChangedReason.Changed:
AddTestContainerIfTestFile(e.File);
break;
}
OnTestContainersChanged();
}
}
/// <summary>
/// Adds a test container for the given file if it is a test file.
/// This will first remove any existing container for that file
/// </summary>
/// <param name="file"></param>
private void AddTestContainerIfTestFile(string file)
{
var isTestFile = IsTestFile(file);
RemoveTestContainer(file); // Remove if there is an existing container
if (isTestFile)
{
var container = new JsTestContainer(this, file.ToLowerInvariant(), Constants.ExecutorUri);
cachedContainers.Add(container);
}
}
/// <summary>
/// Will remove a test container for a given file path
/// </summary>
/// <param name="file"></param>
private void RemoveTestContainer(string file)
{
var index = cachedContainers.FindIndex(x => x.Source.Equals(file, StringComparison.OrdinalIgnoreCase));
if (index >= 0)
{
cachedContainers.RemoveAt(index);
}
}
private IEnumerable<ITestContainer> GetTestContainers()
{
logger.Log("GetTestContainers() are called", "ChutzpahTestContainerDiscoverer", LogType.Information);
if (initialContainerSearch)
{
logger.Log("Initial test container search", "ChutzpahTestContainerDiscoverer", LogType.Information);
cachedContainers.Clear();
var jsFiles = FindPotentialTestFiles();
UpdateTestContainersAndFileWatchers(jsFiles, true);
initialContainerSearch = false;
}
var containers = FilterContainers(cachedContainers);
return containers;
}
private IEnumerable<ITestContainer> FilterContainers(IEnumerable<ITestContainer> containers)
{
var mode = settingsMapper.Settings.TestingMode;
return containers.Where(x => mode.FileBelongsToTestingMode(x.Source));
}
private IEnumerable<string> FindPotentialTestFiles()
{
var solution = (IVsSolution) serviceProvider.GetService(typeof (SVsSolution));
var loadedProjects = solution.EnumerateLoadedProjects(__VSENUMPROJFLAGS.EPF_LOADEDINSOLUTION).OfType<IVsProject>();
return loadedProjects.SelectMany(FindPotentialTestFiles).ToList();
}
private IEnumerable<string> FindPotentialTestFiles(IVsProject project)
{
return from item in VsSolutionHelper.GetProjectItems(project)
where HasTestFileExtension(item) && !fileProbe.IsTemporaryChutzpahFile(item)
select item;
}
private static bool HasTestFileExtension(string path)
{
return TestingMode.All.FileBelongsToTestingMode(path);
}
private bool IsTestFile(string path)
{
try
{
return HasTestFileExtension(path) && testRunner.IsTestFile(path);
}
catch (IOException e)
{
logger.Log("IO error when detecting a test file", "ChutzpahTestContainerDiscoverer", e);
}
return false;
}
public void Dispose()
{
Dispose(true);
// Use SupressFinalize in case a subclass
// of this type implements a finalizer.
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (testFilesUpdateWatcher != null)
{
testFilesUpdateWatcher.FileChangedEvent -= OnProjectItemChanged;
((IDisposable) testFilesUpdateWatcher).Dispose();
testFilesUpdateWatcher = null;
}
if (testFilesAddRemoveListener != null)
{
testFilesAddRemoveListener.TestFileChanged -= OnProjectItemChanged;
testFilesAddRemoveListener.StopListeningForTestFileChanges();
testFilesAddRemoveListener = null;
}
if (solutionListener != null)
{
solutionListener.SolutionProjectChanged -= OnSolutionProjectChanged;
solutionListener.StopListeningForChanges();
solutionListener = null;
}
}
}
}
}