/
BaseBuildTest.cs
109 lines (91 loc) · 3.83 KB
/
BaseBuildTest.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
namespace Microsoft.Maui.IntegrationTests
{
public class BaseBuildTest
{
public const string DotNetCurrent = "net7.0";
public const string DotNetPrevious = "net6.0";
char[] invalidChars = { '{', '}', '(', ')', '$', ':', ';', '\"', '\'', ',', '=', '.', '-', };
public string TestName
{
get
{
var result = TestContext.CurrentContext.Test.Name;
foreach (var c in invalidChars.Concat(Path.GetInvalidPathChars().Concat(Path.GetInvalidFileNameChars())))
{
result = result.Replace(c, '_');
}
return result.Replace("_", string.Empty, StringComparison.OrdinalIgnoreCase);
}
}
public string LogDirectory => Path.Combine(TestEnvironment.GetLogDirectory(), TestName);
public string TestDirectory => Path.Combine(TestEnvironment.GetTestDirectoryRoot(), TestName);
public string TestNuGetConfig => Path.Combine(TestEnvironment.GetTestDirectoryRoot(), "NuGet.config");
// Properties that ensure we don't use cached packages, and *only* the empty NuGet.config
protected List<string> BuildProps => new()
{
"RestoreNoCache=true",
//"GenerateAppxPackageOnBuild=true",
$"RestorePackagesPath={Path.Combine(TestEnvironment.GetTestDirectoryRoot(), "packages")}",
$"RestoreConfigFile={TestNuGetConfig}",
// Avoid iOS build warning as error on Windows: There is no available connection to the Mac. Task 'VerifyXcodeVersion' will not be executed
$"CustomBeforeMicrosoftCSharpTargets={Path.Combine(TestEnvironment.GetMauiDirectory(), "src", "Templates", "TemplateTestExtraTargets.targets")}",
//Try not restore dependencies of 6.0.10
$"DisableTransitiveFrameworkReferenceDownloads=true",
// Surface warnings as build errors
"TreatWarningsAsErrors=true",
};
/// <summary>
/// Copy NuGet packages that are not installed as part of the workload and set up NuGet.config
/// See: `PrepareSeparateBuildContext` in `eng/cake/dotnet.cake`.
/// TODO: Should these be moved to a library-packs workload folder for testing?
/// </summary>
/// <exception cref="DirectoryNotFoundException"></exception>
[OneTimeSetUp]
public void BuildTestFxtSetUp()
{
string[] NuGetOnlyPackages = new string[] {
"Microsoft.Maui.Controls.*.nupkg",
"Microsoft.Maui.Core.*.nupkg",
"Microsoft.Maui.Essentials.*.nupkg",
"Microsoft.Maui.Graphics.*.nupkg",
"Microsoft.Maui.Maps.*.nupkg",
"Microsoft.AspNetCore.Components.WebView.*.nupkg",
};
var artifactDir = Path.Combine(TestEnvironment.GetMauiDirectory(), "artifacts");
if (!Directory.Exists(artifactDir))
throw new DirectoryNotFoundException($"Build artifact directory '{artifactDir}' was not found.");
var extraPacksDir = Path.Combine(TestEnvironment.GetTestDirectoryRoot(), "extra-packages");
if (Directory.Exists(extraPacksDir))
Directory.Delete(extraPacksDir, true);
Directory.CreateDirectory(extraPacksDir);
foreach (var searchPattern in NuGetOnlyPackages)
{
foreach (var pack in Directory.GetFiles(artifactDir, searchPattern))
File.Copy(pack, Path.Combine(extraPacksDir, Path.GetFileName(pack)));
}
File.Copy(Path.Combine(TestEnvironment.GetMauiDirectory(), "NuGet.config"), TestNuGetConfig, true);
FileUtilities.ReplaceInFile(TestNuGetConfig,
"<!-- <add key=\"local\" value=\"artifacts\" /> -->",
$"<add key=\"nuget-only\" value=\"{extraPacksDir}\" />");
}
[SetUp]
public void BuildTestSetUp()
{
if (Directory.Exists(TestDirectory))
Directory.Delete(TestDirectory, recursive: true);
Directory.CreateDirectory(TestDirectory);
}
[OneTimeTearDown]
public void BuildTestFxtTearDown() { }
[TearDown]
public void BuildTestTearDown()
{
// Attach test content and logs as artifacts
foreach (var log in Directory.GetFiles(Path.Combine(TestDirectory), "*log", SearchOption.AllDirectories))
{
TestContext.AddTestAttachment(log, Path.GetFileName(TestDirectory));
}
}
}
}