forked from microsoft/TemplateStudio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseGenAndBuildFixture.cs
More file actions
477 lines (378 loc) · 19.8 KB
/
BaseGenAndBuildFixture.cs
File metadata and controls
477 lines (378 loc) · 19.8 KB
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.TemplateEngine.Abstractions;
using Microsoft.Templates.Core;
using Microsoft.Templates.Core.Extensions;
using Microsoft.Templates.Core.Gen;
using Microsoft.Templates.Core.Naming;
using Microsoft.Templates.Fakes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.Templates.Test
{
public abstract class BaseGenAndBuildFixture
{
protected const string All = "all";
private readonly string _emptyBackendFramework = string.Empty;
public abstract string GetTestRunPath();
public abstract void InitializeFixture(IContextProvider contextProvider, string framework = "");
public string TestProjectsPath => Path.GetFullPath(Path.Combine(GetTestRunPath(), "Proj"));
public string TestNewItemPath => Path.GetFullPath(Path.Combine(GetTestRunPath(), "RightClick"));
public IEnumerable<ITemplateInfo> Templates() => GenContext.ToolBox.Repo.GetAll();
public UserSelection SetupProject(string projectType, string framework, string platform, string language, Func<TemplateInfo, string> getName = null)
{
var userSelection = new UserSelection(projectType, framework, _emptyBackendFramework, platform, language);
var layouts = GenContext.ToolBox.Repo.GetLayoutTemplates(userSelection.Platform, userSelection.ProjectType, userSelection.FrontEndFramework, userSelection.BackEndFramework);
foreach (var item in layouts)
{
if (getName == BaseGenAndBuildFixture.GetDefaultName || getName == null)
{
AddItem(userSelection, item.Layout.Name, item.Template);
}
else
{
AddItem(userSelection, item.Template, getName);
}
}
userSelection.HomeName = userSelection.Pages.FirstOrDefault().Name;
return userSelection;
}
public void AddItems(UserSelection userSelection, IEnumerable<TemplateInfo> templates, Func<TemplateInfo, string> getName, bool includeMultipleInstances = false)
{
foreach (var template in templates)
{
AddItem(userSelection, template, getName);
// Add multiple pages if supported to check these are handled the same
if (includeMultipleInstances && template.MultipleInstance)
{
AddItem(userSelection, template, getName);
}
}
}
public void AddItem(UserSelection userSelection, TemplateInfo template, Func<TemplateInfo, string> getName)
{
if (template.MultipleInstance || !AlreadyAdded(userSelection, template))
{
var itemName = getName(template);
var usedNames = userSelection.Pages.Select(p => p.Name)
.Concat(userSelection.Features.Select(f => f.Name))
.Concat(userSelection.Services.Select(f => f.Name))
.Concat(userSelection.Testing.Select(f => f.Name));
if (template.ItemNameEditable)
{
var itemBameValidationService = new ItemNameService(GenContext.ToolBox.Repo.ItemNameValidationConfig, () => usedNames);
itemName = itemBameValidationService.Infer(itemName);
}
else
{
itemName = template.DefaultName;
}
AddItem(userSelection, itemName, template);
}
}
public void AddItem(UserSelection userSelection, string itemName, TemplateInfo template)
{
var selectedTemplate = new UserSelectionItem { Name = itemName, TemplateId = template.TemplateId };
foreach (var item in template.Dependencies)
{
if (!AlreadyAdded(userSelection, item))
{
AddItem(userSelection, item.DefaultName, item);
}
}
if (template.Requirements.Count() > 0 && !userSelection.Items.Any(u => template.Requirements.Select(r => r.TemplateId).Contains(u.TemplateId)))
{
AddItem(userSelection, template.Requirements.FirstOrDefault().DefaultName, template.Requirements.FirstOrDefault());
}
userSelection.Add(selectedTemplate, template.TemplateType);
}
public (int exitCode, string outputFile) BuildMsixBundle(string projectName, string outputPath, string packagingProjectName, string packagingProjectExtension, string batfile)
{
var outputFile = Path.Combine(outputPath, $"_buildOutput_{projectName}.txt");
var solutionFile = Path.GetFullPath(outputPath + @"\" + projectName + ".sln");
var projectFile = Path.GetFullPath(outputPath + @"\" + packagingProjectName + @"\" + packagingProjectName + $".{packagingProjectExtension}");
Console.Out.WriteLine();
Console.Out.WriteLine($"### > Ready to start building");
Console.Out.Write($"### > Running following command: {GetPath(batfile)} \"{solutionFile}\"");
var startInfo = new ProcessStartInfo(GetPath(batfile))
{
Arguments = $"\"{solutionFile}\" \"{projectFile}\" ",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = false,
WorkingDirectory = outputPath,
};
var process = Process.Start(startInfo);
File.WriteAllText(outputFile, process.StandardOutput.ReadToEnd(), Encoding.UTF8);
process.WaitForExit();
return (process.ExitCode, outputFile);
}
public (int exitCode, string outputFile, string resultFile) RunWackTestOnMsixBundle(string bundleFilePath, string outputPath)
{
var outputFile = Path.Combine(outputPath, $"_wackOutput_{Path.GetFileName(bundleFilePath)}.txt");
var resultFile = Path.Combine(outputPath, "_wackresults.xml");
Console.Out.WriteLine();
Console.Out.WriteLine("### > Ready to run WACK test");
Console.Out.Write($"### > Running following command: {GetPath("bat\\RunWackTest.bat")} \"{bundleFilePath}\" \"{resultFile}\"");
var startInfo = new ProcessStartInfo(GetPath("bat\\RunWackTest.bat"))
{
Arguments = $"\"{bundleFilePath}\" \"{resultFile}\" ",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = false,
WorkingDirectory = outputPath,
};
var process = Process.Start(startInfo);
File.WriteAllText(outputFile, process.StandardOutput.ReadToEnd(), Encoding.UTF8);
process.WaitForExit();
return (process.ExitCode, outputFile, resultFile);
}
private bool AlreadyAdded(UserSelection userSelection, TemplateInfo item)
{
return userSelection.Pages.Any(p => p.TemplateId == item.TemplateId)
|| userSelection.Features.Any(f => f.TemplateId == item.TemplateId)
|| userSelection.Services.Any(f => f.TemplateId == item.TemplateId)
|| userSelection.Testing.Any(f => f.TemplateId == item.TemplateId);
}
public static string GetDefaultName(TemplateInfo template)
{
return template.DefaultName;
}
#pragma warning disable RECS0154 // Parameter is never used - but used by method which takes an action which is passed a template
public static string GetRandomName(TemplateInfo template)
#pragma warning restore RECS0154 // Parameter is never used
{
for (int i = 0; i < 10; i++)
{
var itemNameValidationService = new ItemNameService(GenContext.ToolBox.Repo.ItemNameValidationConfig, () => new string[] { });
var randomName = Path.GetRandomFileName().Replace(".", string.Empty);
if (itemNameValidationService.Validate(randomName).IsValid)
{
return randomName;
}
}
throw new ApplicationException("No valid randomName could be generated");
}
public (int exitCode, string outputFile) BuildSolutionUwp(string solutionName, string outputPath, string platform)
{
return BuildSolution(solutionName, outputPath, platform, "bat\\Uwp\\RestoreAndBuild.bat", "Debug", "x86");
}
public (int exitCode, string outputFile) BuildSolutionWpf(string solutionName, string outputPath, string platform)
{
return BuildSolution(solutionName, outputPath, platform, "bat\\Wpf\\RestoreAndBuild.bat", "Debug", "Any CPU" );
}
public (int exitCode, string outputFile) BuildSolutionWpfWithMsix(string solutionName, string outputPath, string platform)
{
return BuildSolution(solutionName, outputPath, platform, "bat\\Wpf\\RestoreAndBuildWithMsix.bat", "Debug", "x86");
}
private (int exitCode, string outputFile) BuildSolution(string solutionName, string outputPath, string platform, string batfile, string config, string buildPlatform)
{
var outputFile = Path.Combine(outputPath, $"_buildOutput_{solutionName}.txt");
// Build
var solutionFile = Path.GetFullPath(outputPath + @"\" + solutionName + ".sln");
var batPath = Path.GetDirectoryName(GetPath(batfile));
Console.Out.WriteLine();
Console.Out.WriteLine($"### > Ready to start building");
Console.Out.Write($"### > Running following command: {GetPath(batfile)} \"{solutionFile}\" {buildPlatform} {config}");
var startInfo = new ProcessStartInfo(GetPath(batfile))
{
Arguments = $"\"{solutionFile}\" \"{buildPlatform}\" \"{config}\" \"{batPath}\"",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = false,
WorkingDirectory = outputPath,
};
var process = Process.Start(startInfo);
File.WriteAllText(outputFile, process.StandardOutput.ReadToEnd(), Encoding.UTF8);
process.WaitForExit();
return (process.ExitCode, outputFile);
}
public (int exitCode, string outputFile) RunTests(string projectName, string outputPath)
{
var outputFile = Path.Combine(outputPath, $"_testOutput_{projectName}.txt");
var solutionFile = Path.GetFullPath(outputPath + @"\" + projectName + ".sln");
const string batFile = "bat\\Uwp\\RunTests.bat";
// Just run the tests against code in the core library. Can't run UI related/dependent code from the cmd line / on the server
var mstestPath = $"\"{outputPath}\\{projectName}.Core.Tests.MSTest\\bin\\Debug\\netcoreapp3.1\\{projectName}.Core.Tests.MSTest.dll\" ";
var nunitPath = $"\"{outputPath}\\{projectName}.Core.Tests.NUnit\\bin\\Debug\\netcoreapp3.1\\{projectName}.Core.Tests.NUnit.dll\" ";
var xunitPath = $"\"{outputPath}\\{projectName}.Core.Tests.xUnit\\bin\\Debug\\netcoreapp3.1\\{projectName}.Core.Tests.xUnit.dll\" ";
var batPath = Path.GetDirectoryName(GetPath(batFile));
var startInfo = new ProcessStartInfo(GetPath(batFile))
{
Arguments = $"{mstestPath} {nunitPath} {xunitPath}",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = false,
WorkingDirectory = outputPath,
};
var process = Process.Start(startInfo);
File.WriteAllText(outputFile, process.StandardOutput.ReadToEnd(), Encoding.UTF8);
process.WaitForExit();
return (process.ExitCode, outputFile);
}
public string GetErrorLines(string filePath)
{
Regex re = new Regex(@"^.*error .*$", RegexOptions.Multiline & RegexOptions.IgnoreCase);
var outputLines = File.ReadAllLines(filePath);
var errorLines = outputLines.Where(l => re.IsMatch(l));
return errorLines.Any() ? errorLines.Aggregate((i, j) => i + Environment.NewLine + j) : string.Empty;
}
public string GetTestSummary(string filePath)
{
var outputLines = File.ReadAllLines(filePath);
var summaryLines = outputLines.Where(l => l.StartsWith("Total tests", StringComparison.OrdinalIgnoreCase) || l.StartsWith("Test ", StringComparison.OrdinalIgnoreCase));
return summaryLines.Any() ? summaryLines.Aggregate((i, j) => i + Environment.NewLine + j) : string.Empty;
}
private static string GetPath(string fileName)
{
string path = Path.Combine(new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName, fileName);
if (!File.Exists(path))
{
path = Path.GetFullPath($@".\{fileName}");
if (!File.Exists(path))
{
throw new ApplicationException($"Can not find {fileName}");
}
}
return path;
}
public void Dispose()
{
if (Directory.Exists(GetTestRunPath()))
{
CleanUpOldTests();
if ((!Directory.Exists(TestProjectsPath) || !Directory.EnumerateDirectories(TestProjectsPath).Any())
&& (!Directory.Exists(TestNewItemPath) || !Directory.EnumerateDirectories(TestNewItemPath).Any()))
{
Directory.Delete(GetTestRunPath(), true);
}
}
}
private void CleanUpOldTests()
{
var rootDir = new DirectoryInfo(GetTestRunPath()).Parent;
var oldDirectories = rootDir.EnumerateDirectories().Where(d => d.CreationTime < DateTime.Now.AddDays(-7));
foreach (var dir in oldDirectories)
{
try
{
dir.Delete(true);
}
catch
{
// This can happen when a test run as admin (such as some WinAppDriver tests) failed
// but now running a test when not admin and can't tidy up the files previously left behind.
Assert.Fail($"There was an exception while tidying up old test files. Manually delete the contents of '{dir.FullName}'.");
}
}
}
public static void SetCurrentLanguage(string language)
{
GenContext.SetCurrentLanguage(language);
var fakeShell = GenContext.ToolBox.Shell as FakeGenShell;
fakeShell.SetCurrentLanguage(language);
}
public static void SetCurrentPlatform(string platform)
{
GenContext.SetCurrentPlatform(platform);
var fakeShell = GenContext.ToolBox.Shell as FakeGenShell;
fakeShell.SetCurrentPlatform(platform);
}
protected static IEnumerable<object[]> GetPageAndFeatureTemplates(string frameworkFilter, string language = ProgrammingLanguages.CSharp, string platform = Platforms.Uwp, string excludedItem = "")
{
List<object[]> result = new List<object[]>();
SetCurrentLanguage(language);
SetCurrentPlatform(platform);
var projectTypes = GenContext.ToolBox.Repo.GetProjectTypes(platform)
.Where(m => !string.IsNullOrEmpty(m.Description))
.Select(m => m.Name);
foreach (var projectType in projectTypes)
{
var targetFrameworks = GenContext.ToolBox.Repo.GetFrontEndFrameworks(platform, projectType)
.Where(m => m.Name == frameworkFilter)
.Select(m => m.Name).ToList();
foreach (var framework in targetFrameworks)
{
var itemTemplates = GenContext.ToolBox.Repo.GetAll()
.Where(t =>
(t.GetFrontEndFrameworkList().Contains(framework) || t.GetFrontEndFrameworkList().Contains(All))
&& t.GetTemplateType().IsItemTemplate()
&& t.GetPlatform() == platform
&& t.GetLanguage() == language
&& t.Identity != excludedItem
&& !t.GetIsHidden());
foreach (var itemTemplate in itemTemplates)
{
result.Add(new object[]
{
itemTemplate.Name,
projectType,
framework,
platform,
itemTemplate.Identity,
language,
});
}
}
}
return result;
}
protected static IEnumerable<object[]> GetVBProjectTemplates()
{
List<object[]> result = new List<object[]>();
var platform = Platforms.Uwp;
var projectTemplates =
GenContext.ToolBox.Repo.GetAll().Where(
t => t.GetTemplateType() == TemplateType.Project
&& t.GetLanguage() == ProgrammingLanguages.VisualBasic);
foreach (var projectTemplate in projectTemplates)
{
var projectTypeList = projectTemplate.GetProjectTypeList();
foreach (var projectType in projectTypeList)
{
var frameworks = GenContext.ToolBox.Repo.GetFrontEndFrameworks(platform, projectType)
.Select(m => m.Name).ToList();
foreach (var framework in frameworks)
{
result.Add(new object[] { projectType, framework, platform });
}
}
}
return result;
}
protected static IEnumerable<object[]> GetAllProjectTemplates()
{
List<object[]> result = new List<object[]>();
foreach (var language in ProgrammingLanguages.GetAllLanguages())
{
SetCurrentLanguage(language);
foreach (var platform in Platforms.GetAllPlatforms())
{
SetCurrentPlatform(platform);
var projectTypes = GenContext.ToolBox.Repo.GetProjectTypes(platform)
.Where(m => !string.IsNullOrEmpty(m.Description))
.Select(m => m.Name);
foreach (var projectType in projectTypes)
{
var targetFrameworks = GenContext.ToolBox.Repo.GetFrontEndFrameworks(platform, projectType)
.Select(m => m.Name).ToList();
foreach (var framework in targetFrameworks)
{
result.Add(new object[] { projectType, framework, platform, language });
}
}
}
}
return result;
}
}
}