Skip to content

Commit 1a475e9

Browse files
committed
Enable Nullable Reference Types in Test Projects
1 parent 32524ee commit 1a475e9

File tree

80 files changed

+463
-466
lines changed

Some content is hidden

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

80 files changed

+463
-466
lines changed

test/Common/Program.cs

Lines changed: 2 additions & 2 deletions
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+
var newArgs = testCommandLine.RemainingArgs?.ToList()!;
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)
@@ -66,7 +66,7 @@ private static int ShowSdkInfo()
6666
{
6767
var log = new StringTestLogger();
6868
var command = new DotnetCommand(log, "--info");
69-
var testDirectory = TestDirectory.Create(Path.Combine(TestContext.Current.TestExecutionDirectory, "sdkinfo"));
69+
var testDirectory = TestDirectory.Create(Path.Combine(TestContext.Current?.TestExecutionDirectory!, "sdkinfo"));
7070

7171
command.WorkingDirectory = testDirectory.Path;
7272

test/HelixTasks/CreateLocalHelixTestLayout.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ 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-
foreach (var payload in HelixCorrelationPayload)
18+
foreach (var payload in HelixCorrelationPayload!)
1919
{
2020
var copyfrom = new DirectoryInfo(payload.GetMetadata("PayloadDirectory"));
2121
var relativeDestinationPathOnHelix = payload.GetMetadata("Destination");
22-
var destination = new DirectoryInfo(Path.Combine(TestOutputDirectory, relativeDestinationPathOnHelix));
22+
var destination = new DirectoryInfo(Path.Combine(TestOutputDirectory!, relativeDestinationPathOnHelix));
2323

2424
if (Directory.Exists(destination.FullName))
2525
{

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: 13 additions & 13 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,8 @@ public override bool Execute()
7171
/// <returns></returns>
7272
private async Task ExecuteAsync()
7373
{
74-
XUnitWorkItems = (await Task.WhenAll(XUnitProjects.Select(PrepareWorkItem)))
75-
.SelectMany(i => i)
74+
XUnitWorkItems = (await Task.WhenAll(XUnitProjects?.Select(PrepareWorkItem)!))
75+
.SelectMany(i => i!)
7676
.Where(wi => wi != null)
7777
.ToArray();
7878
return;
@@ -83,7 +83,7 @@ private async Task ExecuteAsync()
8383
/// </summary>
8484
/// <param name="publishPath">The non-relative path to the publish directory.</param>
8585
/// <returns>An ITaskItem instance representing the prepared HelixWorkItem.</returns>
86-
private async Task<List<ITaskItem>> PrepareWorkItem(ITaskItem xunitProject)
86+
private async Task<List<ITaskItem>?> PrepareWorkItem(ITaskItem xunitProject)
8787
{
8888
// Forces this task to run asynchronously
8989
await Task.Yield();
@@ -166,12 +166,12 @@ private async Task<List<ITaskItem>> PrepareWorkItem(ITaskItem xunitProject)
166166
Log.LogMessage($"Creating work item with properties Identity: {assemblyName}, PayloadDirectory: {publishDirectory}, Command: {command}");
167167

168168
partitionedWorkItem.Add(new Microsoft.Build.Utilities.TaskItem(assemblyPartitionInfo.DisplayName + testIdentityDifferentiator, new Dictionary<string, string>()
169-
{
170-
{ "Identity", assemblyPartitionInfo.DisplayName + testIdentityDifferentiator},
171-
{ "PayloadDirectory", publishDirectory },
172-
{ "Command", command },
173-
{ "Timeout", timeout.ToString() },
174-
}));
169+
{
170+
{ "Identity", assemblyPartitionInfo.DisplayName + testIdentityDifferentiator},
171+
{ "PayloadDirectory", publishDirectory },
172+
{ "Command", command },
173+
{ "Timeout", timeout.ToString() },
174+
}));
175175
}
176176

177177
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 != 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 (IncludeBaseDirectory && SourceDirectory != null)
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/CreateNewImageTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ public async System.Threading.Tasks.Task CreateNewImage_RootlessBaseImage()
246246
await registry.PushAsync(builtImage, sourceReference, destinationReference, cancellationToken: default).ConfigureAwait(false);
247247

248248
// Build an application image on top of the rootless base runtime image.
249-
DirectoryInfo newProjectDir = new(Path.Combine(TestSettings.TestArtifactsDirectory, nameof(CreateNewImage_RootlessBaseImage)));
249+
DirectoryInfo newProjectDir = new(Path.Combine(TestSettings.TestArtifactsDirectory!, nameof(CreateNewImage_RootlessBaseImage)));
250250

251251
if (newProjectDir.Exists)
252252
{
@@ -307,7 +307,7 @@ private static (IBuildEngine buildEngine, List<string?> errors) SetupBuildEngine
307307
return (buildEngine, errors);
308308
}
309309

310-
private static string GetTestDirectoryName([CallerMemberName] string testName = "DefaultTest") => Path.Combine(TestSettings.TestArtifactsDirectory, testName + "_" + DateTime.Now.ToString("yyyyMMddHHmmss"));
310+
private static string GetTestDirectoryName([CallerMemberName] string testName = "DefaultTest") => Path.Combine(TestSettings.TestArtifactsDirectory!, testName + "_" + DateTime.Now.ToString("yyyyMMddHHmmss"));
311311

312312
private static string FormatBuildMessages(List<string?> messages) => string.Join("\r\n", messages);
313313
}

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public async Task ApiEndToEndWithArchiveWritingAndLoad()
153153
BuiltImage builtImage = imageBuilder.Build();
154154

155155
// Write the image to disk
156-
var archiveFile = Path.Combine(TestSettings.TestArtifactsDirectory,
156+
var archiveFile = Path.Combine(TestSettings.TestArtifactsDirectory!,
157157
nameof(ApiEndToEndWithArchiveWritingAndLoad), "app.tar.gz");
158158
var sourceReference = new SourceImageReference(registry, DockerRegistryManager.RuntimeBaseImage, DockerRegistryManager.Net9PreviewImageTag);
159159
var destinationReference = new DestinationImageReference(new ArchiveFileRegistry(archiveFile), NewImageName(), new[] { "latest", "1.0" });
@@ -178,7 +178,7 @@ public async Task ApiEndToEndWithArchiveWritingAndLoad()
178178

179179
private string BuildLocalApp([CallerMemberName] string testName = "TestName", string tfm = ToolsetInfo.CurrentTargetFramework, string rid = "linux-x64")
180180
{
181-
string workingDirectory = Path.Combine(TestSettings.TestArtifactsDirectory, testName);
181+
string workingDirectory = Path.Combine(TestSettings.TestArtifactsDirectory!, testName);
182182

183183
DirectoryInfo d = new(Path.Combine(workingDirectory, "MinimalTestApp"));
184184
if (d.Exists)
@@ -213,7 +213,7 @@ private string BuildLocalApp([CallerMemberName] string testName = "TestName", st
213213
public async Task EndToEnd_MultiProjectSolution()
214214
{
215215
ILogger logger = _loggerFactory.CreateLogger(nameof(EndToEnd_MultiProjectSolution));
216-
DirectoryInfo newSolutionDir = new(Path.Combine(TestSettings.TestArtifactsDirectory, $"CreateNewImageTest_EndToEnd_MultiProjectSolution"));
216+
DirectoryInfo newSolutionDir = new(Path.Combine(TestSettings.TestArtifactsDirectory!, $"CreateNewImageTest_EndToEnd_MultiProjectSolution"));
217217

218218
if (newSolutionDir.Exists)
219219
{
@@ -298,8 +298,8 @@ public async Task EndToEnd_MultiProjectSolution()
298298
[InlineData("worker", true)]
299299
public async Task EndToEnd_NoAPI_ProjectType(string projectType, bool addPackageReference)
300300
{
301-
DirectoryInfo newProjectDir = new(Path.Combine(TestSettings.TestArtifactsDirectory, $"CreateNewImageTest_{projectType}_{addPackageReference}"));
302-
DirectoryInfo privateNuGetAssets = new(Path.Combine(TestSettings.TestArtifactsDirectory, "ContainerNuGet"));
301+
DirectoryInfo newProjectDir = new(Path.Combine(TestSettings.TestArtifactsDirectory!, $"CreateNewImageTest_{projectType}_{addPackageReference}"));
302+
DirectoryInfo privateNuGetAssets = new(Path.Combine(TestSettings.TestArtifactsDirectory!, "ContainerNuGet"));
303303

304304
if (newProjectDir.Exists)
305305
{
@@ -324,9 +324,9 @@ public async Task EndToEnd_NoAPI_ProjectType(string projectType, bool addPackage
324324

325325
if (addPackageReference)
326326
{
327-
File.Copy(Path.Combine(TestContext.Current.TestExecutionDirectory, "NuGet.config"), Path.Combine(newProjectDir.FullName, "NuGet.config"));
327+
File.Copy(Path.Combine(TestContext.Current?.TestExecutionDirectory!, "NuGet.config"), Path.Combine(newProjectDir.FullName, "NuGet.config"));
328328

329-
(string packagePath, string packageVersion) = ToolsetUtils.GetContainersPackagePath();
329+
(string? packagePath, string? packageVersion) = ToolsetUtils.GetContainersPackagePath();
330330

331331
new DotnetCommand(_testOutput, "nuget", "add", "source", Path.GetDirectoryName(packagePath), "--name", "local-temp")
332332
.WithEnvironmentVariable("NUGET_PACKAGES", privateNuGetAssets.FullName)
@@ -471,8 +471,8 @@ public async Task EndToEnd_NoAPI_ProjectType(string projectType, bool addPackage
471471
[DockerAvailableFact(Skip = "https://github.com/dotnet/sdk/issues/42850")]
472472
public void EndToEnd_NoAPI_Console()
473473
{
474-
DirectoryInfo newProjectDir = new(Path.Combine(TestSettings.TestArtifactsDirectory, "CreateNewImageTest"));
475-
DirectoryInfo privateNuGetAssets = new(Path.Combine(TestSettings.TestArtifactsDirectory, "ContainerNuGet"));
474+
DirectoryInfo newProjectDir = new(Path.Combine(TestSettings.TestArtifactsDirectory!, "CreateNewImageTest"));
475+
DirectoryInfo privateNuGetAssets = new(Path.Combine(TestSettings.TestArtifactsDirectory!, "ContainerNuGet"));
476476

477477
if (newProjectDir.Exists)
478478
{
@@ -495,9 +495,9 @@ public void EndToEnd_NoAPI_Console()
495495
.Execute()
496496
.Should().Pass();
497497

498-
File.Copy(Path.Combine(TestContext.Current.TestExecutionDirectory, "NuGet.config"), Path.Combine(newProjectDir.FullName, "NuGet.config"));
498+
File.Copy(Path.Combine(TestContext.Current?.TestExecutionDirectory!, "NuGet.config"), Path.Combine(newProjectDir.FullName, "NuGet.config"));
499499

500-
(string packagePath, string packageVersion) = ToolsetUtils.GetContainersPackagePath();
500+
(string? packagePath, string? packageVersion) = ToolsetUtils.GetContainersPackagePath();
501501

502502
new DotnetCommand(_testOutput, "nuget", "add", "source", Path.GetDirectoryName(packagePath), "--name", "local-temp")
503503
.WithEnvironmentVariable("NUGET_PACKAGES", privateNuGetAssets.FullName)

test/Microsoft.NET.Build.Containers.IntegrationTests/FullFramework/CreateNewImageToolTaskTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,6 @@ public void GenerateCommandLineCommands_LabelGeneration()
596596

597597
private static string GetPathToContainerize()
598598
{
599-
return Path.Combine(TestContext.Current.TestExecutionDirectory, "Container", "containerize");
599+
return Path.Combine(TestContext.Current?.TestExecutionDirectory!, "Container", "containerize");
600600
}
601601
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public void SanityTest_ContainerizeDependencies()
2323
"..\\..\\Cli\\Microsoft.DotNet.Cli.Utils\\Microsoft.DotNet.Cli.Utils.csproj"
2424
};
2525

26-
string projectFilePath = Path.Combine(TestContext.Current.TestExecutionDirectory, "Container", "ProjectFiles", "containerize.csproj");
26+
string projectFilePath = Path.Combine(TestContext.Current?.TestExecutionDirectory!, "Container", "ProjectFiles", "containerize.csproj");
2727
XDocument project = XDocument.Load(projectFilePath);
2828
XNamespace ns = project.Root?.Name.Namespace ?? throw new InvalidOperationException("Project file is empty");
2929

@@ -52,7 +52,7 @@ public void SanityTest_NET_Build_ContainersDependencies()
5252
"..\\..\\Cli\\Microsoft.DotNet.Cli.Utils\\Microsoft.DotNet.Cli.Utils.csproj"
5353
};
5454

55-
string projectFilePath = Path.Combine(TestContext.Current.TestExecutionDirectory, "Container", "ProjectFiles", "Microsoft.NET.Build.Containers.csproj");
55+
string projectFilePath = Path.Combine(TestContext.Current?.TestExecutionDirectory!, "Container", "ProjectFiles", "Microsoft.NET.Build.Containers.csproj");
5656
XDocument project = XDocument.Load(projectFilePath);
5757
XNamespace ns = project.Root?.Name.Namespace ?? throw new InvalidOperationException("Project file is empty");
5858

@@ -131,7 +131,7 @@ public void PackageContentTest()
131131
$"tasks/{netTFM}/Valleysoft.DockerCredsProvider.dll"
132132
};
133133

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

137137
IEnumerable<string> actualEntries = archive.Entries

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ 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
{
17-
var artifactPackagingDirectory = Path.Combine(TestContext.Current.TestExecutionDirectory, "Container", "packaging");
17+
var artifactPackagingDirectory = Path.Combine(TestContext.Current?.TestExecutionDirectory!, "Container", "packaging");
1818
var targetsFile = Path.Combine(artifactPackagingDirectory, "Microsoft.NET.Build.Containers.targets");
1919
var propsFile = Path.ChangeExtension(targetsFile, ".props");
2020
_combinedTargetsLocation = CombineFiles(propsFile, targetsFile);
@@ -27,7 +27,7 @@ private static string CombineFiles(string propsFile, string targetsFile)
2727
var combinedContent = new List<string>();
2828
combinedContent.AddRange(propsContent[..^1]);
2929
combinedContent.AddRange(targetsContent[1..]);
30-
var tempTargetLocation = Path.Combine(TestSettings.TestArtifactsDirectory, "Containers", "Microsoft.NET.Build.Containers.targets");
30+
var tempTargetLocation = Path.Combine(TestSettings.TestArtifactsDirectory!, "Containers", "Microsoft.NET.Build.Containers.targets");
3131
string? directoryName = Path.GetDirectoryName(tempTargetLocation);
3232
Assert.NotNull(directoryName);
3333
Directory.CreateDirectory(directoryName);

0 commit comments

Comments
 (0)