Skip to content

Commit 14bb58e

Browse files
authored
Enable Nullable Reference Types in Test Projects (#44862)
2 parents 063ddee + c982008 commit 14bb58e

File tree

67 files changed

+573
-443
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+573
-443
lines changed

test/Common/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ partial class Program
1010
public static int Main(string[] args)
1111
{
1212
var testCommandLine = TestCommandLine.HandleCommandLine(args);
13-
var newArgs = testCommandLine.RemainingArgs.ToList();
13+
List<string> newArgs = testCommandLine.RemainingArgs?.ToList() ?? new List<string>();
1414

1515
// Help argument needs to be the first one to xunit, so don't insert assembly location in that case
1616
if (testCommandLine.ShouldShowHelp)

test/HelixTasks/CreateLocalHelixTestLayout.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,23 @@ namespace Microsoft.DotNet.SdkCustomHelix.Sdk
88
public sealed class CreateLocalHelixTestLayout : Build.Utilities.Task
99
{
1010
[Required]
11-
public ITaskItem[] HelixCorrelationPayload { get; set; }
11+
public ITaskItem[]? HelixCorrelationPayload { get; set; }
1212

1313
[Required]
14-
public string TestOutputDirectory { get; set; }
14+
public string? TestOutputDirectory { get; set; }
1515

1616
public override bool Execute()
1717
{
18+
if (HelixCorrelationPayload is null)
19+
{
20+
return false;
21+
};
22+
1823
foreach (var payload in HelixCorrelationPayload)
1924
{
2025
var copyfrom = new DirectoryInfo(payload.GetMetadata("PayloadDirectory"));
2126
var relativeDestinationPathOnHelix = payload.GetMetadata("Destination");
22-
var destination = new DirectoryInfo(Path.Combine(TestOutputDirectory, relativeDestinationPathOnHelix));
27+
var destination = new DirectoryInfo(Path.Combine(TestOutputDirectory ?? string.Empty, relativeDestinationPathOnHelix));
2328

2429
if (Directory.Exists(destination.FullName))
2530
{

test/HelixTasks/HelixTasks.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<TargetFrameworks Condition=" '$([MSBuild]::IsOSPlatform(`Windows`))' == 'false' ">net8.0</TargetFrameworks>
66
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
77
<RootNamespace>Microsoft.DotNet.SDK.Build.Helix</RootNamespace>
8+
<Nullable>enable</Nullable>
89
</PropertyGroup>
910

1011
<ItemGroup>

test/HelixTasks/SDKCustomCreateXUnitWorkItemsWithTestExclusion.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class SDKCustomCreateXUnitWorkItemsWithTestExclusion : Build.Utilities.Ta
2222
/// The two required parameters will be automatically created if XUnitProject.Identity is set to the path of the XUnit csproj file
2323
/// </summary>
2424
[Required]
25-
public ITaskItem[] XUnitProjects { get; set; }
25+
public ITaskItem[]? XUnitProjects { get; set; }
2626

2727
/// <summary>
2828
/// The path to the dotnet executable on the Helix agent. Defaults to "dotnet"
@@ -40,15 +40,15 @@ public class SDKCustomCreateXUnitWorkItemsWithTestExclusion : Build.Utilities.Ta
4040
/// Optional timeout for all created workitems
4141
/// Defaults to 300s
4242
/// </summary>
43-
public string XUnitWorkItemTimeout { get; set; }
43+
public string? XUnitWorkItemTimeout { get; set; }
4444

45-
public string XUnitArguments { get; set; }
45+
public string? XUnitArguments { get; set; }
4646

4747
/// <summary>
4848
/// An array of ITaskItems of type HelixWorkItem
4949
/// </summary>
5050
[Output]
51-
public ITaskItem[] XUnitWorkItems { get; set; }
51+
public ITaskItem[]? XUnitWorkItems { get; set; }
5252

5353
/// <summary>
5454
/// The main method of this MSBuild task which calls the asynchronous execution method and
@@ -71,8 +71,13 @@ public override bool Execute()
7171
/// <returns></returns>
7272
private async Task ExecuteAsync()
7373
{
74+
if(XUnitProjects is null)
75+
{
76+
return;
77+
}
78+
7479
XUnitWorkItems = (await Task.WhenAll(XUnitProjects.Select(PrepareWorkItem)))
75-
.SelectMany(i => i)
80+
.SelectMany(i => i ?? new())
7681
.Where(wi => wi != null)
7782
.ToArray();
7883
return;
@@ -83,7 +88,7 @@ private async Task ExecuteAsync()
8388
/// </summary>
8489
/// <param name="publishPath">The non-relative path to the publish directory.</param>
8590
/// <returns>An ITaskItem instance representing the prepared HelixWorkItem.</returns>
86-
private async Task<List<ITaskItem>> PrepareWorkItem(ITaskItem xunitProject)
91+
private async Task<List<ITaskItem>?> PrepareWorkItem(ITaskItem xunitProject)
8792
{
8893
// Forces this task to run asynchronously
8994
await Task.Yield();
@@ -164,12 +169,12 @@ private async Task<List<ITaskItem>> PrepareWorkItem(ITaskItem xunitProject)
164169
Log.LogMessage($"Creating work item with properties Identity: {assemblyName}, PayloadDirectory: {publishDirectory}, Command: {command}");
165170

166171
partitionedWorkItem.Add(new Microsoft.Build.Utilities.TaskItem(assemblyPartitionInfo.DisplayName + testIdentityDifferentiator, new Dictionary<string, string>()
167-
{
168-
{ "Identity", assemblyPartitionInfo.DisplayName + testIdentityDifferentiator},
169-
{ "PayloadDirectory", publishDirectory },
170-
{ "Command", command },
171-
{ "Timeout", timeout.ToString() },
172-
}));
172+
{
173+
{ "Identity", assemblyPartitionInfo.DisplayName + testIdentityDifferentiator},
174+
{ "PayloadDirectory", publishDirectory },
175+
{ "Command", command },
176+
{ "Timeout", timeout.ToString() },
177+
}));
173178
}
174179

175180
return partitionedWorkItem;

test/HelixTasks/TarGzFileCreateFromDirectory.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ public sealed class TarGzFileCreateFromDirectory : ToolTask
1212
/// The path to the directory to be archived.
1313
/// </summary>
1414
[Required]
15-
public string SourceDirectory { get; set; }
15+
public string? SourceDirectory { get; set; }
1616

1717
/// <summary>
1818
/// The path of the archive to be created.
1919
/// </summary>
2020
[Required]
21-
public string DestinationArchive { get; set; }
21+
public string? DestinationArchive { get; set; }
2222

2323
/// <summary>
2424
/// Indicates if the destination archive should be overwritten if it already exists.
@@ -33,7 +33,7 @@ public sealed class TarGzFileCreateFromDirectory : ToolTask
3333
/// <summary>
3434
/// An item group of regular expressions for content to exclude from the archive.
3535
/// </summary>
36-
public ITaskItem[] ExcludePatterns { get; set; }
36+
public ITaskItem[]? ExcludePatterns { get; set; }
3737

3838
public bool IgnoreExitCode { get; set; }
3939

@@ -69,16 +69,18 @@ protected override bool ValidateParameters()
6969
retVal = false;
7070
}
7171
}
72+
if (SourceDirectory is not null)
73+
{
74+
SourceDirectory = Path.GetFullPath(SourceDirectory);
7275

73-
SourceDirectory = Path.GetFullPath(SourceDirectory);
74-
75-
SourceDirectory = SourceDirectory.EndsWith(Path.DirectorySeparatorChar.ToString())
76-
? SourceDirectory
77-
: SourceDirectory + Path.DirectorySeparatorChar;
76+
SourceDirectory = SourceDirectory.EndsWith(Path.DirectorySeparatorChar.ToString())
77+
? SourceDirectory
78+
: SourceDirectory + Path.DirectorySeparatorChar;
79+
}
7880

7981
if (!Directory.Exists(SourceDirectory))
8082
{
81-
Log.LogError($"SourceDirectory '{SourceDirectory} does not exist.");
83+
Log.LogError($"SourceDirectory '{SourceDirectory}' does not exist.");
8284

8385
retVal = false;
8486
}
@@ -113,9 +115,9 @@ protected override string GenerateCommandLineCommands()
113115

114116
private string GetSourceSpecification()
115117
{
116-
if (IncludeBaseDirectory)
118+
if (SourceDirectory is not null && IncludeBaseDirectory)
117119
{
118-
var parentDirectory = Directory.GetParent(SourceDirectory).Parent.FullName;
120+
var parentDirectory = Directory.GetParent(SourceDirectory)?.Parent?.FullName;
119121

120122
var sourceDirectoryName = Path.GetFileName(Path.GetDirectoryName(SourceDirectory));
121123

test/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -341,16 +341,16 @@ public async Task EndToEnd_NoAPI_ProjectType(string projectType, bool addPackage
341341
{
342342
File.Copy(Path.Combine(TestContext.Current.TestExecutionDirectory, "NuGet.config"), Path.Combine(newProjectDir.FullName, "NuGet.config"));
343343

344-
(string packagePath, string packageVersion) = ToolsetUtils.GetContainersPackagePath();
344+
(string? packagePath, string? packageVersion) = ToolsetUtils.GetContainersPackagePath();
345345

346-
new DotnetCommand(_testOutput, "nuget", "add", "source", Path.GetDirectoryName(packagePath), "--name", "local-temp")
346+
new DotnetCommand(_testOutput, "nuget", "add", "source", Path.GetDirectoryName(packagePath) ?? string.Empty, "--name", "local-temp")
347347
.WithEnvironmentVariable("NUGET_PACKAGES", privateNuGetAssets.FullName)
348348
.WithWorkingDirectory(newProjectDir.FullName)
349349
.Execute()
350350
.Should().Pass();
351351

352352
// Add package to the project
353-
new DotnetCommand(_testOutput, "add", "package", "Microsoft.NET.Build.Containers", "-f", ToolsetInfo.CurrentTargetFramework, "-v", packageVersion)
353+
new DotnetCommand(_testOutput, "add", "package", "Microsoft.NET.Build.Containers", "-f", ToolsetInfo.CurrentTargetFramework, "-v", packageVersion ?? string.Empty)
354354
.WithEnvironmentVariable("NUGET_PACKAGES", privateNuGetAssets.FullName)
355355
.WithWorkingDirectory(newProjectDir.FullName)
356356
.Execute()
@@ -509,16 +509,16 @@ public void EndToEnd_NoAPI_Console()
509509

510510
File.Copy(Path.Combine(TestContext.Current.TestExecutionDirectory, "NuGet.config"), Path.Combine(newProjectDir.FullName, "NuGet.config"));
511511

512-
(string packagePath, string packageVersion) = ToolsetUtils.GetContainersPackagePath();
512+
(string? packagePath, string? packageVersion) = ToolsetUtils.GetContainersPackagePath();
513513

514-
new DotnetCommand(_testOutput, "nuget", "add", "source", Path.GetDirectoryName(packagePath), "--name", "local-temp")
514+
new DotnetCommand(_testOutput, "nuget", "add", "source", Path.GetDirectoryName(packagePath) ?? string.Empty, "--name", "local-temp")
515515
.WithEnvironmentVariable("NUGET_PACKAGES", privateNuGetAssets.FullName)
516516
.WithWorkingDirectory(newProjectDir.FullName)
517517
.Execute()
518518
.Should().Pass();
519519

520520
// Add package to the project
521-
new DotnetCommand(_testOutput, "add", "package", "Microsoft.NET.Build.Containers", "-f", _oldFramework , "-v", packageVersion)
521+
new DotnetCommand(_testOutput, "add", "package", "Microsoft.NET.Build.Containers", "-f", _oldFramework , "-v", packageVersion ?? string.Empty)
522522
.WithEnvironmentVariable("NUGET_PACKAGES", privateNuGetAssets.FullName)
523523
.WithWorkingDirectory(newProjectDir.FullName)
524524
.Execute()

test/Microsoft.NET.Build.Containers.IntegrationTests/PackageTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ public void PackageContentTest()
131131
$"tasks/{netTFM}/Valleysoft.DockerCredsProvider.dll"
132132
};
133133

134-
(string packageFilePath, string packageVersion) = ToolsetUtils.GetContainersPackagePath();
135-
using ZipArchive archive = new(File.OpenRead(packageFilePath), ZipArchiveMode.Read, false);
134+
(string? packageFilePath, string? packageVersion) = ToolsetUtils.GetContainersPackagePath();
135+
using ZipArchive archive = new(File.OpenRead(packageFilePath ?? string.Empty), ZipArchiveMode.Read, false);
136136

137137
IEnumerable<string> actualEntries = archive.Entries
138138
.Select(e => e.FullName)

test/Microsoft.NET.Build.Containers.IntegrationTests/ProjectInitializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Microsoft.NET.Build.Containers.IntegrationTests;
1010

1111
public sealed class ProjectInitializer
1212
{
13-
private static readonly string _combinedTargetsLocation;
13+
private static readonly string? _combinedTargetsLocation;
1414

1515
static ProjectInitializer()
1616
{

test/Microsoft.NET.Build.Containers.IntegrationTests/ToolsetUtils.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@ internal static string GetRuntimeGraphFilePath()
2020
/// Gets path to built Microsoft.NET.Build.Containers.*.nupkg prepared for tests.
2121
/// </summary>
2222
/// <returns></returns>
23-
internal static (string PackagePath, string PackageVersion) GetContainersPackagePath()
23+
internal static (string? PackagePath, string? PackageVersion) GetContainersPackagePath()
2424
{
2525
string packageDir = Path.Combine(TestContext.Current.TestExecutionDirectory, "Container", "package");
2626

2727
//until the package is stabilized, the package version matches TestContext.Current.ToolsetUnderTest.SdkVersion
2828
//after the package is stabilized, the package version doesn't have -prefix (-dev, -ci) anymore
2929
//so one of those is expected
30-
string[] expectedPackageVersions = new[] { TestContext.Current.ToolsetUnderTest.SdkVersion, TestContext.Current.ToolsetUnderTest.SdkVersion.Split('-')[0] };
30+
string?[] expectedPackageVersions = new[] { TestContext.Current.ToolsetUnderTest?.SdkVersion, TestContext.Current.ToolsetUnderTest?.SdkVersion?.Split('-')[0] };
3131

32-
foreach (string expectedVersion in expectedPackageVersions)
32+
foreach (string? expectedVersion in expectedPackageVersions)
3333
{
34-
string fullFileName = Path.Combine(packageDir, $"Microsoft.NET.Build.Containers.{expectedVersion}.nupkg");
34+
string? fullFileName = Path.Combine(packageDir, $"Microsoft.NET.Build.Containers.{expectedVersion}.nupkg");
3535
if (File.Exists(fullFileName))
3636
{
3737
return (fullFileName, expectedVersion);

0 commit comments

Comments
 (0)