From 1a475e9d20cea2c21373e71d684fc0c1998d5f05 Mon Sep 17 00:00:00 2001 From: Jason Zhai Date: Wed, 13 Nov 2024 21:54:56 -0800 Subject: [PATCH 1/6] Enable Nullable Reference Types in Test Projects --- test/Common/Program.cs | 4 +- test/HelixTasks/CreateLocalHelixTestLayout.cs | 8 +- test/HelixTasks/HelixTasks.csproj | 1 + ...omCreateXUnitWorkItemsWithTestExclusion.cs | 26 +++--- .../TarGzFileCreateFromDirectory.cs | 24 +++--- .../CreateNewImageTests.cs | 4 +- .../EndToEndTests.cs | 22 ++--- .../CreateNewImageToolTaskTests.cs | 2 +- .../PackageTests.cs | 6 +- .../ProjectInitializer.cs | 6 +- .../TestSettings.cs | 4 +- .../ToolsetUtils.cs | 4 +- .../TransientTestFolder.cs | 2 +- .../WorkloadPackGroupTests.cs | 6 +- .../AspNetSdkTest.cs | 34 ++++---- .../Assertions/CommandResultAssertions.cs | 6 +- .../Assertions/DirectoryInfoAssertions.cs | 2 +- .../CoreMSBuildAndWindowsOnlyFactAttribute.cs | 2 +- ...oreMSBuildAndWindowsOnlyTheoryAttribute.cs | 2 +- .../CoreMSBuildOnlyFactAttribute.cs | 2 +- .../CoreMSBuildOnlyTheoryAttribute.cs | 2 +- .../FullMSBuildOnlyFactAttribute.cs | 2 +- .../FullMSBuildOnlyTheoryAttribute.cs | 2 +- .../RequiresMSBuildVersionFactAttribute.cs | 2 +- .../RequiresMSBuildVersionTheoryAttribute.cs | 10 +-- ...OnlyRequiresMSBuildVersionFactAttribute.cs | 2 +- .../Commands/AddReferenceCommand.cs | 4 +- .../Commands/BuildCommand.cs | 4 +- .../Commands/CleanCommand.cs | 4 +- .../Commands/ComposeStoreCommand.cs | 12 +-- .../Commands/DotnetBuildCommand.cs | 2 +- .../Commands/DotnetCommand.cs | 8 +- .../Commands/DotnetPublishCommand.cs | 4 +- .../Commands/GetValuesCommand.cs | 14 +-- .../Commands/ListPackageCommand.cs | 4 +- .../Commands/ListReferenceCommand.cs | 4 +- .../Commands/MSBuildCommand.cs | 18 ++-- .../Commands/MSBuildVersionCommand.cs | 4 +- .../Commands/NuGetExeRestoreCommand.cs | 22 ++--- .../Commands/PackCommand.cs | 8 +- .../Commands/PublishCommand.cs | 6 +- .../Commands/RebuildCommand.cs | 2 +- .../Commands/RemoveReferenceCommand.cs | 4 +- .../Commands/RestoreCommand.cs | 4 +- .../Commands/RunExeCommand.cs | 2 +- .../Commands/SdkCommandSpec.cs | 4 +- .../Commands/TestCommand.cs | 6 +- .../ConflictResolutionAssets.cs | 2 +- .../EnvironmentInfo.cs | 6 +- .../FileConstants.cs | 2 +- .../InMemoryLoggerProvider.cs | 2 - .../Microsoft.NET.TestFramework.csproj | 1 + .../Mock/FileSystemMockBuilder.cs | 82 +++++++++--------- .../NuGetTestLogger.cs | 4 +- .../OutputPathCalculator.cs | 66 +++++++------- .../ProjectConstruction/TestProject.cs | 70 +++++++-------- test/Microsoft.NET.TestFramework/SdkTest.cs | 2 +- test/Microsoft.NET.TestFramework/TestAsset.cs | 48 +++++------ .../TestAssetsManager.cs | 28 +++--- .../TestCommandLine.cs | 32 +++---- .../TestContext.cs | 68 +++++++-------- .../TestDirectory.cs | 6 +- .../TestLoggerFactory.cs | 2 - .../TestPackageReference.cs | 14 +-- .../ToolsetInfo.cs | 86 +++++++++---------- .../Utilities/BufferedReporter.cs | 2 - .../Utilities/FileThumbPrint.cs | 6 +- .../Utilities/PeReaderUtils.cs | 6 +- .../Utilities/ProjectModification.cs | 2 +- .../XunitLoggerProvider.cs | 2 - .../BaseTest.cs | 4 +- .../Microsoft.Win32.Msi.Manual.Tests.csproj | 1 + .../Program.cs | 8 +- test/dotnet-new.Tests/BaseIntegrationTest.cs | 4 +- test/dotnet-new.Tests/CommonTemplatesTests.cs | 16 ++-- .../DotnetClassTemplateTests.cs | 12 +-- .../TemplateEngineSamplesTest.cs | 8 +- test/dotnet-new.Tests/Utilities.cs | 2 +- .../HotReload/RuntimeProcessLauncherTests.cs | 6 +- .../dotnet-new/SdkInfoProviderTests.cs | 2 +- 80 files changed, 463 insertions(+), 466 deletions(-) diff --git a/test/Common/Program.cs b/test/Common/Program.cs index 226c95f28371..10bdde6027f5 100644 --- a/test/Common/Program.cs +++ b/test/Common/Program.cs @@ -10,7 +10,7 @@ partial class Program public static int Main(string[] args) { var testCommandLine = TestCommandLine.HandleCommandLine(args); - var newArgs = testCommandLine.RemainingArgs.ToList(); + var newArgs = testCommandLine.RemainingArgs?.ToList()!; // Help argument needs to be the first one to xunit, so don't insert assembly location in that case if (testCommandLine.ShouldShowHelp) @@ -66,7 +66,7 @@ private static int ShowSdkInfo() { var log = new StringTestLogger(); var command = new DotnetCommand(log, "--info"); - var testDirectory = TestDirectory.Create(Path.Combine(TestContext.Current.TestExecutionDirectory, "sdkinfo")); + var testDirectory = TestDirectory.Create(Path.Combine(TestContext.Current?.TestExecutionDirectory!, "sdkinfo")); command.WorkingDirectory = testDirectory.Path; diff --git a/test/HelixTasks/CreateLocalHelixTestLayout.cs b/test/HelixTasks/CreateLocalHelixTestLayout.cs index 8b81992258af..cb8b158f7b55 100644 --- a/test/HelixTasks/CreateLocalHelixTestLayout.cs +++ b/test/HelixTasks/CreateLocalHelixTestLayout.cs @@ -8,18 +8,18 @@ namespace Microsoft.DotNet.SdkCustomHelix.Sdk public sealed class CreateLocalHelixTestLayout : Build.Utilities.Task { [Required] - public ITaskItem[] HelixCorrelationPayload { get; set; } + public ITaskItem[]? HelixCorrelationPayload { get; set; } [Required] - public string TestOutputDirectory { get; set; } + public string? TestOutputDirectory { get; set; } public override bool Execute() { - foreach (var payload in HelixCorrelationPayload) + foreach (var payload in HelixCorrelationPayload!) { var copyfrom = new DirectoryInfo(payload.GetMetadata("PayloadDirectory")); var relativeDestinationPathOnHelix = payload.GetMetadata("Destination"); - var destination = new DirectoryInfo(Path.Combine(TestOutputDirectory, relativeDestinationPathOnHelix)); + var destination = new DirectoryInfo(Path.Combine(TestOutputDirectory!, relativeDestinationPathOnHelix)); if (Directory.Exists(destination.FullName)) { diff --git a/test/HelixTasks/HelixTasks.csproj b/test/HelixTasks/HelixTasks.csproj index 02350e724ec7..ca4e7c73653b 100644 --- a/test/HelixTasks/HelixTasks.csproj +++ b/test/HelixTasks/HelixTasks.csproj @@ -5,6 +5,7 @@ net8.0 true Microsoft.DotNet.SDK.Build.Helix + enable diff --git a/test/HelixTasks/SDKCustomCreateXUnitWorkItemsWithTestExclusion.cs b/test/HelixTasks/SDKCustomCreateXUnitWorkItemsWithTestExclusion.cs index 65f2b992a07d..9472f0b8a638 100644 --- a/test/HelixTasks/SDKCustomCreateXUnitWorkItemsWithTestExclusion.cs +++ b/test/HelixTasks/SDKCustomCreateXUnitWorkItemsWithTestExclusion.cs @@ -22,7 +22,7 @@ public class SDKCustomCreateXUnitWorkItemsWithTestExclusion : Build.Utilities.Ta /// The two required parameters will be automatically created if XUnitProject.Identity is set to the path of the XUnit csproj file /// [Required] - public ITaskItem[] XUnitProjects { get; set; } + public ITaskItem[]? XUnitProjects { get; set; } /// /// The path to the dotnet executable on the Helix agent. Defaults to "dotnet" @@ -40,15 +40,15 @@ public class SDKCustomCreateXUnitWorkItemsWithTestExclusion : Build.Utilities.Ta /// Optional timeout for all created workitems /// Defaults to 300s /// - public string XUnitWorkItemTimeout { get; set; } + public string? XUnitWorkItemTimeout { get; set; } - public string XUnitArguments { get; set; } + public string? XUnitArguments { get; set; } /// /// An array of ITaskItems of type HelixWorkItem /// [Output] - public ITaskItem[] XUnitWorkItems { get; set; } + public ITaskItem[]? XUnitWorkItems { get; set; } /// /// The main method of this MSBuild task which calls the asynchronous execution method and @@ -71,8 +71,8 @@ public override bool Execute() /// private async Task ExecuteAsync() { - XUnitWorkItems = (await Task.WhenAll(XUnitProjects.Select(PrepareWorkItem))) - .SelectMany(i => i) + XUnitWorkItems = (await Task.WhenAll(XUnitProjects?.Select(PrepareWorkItem)!)) + .SelectMany(i => i!) .Where(wi => wi != null) .ToArray(); return; @@ -83,7 +83,7 @@ private async Task ExecuteAsync() /// /// The non-relative path to the publish directory. /// An ITaskItem instance representing the prepared HelixWorkItem. - private async Task> PrepareWorkItem(ITaskItem xunitProject) + private async Task?> PrepareWorkItem(ITaskItem xunitProject) { // Forces this task to run asynchronously await Task.Yield(); @@ -166,12 +166,12 @@ private async Task> PrepareWorkItem(ITaskItem xunitProject) Log.LogMessage($"Creating work item with properties Identity: {assemblyName}, PayloadDirectory: {publishDirectory}, Command: {command}"); partitionedWorkItem.Add(new Microsoft.Build.Utilities.TaskItem(assemblyPartitionInfo.DisplayName + testIdentityDifferentiator, new Dictionary() - { - { "Identity", assemblyPartitionInfo.DisplayName + testIdentityDifferentiator}, - { "PayloadDirectory", publishDirectory }, - { "Command", command }, - { "Timeout", timeout.ToString() }, - })); + { + { "Identity", assemblyPartitionInfo.DisplayName + testIdentityDifferentiator}, + { "PayloadDirectory", publishDirectory }, + { "Command", command }, + { "Timeout", timeout.ToString() }, + })); } return partitionedWorkItem; diff --git a/test/HelixTasks/TarGzFileCreateFromDirectory.cs b/test/HelixTasks/TarGzFileCreateFromDirectory.cs index 96a905cb041d..da8c7fb7d8b4 100644 --- a/test/HelixTasks/TarGzFileCreateFromDirectory.cs +++ b/test/HelixTasks/TarGzFileCreateFromDirectory.cs @@ -12,13 +12,13 @@ public sealed class TarGzFileCreateFromDirectory : ToolTask /// The path to the directory to be archived. /// [Required] - public string SourceDirectory { get; set; } + public string? SourceDirectory { get; set; } /// /// The path of the archive to be created. /// [Required] - public string DestinationArchive { get; set; } + public string? DestinationArchive { get; set; } /// /// Indicates if the destination archive should be overwritten if it already exists. @@ -33,7 +33,7 @@ public sealed class TarGzFileCreateFromDirectory : ToolTask /// /// An item group of regular expressions for content to exclude from the archive. /// - public ITaskItem[] ExcludePatterns { get; set; } + public ITaskItem[]? ExcludePatterns { get; set; } public bool IgnoreExitCode { get; set; } @@ -69,16 +69,18 @@ protected override bool ValidateParameters() retVal = false; } } + if (SourceDirectory != null) + { + SourceDirectory = Path.GetFullPath(SourceDirectory); - SourceDirectory = Path.GetFullPath(SourceDirectory); - - SourceDirectory = SourceDirectory.EndsWith(Path.DirectorySeparatorChar.ToString()) - ? SourceDirectory - : SourceDirectory + Path.DirectorySeparatorChar; + SourceDirectory = SourceDirectory.EndsWith(Path.DirectorySeparatorChar.ToString()) + ? SourceDirectory + : SourceDirectory + Path.DirectorySeparatorChar; + } if (!Directory.Exists(SourceDirectory)) { - Log.LogError($"SourceDirectory '{SourceDirectory} does not exist."); + Log.LogError($"SourceDirectory '{SourceDirectory}' does not exist."); retVal = false; } @@ -113,9 +115,9 @@ protected override string GenerateCommandLineCommands() private string GetSourceSpecification() { - if (IncludeBaseDirectory) + if (IncludeBaseDirectory && SourceDirectory != null) { - var parentDirectory = Directory.GetParent(SourceDirectory).Parent.FullName; + var parentDirectory = Directory.GetParent(SourceDirectory)?.Parent?.FullName; var sourceDirectoryName = Path.GetFileName(Path.GetDirectoryName(SourceDirectory)); diff --git a/test/Microsoft.NET.Build.Containers.IntegrationTests/CreateNewImageTests.cs b/test/Microsoft.NET.Build.Containers.IntegrationTests/CreateNewImageTests.cs index 43b766d65c1a..ec9c460fc748 100644 --- a/test/Microsoft.NET.Build.Containers.IntegrationTests/CreateNewImageTests.cs +++ b/test/Microsoft.NET.Build.Containers.IntegrationTests/CreateNewImageTests.cs @@ -246,7 +246,7 @@ public async System.Threading.Tasks.Task CreateNewImage_RootlessBaseImage() await registry.PushAsync(builtImage, sourceReference, destinationReference, cancellationToken: default).ConfigureAwait(false); // Build an application image on top of the rootless base runtime image. - DirectoryInfo newProjectDir = new(Path.Combine(TestSettings.TestArtifactsDirectory, nameof(CreateNewImage_RootlessBaseImage))); + DirectoryInfo newProjectDir = new(Path.Combine(TestSettings.TestArtifactsDirectory!, nameof(CreateNewImage_RootlessBaseImage))); if (newProjectDir.Exists) { @@ -307,7 +307,7 @@ private static (IBuildEngine buildEngine, List errors) SetupBuildEngine return (buildEngine, errors); } - private static string GetTestDirectoryName([CallerMemberName] string testName = "DefaultTest") => Path.Combine(TestSettings.TestArtifactsDirectory, testName + "_" + DateTime.Now.ToString("yyyyMMddHHmmss")); + private static string GetTestDirectoryName([CallerMemberName] string testName = "DefaultTest") => Path.Combine(TestSettings.TestArtifactsDirectory!, testName + "_" + DateTime.Now.ToString("yyyyMMddHHmmss")); private static string FormatBuildMessages(List messages) => string.Join("\r\n", messages); } diff --git a/test/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs b/test/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs index 717421b9dd83..271553662518 100644 --- a/test/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs +++ b/test/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs @@ -153,7 +153,7 @@ public async Task ApiEndToEndWithArchiveWritingAndLoad() BuiltImage builtImage = imageBuilder.Build(); // Write the image to disk - var archiveFile = Path.Combine(TestSettings.TestArtifactsDirectory, + var archiveFile = Path.Combine(TestSettings.TestArtifactsDirectory!, nameof(ApiEndToEndWithArchiveWritingAndLoad), "app.tar.gz"); var sourceReference = new SourceImageReference(registry, DockerRegistryManager.RuntimeBaseImage, DockerRegistryManager.Net9PreviewImageTag); var destinationReference = new DestinationImageReference(new ArchiveFileRegistry(archiveFile), NewImageName(), new[] { "latest", "1.0" }); @@ -178,7 +178,7 @@ public async Task ApiEndToEndWithArchiveWritingAndLoad() private string BuildLocalApp([CallerMemberName] string testName = "TestName", string tfm = ToolsetInfo.CurrentTargetFramework, string rid = "linux-x64") { - string workingDirectory = Path.Combine(TestSettings.TestArtifactsDirectory, testName); + string workingDirectory = Path.Combine(TestSettings.TestArtifactsDirectory!, testName); DirectoryInfo d = new(Path.Combine(workingDirectory, "MinimalTestApp")); if (d.Exists) @@ -213,7 +213,7 @@ private string BuildLocalApp([CallerMemberName] string testName = "TestName", st public async Task EndToEnd_MultiProjectSolution() { ILogger logger = _loggerFactory.CreateLogger(nameof(EndToEnd_MultiProjectSolution)); - DirectoryInfo newSolutionDir = new(Path.Combine(TestSettings.TestArtifactsDirectory, $"CreateNewImageTest_EndToEnd_MultiProjectSolution")); + DirectoryInfo newSolutionDir = new(Path.Combine(TestSettings.TestArtifactsDirectory!, $"CreateNewImageTest_EndToEnd_MultiProjectSolution")); if (newSolutionDir.Exists) { @@ -298,8 +298,8 @@ public async Task EndToEnd_MultiProjectSolution() [InlineData("worker", true)] public async Task EndToEnd_NoAPI_ProjectType(string projectType, bool addPackageReference) { - DirectoryInfo newProjectDir = new(Path.Combine(TestSettings.TestArtifactsDirectory, $"CreateNewImageTest_{projectType}_{addPackageReference}")); - DirectoryInfo privateNuGetAssets = new(Path.Combine(TestSettings.TestArtifactsDirectory, "ContainerNuGet")); + DirectoryInfo newProjectDir = new(Path.Combine(TestSettings.TestArtifactsDirectory!, $"CreateNewImageTest_{projectType}_{addPackageReference}")); + DirectoryInfo privateNuGetAssets = new(Path.Combine(TestSettings.TestArtifactsDirectory!, "ContainerNuGet")); if (newProjectDir.Exists) { @@ -324,9 +324,9 @@ public async Task EndToEnd_NoAPI_ProjectType(string projectType, bool addPackage if (addPackageReference) { - File.Copy(Path.Combine(TestContext.Current.TestExecutionDirectory, "NuGet.config"), Path.Combine(newProjectDir.FullName, "NuGet.config")); + File.Copy(Path.Combine(TestContext.Current?.TestExecutionDirectory!, "NuGet.config"), Path.Combine(newProjectDir.FullName, "NuGet.config")); - (string packagePath, string packageVersion) = ToolsetUtils.GetContainersPackagePath(); + (string? packagePath, string? packageVersion) = ToolsetUtils.GetContainersPackagePath(); new DotnetCommand(_testOutput, "nuget", "add", "source", Path.GetDirectoryName(packagePath), "--name", "local-temp") .WithEnvironmentVariable("NUGET_PACKAGES", privateNuGetAssets.FullName) @@ -471,8 +471,8 @@ public async Task EndToEnd_NoAPI_ProjectType(string projectType, bool addPackage [DockerAvailableFact(Skip = "https://github.com/dotnet/sdk/issues/42850")] public void EndToEnd_NoAPI_Console() { - DirectoryInfo newProjectDir = new(Path.Combine(TestSettings.TestArtifactsDirectory, "CreateNewImageTest")); - DirectoryInfo privateNuGetAssets = new(Path.Combine(TestSettings.TestArtifactsDirectory, "ContainerNuGet")); + DirectoryInfo newProjectDir = new(Path.Combine(TestSettings.TestArtifactsDirectory!, "CreateNewImageTest")); + DirectoryInfo privateNuGetAssets = new(Path.Combine(TestSettings.TestArtifactsDirectory!, "ContainerNuGet")); if (newProjectDir.Exists) { @@ -495,9 +495,9 @@ public void EndToEnd_NoAPI_Console() .Execute() .Should().Pass(); - File.Copy(Path.Combine(TestContext.Current.TestExecutionDirectory, "NuGet.config"), Path.Combine(newProjectDir.FullName, "NuGet.config")); + File.Copy(Path.Combine(TestContext.Current?.TestExecutionDirectory!, "NuGet.config"), Path.Combine(newProjectDir.FullName, "NuGet.config")); - (string packagePath, string packageVersion) = ToolsetUtils.GetContainersPackagePath(); + (string? packagePath, string? packageVersion) = ToolsetUtils.GetContainersPackagePath(); new DotnetCommand(_testOutput, "nuget", "add", "source", Path.GetDirectoryName(packagePath), "--name", "local-temp") .WithEnvironmentVariable("NUGET_PACKAGES", privateNuGetAssets.FullName) diff --git a/test/Microsoft.NET.Build.Containers.IntegrationTests/FullFramework/CreateNewImageToolTaskTests.cs b/test/Microsoft.NET.Build.Containers.IntegrationTests/FullFramework/CreateNewImageToolTaskTests.cs index cfffd3a8ca47..9537fd9cbb29 100644 --- a/test/Microsoft.NET.Build.Containers.IntegrationTests/FullFramework/CreateNewImageToolTaskTests.cs +++ b/test/Microsoft.NET.Build.Containers.IntegrationTests/FullFramework/CreateNewImageToolTaskTests.cs @@ -596,6 +596,6 @@ public void GenerateCommandLineCommands_LabelGeneration() private static string GetPathToContainerize() { - return Path.Combine(TestContext.Current.TestExecutionDirectory, "Container", "containerize"); + return Path.Combine(TestContext.Current?.TestExecutionDirectory!, "Container", "containerize"); } } diff --git a/test/Microsoft.NET.Build.Containers.IntegrationTests/PackageTests.cs b/test/Microsoft.NET.Build.Containers.IntegrationTests/PackageTests.cs index 92b767a3990a..fe2ac53f7a38 100644 --- a/test/Microsoft.NET.Build.Containers.IntegrationTests/PackageTests.cs +++ b/test/Microsoft.NET.Build.Containers.IntegrationTests/PackageTests.cs @@ -23,7 +23,7 @@ public void SanityTest_ContainerizeDependencies() "..\\..\\Cli\\Microsoft.DotNet.Cli.Utils\\Microsoft.DotNet.Cli.Utils.csproj" }; - string projectFilePath = Path.Combine(TestContext.Current.TestExecutionDirectory, "Container", "ProjectFiles", "containerize.csproj"); + string projectFilePath = Path.Combine(TestContext.Current?.TestExecutionDirectory!, "Container", "ProjectFiles", "containerize.csproj"); XDocument project = XDocument.Load(projectFilePath); XNamespace ns = project.Root?.Name.Namespace ?? throw new InvalidOperationException("Project file is empty"); @@ -52,7 +52,7 @@ public void SanityTest_NET_Build_ContainersDependencies() "..\\..\\Cli\\Microsoft.DotNet.Cli.Utils\\Microsoft.DotNet.Cli.Utils.csproj" }; - string projectFilePath = Path.Combine(TestContext.Current.TestExecutionDirectory, "Container", "ProjectFiles", "Microsoft.NET.Build.Containers.csproj"); + string projectFilePath = Path.Combine(TestContext.Current?.TestExecutionDirectory!, "Container", "ProjectFiles", "Microsoft.NET.Build.Containers.csproj"); XDocument project = XDocument.Load(projectFilePath); XNamespace ns = project.Root?.Name.Namespace ?? throw new InvalidOperationException("Project file is empty"); @@ -131,7 +131,7 @@ public void PackageContentTest() $"tasks/{netTFM}/Valleysoft.DockerCredsProvider.dll" }; - (string packageFilePath, string packageVersion) = ToolsetUtils.GetContainersPackagePath(); + (string? packageFilePath, string? packageVersion) = ToolsetUtils.GetContainersPackagePath(); using ZipArchive archive = new(File.OpenRead(packageFilePath), ZipArchiveMode.Read, false); IEnumerable actualEntries = archive.Entries diff --git a/test/Microsoft.NET.Build.Containers.IntegrationTests/ProjectInitializer.cs b/test/Microsoft.NET.Build.Containers.IntegrationTests/ProjectInitializer.cs index 58a42fbedae4..620265ab896d 100644 --- a/test/Microsoft.NET.Build.Containers.IntegrationTests/ProjectInitializer.cs +++ b/test/Microsoft.NET.Build.Containers.IntegrationTests/ProjectInitializer.cs @@ -10,11 +10,11 @@ namespace Microsoft.NET.Build.Containers.IntegrationTests; public sealed class ProjectInitializer { - private static readonly string _combinedTargetsLocation; + private static readonly string? _combinedTargetsLocation; static ProjectInitializer() { - var artifactPackagingDirectory = Path.Combine(TestContext.Current.TestExecutionDirectory, "Container", "packaging"); + var artifactPackagingDirectory = Path.Combine(TestContext.Current?.TestExecutionDirectory!, "Container", "packaging"); var targetsFile = Path.Combine(artifactPackagingDirectory, "Microsoft.NET.Build.Containers.targets"); var propsFile = Path.ChangeExtension(targetsFile, ".props"); _combinedTargetsLocation = CombineFiles(propsFile, targetsFile); @@ -27,7 +27,7 @@ private static string CombineFiles(string propsFile, string targetsFile) var combinedContent = new List(); combinedContent.AddRange(propsContent[..^1]); combinedContent.AddRange(targetsContent[1..]); - var tempTargetLocation = Path.Combine(TestSettings.TestArtifactsDirectory, "Containers", "Microsoft.NET.Build.Containers.targets"); + var tempTargetLocation = Path.Combine(TestSettings.TestArtifactsDirectory!, "Containers", "Microsoft.NET.Build.Containers.targets"); string? directoryName = Path.GetDirectoryName(tempTargetLocation); Assert.NotNull(directoryName); Directory.CreateDirectory(directoryName); diff --git a/test/Microsoft.NET.Build.Containers.IntegrationTests/TestSettings.cs b/test/Microsoft.NET.Build.Containers.IntegrationTests/TestSettings.cs index 7240fff45640..1e27c8ec47e6 100644 --- a/test/Microsoft.NET.Build.Containers.IntegrationTests/TestSettings.cs +++ b/test/Microsoft.NET.Build.Containers.IntegrationTests/TestSettings.cs @@ -13,7 +13,7 @@ internal static class TestSettings /// /// Gets temporary location for test artifacts. /// - internal static string TestArtifactsDirectory + internal static string? TestArtifactsDirectory { get { @@ -23,7 +23,7 @@ internal static string TestArtifactsDirectory { if (_testArtifactsDir == null) { - string tmpDir = Path.Combine(TestContext.Current.TestExecutionDirectory, "ContainersTests", DateTime.Now.ToString("yyyyMMddHHmmssfff", CultureInfo.InvariantCulture)); + string tmpDir = Path.Combine(TestContext.Current?.TestExecutionDirectory!, "ContainersTests", DateTime.Now.ToString("yyyyMMddHHmmssfff", CultureInfo.InvariantCulture)); if (!Directory.Exists(tmpDir)) { Directory.CreateDirectory(tmpDir); diff --git a/test/Microsoft.NET.Build.Containers.IntegrationTests/ToolsetUtils.cs b/test/Microsoft.NET.Build.Containers.IntegrationTests/ToolsetUtils.cs index 41c9d703d415..d078218def58 100644 --- a/test/Microsoft.NET.Build.Containers.IntegrationTests/ToolsetUtils.cs +++ b/test/Microsoft.NET.Build.Containers.IntegrationTests/ToolsetUtils.cs @@ -22,12 +22,12 @@ internal static string GetRuntimeGraphFilePath() /// internal static (string PackagePath, string PackageVersion) GetContainersPackagePath() { - string packageDir = Path.Combine(TestContext.Current.TestExecutionDirectory, "Container", "package"); + string packageDir = Path.Combine(TestContext.Current?.TestExecutionDirectory!, "Container", "package"); //until the package is stabilized, the package version matches TestContext.Current.ToolsetUnderTest.SdkVersion //after the package is stabilized, the package version doesn't have -prefix (-dev, -ci) anymore //so one of those is expected - string[] expectedPackageVersions = new[] { TestContext.Current.ToolsetUnderTest.SdkVersion, TestContext.Current.ToolsetUnderTest.SdkVersion.Split('-')[0] }; + string[] expectedPackageVersions = new[] { TestContext.Current?.ToolsetUnderTest?.SdkVersion, TestContext.Current?.ToolsetUnderTest?.SdkVersion?.Split('-')[0] }!; foreach (string expectedVersion in expectedPackageVersions) { diff --git a/test/Microsoft.NET.Build.Containers.IntegrationTests/TransientTestFolder.cs b/test/Microsoft.NET.Build.Containers.IntegrationTests/TransientTestFolder.cs index b2deedcf065f..65e191945dcb 100644 --- a/test/Microsoft.NET.Build.Containers.IntegrationTests/TransientTestFolder.cs +++ b/test/Microsoft.NET.Build.Containers.IntegrationTests/TransientTestFolder.cs @@ -10,7 +10,7 @@ namespace Microsoft.NET.Build.Containers.IntegrationTests; /// internal sealed class TransientTestFolder : IDisposable { - public readonly string Path = Combine(TestSettings.TestArtifactsDirectory, GetRandomFileName()); + public readonly string Path = Combine(TestSettings.TestArtifactsDirectory!, GetRandomFileName()); public readonly DirectoryInfo DirectoryInfo; public TransientTestFolder() diff --git a/test/Microsoft.NET.Sdk.WorkloadManifestReader.Tests/WorkloadPackGroupTests.cs b/test/Microsoft.NET.Sdk.WorkloadManifestReader.Tests/WorkloadPackGroupTests.cs index 8dce2302d652..b0558f4ae577 100644 --- a/test/Microsoft.NET.Sdk.WorkloadManifestReader.Tests/WorkloadPackGroupTests.cs +++ b/test/Microsoft.NET.Sdk.WorkloadManifestReader.Tests/WorkloadPackGroupTests.cs @@ -82,7 +82,7 @@ public void TestGetPackGroups() public void TestGetManifestFeatureBands() { var manifestProvider = CreateManifestProvider(); - var workloadResolver = WorkloadResolver.CreateForTests(manifestProvider, TestContext.Current.ToolsetUnderTest.DotNetRoot); + var workloadResolver = WorkloadResolver.CreateForTests(manifestProvider, TestContext.Current?.ToolsetUnderTest?.DotNetRoot!); foreach (var manifestInfo in workloadResolver.GetInstalledManifests()) { @@ -93,7 +93,7 @@ public void TestGetManifestFeatureBands() SdkDirectoryWorkloadManifestProvider CreateManifestProvider() { - return new(TestContext.Current.ToolsetUnderTest.DotNetRoot, TestContext.Current.ToolsetUnderTest.SdkVersion, userProfileDir: null, globalJsonPath: null); + return new(TestContext.Current?.ToolsetUnderTest?.DotNetRoot!, TestContext.Current?.ToolsetUnderTest?.SdkVersion!, userProfileDir: null, globalJsonPath: null); } public IEnumerable GetManifests(SdkDirectoryWorkloadManifestProvider? manifestProvider = null) @@ -149,7 +149,7 @@ List GetPackGroups() var manifestProvider = CreateManifestProvider(); var manifests = GetManifests(manifestProvider); - var workloadResolver = WorkloadResolver.CreateForTests(manifestProvider, TestContext.Current.ToolsetUnderTest.DotNetRoot); + var workloadResolver = WorkloadResolver.CreateForTests(manifestProvider, TestContext.Current?.ToolsetUnderTest?.DotNetRoot!); foreach (var manifest in manifests) { diff --git a/test/Microsoft.NET.TestFramework/AspNetSdkTest.cs b/test/Microsoft.NET.TestFramework/AspNetSdkTest.cs index 08a5b1afdb0a..3ea210276603 100644 --- a/test/Microsoft.NET.TestFramework/AspNetSdkTest.cs +++ b/test/Microsoft.NET.TestFramework/AspNetSdkTest.cs @@ -11,7 +11,7 @@ namespace Microsoft.NET.TestFramework [Trait("AspNetCore", "Integration")] public abstract class AspNetSdkTest : SdkTest { - public readonly string DefaultTfm; + public readonly string? DefaultTfm; #if !GENERATE_MSBUILD_LOGS public static bool GenerateMSbuildLogs = true; @@ -25,27 +25,27 @@ protected AspNetSdkTest(ITestOutputHelper log) : base(log) { var assembly = Assembly.GetCallingAssembly(); var testAssemblyMetadata = assembly.GetCustomAttributes(); - DefaultTfm = testAssemblyMetadata.SingleOrDefault(a => a.Key == "AspNetTestTfm").Value; + DefaultTfm = testAssemblyMetadata.SingleOrDefault(a => a.Key == "AspNetTestTfm")?.Value; } public TestAsset CreateAspNetSdkTestAsset( string testAsset, [CallerMemberName] string callerName = "", string subdirectory = "", - string overrideTfm = null, - string identifier = null) + string? overrideTfm = null, + string? identifier = null) { var projectDirectory = _testAssetsManager .CopyTestAsset(testAsset, callingMethod: callerName, testAssetSubdirectory: subdirectory, identifier: identifier) .WithSource() .WithProjectChanges(project => { - var ns = project.Root.Name.Namespace; + var ns = project.Root?.Name.Namespace; var targetFramework = project.Descendants() .SingleOrDefault(e => e.Name.LocalName == "TargetFramework"); if (targetFramework?.Value == "$(AspNetTestTfm)") { - targetFramework.Value = overrideTfm ?? DefaultTfm; + targetFramework.Value = overrideTfm ?? DefaultTfm ?? string.Empty; targetFramework.AddAfterSelf(new XElement("StaticWebAssetsFingerprintContent", "false")); } var targetFrameworks = project.Descendants() @@ -67,15 +67,15 @@ public TestAsset CreateMultitargetAspNetSdkTestAsset( string testAsset, [CallerMemberName] string callerName = "", string subdirectory = "", - string overrideTfm = null, - string identifier = null) + string? overrideTfm = null, + string? identifier = null) { var projectDirectory = _testAssetsManager .CopyTestAsset(testAsset, callingMethod: callerName, testAssetSubdirectory: subdirectory, identifier: identifier) .WithSource() .WithProjectChanges(project => { - var ns = project.Root.Name.Namespace; + var ns = project.Root?.Name.Namespace; var targetFramework = project.Descendants() .Single(e => e.Name.LocalName == "TargetFrameworks"); targetFramework.Value = targetFramework.Value.Replace("$(AspNetTestTfm)", overrideTfm ?? DefaultTfm); @@ -83,7 +83,7 @@ public TestAsset CreateMultitargetAspNetSdkTestAsset( return projectDirectory; } - protected virtual RestoreCommand CreateRestoreCommand(TestAsset asset, string relativePathToProject = null) + protected virtual RestoreCommand CreateRestoreCommand(TestAsset asset, string? relativePathToProject = null) { var restore = new RestoreCommand(asset, relativePathToProject); restore.WithWorkingDirectory(asset.TestRoot); @@ -91,7 +91,7 @@ protected virtual RestoreCommand CreateRestoreCommand(TestAsset asset, string re return restore; } - protected virtual BuildCommand CreateBuildCommand(TestAsset asset, string relativePathToProject = null) + protected virtual BuildCommand CreateBuildCommand(TestAsset asset, string? relativePathToProject = null) { var build = new BuildCommand(asset, relativePathToProject); build.WithWorkingDirectory(asset.TestRoot); @@ -100,7 +100,7 @@ protected virtual BuildCommand CreateBuildCommand(TestAsset asset, string relati return build; } - protected virtual RebuildCommand CreateRebuildCommand(TestAsset asset, string relativePathToProject = null) + protected virtual RebuildCommand CreateRebuildCommand(TestAsset asset, string? relativePathToProject = null) { var rebuild = new RebuildCommand(Log, asset.Path, relativePathToProject); rebuild.WithWorkingDirectory(asset.TestRoot); @@ -109,7 +109,7 @@ protected virtual RebuildCommand CreateRebuildCommand(TestAsset asset, string re return rebuild; } - protected virtual PackCommand CreatePackCommand(TestAsset asset, string relativePathToProject = null) + protected virtual PackCommand CreatePackCommand(TestAsset asset, string? relativePathToProject = null) { var pack = new PackCommand(asset, relativePathToProject); pack.WithWorkingDirectory(asset.TestRoot); @@ -118,7 +118,7 @@ protected virtual PackCommand CreatePackCommand(TestAsset asset, string relative return pack; } - protected virtual PublishCommand CreatePublishCommand(TestAsset asset, string relativePathToProject = null) + protected virtual PublishCommand CreatePublishCommand(TestAsset asset, string? relativePathToProject = null) { var publish = new PublishCommand(asset, relativePathToProject); publish.WithWorkingDirectory(asset.TestRoot); @@ -134,7 +134,7 @@ protected virtual CommandResult ExecuteCommand(TestCommand command, params strin if (_generateMSbuildLogs) { var i = 0; - for (i = 0; File.Exists(Path.Combine(command.WorkingDirectory, $"msbuild{i}.binlog")) && i < 20; i++) { } + for (i = 0; File.Exists(Path.Combine(command.WorkingDirectory!, $"msbuild{i}.binlog")) && i < 20; i++) { } var log = $"msbuild{i}.binlog"; return command.Execute([$"/bl:{log}", .. arguments]); @@ -152,7 +152,7 @@ protected virtual CommandResult ExecuteCommandWithoutRestore(MSBuildCommand comm if (_generateMSbuildLogs) { var i = 0; - for (i = 0; File.Exists(Path.Combine(command.WorkingDirectory, $"msbuild{i}.binlog")) && i < 20; i++) { } + for (i = 0; File.Exists(Path.Combine(command.WorkingDirectory!, $"msbuild{i}.binlog")) && i < 20; i++) { } var log = $"msbuild{i}.binlog"; return command.ExecuteWithoutRestore([$"/bl:{log}", .. arguments]); } @@ -184,6 +184,6 @@ private void ApplyDefaults(MSBuildCommand command) } } - protected virtual string GetNuGetCachePath() => null; + protected virtual string? GetNuGetCachePath() => null; } } diff --git a/test/Microsoft.NET.TestFramework/Assertions/CommandResultAssertions.cs b/test/Microsoft.NET.TestFramework/Assertions/CommandResultAssertions.cs index add580802130..30bdcf8cd9bd 100644 --- a/test/Microsoft.NET.TestFramework/Assertions/CommandResultAssertions.cs +++ b/test/Microsoft.NET.TestFramework/Assertions/CommandResultAssertions.cs @@ -66,7 +66,7 @@ public AndConstraint HaveStdOutContaining(Func(this); } - public AndConstraint NotHaveStdOutContaining(string pattern, string[] ignoredPatterns = null) + public AndConstraint NotHaveStdOutContaining(string pattern, string[]? ignoredPatterns = null) { string filteredStdOut = _commandResult.StdOut; if (ignoredPatterns != null && ignoredPatterns.Length > 0) @@ -222,7 +222,7 @@ public AndConstraint NuPkgContainsPatterns(string nupkg foreach (var pattern in filePatterns) { var directory = Path.GetDirectoryName(pattern); - var path = Path.Combine(unzipped, directory); + var path = Path.Combine(unzipped, directory!); var searchPattern = Path.GetFileName(pattern); var condition = Directory.GetFiles(path, searchPattern).Length < 1; @@ -240,7 +240,7 @@ public AndConstraint NuPkgDoesNotContainPatterns(string foreach (var pattern in filePatterns) { var directory = Path.GetDirectoryName(pattern); - var path = Path.Combine(unzipped, directory); + var path = Path.Combine(unzipped, directory!); var searchPattern = Path.GetFileName(pattern); var condition = Directory.Exists(path) && Directory.GetFiles(path, searchPattern).Length > 0; diff --git a/test/Microsoft.NET.TestFramework/Assertions/DirectoryInfoAssertions.cs b/test/Microsoft.NET.TestFramework/Assertions/DirectoryInfoAssertions.cs index a9982658f6f0..37f6439a4dae 100644 --- a/test/Microsoft.NET.TestFramework/Assertions/DirectoryInfoAssertions.cs +++ b/test/Microsoft.NET.TestFramework/Assertions/DirectoryInfoAssertions.cs @@ -99,7 +99,7 @@ public AndConstraint HaveDirectory(string expectedDir) Execute.Assertion.ForCondition(dir != null) .FailWith("Expected directory {0} cannot be found inside directory {1}.", expectedDir, _dirInfo.FullName); - return new AndConstraint(new DirectoryInfoAssertions(dir)); + return new AndConstraint(new DirectoryInfoAssertions(dir!)); } public AndConstraint OnlyHaveFiles(IEnumerable expectedFiles, SearchOption searchOption = SearchOption.AllDirectories) diff --git a/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildAndWindowsOnlyFactAttribute.cs b/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildAndWindowsOnlyFactAttribute.cs index d7f9a4994525..9a5a6009926f 100644 --- a/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildAndWindowsOnlyFactAttribute.cs +++ b/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildAndWindowsOnlyFactAttribute.cs @@ -7,7 +7,7 @@ public class CoreMSBuildAndWindowsOnlyFactAttribute : FactAttribute { public CoreMSBuildAndWindowsOnlyFactAttribute() { - if (TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild || !RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + if (TestContext.Current?.ToolsetUnderTest != null && TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild || !RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { Skip = "This test requires Core MSBuild and Windows to run"; } diff --git a/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildAndWindowsOnlyTheoryAttribute.cs b/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildAndWindowsOnlyTheoryAttribute.cs index ecf9e48bf808..d583e21137a6 100644 --- a/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildAndWindowsOnlyTheoryAttribute.cs +++ b/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildAndWindowsOnlyTheoryAttribute.cs @@ -7,7 +7,7 @@ public class CoreMSBuildAndWindowsOnlyTheoryAttribute : TheoryAttribute { public CoreMSBuildAndWindowsOnlyTheoryAttribute() { - if (TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild || !RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + if (TestContext.Current?.ToolsetUnderTest != null && TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild || !RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { Skip = "This test requires Core MSBuild and Windows to run"; } diff --git a/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildOnlyFactAttribute.cs b/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildOnlyFactAttribute.cs index c09394700ae8..b2540b5e84bf 100644 --- a/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildOnlyFactAttribute.cs +++ b/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildOnlyFactAttribute.cs @@ -7,7 +7,7 @@ public class CoreMSBuildOnlyFactAttribute : FactAttribute { public CoreMSBuildOnlyFactAttribute() { - if (TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild) + if (TestContext.Current?.ToolsetUnderTest != null && TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild) { Skip = "This test requires Core MSBuild to run"; } diff --git a/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildOnlyTheoryAttribute.cs b/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildOnlyTheoryAttribute.cs index a756d6e95a88..0398d893c79f 100644 --- a/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildOnlyTheoryAttribute.cs +++ b/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildOnlyTheoryAttribute.cs @@ -7,7 +7,7 @@ public class CoreMSBuildOnlyTheoryAttribute : TheoryAttribute { public CoreMSBuildOnlyTheoryAttribute() { - if (TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild) + if (TestContext.Current?.ToolsetUnderTest != null && TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild) { Skip = "This test requires Core MSBuild to run"; } diff --git a/test/Microsoft.NET.TestFramework/Attributes/FullMSBuildOnlyFactAttribute.cs b/test/Microsoft.NET.TestFramework/Attributes/FullMSBuildOnlyFactAttribute.cs index b7a4c9961925..ff50120ac14c 100644 --- a/test/Microsoft.NET.TestFramework/Attributes/FullMSBuildOnlyFactAttribute.cs +++ b/test/Microsoft.NET.TestFramework/Attributes/FullMSBuildOnlyFactAttribute.cs @@ -7,7 +7,7 @@ public class FullMSBuildOnlyFactAttribute : FactAttribute { public FullMSBuildOnlyFactAttribute() { - if (!TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild) + if (TestContext.Current?.ToolsetUnderTest != null && !TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild) { Skip = "This test requires Full MSBuild to run"; } diff --git a/test/Microsoft.NET.TestFramework/Attributes/FullMSBuildOnlyTheoryAttribute.cs b/test/Microsoft.NET.TestFramework/Attributes/FullMSBuildOnlyTheoryAttribute.cs index 09abd77f8dfd..2b6d559753e9 100644 --- a/test/Microsoft.NET.TestFramework/Attributes/FullMSBuildOnlyTheoryAttribute.cs +++ b/test/Microsoft.NET.TestFramework/Attributes/FullMSBuildOnlyTheoryAttribute.cs @@ -7,7 +7,7 @@ public class FullMSBuildOnlyTheoryAttribute : TheoryAttribute { public FullMSBuildOnlyTheoryAttribute() { - if (!TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild) + if (TestContext.Current?.ToolsetUnderTest != null && !TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild) { Skip = "This test requires Full MSBuild to run"; } diff --git a/test/Microsoft.NET.TestFramework/Attributes/RequiresMSBuildVersionFactAttribute.cs b/test/Microsoft.NET.TestFramework/Attributes/RequiresMSBuildVersionFactAttribute.cs index 583255c87145..ae359363546e 100644 --- a/test/Microsoft.NET.TestFramework/Attributes/RequiresMSBuildVersionFactAttribute.cs +++ b/test/Microsoft.NET.TestFramework/Attributes/RequiresMSBuildVersionFactAttribute.cs @@ -8,7 +8,7 @@ public class RequiresMSBuildVersionFactAttribute : FactAttribute /// /// Can be used to document the reason a test needs a specific version of MSBuild /// - public string Reason { get; set; } + public string? Reason { get; set; } public RequiresMSBuildVersionFactAttribute(string version) { diff --git a/test/Microsoft.NET.TestFramework/Attributes/RequiresMSBuildVersionTheoryAttribute.cs b/test/Microsoft.NET.TestFramework/Attributes/RequiresMSBuildVersionTheoryAttribute.cs index 690a2e45b964..084b3ac7102d 100644 --- a/test/Microsoft.NET.TestFramework/Attributes/RequiresMSBuildVersionTheoryAttribute.cs +++ b/test/Microsoft.NET.TestFramework/Attributes/RequiresMSBuildVersionTheoryAttribute.cs @@ -8,7 +8,7 @@ public class RequiresMSBuildVersionTheoryAttribute : TheoryAttribute /// /// Can be used to document the reason a test needs a specific version of MSBuild /// - public string Reason { get; set; } + public string? Reason { get; set; } public RequiresMSBuildVersionTheoryAttribute(string version) { @@ -17,19 +17,19 @@ public RequiresMSBuildVersionTheoryAttribute(string version) public static void CheckForRequiredMSBuildVersion(FactAttribute attribute, string version) { - if (!Version.TryParse(TestContext.Current.ToolsetUnderTest.MSBuildVersion, out Version msbuildVersion)) + if (!Version.TryParse(TestContext.Current?.ToolsetUnderTest?.MSBuildVersion, out Version? msbuildVersion)) { - attribute.Skip = $"Failed to determine the version of MSBuild ({TestContext.Current.ToolsetUnderTest.MSBuildVersion})."; + attribute.Skip = $"Failed to determine the version of MSBuild ({TestContext.Current?.ToolsetUnderTest?.MSBuildVersion})."; return; } - if (!Version.TryParse(version, out Version requiredVersion)) + if (!Version.TryParse(version, out Version? requiredVersion)) { attribute.Skip = $"Failed to determine the version required by this test ({version})."; return; } if (requiredVersion > msbuildVersion) { - attribute.Skip = $"This test requires MSBuild version {version} to run (using {TestContext.Current.ToolsetUnderTest.MSBuildVersion})."; + attribute.Skip = $"This test requires MSBuild version {version} to run (using {TestContext.Current?.ToolsetUnderTest?.MSBuildVersion})."; } } } diff --git a/test/Microsoft.NET.TestFramework/Attributes/WindowsOnlyRequiresMSBuildVersionFactAttribute.cs b/test/Microsoft.NET.TestFramework/Attributes/WindowsOnlyRequiresMSBuildVersionFactAttribute.cs index 8e3b6430fe51..73121c780841 100644 --- a/test/Microsoft.NET.TestFramework/Attributes/WindowsOnlyRequiresMSBuildVersionFactAttribute.cs +++ b/test/Microsoft.NET.TestFramework/Attributes/WindowsOnlyRequiresMSBuildVersionFactAttribute.cs @@ -8,7 +8,7 @@ public class WindowsOnlyRequiresMSBuildVersionFactAttribute : FactAttribute /// /// Gets or sets the reason for potentially skipping the test if conditions are not met. /// - public string Reason { get; set; } + public string? Reason { get; set; } public WindowsOnlyRequiresMSBuildVersionFactAttribute(string version) { diff --git a/test/Microsoft.NET.TestFramework/Commands/AddReferenceCommand.cs b/test/Microsoft.NET.TestFramework/Commands/AddReferenceCommand.cs index 3a3fef587f64..e4c8db860714 100644 --- a/test/Microsoft.NET.TestFramework/Commands/AddReferenceCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/AddReferenceCommand.cs @@ -7,7 +7,7 @@ namespace Microsoft.NET.TestFramework.Commands { public class AddReferenceCommand : DotnetCommand { - private string _projectName = null; + private string? _projectName = null; public AddReferenceCommand(ITestOutputHelper log, params string[] args) : base(log, args) { @@ -17,7 +17,7 @@ public override CommandResult Execute(IEnumerable args) { List newArgs = new(); newArgs.Add("add"); - if (!string.IsNullOrEmpty(_projectName)) + if (_projectName != null && !string.IsNullOrEmpty(_projectName)) { newArgs.Add(_projectName); } diff --git a/test/Microsoft.NET.TestFramework/Commands/BuildCommand.cs b/test/Microsoft.NET.TestFramework/Commands/BuildCommand.cs index 2c884087235f..598288455c08 100644 --- a/test/Microsoft.NET.TestFramework/Commands/BuildCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/BuildCommand.cs @@ -9,12 +9,12 @@ public sealed class BuildCommand : MSBuildCommand { // Encourage use of the other overload, which is generally simpler to use [EditorBrowsable(EditorBrowsableState.Never)] - public BuildCommand(ITestOutputHelper log, string projectRootPath, string relativePathToProject = null) + public BuildCommand(ITestOutputHelper log, string projectRootPath, string? relativePathToProject = null) : base(log, "Build", projectRootPath, relativePathToProject) { } - public BuildCommand(TestAsset testAsset, string relativePathToProject = null) + public BuildCommand(TestAsset testAsset, string? relativePathToProject = null) : base(testAsset, "Build", relativePathToProject) { } diff --git a/test/Microsoft.NET.TestFramework/Commands/CleanCommand.cs b/test/Microsoft.NET.TestFramework/Commands/CleanCommand.cs index 94a00850f95a..ed3514ebd51a 100644 --- a/test/Microsoft.NET.TestFramework/Commands/CleanCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/CleanCommand.cs @@ -5,12 +5,12 @@ namespace Microsoft.NET.TestFramework.Commands { public sealed class CleanCommand : MSBuildCommand { - public CleanCommand(ITestOutputHelper log, string projectPath, string relativePathToProject = null) + public CleanCommand(ITestOutputHelper log, string projectPath, string? relativePathToProject = null) : base(log, "Clean", projectPath, relativePathToProject) { } - public CleanCommand(TestAsset testAsset, string relativePathToProject = null) + public CleanCommand(TestAsset testAsset, string? relativePathToProject = null) : base(testAsset, "Clean", relativePathToProject) { } diff --git a/test/Microsoft.NET.TestFramework/Commands/ComposeStoreCommand.cs b/test/Microsoft.NET.TestFramework/Commands/ComposeStoreCommand.cs index 61db5a4d7d5e..c6e16960fb95 100644 --- a/test/Microsoft.NET.TestFramework/Commands/ComposeStoreCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/ComposeStoreCommand.cs @@ -14,12 +14,12 @@ public sealed class ComposeStoreCommand : MSBuildCommand { private const string PublishSubfolderName = "packages"; - public ComposeStoreCommand(ITestOutputHelper log, string projectPath, string relativePathToProject = null) + public ComposeStoreCommand(ITestOutputHelper log, string projectPath, string? relativePathToProject = null) : base(log, "ComposeStore", projectPath, relativePathToProject) { } - public override DirectoryInfo GetOutputDirectory(string targetFramework = "netcoreapp1.0", string configuration = "Debug", string runtimeIdentifier = "", string platformIdentifier = "") + public override DirectoryInfo GetOutputDirectory(string? targetFramework = "netcoreapp1.0", string configuration = "Debug", string? runtimeIdentifier = "", string? platformIdentifier = "") { string output = Path.Combine(ProjectRootPath, "bin", BuildRelativeOutputPath(targetFramework, configuration, runtimeIdentifier, platformIdentifier)); return new DirectoryInfo(output); @@ -30,14 +30,14 @@ public string GetPublishedAppPath(string appName) return Path.Combine(GetOutputDirectory().FullName, $"{appName}.dll"); } - private string BuildRelativeOutputPath(string targetFramework, string configuration, string runtimeIdentifier, string platformIdentifier) + private string BuildRelativeOutputPath(string? targetFramework, string configuration, string? runtimeIdentifier, string? platformIdentifier) { - if (runtimeIdentifier.Length == 0) + if (runtimeIdentifier?.Length == 0) { runtimeIdentifier = RuntimeInformation.RuntimeIdentifier; } - string arch = runtimeIdentifier.Substring(runtimeIdentifier.LastIndexOf("-") + 1); - return Path.Combine(platformIdentifier, configuration, arch, targetFramework, PublishSubfolderName); + string? arch = runtimeIdentifier?.Substring(runtimeIdentifier.LastIndexOf("-") + 1); + return Path.Combine(platformIdentifier!, configuration, arch!, targetFramework!, PublishSubfolderName); } } } diff --git a/test/Microsoft.NET.TestFramework/Commands/DotnetBuildCommand.cs b/test/Microsoft.NET.TestFramework/Commands/DotnetBuildCommand.cs index 42a4b9dab46c..c3c2fbd6c9da 100644 --- a/test/Microsoft.NET.TestFramework/Commands/DotnetBuildCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/DotnetBuildCommand.cs @@ -15,7 +15,7 @@ public DotnetBuildCommand(TestAsset testAsset, params string[] args) : this(test { if (testAsset.TestProject != null) { - WorkingDirectory = Path.Combine(testAsset.TestRoot, testAsset.TestProject.Name); + WorkingDirectory = Path.Combine(testAsset.TestRoot, testAsset.TestProject.Name!); } else { diff --git a/test/Microsoft.NET.TestFramework/Commands/DotnetCommand.cs b/test/Microsoft.NET.TestFramework/Commands/DotnetCommand.cs index 8122ca293148..114ddbc21ce3 100644 --- a/test/Microsoft.NET.TestFramework/Commands/DotnetCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/DotnetCommand.cs @@ -5,20 +5,20 @@ namespace Microsoft.NET.TestFramework.Commands { public class DotnetCommand : TestCommand { - public DotnetCommand(ITestOutputHelper log, params string[] args) : base(log) + public DotnetCommand(ITestOutputHelper log, params string?[] args) : base(log) { - Arguments.AddRange(args); + Arguments.AddRange(args!); } protected override SdkCommandSpec CreateCommand(IEnumerable args) { var sdkCommandSpec = new SdkCommandSpec() { - FileName = TestContext.Current.ToolsetUnderTest.DotNetHostPath, + FileName = TestContext.Current?.ToolsetUnderTest?.DotNetHostPath, Arguments = args.ToList(), WorkingDirectory = WorkingDirectory }; - TestContext.Current.AddTestEnvironmentVariables(sdkCommandSpec.Environment); + TestContext.Current?.AddTestEnvironmentVariables(sdkCommandSpec.Environment!); return sdkCommandSpec; } } diff --git a/test/Microsoft.NET.TestFramework/Commands/DotnetPublishCommand.cs b/test/Microsoft.NET.TestFramework/Commands/DotnetPublishCommand.cs index ad88c37be58e..b5dc4c2fa115 100644 --- a/test/Microsoft.NET.TestFramework/Commands/DotnetPublishCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/DotnetPublishCommand.cs @@ -5,7 +5,7 @@ namespace Microsoft.NET.TestFramework.Commands { public class DotnetPublishCommand : DotnetCommand { - private string _runtime; + private string? _runtime; public DotnetPublishCommand(ITestOutputHelper log, params string[] args) : base(log) { @@ -16,7 +16,7 @@ public DotnetPublishCommand(ITestOutputHelper log, params string[] args) : base( protected override SdkCommandSpec CreateCommand(IEnumerable args) { List newArgs = new(args); - if (!string.IsNullOrEmpty(_runtime)) + if (_runtime != null && !string.IsNullOrEmpty(_runtime)) { newArgs.Add("-r"); newArgs.Add(_runtime); diff --git a/test/Microsoft.NET.TestFramework/Commands/GetValuesCommand.cs b/test/Microsoft.NET.TestFramework/Commands/GetValuesCommand.cs index 7288f7a5bc54..209c9fc39a61 100644 --- a/test/Microsoft.NET.TestFramework/Commands/GetValuesCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/GetValuesCommand.cs @@ -22,7 +22,7 @@ public enum ValueType public string TargetName { get; set; } = "WriteValuesToFile"; - public string Configuration { get; set; } + public string? Configuration { get; set; } public List MetadataNames { get; set; } = new List(); public Dictionary Properties { get; } = new Dictionary(); @@ -43,10 +43,10 @@ public GetValuesCommand(ITestOutputHelper log, string projectPath, string target public GetValuesCommand(TestAsset testAsset, string valueName, ValueType valueType = ValueType.Property, - string targetFramework = null) + string? targetFramework = null) : base(testAsset, "WriteValuesToFile", relativePathToProject: null) { - _targetFramework = targetFramework ?? OutputPathCalculator.FromProject(ProjectFile, testAsset).TargetFramework; + _targetFramework = targetFramework ?? OutputPathCalculator.FromProject(ProjectFile, testAsset).TargetFramework!; _valueName = valueName; _valueType = valueType; @@ -67,7 +67,7 @@ protected override SdkCommandSpec CreateCommand(IEnumerable args) var project = XDocument.Load(ProjectFile); - var ns = project.Root.Name.Namespace; + var ns = project.Root!.Name.Namespace; string linesAttribute; if (_valueType == ValueType.Property) @@ -102,12 +102,12 @@ protected override SdkCommandSpec CreateCommand(IEnumerable args) new XAttribute("Name", TargetName), ShouldCompile ? new XAttribute("DependsOnTargets", DependsOnTargets) : null); - customAfterDirectoryBuildTargets.Root.Add(target); + customAfterDirectoryBuildTargets.Root?.Add(target); if (Properties.Count != 0) { propertyGroup = new XElement(ns + "PropertyGroup"); - customAfterDirectoryBuildTargets.Root.Add(propertyGroup); + customAfterDirectoryBuildTargets.Root?.Add(propertyGroup); foreach (var pair in Properties) { @@ -134,7 +134,7 @@ protected override SdkCommandSpec CreateCommand(IEnumerable args) var outputDirectory = GetValuesOutputDirectory(_targetFramework); outputDirectory.Create(); - return TestContext.Current.ToolsetUnderTest.CreateCommandForTarget(TargetName, newArgs); + return TestContext.Current?.ToolsetUnderTest?.CreateCommandForTarget(TargetName, newArgs)!; } public List GetValues() diff --git a/test/Microsoft.NET.TestFramework/Commands/ListPackageCommand.cs b/test/Microsoft.NET.TestFramework/Commands/ListPackageCommand.cs index 888b8e98fcb1..d84b5422f54b 100644 --- a/test/Microsoft.NET.TestFramework/Commands/ListPackageCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/ListPackageCommand.cs @@ -7,7 +7,7 @@ namespace Microsoft.NET.TestFramework.Commands { public class ListPackageCommand : DotnetCommand { - private string _projectName = null; + private string? _projectName = null; public ListPackageCommand(ITestOutputHelper log, params string[] args) : base(log, args) { @@ -17,7 +17,7 @@ public override CommandResult Execute(IEnumerable args) { List newArgs = new(); newArgs.Add("list"); - if (!string.IsNullOrEmpty(_projectName)) + if (_projectName != null && !string.IsNullOrEmpty(_projectName)) { newArgs.Add(_projectName); } diff --git a/test/Microsoft.NET.TestFramework/Commands/ListReferenceCommand.cs b/test/Microsoft.NET.TestFramework/Commands/ListReferenceCommand.cs index 11e4d0c9b894..77b938abbc11 100644 --- a/test/Microsoft.NET.TestFramework/Commands/ListReferenceCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/ListReferenceCommand.cs @@ -7,7 +7,7 @@ namespace Microsoft.NET.TestFramework.Commands { public class ListReferenceCommand : DotnetCommand { - private string _projectName = null; + private string? _projectName = null; public ListReferenceCommand(ITestOutputHelper log, params string[] args) : base(log, args) { @@ -17,7 +17,7 @@ public override CommandResult Execute(IEnumerable args) { List newArgs = new(); newArgs.Add("list"); - if (!string.IsNullOrEmpty(_projectName)) + if (_projectName != null && !string.IsNullOrEmpty(_projectName)) { newArgs.Add(_projectName); } diff --git a/test/Microsoft.NET.TestFramework/Commands/MSBuildCommand.cs b/test/Microsoft.NET.TestFramework/Commands/MSBuildCommand.cs index 73f3416ea8f8..bc6ce4eb26bd 100644 --- a/test/Microsoft.NET.TestFramework/Commands/MSBuildCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/MSBuildCommand.cs @@ -15,11 +15,11 @@ public class MSBuildCommand : TestCommand public string ProjectFile { get; } - public TestAsset TestAsset { get; } + public TestAsset? TestAsset { get; } public string FullPathProjectFile => Path.Combine(ProjectRootPath, ProjectFile); - public MSBuildCommand(ITestOutputHelper log, string target, string projectRootPath, string relativePathToProject = null) + public MSBuildCommand(ITestOutputHelper log, string target, string projectRootPath, string? relativePathToProject = null) : base(log) { Target = target; @@ -29,13 +29,13 @@ public MSBuildCommand(ITestOutputHelper log, string target, string projectRootPa ProjectFile = FindProjectFile(ref _projectRootPath, relativePathToProject); } - public MSBuildCommand(TestAsset testAsset, string target, string relativePathToProject = null) + public MSBuildCommand(TestAsset testAsset, string target, string? relativePathToProject = null) : this(testAsset.Log, target, testAsset.TestRoot, relativePathToProject ?? testAsset.TestProject?.Name) { TestAsset = testAsset; } - internal static string FindProjectFile(ref string projectRootPath, string relativePathToProject) + internal static string FindProjectFile(ref string projectRootPath, string? relativePathToProject) { if (File.Exists(projectRootPath) && string.IsNullOrEmpty(relativePathToProject)) { @@ -68,11 +68,11 @@ internal static string FindProjectFile(ref string projectRootPath, string relati return buildProjectFiles[0]; } - public virtual DirectoryInfo GetOutputDirectory(string targetFramework = null, string configuration = "Debug", string runtimeIdentifier = null, string platform = null) + public virtual DirectoryInfo GetOutputDirectory(string? targetFramework = null, string configuration = "Debug", string? runtimeIdentifier = null, string? platform = null) { if (TestAsset != null) { - return new DirectoryInfo(OutputPathCalculator.FromProject(ProjectFile, TestAsset).GetOutputDirectory(targetFramework, configuration, runtimeIdentifier, platform)); + return new DirectoryInfo(OutputPathCalculator.FromProject(ProjectFile, TestAsset).GetOutputDirectory(targetFramework!, configuration, runtimeIdentifier!, platform!)); } platform ??= string.Empty; @@ -84,11 +84,11 @@ public virtual DirectoryInfo GetOutputDirectory(string targetFramework = null, s return new DirectoryInfo(output); } - public virtual DirectoryInfo GetIntermediateDirectory(string targetFramework = null, string configuration = "Debug", string runtimeIdentifier = null) + public virtual DirectoryInfo GetIntermediateDirectory(string? targetFramework = null, string configuration = "Debug", string? runtimeIdentifier = null) { if (TestAsset != null) { - return new DirectoryInfo(OutputPathCalculator.FromProject(ProjectFile, TestAsset).GetIntermediateDirectory(targetFramework, configuration, runtimeIdentifier)); + return new DirectoryInfo(OutputPathCalculator.FromProject(ProjectFile, TestAsset).GetIntermediateDirectory(targetFramework!, configuration, runtimeIdentifier!)); } targetFramework = targetFramework ?? string.Empty; @@ -152,7 +152,7 @@ protected override SdkCommandSpec CreateCommand(IEnumerable args) var newArgs = args.ToList(); newArgs.Insert(0, FullPathProjectFile); - return TestContext.Current.ToolsetUnderTest.CreateCommandForTarget(Target, newArgs); + return TestContext.Current?.ToolsetUnderTest?.CreateCommandForTarget(Target, newArgs)!; } } } diff --git a/test/Microsoft.NET.TestFramework/Commands/MSBuildVersionCommand.cs b/test/Microsoft.NET.TestFramework/Commands/MSBuildVersionCommand.cs index 178daab62b70..377d0bc2f4f2 100644 --- a/test/Microsoft.NET.TestFramework/Commands/MSBuildVersionCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/MSBuildVersionCommand.cs @@ -9,7 +9,7 @@ public MSBuildVersionCommand(ITestOutputHelper log) : base(log) { } protected override SdkCommandSpec CreateCommand(IEnumerable args) { - if (TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild) + if (TestContext.Current?.ToolsetUnderTest != null && TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild) { return new SdkCommandSpec() { @@ -22,7 +22,7 @@ protected override SdkCommandSpec CreateCommand(IEnumerable args) { return new SdkCommandSpec() { - FileName = TestContext.Current.ToolsetUnderTest.DotNetHostPath, + FileName = TestContext.Current?.ToolsetUnderTest?.DotNetHostPath, Arguments = { "msbuild", "-version" }, WorkingDirectory = WorkingDirectory }; diff --git a/test/Microsoft.NET.TestFramework/Commands/NuGetExeRestoreCommand.cs b/test/Microsoft.NET.TestFramework/Commands/NuGetExeRestoreCommand.cs index 0d6e8515e1b3..a68fd4056862 100644 --- a/test/Microsoft.NET.TestFramework/Commands/NuGetExeRestoreCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/NuGetExeRestoreCommand.cs @@ -10,13 +10,13 @@ public class NuGetExeRestoreCommand : TestCommand public string ProjectFile { get; } - public string NuGetExeVersion { get; set; } + public string? NuGetExeVersion { get; set; } public string FullPathProjectFile => Path.Combine(ProjectRootPath, ProjectFile); - public string PackagesDirectory { get; set; } + public string? PackagesDirectory { get; set; } - public NuGetExeRestoreCommand(ITestOutputHelper log, string projectRootPath, string relativePathToProject = null) : base(log) + public NuGetExeRestoreCommand(ITestOutputHelper log, string projectRootPath, string? relativePathToProject = null) : base(log) { _projectRootPath = projectRootPath; ProjectFile = MSBuildCommand.FindProjectFile(ref _projectRootPath, relativePathToProject); @@ -31,27 +31,27 @@ protected override SdkCommandSpec CreateCommand(IEnumerable args) newArgs.Add(FullPathProjectFile); newArgs.Add("-PackagesDirectory"); - newArgs.Add(PackagesDirectory ?? TestContext.Current.NuGetCachePath); + newArgs.Add(PackagesDirectory ?? TestContext.Current?.NuGetCachePath ?? string.Empty); newArgs.AddRange(args); - if (string.IsNullOrEmpty(TestContext.Current.NuGetExePath)) + if (string.IsNullOrEmpty(TestContext.Current?.NuGetExePath)) { throw new InvalidOperationException("Path to nuget.exe not set"); } - var nugetExePath = TestContext.Current.NuGetExePath; + var nugetExePath = TestContext.Current?.NuGetExePath; if (!string.IsNullOrEmpty(NuGetExeVersion)) { - nugetExePath = Path.Combine(Path.GetDirectoryName(nugetExePath), NuGetExeVersion, "nuget.exe"); + nugetExePath = Path.Combine(Path.GetDirectoryName(nugetExePath)!, NuGetExeVersion, "nuget.exe"); } if (!File.Exists(nugetExePath)) { - string directory = Path.GetDirectoryName(nugetExePath); + string? directory = Path.GetDirectoryName(nugetExePath); if (!Directory.Exists(directory)) { - Directory.CreateDirectory(directory); + Directory.CreateDirectory(directory!); } string url = string.IsNullOrEmpty(NuGetExeVersion) ? @@ -60,7 +60,7 @@ protected override SdkCommandSpec CreateCommand(IEnumerable args) using (var client = new System.Net.Http.HttpClient()) using (var response = client.GetAsync(url).ConfigureAwait(false).GetAwaiter().GetResult()) - using (var fs = new FileStream(nugetExePath, FileMode.CreateNew)) + using (var fs = new FileStream(nugetExePath!, FileMode.CreateNew)) { response.Content.CopyToAsync(fs).ConfigureAwait(false).GetAwaiter().GetResult(); } @@ -72,7 +72,7 @@ protected override SdkCommandSpec CreateCommand(IEnumerable args) Arguments = newArgs }; - TestContext.Current.AddTestEnvironmentVariables(ret.Environment); + TestContext.Current?.AddTestEnvironmentVariables(ret.Environment!); return ret; } diff --git a/test/Microsoft.NET.TestFramework/Commands/PackCommand.cs b/test/Microsoft.NET.TestFramework/Commands/PackCommand.cs index d2944f953b93..5ea180ba8e01 100644 --- a/test/Microsoft.NET.TestFramework/Commands/PackCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/PackCommand.cs @@ -5,19 +5,19 @@ namespace Microsoft.NET.TestFramework.Commands { public sealed class PackCommand : MSBuildCommand { - public PackCommand(ITestOutputHelper log, string projectPath, string relativePathToProject = null) + public PackCommand(ITestOutputHelper log, string projectPath, string? relativePathToProject = null) : base(log, "Pack", projectPath, relativePathToProject) { } - public PackCommand(TestAsset testAsset, string relativePathToProject = null) + public PackCommand(TestAsset testAsset, string? relativePathToProject = null) : base(testAsset, "Pack", relativePathToProject) { } - public string GetIntermediateNuspecPath(string packageId = null, string configuration = "Debug", string packageVersion = "1.0.0") + public string GetIntermediateNuspecPath(string? packageId = null, string configuration = "Debug", string packageVersion = "1.0.0") { if (packageId == null) { @@ -27,7 +27,7 @@ public string GetIntermediateNuspecPath(string packageId = null, string configur return Path.Combine(GetBaseIntermediateDirectory().FullName, configuration, $"{packageId}.{packageVersion}.nuspec"); } - public string GetNuGetPackage(string packageId = null, string configuration = "Debug", string packageVersion = "1.0.0") + public string GetNuGetPackage(string? packageId = null, string configuration = "Debug", string packageVersion = "1.0.0") { if (packageId == null) { diff --git a/test/Microsoft.NET.TestFramework/Commands/PublishCommand.cs b/test/Microsoft.NET.TestFramework/Commands/PublishCommand.cs index b46caf82e96d..bd560ba4ea6d 100644 --- a/test/Microsoft.NET.TestFramework/Commands/PublishCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/PublishCommand.cs @@ -16,17 +16,17 @@ public PublishCommand(ITestOutputHelper log, string projectPath) { } - public PublishCommand(TestAsset testAsset, string relativePathToProject = null) + public PublishCommand(TestAsset testAsset, string? relativePathToProject = null) : base(testAsset, "Publish", relativePathToProject) { } - public override DirectoryInfo GetOutputDirectory(string targetFramework = null, string configuration = "Debug", string runtimeIdentifier = "", string platformIdentifier = "") + public override DirectoryInfo GetOutputDirectory(string? targetFramework = null, string configuration = "Debug", string? runtimeIdentifier = "", string? platformIdentifier = "") { if (TestAsset != null) { - return new DirectoryInfo(OutputPathCalculator.FromProject(ProjectFile, TestAsset).GetPublishDirectory(targetFramework, configuration, runtimeIdentifier, platformIdentifier)); + return new DirectoryInfo(OutputPathCalculator.FromProject(ProjectFile, TestAsset).GetPublishDirectory(targetFramework!, configuration, runtimeIdentifier!, platformIdentifier!)); } if (string.IsNullOrEmpty(targetFramework)) diff --git a/test/Microsoft.NET.TestFramework/Commands/RebuildCommand.cs b/test/Microsoft.NET.TestFramework/Commands/RebuildCommand.cs index 4d820b5845a2..0d8aa5964997 100644 --- a/test/Microsoft.NET.TestFramework/Commands/RebuildCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/RebuildCommand.cs @@ -5,7 +5,7 @@ namespace Microsoft.NET.TestFramework.Commands { public sealed class RebuildCommand : MSBuildCommand { - public RebuildCommand(ITestOutputHelper log, string projectPath, string relativePathToProject = null) + public RebuildCommand(ITestOutputHelper log, string projectPath, string? relativePathToProject = null) : base(log, "Rebuild", projectPath, relativePathToProject) { } diff --git a/test/Microsoft.NET.TestFramework/Commands/RemoveReferenceCommand.cs b/test/Microsoft.NET.TestFramework/Commands/RemoveReferenceCommand.cs index 4147cf08de94..a3ac184a53dc 100644 --- a/test/Microsoft.NET.TestFramework/Commands/RemoveReferenceCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/RemoveReferenceCommand.cs @@ -7,7 +7,7 @@ namespace Microsoft.NET.TestFramework.Commands { public class RemoveReferenceCommand : DotnetCommand { - private string _projectName = null; + private string? _projectName = null; public RemoveReferenceCommand(ITestOutputHelper log, params string[] args) : base(log, args) { @@ -17,7 +17,7 @@ public override CommandResult Execute(IEnumerable args) { List newArgs = new(); newArgs.Add("remove"); - if (!string.IsNullOrEmpty(_projectName)) + if (_projectName != null && !string.IsNullOrEmpty(_projectName)) { newArgs.Add(_projectName); } diff --git a/test/Microsoft.NET.TestFramework/Commands/RestoreCommand.cs b/test/Microsoft.NET.TestFramework/Commands/RestoreCommand.cs index 137bd5996c5f..8230a3706222 100644 --- a/test/Microsoft.NET.TestFramework/Commands/RestoreCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/RestoreCommand.cs @@ -9,12 +9,12 @@ public sealed class RestoreCommand : MSBuildCommand { // Encourage use of the other overload, which is generally simpler to use [EditorBrowsable(EditorBrowsableState.Never)] - public RestoreCommand(ITestOutputHelper log, string projectPath, string relativePathToProject = null) + public RestoreCommand(ITestOutputHelper log, string projectPath, string? relativePathToProject = null) : base(log, "Restore", projectPath, relativePathToProject) { } - public RestoreCommand(TestAsset testAsset, string relativePathToProject = null) + public RestoreCommand(TestAsset testAsset, string? relativePathToProject = null) : base(testAsset, "Restore", relativePathToProject) { } diff --git a/test/Microsoft.NET.TestFramework/Commands/RunExeCommand.cs b/test/Microsoft.NET.TestFramework/Commands/RunExeCommand.cs index 97751154a6a4..22c89740f650 100644 --- a/test/Microsoft.NET.TestFramework/Commands/RunExeCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/RunExeCommand.cs @@ -26,7 +26,7 @@ protected override SdkCommandSpec CreateCommand(IEnumerable args) Arguments = args.ToList(), WorkingDirectory = WorkingDirectory, }; - TestContext.Current.AddTestEnvironmentVariables(sdkCommandSpec.Environment); + TestContext.Current?.AddTestEnvironmentVariables(sdkCommandSpec.Environment!); return sdkCommandSpec; } } diff --git a/test/Microsoft.NET.TestFramework/Commands/SdkCommandSpec.cs b/test/Microsoft.NET.TestFramework/Commands/SdkCommandSpec.cs index ee8c0055b82e..c185bb5ef43e 100644 --- a/test/Microsoft.NET.TestFramework/Commands/SdkCommandSpec.cs +++ b/test/Microsoft.NET.TestFramework/Commands/SdkCommandSpec.cs @@ -8,14 +8,14 @@ namespace Microsoft.NET.TestFramework.Commands { public class SdkCommandSpec { - public string FileName { get; set; } + public string? FileName { get; set; } public List Arguments { get; set; } = new List(); public Dictionary Environment { get; set; } = new Dictionary(); public List EnvironmentToRemove { get; } = new List(); - public string WorkingDirectory { get; set; } + public string? WorkingDirectory { get; set; } private string EscapeArgs() { diff --git a/test/Microsoft.NET.TestFramework/Commands/TestCommand.cs b/test/Microsoft.NET.TestFramework/Commands/TestCommand.cs index 55b8c0181e83..d46175d4e229 100644 --- a/test/Microsoft.NET.TestFramework/Commands/TestCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/TestCommand.cs @@ -14,15 +14,15 @@ public abstract class TestCommand public ITestOutputHelper Log { get; } - public string WorkingDirectory { get; set; } + public string? WorkingDirectory { get; set; } public List Arguments { get; set; } = new List(); public List EnvironmentToRemove { get; } = new List(); // These only work via Execute(), not when using GetProcessStartInfo() - public Action CommandOutputHandler { get; set; } - public Action ProcessStartedHandler { get; set; } + public Action? CommandOutputHandler { get; set; } + public Action? ProcessStartedHandler { get; set; } protected TestCommand(ITestOutputHelper log) { diff --git a/test/Microsoft.NET.TestFramework/ConflictResolutionAssets.cs b/test/Microsoft.NET.TestFramework/ConflictResolutionAssets.cs index 5e17bb7f0971..5b74e3c20aa5 100644 --- a/test/Microsoft.NET.TestFramework/ConflictResolutionAssets.cs +++ b/test/Microsoft.NET.TestFramework/ConflictResolutionAssets.cs @@ -61,7 +61,7 @@ public static IEnumerable> ConflictResolutionDependencies foreach (var dependency in netStandardDependencies.Elements("dependency")) { - yield return Tuple.Create(dependency.Attribute("id").Value, dependency.Attribute("version").Value); + yield return Tuple.Create(dependency.Attribute("id")?.Value, dependency.Attribute("version")?.Value)!; } yield return Tuple.Create("System.Diagnostics.TraceSource", "4.0.0"); diff --git a/test/Microsoft.NET.TestFramework/EnvironmentInfo.cs b/test/Microsoft.NET.TestFramework/EnvironmentInfo.cs index 813943ca1bd1..a1ef8b832be7 100644 --- a/test/Microsoft.NET.TestFramework/EnvironmentInfo.cs +++ b/test/Microsoft.NET.TestFramework/EnvironmentInfo.cs @@ -19,7 +19,7 @@ public static string ExecutableExtension } } - public static string GetCompatibleRid(string targetFramework = null) + public static string GetCompatibleRid(string? targetFramework = null) { string rid = RuntimeInformation.RuntimeIdentifier; @@ -62,7 +62,7 @@ public static string GetCompatibleRid(string targetFramework = null) // able to run on the current OS public static bool SupportsTargetFramework(string targetFramework) { - NuGetFramework nugetFramework = null; + NuGetFramework? nugetFramework = null; try { nugetFramework = NuGetFramework.Parse(targetFramework); @@ -101,7 +101,7 @@ public static bool SupportsTargetFramework(string targetFramework) return false; } } - else if (Version.TryParse(versionString, out Version osVersion)) + else if (Version.TryParse(versionString, out Version? osVersion)) { if (osId.Equals("fedora", StringComparison.OrdinalIgnoreCase)) { diff --git a/test/Microsoft.NET.TestFramework/FileConstants.cs b/test/Microsoft.NET.TestFramework/FileConstants.cs index 37ac9b06e66d..54902a4e0b5d 100644 --- a/test/Microsoft.NET.TestFramework/FileConstants.cs +++ b/test/Microsoft.NET.TestFramework/FileConstants.cs @@ -9,7 +9,7 @@ public static class FileConstants public static readonly string DynamicLibSuffix = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".dll" : RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? ".dylib" : ".so"; - public static readonly string UserProfileFolder = Environment.GetEnvironmentVariable( + public static readonly string? UserProfileFolder = Environment.GetEnvironmentVariable( RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "USERPROFILE" : "HOME"); } diff --git a/test/Microsoft.NET.TestFramework/InMemoryLoggerProvider.cs b/test/Microsoft.NET.TestFramework/InMemoryLoggerProvider.cs index bddb608c52ca..f37307211a45 100644 --- a/test/Microsoft.NET.TestFramework/InMemoryLoggerProvider.cs +++ b/test/Microsoft.NET.TestFramework/InMemoryLoggerProvider.cs @@ -2,8 +2,6 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. // -#nullable enable - using Microsoft.Extensions.Logging; namespace Microsoft.NET.TestFramework diff --git a/test/Microsoft.NET.TestFramework/Microsoft.NET.TestFramework.csproj b/test/Microsoft.NET.TestFramework/Microsoft.NET.TestFramework.csproj index 7ef173306cc0..79ed1e5379c8 100644 --- a/test/Microsoft.NET.TestFramework/Microsoft.NET.TestFramework.csproj +++ b/test/Microsoft.NET.TestFramework/Microsoft.NET.TestFramework.csproj @@ -4,6 +4,7 @@ false + enable diff --git a/test/Microsoft.NET.TestFramework/Mock/FileSystemMockBuilder.cs b/test/Microsoft.NET.TestFramework/Mock/FileSystemMockBuilder.cs index 2648c0b6bf8e..1c0f27015798 100644 --- a/test/Microsoft.NET.TestFramework/Mock/FileSystemMockBuilder.cs +++ b/test/Microsoft.NET.TestFramework/Mock/FileSystemMockBuilder.cs @@ -11,9 +11,9 @@ namespace Microsoft.Extensions.DependencyModel.Tests public class FileSystemMockBuilder { private readonly List _actions = new(); - private MockFileSystemModel _mockFileSystemModel; - public string TemporaryFolder { get; set; } - public string WorkingDirectory { get; set; } + private MockFileSystemModel? _mockFileSystemModel; + public string? TemporaryFolder { get; set; } + public string? WorkingDirectory { get; set; } internal static IFileSystem Empty { get; } = Create().Build(); @@ -24,18 +24,18 @@ public static FileSystemMockBuilder Create() public FileSystemMockBuilder AddFile(string name, string content = "") { - _actions.Add(() => _mockFileSystemModel.CreateDirectory(Path.GetDirectoryName(name))); - _actions.Add(() => _mockFileSystemModel.CreateFile(name, content)); + _actions.Add(() => _mockFileSystemModel?.CreateDirectory(Path.GetDirectoryName(name)!)); + _actions.Add(() => _mockFileSystemModel?.CreateFile(name, content)); return this; } public FileSystemMockBuilder AddFiles(string basePath, params string[] files) { - _actions.Add(() => _mockFileSystemModel.CreateDirectory(basePath)); + _actions.Add(() => _mockFileSystemModel?.CreateDirectory(basePath)); foreach (string file in files) { - _actions.Add(() => _mockFileSystemModel.CreateFile(Path.Combine(basePath, file), "")); + _actions.Add(() => _mockFileSystemModel?.CreateFile(Path.Combine(basePath, file), "")); } return this; @@ -67,9 +67,9 @@ internal IFileSystem Build() private class MockFileSystemModel { - public MockFileSystemModel(string temporaryFolder, - FileSystemRoot files = null, - string fileSystemMockWorkingDirectory = null) + public MockFileSystemModel(string? temporaryFolder, + FileSystemRoot? files = null, + string? fileSystemMockWorkingDirectory = null) { if (fileSystemMockWorkingDirectory == null) { @@ -125,7 +125,7 @@ public void CreateDirectory(string path) { PathModel pathModel = CreateFullPathModel(path); - if (!Files.Volume.TryGetValue(pathModel.Volume, out DirectoryNode current)) + if (!Files.Volume.TryGetValue(pathModel.Volume, out DirectoryNode? current)) { current = new DirectoryNode(); current = Files.Volume.GetOrAdd(pathModel.Volume, current); @@ -209,18 +209,18 @@ public void CreateFile(string path, string content) throw new FileNotFoundException($"Could not find file '{path}'"); } - public IEnumerable EnumerateDirectory( + public IEnumerable? EnumerateDirectory( string path, Func, IEnumerable> predicate) { DirectoryNode current = GetParentOfDirectoryNode(path); PathModel pathModel = new(path); - DirectoryNode directoryNode = current.Subs[pathModel.FileOrDirectoryName()] as DirectoryNode; + DirectoryNode? directoryNode = current.Subs[pathModel.FileOrDirectoryName()] as DirectoryNode; Debug.Assert(directoryNode != null, nameof(directoryNode) + " != null"); - return predicate(directoryNode.Subs); + return predicate(directoryNode?.Subs!); } public DirectoryNode GetParentOfDirectoryNode(string path) @@ -503,7 +503,7 @@ public void Delete(string path) // facade private class DirectoryMock : IDirectory { - private readonly MockFileSystemModel _files; + private readonly MockFileSystemModel? _files; public DirectoryMock(MockFileSystemModel files) { @@ -517,7 +517,7 @@ public bool Exists(string path) { if (path == null) throw new ArgumentNullException(nameof(path)); - if (_files.TryGetNodeParent(path, out DirectoryNode current)) + if (_files != null && _files.TryGetNodeParent(path, out DirectoryNode current)) { PathModel pathModel = new(path); @@ -530,47 +530,47 @@ public bool Exists(string path) public ITemporaryDirectory CreateTemporaryDirectory() { - TemporaryDirectoryMock temporaryDirectoryMock = new(_files.TemporaryFolder); + TemporaryDirectoryMock temporaryDirectoryMock = new(_files?.TemporaryFolder); CreateDirectory(temporaryDirectoryMock.DirectoryPath); return temporaryDirectoryMock; } - public IEnumerable EnumerateDirectories(string path) + public IEnumerable? EnumerateDirectories(string path) { if (path == null) throw new ArgumentNullException(nameof(path)); - return _files.EnumerateDirectory(path, + return _files?.EnumerateDirectory(path, subs => subs.Where(s => s.Value is DirectoryNode) - .Select(s => Path.Combine(path, s.Key))); + .Select(s => Path.Combine(path, s.Key)))!; } - public IEnumerable EnumerateFiles(string path) + public IEnumerable? EnumerateFiles(string path) { if (path == null) throw new ArgumentNullException(nameof(path)); - return _files.EnumerateDirectory(path, + return _files?.EnumerateDirectory(path, subs => subs.Where(s => s.Value is FileNode) - .Select(s => Path.Combine(path, s.Key))); + .Select(s => Path.Combine(path, s.Key)))!; } - public IEnumerable EnumerateFileSystemEntries(string path) + public IEnumerable? EnumerateFileSystemEntries(string path) { if (path == null) throw new ArgumentNullException(nameof(path)); - return _files.EnumerateDirectory(path, - subs => subs.Select(s => Path.Combine(path, s.Key))); + return _files?.EnumerateDirectory(path, + subs => subs.Select(s => Path.Combine(path, s.Key)))!; } public string GetCurrentDirectory() { - return _files.WorkingDirectory; + return _files?.WorkingDirectory!; } - public void CreateDirectory(string path) + public void CreateDirectory(string? path) { if (path == null) throw new ArgumentNullException(nameof(path)); - _files.CreateDirectory(path); + _files?.CreateDirectory(path); } public string CreateTemporarySubdirectory() @@ -582,20 +582,20 @@ public void Delete(string path, bool recursive) { if (path == null) throw new ArgumentNullException(nameof(path)); - DirectoryNode parentOfPath = _files.GetParentOfDirectoryNode(path); + DirectoryNode? parentOfPath = _files?.GetParentOfDirectoryNode(path); PathModel pathModel = new(path); if (recursive) { - parentOfPath.Subs.TryRemove(pathModel.FileOrDirectoryName(), out _); + parentOfPath?.Subs.TryRemove(pathModel.FileOrDirectoryName(), out _); } else { - if (EnumerateFiles(path).Any()) + if (EnumerateFiles(path)!.Any()) { throw new IOException("Directory not empty"); } - parentOfPath.Subs.TryRemove(pathModel.FileOrDirectoryName(), out _); + parentOfPath?.Subs.TryRemove(pathModel.FileOrDirectoryName(), out _); } } @@ -611,14 +611,14 @@ public void Move(string source, string destination) throw new ArgumentNullException(nameof(destination)); } - DirectoryNode sourceParent - = _files.GetParentOfDirectoryNode(source); + DirectoryNode? sourceParent + = _files?.GetParentOfDirectoryNode(source); PathModel parentPathModel = new(source); - IFileSystemTreeNode sourceNode = sourceParent.Subs[parentPathModel.FileOrDirectoryName()]; + IFileSystemTreeNode? sourceNode = sourceParent?.Subs[parentPathModel.FileOrDirectoryName()]; - if (_files.TryGetNodeParent(destination, out DirectoryNode current) && current != null) + if (_files != null && _files.TryGetNodeParent(destination, out DirectoryNode current) && current != null) { PathModel destinationPathModel = new(destination); @@ -633,8 +633,8 @@ DirectoryNode sourceParent " directory with the same name already exists"); } - sourceNode = current.Subs.GetOrAdd(destinationPathModel.FileOrDirectoryName(), sourceNode); - sourceParent.Subs.TryRemove(parentPathModel.FileOrDirectoryName(), out _); + sourceNode = current.Subs.GetOrAdd(destinationPathModel.FileOrDirectoryName(), sourceNode!); + sourceParent?.Subs.TryRemove(parentPathModel.FileOrDirectoryName(), out _); } else { @@ -671,14 +671,14 @@ public FileNode(string content) private class TemporaryDirectoryMock : ITemporaryDirectoryMock { - public TemporaryDirectoryMock(string temporaryDirectory) + public TemporaryDirectoryMock(string? temporaryDirectory) { DirectoryPath = temporaryDirectory; } public bool DisposedTemporaryDirectory { get; private set; } - public string DirectoryPath { get; } + public string? DirectoryPath { get; } public void Dispose() { diff --git a/test/Microsoft.NET.TestFramework/NuGetTestLogger.cs b/test/Microsoft.NET.TestFramework/NuGetTestLogger.cs index 66c86fef5405..7a08d1ab1f2c 100644 --- a/test/Microsoft.NET.TestFramework/NuGetTestLogger.cs +++ b/test/Microsoft.NET.TestFramework/NuGetTestLogger.cs @@ -8,7 +8,7 @@ namespace Microsoft.NET.TestFramework { public class NuGetTestLogger : ILogger { - private readonly ITestOutputHelper _output; + private readonly ITestOutputHelper? _output; public NuGetTestLogger() { @@ -95,7 +95,7 @@ private void DumpMessage(string level, string data) public void Clear() { - string msg; + string? msg; while (Messages.TryDequeue(out msg)) { // do nothing diff --git a/test/Microsoft.NET.TestFramework/OutputPathCalculator.cs b/test/Microsoft.NET.TestFramework/OutputPathCalculator.cs index 18fd2d8c9cfa..ebd156612efc 100644 --- a/test/Microsoft.NET.TestFramework/OutputPathCalculator.cs +++ b/test/Microsoft.NET.TestFramework/OutputPathCalculator.cs @@ -5,25 +5,25 @@ namespace Microsoft.NET.TestFramework { public class OutputPathCalculator { - public string ProjectPath { get; set; } + public string? ProjectPath { get; set; } public bool UseArtifactsOutput { get; set; } public bool IncludeProjectNameInArtifactsPaths { get; set; } - public string ArtifactsPath { get; set; } - public string TargetFramework { get; set; } - public string TargetFrameworks { get; set; } + public string? ArtifactsPath { get; set; } + public string? TargetFramework { get; set; } + public string? TargetFrameworks { get; set; } - public string RuntimeIdentifier { get; set; } + public string? RuntimeIdentifier { get; set; } public bool IsSdkProject { get; set; } = true; public static OutputPathCalculator FromProject(string projectPath, TestAsset testAsset) { - return FromProject(projectPath, testAsset?.TestProject); + return FromProject(projectPath, testAsset.TestProject); } - public static OutputPathCalculator FromProject(string projectPath, TestProject testProject = null) + public static OutputPathCalculator FromProject(string? projectPath, TestProject? testProject = null) { - string originalProjectPath = projectPath; + string? originalProjectPath = projectPath; if (!File.Exists(projectPath) && Directory.Exists(projectPath)) { @@ -33,7 +33,7 @@ public static OutputPathCalculator FromProject(string projectPath, TestProject t // Support passing in the root test path and looking in subfolder specified by testProject if (projectPath == null && testProject != null) { - projectPath = Path.Combine(originalProjectPath, testProject.Name); + projectPath = Path.Combine(originalProjectPath!, testProject.Name!); if (!File.Exists(projectPath) && Directory.Exists(projectPath)) { @@ -70,22 +70,22 @@ public static OutputPathCalculator FromProject(string projectPath, TestProject t if (calculator.IncludeProjectNameInArtifactsPaths) { - string directoryBuildPropsFile = GetDirectoryBuildPropsPath(projectPath); + string? directoryBuildPropsFile = GetDirectoryBuildPropsPath(projectPath); if (directoryBuildPropsFile == null) { throw new InvalidOperationException("Couldn't find Directory.Build.props for test project " + projectPath); } - calculator.ArtifactsPath = Path.Combine(Path.GetDirectoryName(directoryBuildPropsFile), "artifacts"); + calculator.ArtifactsPath = Path.Combine(Path.GetDirectoryName(directoryBuildPropsFile)!, "artifacts"); } else { - calculator.ArtifactsPath = Path.Combine(Path.GetDirectoryName(projectPath), "artifacts"); + calculator.ArtifactsPath = Path.Combine(Path.GetDirectoryName(projectPath)!, "artifacts"); } } else { var project = XDocument.Load(projectPath); - var ns = project.Root.Name.Namespace; + var ns = project.Root!.Name.Namespace; var useArtifactsOutputElement = project.Root.Elements(ns + "PropertyGroup").Elements(ns + "UseArtifactsOutput").FirstOrDefault(); if (useArtifactsOutputElement != null) @@ -94,7 +94,7 @@ public static OutputPathCalculator FromProject(string projectPath, TestProject t if (calculator.UseArtifactsOutput) { calculator.IncludeProjectNameInArtifactsPaths = false; - calculator.ArtifactsPath = Path.Combine(Path.GetDirectoryName(projectPath), "artifacts"); + calculator.ArtifactsPath = Path.Combine(Path.GetDirectoryName(projectPath)!, "artifacts"); } } @@ -120,7 +120,7 @@ public static OutputPathCalculator FromProject(string projectPath, TestProject t if (directoryBuildPropsFile != null) { var dbp = XDocument.Load(directoryBuildPropsFile); - var dbpns = dbp.Root.Name.Namespace; + var dbpns = dbp.Root!.Name.Namespace; var dbpUsesArtifacts = dbp.Root.Elements(dbpns + "PropertyGroup").Elements(dbpns + "UseArtifactsOutput").FirstOrDefault(); if (dbpUsesArtifacts != null) @@ -130,7 +130,7 @@ public static OutputPathCalculator FromProject(string projectPath, TestProject t if (calculator.UseArtifactsOutput) { calculator.IncludeProjectNameInArtifactsPaths = true; - calculator.ArtifactsPath = Path.Combine(Path.GetDirectoryName(directoryBuildPropsFile), "artifacts"); + calculator.ArtifactsPath = Path.Combine(Path.GetDirectoryName(directoryBuildPropsFile)!, "artifacts"); } } } @@ -139,9 +139,9 @@ public static OutputPathCalculator FromProject(string projectPath, TestProject t return calculator; } - private static string GetDirectoryBuildPropsPath(string projectPath) + private static string? GetDirectoryBuildPropsPath(string projectPath) { - string folder = Path.GetDirectoryName(projectPath); + string? folder = Path.GetDirectoryName(projectPath); while (folder != null) { string directoryBuildPropsFile = Path.Combine(folder, "Directory.Build.props"); @@ -159,7 +159,7 @@ public bool IsMultiTargeted() return !string.IsNullOrEmpty(TargetFrameworks); } - public string GetOutputDirectory(string targetFramework = null, string configuration = "Debug", string runtimeIdentifier = "", string platform = "") + public string GetOutputDirectory(string? targetFramework = null, string configuration = "Debug", string? runtimeIdentifier = "", string platform = "") { if (UseArtifactsOutput) { @@ -179,11 +179,11 @@ public string GetOutputDirectory(string targetFramework = null, string configura if (IncludeProjectNameInArtifactsPaths) { - return Path.Combine(ArtifactsPath, "bin", Path.GetFileNameWithoutExtension(ProjectPath), pivot); + return Path.Combine(ArtifactsPath!, "bin", Path.GetFileNameWithoutExtension(ProjectPath)!, pivot); } else { - return Path.Combine(ArtifactsPath, "bin", pivot); + return Path.Combine(ArtifactsPath!, "bin", pivot); } } else @@ -195,18 +195,18 @@ public string GetOutputDirectory(string targetFramework = null, string configura if (IsSdkProject) { - string output = Path.Combine(Path.GetDirectoryName(ProjectPath), "bin", platform, configuration, targetFramework, runtimeIdentifier); + string output = Path.Combine(Path.GetDirectoryName(ProjectPath)!, "bin", platform, configuration, targetFramework, runtimeIdentifier); return output; } else { - string output = Path.Combine(Path.GetDirectoryName(ProjectPath), "bin", platform, configuration); + string output = Path.Combine(Path.GetDirectoryName(ProjectPath)!, "bin", platform, configuration); return output; } } } - public string GetPublishDirectory(string targetFramework = null, string configuration = "Debug", string runtimeIdentifier = "", string platform = "") + public string GetPublishDirectory(string? targetFramework = null, string configuration = "Debug", string? runtimeIdentifier = "", string platform = "") { if (UseArtifactsOutput) { @@ -226,11 +226,11 @@ public string GetPublishDirectory(string targetFramework = null, string configur if (IncludeProjectNameInArtifactsPaths) { - return Path.Combine(ArtifactsPath, "publish", Path.GetFileNameWithoutExtension(ProjectPath), pivot); + return Path.Combine(ArtifactsPath!, "publish", Path.GetFileNameWithoutExtension(ProjectPath)!, pivot); } else { - return Path.Combine(ArtifactsPath, "publish", pivot); + return Path.Combine(ArtifactsPath!, "publish", pivot); } } else @@ -240,12 +240,12 @@ public string GetPublishDirectory(string targetFramework = null, string configur runtimeIdentifier ??= RuntimeIdentifier ?? string.Empty; platform ??= string.Empty; - string output = Path.Combine(Path.GetDirectoryName(ProjectPath), "bin", platform, configuration, targetFramework, runtimeIdentifier, "publish"); + string output = Path.Combine(Path.GetDirectoryName(ProjectPath)!, "bin", platform, configuration, targetFramework, runtimeIdentifier, "publish"); return output; } } - public string GetIntermediateDirectory(string targetFramework = null, string configuration = "Debug", string runtimeIdentifier = "") + public string GetIntermediateDirectory(string? targetFramework = null, string configuration = "Debug", string? runtimeIdentifier = "") { if (UseArtifactsOutput) { @@ -265,11 +265,11 @@ public string GetIntermediateDirectory(string targetFramework = null, string con if (IncludeProjectNameInArtifactsPaths) { - return Path.Combine(ArtifactsPath, "obj", Path.GetFileNameWithoutExtension(ProjectPath), pivot); + return Path.Combine(ArtifactsPath!, "obj", Path.GetFileNameWithoutExtension(ProjectPath)!, pivot); } else { - return Path.Combine(ArtifactsPath, "obj", pivot); + return Path.Combine(ArtifactsPath!, "obj", pivot); } } @@ -278,7 +278,7 @@ public string GetIntermediateDirectory(string targetFramework = null, string con configuration = configuration ?? string.Empty; runtimeIdentifier = runtimeIdentifier ?? RuntimeIdentifier ?? string.Empty; - string output = Path.Combine(Path.GetDirectoryName(ProjectPath), "obj", configuration, targetFramework, runtimeIdentifier); + string output = Path.Combine(Path.GetDirectoryName(ProjectPath)!, "obj", configuration, targetFramework, runtimeIdentifier); return output; } @@ -286,11 +286,11 @@ public string GetPackageDirectory(string configuration = "Debug") { if (UseArtifactsOutput) { - return Path.Combine(ArtifactsPath, "package", configuration.ToLowerInvariant()); + return Path.Combine(ArtifactsPath!, "package", configuration.ToLowerInvariant()); } else { - return Path.Combine(Path.GetDirectoryName(ProjectPath), "bin", configuration); + return Path.Combine(Path.GetDirectoryName(ProjectPath)!, "bin", configuration); } } } diff --git a/test/Microsoft.NET.TestFramework/ProjectConstruction/TestProject.cs b/test/Microsoft.NET.TestFramework/ProjectConstruction/TestProject.cs index 0299e803f6c4..a24d6ed50e60 100644 --- a/test/Microsoft.NET.TestFramework/ProjectConstruction/TestProject.cs +++ b/test/Microsoft.NET.TestFramework/ProjectConstruction/TestProject.cs @@ -9,7 +9,7 @@ namespace Microsoft.NET.TestFramework.ProjectConstruction { public class TestProject { - public TestProject([CallerMemberName] string name = null) + public TestProject([CallerMemberName] string? name = null) { if (name != null) { @@ -21,7 +21,7 @@ public TestProject([CallerMemberName] string name = null) /// A name for the test project that's used to isolate it from a test's root folder by appending it to the root test path. /// By default, it is the unhashed name of the function that instantiated the TestProject object. /// - public string Name { get; set; } + public string? Name { get; set; } public bool IsSdkProject { get; set; } = true; @@ -33,7 +33,7 @@ public TestProject([CallerMemberName] string name = null) public bool IsWinExe { get; set; } - public string ProjectSdk { get; set; } + public string? ProjectSdk { get; set; } /// /// Applies to SDK-style projects. If the value has only one target framework (ie no semicolons), the value will be used @@ -41,17 +41,17 @@ public TestProject([CallerMemberName] string name = null) /// public string TargetFrameworks { get; set; } = ToolsetInfo.CurrentTargetFramework; - public string RuntimeFrameworkVersion { get; set; } + public string? RuntimeFrameworkVersion { get; set; } - public string RuntimeIdentifier { get; set; } + public string? RuntimeIdentifier { get; set; } // Set to either true, false, or empty string "". The empty string does not undefine SelfContained, it just doesn't specify it. public string SelfContained { get; set; } = ""; // TargetFrameworkVersion applies to non-SDK projects - public string TargetFrameworkVersion { get; set; } + public string? TargetFrameworkVersion { get; set; } - public string TargetFrameworkProfile { get; set; } + public string? TargetFrameworkProfile { get; set; } public bool UseArtifactsOutput { get; set; } @@ -127,7 +127,7 @@ public bool BuildsOnNonWindows internal void Create(TestAsset targetTestAsset, string testProjectsSourceFolder, string targetExtension = ".csproj") { - string targetFolder = Path.Combine(targetTestAsset.Path, Name); + string targetFolder = Path.Combine(targetTestAsset.Path, Name!); Directory.CreateDirectory(targetFolder); string targetProjectPath = Path.Combine(targetFolder, Name + targetExtension); @@ -144,7 +144,7 @@ internal void Create(TestAsset targetTestAsset, string testProjectsSourceFolder, } // Copy any additional files from template - foreach (var file in Directory.GetFiles(Path.GetDirectoryName(sourceProject))) + foreach (var file in Directory.GetFiles(Path.GetDirectoryName(sourceProject)!)) { if (file != sourceProject) { @@ -154,16 +154,16 @@ internal void Create(TestAsset targetTestAsset, string testProjectsSourceFolder, var projectXml = XDocument.Load(sourceProject); - var ns = projectXml.Root.Name.Namespace; + var ns = projectXml.Root!.Name.Namespace; if (ProjectSdk != null) { - projectXml.Root.Attribute("Sdk").Value = ProjectSdk; + projectXml.Root.Attribute("Sdk")!.Value = ProjectSdk; } - var propertyGroup = projectXml.Root.Elements(ns + "PropertyGroup").First(); + var propertyGroup = projectXml.Root?.Elements(ns + "PropertyGroup").First(); - var packageReferenceItemGroup = projectXml.Root.Elements(ns + "ItemGroup") + var packageReferenceItemGroup = projectXml.Root!.Elements(ns + "ItemGroup") .FirstOrDefault(itemGroup => itemGroup.Elements(ns + "PackageReference").Count() > 0); if (packageReferenceItemGroup == null) { @@ -213,41 +213,41 @@ internal void Create(TestAsset targetTestAsset, string testProjectsSourceFolder, { if (TargetFrameworks.Contains(";")) { - propertyGroup.Add(new XElement(ns + "TargetFrameworks", TargetFrameworks)); + propertyGroup?.Add(new XElement(ns + "TargetFrameworks", TargetFrameworks)); } else { - propertyGroup.Add(new XElement(ns + "TargetFramework", TargetFrameworks)); + propertyGroup?.Add(new XElement(ns + "TargetFramework", TargetFrameworks)); } if (!string.IsNullOrEmpty(RuntimeFrameworkVersion)) { - propertyGroup.Add(new XElement(ns + "RuntimeFrameworkVersion", RuntimeFrameworkVersion)); + propertyGroup?.Add(new XElement(ns + "RuntimeFrameworkVersion", RuntimeFrameworkVersion)); } if (!string.IsNullOrEmpty(RuntimeIdentifier)) { - propertyGroup.Add(new XElement(ns + "RuntimeIdentifier", RuntimeIdentifier)); + propertyGroup?.Add(new XElement(ns + "RuntimeIdentifier", RuntimeIdentifier)); } } else { if (!string.IsNullOrEmpty(TargetFrameworkProfile)) { - propertyGroup.Add(new XElement(ns + "TargetFrameworkProfile", TargetFrameworkProfile)); + propertyGroup?.Add(new XElement(ns + "TargetFrameworkProfile", TargetFrameworkProfile)); // To construct an accurate PCL project file, we must modify the import of the CSharp targets; // building/testing the SDK requires a VSDev command prompt which sets 'VSINSTALLDIR' var importGroup = projectXml.Root.Elements(ns + "Import").Last(); - importGroup.Attribute("Project").Value = "$(VSINSTALLDIR)\\MSBuild\\Microsoft\\Portable\\$(TargetFrameworkVersion)\\Microsoft.Portable.CSharp.targets"; + importGroup.Attribute("Project")!.Value = "$(VSINSTALLDIR)\\MSBuild\\Microsoft\\Portable\\$(TargetFrameworkVersion)\\Microsoft.Portable.CSharp.targets"; } - propertyGroup.Element(ns + "TargetFrameworkVersion").SetValue(TargetFrameworkVersion); + propertyGroup?.Element(ns + "TargetFrameworkVersion")?.SetValue(TargetFrameworkVersion!); } foreach (var additionalProperty in AdditionalProperties) { - propertyGroup.Add(new XElement(ns + additionalProperty.Key, additionalProperty.Value)); + propertyGroup?.Add(new XElement(ns + additionalProperty.Key, additionalProperty.Value)); } if (AdditionalItems.Any()) @@ -269,16 +269,16 @@ internal void Create(TestAsset targetTestAsset, string testProjectsSourceFolder, if (IsExe && !IsWinExe) { - propertyGroup.Element(ns + "OutputType").SetValue("Exe"); + propertyGroup?.Element(ns + "OutputType")?.SetValue("Exe"); } else if (IsWinExe) { - propertyGroup.Element(ns + "OutputType").SetValue("WinExe"); + propertyGroup?.Element(ns + "OutputType")?.SetValue("WinExe"); } if (SelfContained != "") { - propertyGroup.Add(new XElement(ns + "SelfContained", string.Equals(SelfContained, "true", StringComparison.OrdinalIgnoreCase) ? "true" : "false")); + propertyGroup?.Add(new XElement(ns + "SelfContained", string.Equals(SelfContained, "true", StringComparison.OrdinalIgnoreCase) ? "true" : "false")); } if (ReferencedProjects.Any()) @@ -355,10 +355,10 @@ internal void Create(TestAsset targetTestAsset, string testProjectsSourceFolder, if (PropertiesToRecord.Any()) { var customAfterDirectoryBuildTargetsPath = new FileInfo(Path.Combine(targetFolder, "obj", "Custom.After.Directory.Build.targets")); - customAfterDirectoryBuildTargetsPath.Directory.Create(); + customAfterDirectoryBuildTargetsPath.Directory?.Create(); - propertyGroup.Add(new XElement(ns + "CustomAfterDirectoryBuildTargets", $"$(CustomAfterDirectoryBuildTargets);{customAfterDirectoryBuildTargetsPath.FullName}")); - propertyGroup.Add(new XElement(ns + "CustomAfterMicrosoftCommonCrossTargetingTargets", $"$(CustomAfterMicrosoftCommonCrossTargetingTargets);{customAfterDirectoryBuildTargetsPath.FullName}")); + propertyGroup?.Add(new XElement(ns + "CustomAfterDirectoryBuildTargets", $"$(CustomAfterDirectoryBuildTargets);{customAfterDirectoryBuildTargetsPath.FullName}")); + propertyGroup?.Add(new XElement(ns + "CustomAfterMicrosoftCommonCrossTargetingTargets", $"$(CustomAfterMicrosoftCommonCrossTargetingTargets);{customAfterDirectoryBuildTargetsPath.FullName}")); var customAfterDirectoryBuildTargets = new XDocument(new XElement(ns + "Project")); @@ -366,7 +366,7 @@ internal void Create(TestAsset targetTestAsset, string testProjectsSourceFolder, new XAttribute("Name", "WritePropertyValues"), new XAttribute("BeforeTargets", "AfterBuild")); - customAfterDirectoryBuildTargets.Root.Add(target); + customAfterDirectoryBuildTargets.Root?.Add(target); var itemGroup = new XElement(ns + "ItemGroup"); target.Add(itemGroup); @@ -409,7 +409,7 @@ static void Main(string[] args) foreach (var dependency in ReferencedProjects) { - string safeDependencyName = dependency.Name.Replace('.', '_'); + string? safeDependencyName = dependency.Name?.Replace('.', '_'); source += $" Console.WriteLine({safeDependencyName}.{safeDependencyName}Class.Name);" + Environment.NewLine; source += $" Console.WriteLine({safeDependencyName}.{safeDependencyName}Class.List);" + Environment.NewLine; @@ -424,7 +424,7 @@ static void Main(string[] args) } { - string safeThisName = Name.Replace('.', '_'); + string? safeThisName = Name?.Replace('.', '_'); string source = $@"using System; using System.Collections.Generic; @@ -438,7 +438,7 @@ public class {safeThisName}Class "; foreach (var dependency in ReferencedProjects) { - string safeDependencyName = dependency.Name.Replace('.', '_'); + string? safeDependencyName = dependency.Name?.Replace('.', '_'); source += $" public string {safeDependencyName}Name {{ get {{ return {safeDependencyName}.{safeDependencyName}Class.Name; }} }}" + Environment.NewLine; source += $" public List {safeDependencyName}List {{ get {{ return {safeDependencyName}.{safeDependencyName}Class.List; }} }}" + Environment.NewLine; @@ -486,11 +486,11 @@ public void RecordProperties(params string[] propertyNames) /// A dictionary of property keys to property value strings, case sensitive. /// Only properties added to the member will be observed. /// - public Dictionary GetPropertyValues(string testRoot, string targetFramework = null, string configuration = "Debug") + public Dictionary GetPropertyValues(string testRoot, string? targetFramework = null, string configuration = "Debug") { var propertyValues = new Dictionary(); - string intermediateOutputPath = Path.Combine(testRoot, Name, "obj", configuration, targetFramework ?? TargetFrameworks); + string intermediateOutputPath = Path.Combine(testRoot, Name!, "obj", configuration, targetFramework ?? TargetFrameworks); foreach (var line in File.ReadAllLines(Path.Combine(intermediateOutputPath, "PropertyValues.txt"))) { @@ -514,10 +514,10 @@ public static bool ReferenceAssembliesAreInstalled(TargetDotNetFrameworkVersion private OutputPathCalculator GetOutputPathCalculator(string testRoot) { - return OutputPathCalculator.FromProject(Path.Combine(testRoot, Name, Name + ".csproj"), this); + return OutputPathCalculator.FromProject(Path.Combine(testRoot, Name!, Name + ".csproj"), this); } - public string GetOutputDirectory(string testRoot, string targetFramework = null, string configuration = "Debug", string runtimeIdentifier = "") + public string GetOutputDirectory(string testRoot, string? targetFramework = null, string configuration = "Debug", string runtimeIdentifier = "") { return GetOutputPathCalculator(testRoot) .GetOutputDirectory(targetFramework, configuration, runtimeIdentifier); diff --git a/test/Microsoft.NET.TestFramework/SdkTest.cs b/test/Microsoft.NET.TestFramework/SdkTest.cs index 57403ba29a0d..6dca47557f74 100644 --- a/test/Microsoft.NET.TestFramework/SdkTest.cs +++ b/test/Microsoft.NET.TestFramework/SdkTest.cs @@ -7,7 +7,7 @@ public abstract class SdkTest { protected TestAssetsManager _testAssetsManager; - protected bool UsingFullFrameworkMSBuild => TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild; + protected bool? UsingFullFrameworkMSBuild => TestContext.Current?.ToolsetUnderTest?.ShouldUseFullFrameworkMSBuild; protected ITestOutputHelper Log { get; } diff --git a/test/Microsoft.NET.TestFramework/TestAsset.cs b/test/Microsoft.NET.TestFramework/TestAsset.cs index b9f9f935da48..98e5265e2ccd 100644 --- a/test/Microsoft.NET.TestFramework/TestAsset.cs +++ b/test/Microsoft.NET.TestFramework/TestAsset.cs @@ -9,9 +9,9 @@ namespace Microsoft.NET.TestFramework /// public class TestAsset : TestDirectory { - private readonly string _testAssetRoot; + private readonly string? _testAssetRoot; - private List _projectFiles; + private List? _projectFiles; public string TestRoot => Path; @@ -25,15 +25,15 @@ public class TestAsset : TestDirectory public ITestOutputHelper Log { get; } // The TestProject from which this asset was created, if any - public TestProject TestProject { get; set; } + public TestProject? TestProject { get; set; } - internal TestAsset(string testDestination, string sdkVersion, ITestOutputHelper log) : base(testDestination, sdkVersion) + internal TestAsset(string testDestination, string? sdkVersion, ITestOutputHelper log) : base(testDestination, sdkVersion) { Log = log; Name = new DirectoryInfo(testDestination).Name; } - internal TestAsset(string testAssetRoot, string testDestination, string sdkVersion, ITestOutputHelper log) : base(testDestination, sdkVersion) + internal TestAsset(string testAssetRoot, string testDestination, string? sdkVersion, ITestOutputHelper log) : base(testDestination, sdkVersion) { if (string.IsNullOrEmpty(testAssetRoot)) { @@ -64,15 +64,15 @@ public TestAsset WithSource() { _projectFiles = new List(); - var sourceDirs = Directory.GetDirectories(_testAssetRoot, "*", SearchOption.AllDirectories) + var sourceDirs = Directory.GetDirectories(_testAssetRoot!, "*", SearchOption.AllDirectories) .Where(dir => !IsBinOrObjFolder(dir)); foreach (string sourceDir in sourceDirs) { - Directory.CreateDirectory(sourceDir.Replace(_testAssetRoot, Path)); + Directory.CreateDirectory(sourceDir.Replace(_testAssetRoot!, Path)); } - var sourceFiles = Directory.GetFiles(_testAssetRoot, "*.*", SearchOption.AllDirectories) + var sourceFiles = Directory.GetFiles(_testAssetRoot!, "*.*", SearchOption.AllDirectories) .Where(file => { return !IsInBinOrObjFolder(file); @@ -80,7 +80,7 @@ public TestAsset WithSource() foreach (string srcFile in sourceFiles) { - string destFile = srcFile.Replace(_testAssetRoot, Path); + string destFile = srcFile.Replace(_testAssetRoot!, Path); if (System.IO.Path.GetFileName(srcFile).EndsWith("proj") || System.IO.Path.GetFileName(srcFile).EndsWith("xml")) { @@ -115,10 +115,10 @@ public TestAsset UpdateProjProperty(string propertyName, string variableName, st return WithProjectChanges( p => { - var ns = p.Root.Name.Namespace; + var ns = p.Root!.Name.Namespace; var getNode = p.Root.Elements(ns + "PropertyGroup").Elements(ns + propertyName).FirstOrDefault(); getNode ??= p.Root.Elements(ns + "PropertyGroup").Elements(ns + $"{propertyName}s").FirstOrDefault(); - getNode?.SetValue(getNode?.Value.Replace(variableName, targetValue)); + getNode?.SetValue(getNode?.Value.Replace(variableName, targetValue)!); }); } @@ -128,21 +128,21 @@ public TestAsset ReplacePackageVersionVariable(string targetName, string targetV return WithProjectChanges(project => { - var ns = project.Root.Name.Namespace; + var ns = project.Root!.Name.Namespace; foreach (var PropertyName in PropertyNames) { var packageReferencesToUpdate = project.Root.Descendants(ns + PropertyName) - .Where(p => p.Attribute("Version") != null && p.Attribute("Version").Value.Equals($"$({targetName})", StringComparison.OrdinalIgnoreCase)); + .Where(p => p.Attribute("Version") != null && p.Attribute("Version")!.Value.Equals($"$({targetName})", StringComparison.OrdinalIgnoreCase)); foreach (var packageReference in packageReferencesToUpdate) { - packageReference.Attribute("Version").Value = targetValue; + packageReference.Attribute("Version")!.Value = targetValue; } } }); } - public TestAsset WithTargetFramework(string targetFramework, string projectName = null) + public TestAsset WithTargetFramework(string targetFramework, string? projectName = null) { if (targetFramework == null) { @@ -151,13 +151,13 @@ public TestAsset WithTargetFramework(string targetFramework, string projectName return WithProjectChanges( p => { - var ns = p.Root.Name.Namespace; + var ns = p.Root!.Name.Namespace; p.Root.Elements(ns + "PropertyGroup").Elements(ns + "TargetFramework").Single().SetValue(targetFramework); }, projectName); } - public TestAsset WithTargetFrameworks(string targetFrameworks, string projectName = null) + public TestAsset WithTargetFrameworks(string targetFrameworks, string? projectName = null) { if (targetFrameworks == null) { @@ -166,7 +166,7 @@ public TestAsset WithTargetFrameworks(string targetFrameworks, string projectNam return WithProjectChanges( p => { - var ns = p.Root.Name.Namespace; + var ns = p.Root!.Name.Namespace; var propertyGroup = p.Root.Elements(ns + "PropertyGroup").First(); propertyGroup.Elements(ns + "TargetFramework").SingleOrDefault()?.Remove(); propertyGroup.Elements(ns + "TargetFrameworks").SingleOrDefault()?.Remove(); @@ -175,7 +175,7 @@ public TestAsset WithTargetFrameworks(string targetFrameworks, string projectNam projectName); } - public TestAsset WithTargetFrameworkOrFrameworks(string targetFrameworkOrFrameworks, bool multitarget, string projectName = null) + public TestAsset WithTargetFrameworkOrFrameworks(string targetFrameworkOrFrameworks, bool multitarget, string? projectName = null) { if (multitarget) { @@ -187,19 +187,19 @@ public TestAsset WithTargetFrameworkOrFrameworks(string targetFrameworkOrFramewo } } - private TestAsset WithProjectChanges(Action actionOnProject, string projectName = null) + private TestAsset WithProjectChanges(Action actionOnProject, string? projectName = null) { return WithProjectChanges((path, project) => { if (!string.IsNullOrEmpty(projectName)) { - if (!projectName.Equals(System.IO.Path.GetFileNameWithoutExtension(path), StringComparison.OrdinalIgnoreCase)) + if (projectName != null && !projectName.Equals(System.IO.Path.GetFileNameWithoutExtension(path), StringComparison.OrdinalIgnoreCase)) { return; } } - var ns = project.Root.Name.Namespace; + var ns = project.Root!.Name.Namespace; actionOnProject(project); }); } @@ -215,7 +215,7 @@ public TestAsset WithProjectChanges(Action xmlAction) { FindProjectFiles(); } - foreach (var projectFile in _projectFiles) + foreach (var projectFile in _projectFiles!) { var project = XDocument.Load(projectFile); @@ -227,7 +227,7 @@ public TestAsset WithProjectChanges(Action xmlAction) } } return this; - + } public RestoreCommand GetRestoreCommand(ITestOutputHelper log, string relativePath = "") diff --git a/test/Microsoft.NET.TestFramework/TestAssetsManager.cs b/test/Microsoft.NET.TestFramework/TestAssetsManager.cs index 17a4fa411dab..e62ebe9295c1 100644 --- a/test/Microsoft.NET.TestFramework/TestAssetsManager.cs +++ b/test/Microsoft.NET.TestFramework/TestAssetsManager.cs @@ -8,7 +8,7 @@ namespace Microsoft.NET.TestFramework { public class TestAssetsManager { - public string TestAssetsRoot { get; private set; } + public string TestAssetsRoot { get; private set; } private List TestDestinationDirectories { get; } = new List(); @@ -16,7 +16,7 @@ public class TestAssetsManager public TestAssetsManager(ITestOutputHelper log) { - var testAssetsDirectory = TestContext.Current.TestAssetsDirectory; + var testAssetsDirectory = TestContext.Current?.TestAssetsDirectory!; Log = log; if (!Directory.Exists(testAssetsDirectory)) @@ -30,10 +30,10 @@ public TestAssetsManager(ITestOutputHelper log) public TestAsset CopyTestAsset( string testProjectName, [CallerMemberName] string callingMethod = "", - [CallerFilePath] string callerFilePath = null, - string identifier = "", + [CallerFilePath] string? callerFilePath = null, + string? identifier = "", string testAssetSubdirectory = "", - string testDestinationDirectory = null, + string? testDestinationDirectory = null, bool allowCopyIfPresent = false) { var testProjectDirectory = GetAndValidateTestProjectDirectory(testProjectName, testAssetSubdirectory); @@ -42,7 +42,7 @@ public TestAsset CopyTestAsset( testDestinationDirectory ??= GetTestDestinationDirectoryPath(testProjectName, callingMethod + "_" + fileName, identifier, allowCopyIfPresent); TestDestinationDirectories.Add(testDestinationDirectory); - var testAsset = new TestAsset(testProjectDirectory, testDestinationDirectory, TestContext.Current.SdkVersion, Log); + var testAsset = new TestAsset(testProjectDirectory, testDestinationDirectory, TestContext.Current?.SdkVersion, Log); return testAsset; } @@ -125,7 +125,7 @@ private TestAsset CreateTestProjectsInDirectory( string testDestinationDirectory, string targetExtension = ".csproj") { - var testAsset = new TestAsset(testDestinationDirectory, TestContext.Current.SdkVersion, Log); + var testAsset = new TestAsset(testDestinationDirectory, TestContext.Current?.SdkVersion, Log); Stack projectStack = new(testProjects); HashSet createdProjects = new(); @@ -148,10 +148,10 @@ private TestAsset CreateTestProjectsInDirectory( return testAsset; } - public TestDirectory CreateTestDirectory([CallerMemberName] string testName = null, string identifier = null) + public TestDirectory CreateTestDirectory([CallerMemberName] string? testName = null, string? identifier = null) { string dir = GetTestDestinationDirectoryPath(testName, testName, identifier ?? string.Empty); - return new TestDirectory(dir, TestContext.Current.SdkVersion); + return new TestDirectory(dir, TestContext.Current?.SdkVersion); } public string GetAndValidateTestProjectDirectory(string testProjectName, string testAssetSubdirectory = "") @@ -171,12 +171,12 @@ public string GetAndValidateTestProjectDirectory(string testProjectName, string } public static string GetTestDestinationDirectoryPath( - string testProjectName, - string callingMethodAndFileName, - string identifier, + string? testProjectName, + string? callingMethodAndFileName, + string? identifier, bool allowCopyIfPresent = false) { - string baseDirectory = TestContext.Current.TestExecutionDirectory; + string? baseDirectory = TestContext.Current?.TestExecutionDirectory; var directoryName = new StringBuilder(callingMethodAndFileName).Append(identifier); if (testProjectName != callingMethodAndFileName) @@ -199,7 +199,7 @@ public static string GetTestDestinationDirectoryPath( .AppendFormat("{0:X2}", hash[3]); } - var directoryPath = Path.Combine(baseDirectory, directoryName.ToString()); + var directoryPath = Path.Combine(baseDirectory!, directoryName.ToString()); #if CI_BUILD if (!allowCopyIfPresent && Directory.Exists(directoryPath)) { diff --git a/test/Microsoft.NET.TestFramework/TestCommandLine.cs b/test/Microsoft.NET.TestFramework/TestCommandLine.cs index 2519a063e819..38bd22202a91 100644 --- a/test/Microsoft.NET.TestFramework/TestCommandLine.cs +++ b/test/Microsoft.NET.TestFramework/TestCommandLine.cs @@ -7,27 +7,27 @@ namespace Microsoft.NET.TestFramework { public class TestCommandLine { - public List RemainingArgs { get; private set; } + public List? RemainingArgs { get; private set; } public bool UseFullFrameworkMSBuild { get; private set; } - public string FullFrameworkMSBuildPath { get; private set; } + public string? FullFrameworkMSBuildPath { get; private set; } - public string DotnetHostPath { get; private set; } + public string? DotnetHostPath { get; private set; } - public string SDKRepoPath { get; private set; } + public string? SDKRepoPath { get; private set; } - public string SDKRepoConfiguration { get; private set; } + public string? SDKRepoConfiguration { get; private set; } public bool NoRepoInference { get; private set; } public bool ShouldShowHelp { get; private set; } - public string SdkVersion { get; private set; } + public string? SdkVersion { get; private set; } - public string TestExecutionDirectory { get; set; } + public string? TestExecutionDirectory { get; set; } - public string MsbuildAdditionalSdkResolverFolder { get; set; } + public string? MsbuildAdditionalSdkResolverFolder { get; set; } public List<(string name, string value)> EnvironmentVariables { get; set; } = []; @@ -121,7 +121,7 @@ public static TestCommandLine Parse(string[] args) if (string.IsNullOrEmpty(ret.FullFrameworkMSBuildPath)) { // Run tests on full framework MSBuild if environment variable is set pointing to it - string msbuildPath = Environment.GetEnvironmentVariable("DOTNET_SDK_TEST_MSBUILD_PATH"); + string? msbuildPath = Environment.GetEnvironmentVariable("DOTNET_SDK_TEST_MSBUILD_PATH"); if (!string.IsNullOrEmpty(msbuildPath)) { ret.FullFrameworkMSBuildPath = msbuildPath; @@ -151,7 +151,7 @@ public List GetXunitArgsFromTestConfig() foreach (var testConfigFile in TestConfigFiles) { var testConfig = XDocument.Load(testConfigFile); - foreach (var item in testConfig.Root.Elements()) + foreach (var item in testConfig.Root!.Elements()) { if (item.Name.LocalName.Equals("TestList", StringComparison.OrdinalIgnoreCase)) { @@ -173,7 +173,7 @@ public List GetXunitArgsFromTestConfig() } } - foreach (var testList in testLists.Where(g => TestListsToRun.Contains(g.Name))) + foreach (var testList in testLists.Where(g => TestListsToRun.Contains(g.Name!))) { foreach (var testSpec in testList.TestSpecifiers) { @@ -193,7 +193,7 @@ public List GetXunitArgsFromTestConfig() { throw new ArgumentException("Unrecognized test specifier type: " + testSpec.Type); } - ret.Add(testSpec.Specifier); + ret.Add(testSpec.Specifier!); } } @@ -215,7 +215,7 @@ public List GetXunitArgsFromTestConfig() { throw new ArgumentException("Unrecognized test specifier type: " + testSpec.Type); } - ret.Add(testSpec.Specifier); + ret.Add(testSpec.Specifier!); } return ret; @@ -223,7 +223,7 @@ public List GetXunitArgsFromTestConfig() private class TestList { - public string Name { get; set; } + public string? Name { get; set; } public List TestSpecifiers { get; set; } = new List(); @@ -253,7 +253,7 @@ public enum TestSpecifierType } public TestSpecifierType Type { get; set; } - public string Specifier { get; set; } + public string? Specifier { get; set; } public static TestSpecifier Parse(XElement element) { @@ -273,7 +273,7 @@ public static TestSpecifier Parse(XElement element) throw new XmlException("Unrecognized node: " + element.Name); } - spec.Specifier = element.Attribute("Name").Value; + spec.Specifier = element.Attribute("Name")?.Value; return spec; } diff --git a/test/Microsoft.NET.TestFramework/TestContext.cs b/test/Microsoft.NET.TestFramework/TestContext.cs index 1aed029fa612..89f370097455 100644 --- a/test/Microsoft.NET.TestFramework/TestContext.cs +++ b/test/Microsoft.NET.TestFramework/TestContext.cs @@ -9,29 +9,29 @@ namespace Microsoft.NET.TestFramework public class TestContext { // Generally the folder the test DLL is in - public string TestExecutionDirectory { get; set; } + public string? TestExecutionDirectory { get; set; } - public string TestAssetsDirectory { get; set; } + public string? TestAssetsDirectory { get; set; } - public string TestPackages { get; set; } + public string? TestPackages { get; set; } // For tests which want the global packages folder isolated in the repo, but // can share it with other tests - public string TestGlobalPackagesFolder { get; set; } + public string? TestGlobalPackagesFolder { get; set; } - public string NuGetCachePath { get; set; } + public string? NuGetCachePath { get; set; } - public string NuGetFallbackFolder { get; set; } + public string? NuGetFallbackFolder { get; set; } - public string NuGetExePath { get; set; } + public string? NuGetExePath { get; set; } - public string SdkVersion { get; set; } + public string? SdkVersion { get; set; } - public ToolsetInfo ToolsetUnderTest { get; set; } + public ToolsetInfo? ToolsetUnderTest { get; set; } - private static TestContext _current; + private static TestContext? _current; - public static TestContext Current + public static TestContext? Current { get { @@ -53,7 +53,7 @@ public static TestContext Current public static string GetRuntimeGraphFilePath() { - string dotnetRoot = TestContext.Current.ToolsetUnderTest.DotNetRoot; + string dotnetRoot = TestContext.Current?.ToolsetUnderTest?.DotNetRoot!; DirectoryInfo sdksDir = new(Path.Combine(dotnetRoot, "sdk")); @@ -62,7 +62,7 @@ public static string GetRuntimeGraphFilePath() return lastWrittenSdk.GetFiles("RuntimeIdentifierGraph.json").Single().FullName; } - public void AddTestEnvironmentVariables(IDictionary environment) + public void AddTestEnvironmentVariables(IDictionary environment) { environment["DOTNET_MULTILEVEL_LOOKUP"] = "0"; @@ -75,7 +75,7 @@ public void AddTestEnvironmentVariables(IDictionary environment) // Prevent test MSBuild nodes from persisting environment["MSBUILDDISABLENODEREUSE"] = "1"; - ToolsetUnderTest.AddTestEnvironmentVariables(environment); + ToolsetUnderTest?.AddTestEnvironmentVariables(environment); } @@ -118,7 +118,7 @@ public static void Initialize(TestCommandLine commandLine) testContext.TestAssetsDirectory = Environment.GetEnvironmentVariable("DOTNET_SDK_TEST_ASSETS_DIRECTORY"); } - string repoRoot = null; + string? repoRoot = null; #if DEBUG string repoConfiguration = "Debug"; #else @@ -148,14 +148,14 @@ public static void Initialize(TestCommandLine commandLine) } else { - testContext.TestExecutionDirectory = (Path.Combine(FindFolderInTree("artifacts", AppContext.BaseDirectory), "tmp", repoConfiguration)); + testContext.TestExecutionDirectory = (Path.Combine(FindFolderInTree("artifacts", AppContext.BaseDirectory)!, "tmp", repoConfiguration)); testContext.TestAssetsDirectory = FindFolderInTree(Path.Combine("test", "TestAssets"), AppContext.BaseDirectory); } - Directory.CreateDirectory(testContext.TestExecutionDirectory); + Directory.CreateDirectory(testContext.TestExecutionDirectory!); - string artifactsDir = Environment.GetEnvironmentVariable("DOTNET_SDK_ARTIFACTS_DIR"); + string? artifactsDir = Environment.GetEnvironmentVariable("DOTNET_SDK_ARTIFACTS_DIR"); if (string.IsNullOrEmpty(artifactsDir) && !string.IsNullOrEmpty(repoRoot)) { artifactsDir = Path.Combine(repoRoot, "artifacts"); @@ -167,24 +167,24 @@ public static void Initialize(TestCommandLine commandLine) } else { - testContext.TestGlobalPackagesFolder = Path.Combine(testContext.TestExecutionDirectory, ".nuget", "packages"); + testContext.TestGlobalPackagesFolder = Path.Combine(testContext.TestExecutionDirectory!, ".nuget", "packages"); } if (repoRoot != null) { - testContext.NuGetFallbackFolder = Path.Combine(artifactsDir, ".nuget", "NuGetFallbackFolder"); - testContext.NuGetExePath = Path.Combine(artifactsDir, ".nuget", $"nuget{Constants.ExeSuffix}"); - testContext.NuGetCachePath = Path.Combine(artifactsDir, ".nuget", "packages"); + testContext.NuGetFallbackFolder = Path.Combine(artifactsDir!, ".nuget", "NuGetFallbackFolder"); + testContext.NuGetExePath = Path.Combine(artifactsDir!, ".nuget", $"nuget{Constants.ExeSuffix}"); + testContext.NuGetCachePath = Path.Combine(artifactsDir!, ".nuget", "packages"); - testContext.TestPackages = Path.Combine(artifactsDir, "tmp", repoConfiguration, "testpackages"); + testContext.TestPackages = Path.Combine(artifactsDir!, "tmp", repoConfiguration, "testpackages"); } else if (runAsTool) { - testContext.NuGetFallbackFolder = Path.Combine(testContext.TestExecutionDirectory, ".nuget", "NuGetFallbackFolder"); - testContext.NuGetExePath = Path.Combine(testContext.TestExecutionDirectory, ".nuget", $"nuget{Constants.ExeSuffix}"); - testContext.NuGetCachePath = Path.Combine(testContext.TestExecutionDirectory, ".nuget", "packages"); + testContext.NuGetFallbackFolder = Path.Combine(testContext.TestExecutionDirectory!, ".nuget", "NuGetFallbackFolder"); + testContext.NuGetExePath = Path.Combine(testContext.TestExecutionDirectory!, ".nuget", $"nuget{Constants.ExeSuffix}"); + testContext.NuGetCachePath = Path.Combine(testContext.TestExecutionDirectory!, ".nuget", "packages"); - var testPackages = Path.Combine(testContext.TestExecutionDirectory, "Testpackages"); + var testPackages = Path.Combine(testContext.TestExecutionDirectory!, "Testpackages"); if (Directory.Exists(testPackages)) { testContext.TestPackages = testPackages; @@ -193,13 +193,13 @@ public static void Initialize(TestCommandLine commandLine) else { var nugetFolder = FindFolderInTree(".nuget", AppContext.BaseDirectory, false) - ?? Path.Combine(testContext.TestExecutionDirectory, ".nuget"); + ?? Path.Combine(testContext.TestExecutionDirectory!, ".nuget"); testContext.NuGetFallbackFolder = Path.Combine(nugetFolder, "NuGetFallbackFolder"); testContext.NuGetExePath = Path.Combine(nugetFolder, $"nuget{Constants.ExeSuffix}"); testContext.NuGetCachePath = Path.Combine(nugetFolder, "packages"); - var testPackages = Path.Combine(testContext.TestExecutionDirectory, "Testpackages"); + var testPackages = Path.Combine(testContext.TestExecutionDirectory!, "Testpackages"); if (Directory.Exists(testPackages)) { testContext.TestPackages = testPackages; @@ -232,9 +232,9 @@ public static void Initialize(TestCommandLine commandLine) #endif } - public static string GetRepoRoot() + public static string? GetRepoRoot() { - string directory = AppContext.BaseDirectory; + string? directory = AppContext.BaseDirectory; while (directory is not null) { @@ -253,7 +253,7 @@ public static string GetRepoRoot() } private static string FindOrCreateFolderInTree(string relativePath, string startPath) { - string ret = FindFolderInTree(relativePath, startPath, throwIfNotFound: false); + string? ret = FindFolderInTree(relativePath, startPath, throwIfNotFound: false); if (ret != null) { return ret; @@ -262,7 +262,7 @@ private static string FindOrCreateFolderInTree(string relativePath, string start Directory.CreateDirectory(ret); return ret; } - private static string FindFolderInTree(string relativePath, string startPath, bool throwIfNotFound = true) + private static string? FindFolderInTree(string relativePath, string startPath, bool throwIfNotFound = true) { string currentPath = startPath; while (true) @@ -293,7 +293,7 @@ public void WriteGlobalJson(string path) WriteGlobalJson(path, SdkVersion); } - public static void WriteGlobalJson(string path, string sdkVersion) + public static void WriteGlobalJson(string path, string? sdkVersion) { if (!string.IsNullOrEmpty(sdkVersion)) { diff --git a/test/Microsoft.NET.TestFramework/TestDirectory.cs b/test/Microsoft.NET.TestFramework/TestDirectory.cs index 543e415c84eb..4aa88e5c6d28 100644 --- a/test/Microsoft.NET.TestFramework/TestDirectory.cs +++ b/test/Microsoft.NET.TestFramework/TestDirectory.cs @@ -5,7 +5,7 @@ namespace Microsoft.NET.TestFramework { public class TestDirectory { - internal TestDirectory(string path, string sdkVersion) + internal TestDirectory(string path, string? sdkVersion) { if (string.IsNullOrEmpty(path)) { @@ -19,12 +19,12 @@ internal TestDirectory(string path, string sdkVersion) public static TestDirectory Create(string path) { - return new TestDirectory(path, TestContext.Current.SdkVersion); + return new TestDirectory(path, TestContext.Current?.SdkVersion); } public string Path { get; private set; } - private static void EnsureExistsAndEmpty(string path, string sdkVersion) + private static void EnsureExistsAndEmpty(string path, string? sdkVersion) { if (Directory.Exists(path)) { diff --git a/test/Microsoft.NET.TestFramework/TestLoggerFactory.cs b/test/Microsoft.NET.TestFramework/TestLoggerFactory.cs index c324cb14d33d..dee1851be484 100644 --- a/test/Microsoft.NET.TestFramework/TestLoggerFactory.cs +++ b/test/Microsoft.NET.TestFramework/TestLoggerFactory.cs @@ -2,8 +2,6 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. // -#nullable enable - using Microsoft.Extensions.Logging; namespace Microsoft.NET.TestFramework diff --git a/test/Microsoft.NET.TestFramework/TestPackageReference.cs b/test/Microsoft.NET.TestFramework/TestPackageReference.cs index f58a57ba3991..d97e5ec16c5d 100644 --- a/test/Microsoft.NET.TestFramework/TestPackageReference.cs +++ b/test/Microsoft.NET.TestFramework/TestPackageReference.cs @@ -5,7 +5,7 @@ namespace Microsoft.NET.TestFramework { public class TestPackageReference { - public TestPackageReference(string id, string version = null, string nupkgPath = null, string privateAssets = null, string aliases = null, bool updatePackageReference = false, string publish = null) + public TestPackageReference(string id, string? version = null, string? nupkgPath = null, string? privateAssets = null, string? aliases = null, bool updatePackageReference = false, string? publish = null) { ID = id; Version = version; @@ -17,15 +17,15 @@ public TestPackageReference(string id, string version = null, string nupkgPath = } public string ID { get; private set; } - public string Version { get; private set; } - public string NupkgPath { get; private set; } - public string PrivateAssets { get; private set; } - public string Aliases { get; private set; } - public string Publish { get; private set; } + public string? Version { get; private set; } + public string? NupkgPath { get; private set; } + public string? PrivateAssets { get; private set; } + public string? Aliases { get; private set; } + public string? Publish { get; private set; } public bool UpdatePackageReference { get; private set; } public bool NuGetPackageExists() { - return File.Exists(Path.Combine(NupkgPath, string.Concat(ID + "." + Version + ".nupkg"))); + return File.Exists(Path.Combine(NupkgPath!, string.Concat(ID + "." + Version + ".nupkg"))); } } } diff --git a/test/Microsoft.NET.TestFramework/ToolsetInfo.cs b/test/Microsoft.NET.TestFramework/ToolsetInfo.cs index c12b13ac4fb5..02fc97cf6cc3 100644 --- a/test/Microsoft.NET.TestFramework/ToolsetInfo.cs +++ b/test/Microsoft.NET.TestFramework/ToolsetInfo.cs @@ -18,11 +18,11 @@ public class ToolsetInfo public const string LatestMacRuntimeIdentifier = "osx"; public const string LatestRuntimeIdentifiers = $"{LatestWinRuntimeIdentifier}-x64;{LatestWinRuntimeIdentifier}-x86;osx-x64;{LatestMacRuntimeIdentifier}-x64;{LatestLinuxRuntimeIdentifier}-x64;linux-musl-x64"; - public string DotNetRoot { get; } + public string? DotNetRoot { get; } public string DotNetHostPath { get; } - private string _sdkVersion; - public string SdkVersion + private string? _sdkVersion; + public string? SdkVersion { get { @@ -36,8 +36,8 @@ public string SdkVersion } } - private string _msbuildVersion; - public string MSBuildVersion + private string? _msbuildVersion; + public string? MSBuildVersion { get { @@ -58,23 +58,23 @@ public string MSBuildVersion Lazy _sdksPath; public string SdksPath => _sdksPath.Value; - public string CliHomePath { get; set; } + public string? CliHomePath { get; set; } - public string MicrosoftNETBuildExtensionsPathOverride { get; set; } + public string? MicrosoftNETBuildExtensionsPathOverride { get; set; } public bool ShouldUseFullFrameworkMSBuild => !string.IsNullOrEmpty(FullFrameworkMSBuildPath); - public string FullFrameworkMSBuildPath { get; set; } + public string? FullFrameworkMSBuildPath { get; set; } - public string SdkResolverPath { get; set; } + public string? SdkResolverPath { get; set; } - public ToolsetInfo(string dotNetRoot) + public ToolsetInfo(string? dotNetRoot) { DotNetRoot = dotNetRoot; - DotNetHostPath = Path.Combine(dotNetRoot, $"dotnet{Constants.ExeSuffix}"); + DotNetHostPath = Path.Combine(dotNetRoot!, $"dotnet{Constants.ExeSuffix}"); - _sdkFolderUnderTest = new Lazy(() => Path.Combine(DotNetRoot, "sdk", SdkVersion)); + _sdkFolderUnderTest = new Lazy(() => Path.Combine(DotNetRoot!, "sdk", SdkVersion!)); _sdksPath = new Lazy(() => Path.Combine(SdkFolderUnderTest, "Sdks")); } @@ -83,14 +83,14 @@ private void InitSdkVersion() // If using full framework MSBuild, then running a command tries to get the SdkVersion in order to set the // DOTNET_MSBUILD_SDK_RESOLVER_SDKS_DIR environment variable. So turn that off when getting the SDK version // in order to avoid stack overflow - string oldFullFrameworkMSBuildPath = FullFrameworkMSBuildPath; + string? oldFullFrameworkMSBuildPath = FullFrameworkMSBuildPath; try { FullFrameworkMSBuildPath = null; var logger = new StringTestLogger(); var command = new DotnetCommand(logger, "--version") { - WorkingDirectory = TestContext.Current.TestExecutionDirectory + WorkingDirectory = TestContext.Current?.TestExecutionDirectory }; var result = command.Execute(); @@ -113,7 +113,7 @@ private void InitMSBuildVersion() var logger = new StringTestLogger(); var command = new MSBuildVersionCommand(logger) { - WorkingDirectory = TestContext.Current.TestExecutionDirectory + WorkingDirectory = TestContext.Current?.TestExecutionDirectory }; var result = command.Execute(); @@ -126,7 +126,7 @@ private void InitMSBuildVersion() _msbuildVersion = result.StdOut.Split().Last(); } - public string GetMicrosoftNETBuildExtensionsPath() + public string? GetMicrosoftNETBuildExtensionsPath() { if (!string.IsNullOrEmpty(MicrosoftNETBuildExtensionsPathOverride)) { @@ -137,21 +137,21 @@ public string GetMicrosoftNETBuildExtensionsPath() if (ShouldUseFullFrameworkMSBuild) { var msbuildBinPath = Path.GetDirectoryName(FullFrameworkMSBuildPath); - var msbuildRoot = Directory.GetParent(msbuildBinPath).Parent.FullName; - return Path.Combine(msbuildRoot, @"Microsoft\Microsoft.NET.Build.Extensions"); + var msbuildRoot = Directory.GetParent(msbuildBinPath!)?.Parent?.FullName; + return Path.Combine(msbuildRoot!, @"Microsoft\Microsoft.NET.Build.Extensions"); } else { - return Path.Combine(DotNetRoot, "sdk", SdkVersion, @"Microsoft\Microsoft.NET.Build.Extensions"); + return Path.Combine(DotNetRoot!, "sdk", SdkVersion!, @"Microsoft\Microsoft.NET.Build.Extensions"); } } } - public void AddTestEnvironmentVariables(IDictionary environment) + public void AddTestEnvironmentVariables(IDictionary environment) { if (ShouldUseFullFrameworkMSBuild) { - string sdksPath = Path.Combine(DotNetRoot, "sdk", SdkVersion, "Sdks"); + string sdksPath = Path.Combine(DotNetRoot!, "sdk", SdkVersion!, "Sdks"); // Use stage 2 MSBuild SDK resolver environment["MSBUILDADDITIONALSDKRESOLVERSFOLDER"] = SdkResolverPath; @@ -165,7 +165,7 @@ public void AddTestEnvironmentVariables(IDictionary environment) if (!string.IsNullOrEmpty(MicrosoftNETBuildExtensionsPathOverride)) { var microsoftNETBuildExtensionsPath = GetMicrosoftNETBuildExtensionsPath(); - environment["MicrosoftNETBuildExtensionsTargets"] = Path.Combine(microsoftNETBuildExtensionsPath, "Microsoft.NET.Build.Extensions.targets"); + environment["MicrosoftNETBuildExtensionsTargets"] = Path.Combine(microsoftNETBuildExtensionsPath!, "Microsoft.NET.Build.Extensions.targets"); if (UsingFullMSBuildWithoutExtensionsTargets()) { @@ -218,7 +218,7 @@ private SdkCommandSpec CreateCommand(params string[] args) ret.Arguments = args.ToList(); // Don't propagate DOTNET_HOST_PATH to the msbuild process, to match behavior // when running desktop msbuild outside of the test harness. - ret.Environment["DOTNET_HOST_PATH"] = null; + ret.Environment["DOTNET_HOST_PATH"] = string.Empty; } else { @@ -229,22 +229,22 @@ private SdkCommandSpec CreateCommand(params string[] args) ret.Arguments = newArgs; } - TestContext.Current.AddTestEnvironmentVariables(ret.Environment); + TestContext.Current?.AddTestEnvironmentVariables(ret.Environment!); return ret; } - private static string GetDotnetHostPath(string dotnetRoot) - => Path.Combine(dotnetRoot, "dotnet" + Constants.ExeSuffix); + private static string GetDotnetHostPath(string? dotnetRoot) + => Path.Combine(dotnetRoot!, "dotnet" + Constants.ExeSuffix); - public static ToolsetInfo Create(string repoRoot, string repoArtifactsDir, string configuration, TestCommandLine commandLine) + public static ToolsetInfo Create(string? repoRoot, string? repoArtifactsDir, string configuration, TestCommandLine commandLine) { repoRoot = commandLine.SDKRepoPath ?? repoRoot; configuration = commandLine.SDKRepoConfiguration ?? configuration; - string dotnetInstallDirFromEnvironment = Environment.GetEnvironmentVariable("DOTNET_INSTALL_DIR"); + string? dotnetInstallDirFromEnvironment = Environment.GetEnvironmentVariable("DOTNET_INSTALL_DIR"); - string dotnetRoot; + string? dotnetRoot; string hostNotFoundReason; if (!string.IsNullOrEmpty(commandLine.DotnetHostPath)) @@ -254,7 +254,7 @@ public static ToolsetInfo Create(string repoRoot, string repoArtifactsDir, strin } else if (repoRoot != null) { - dotnetRoot = Path.Combine(repoArtifactsDir, "bin", "redist", configuration, "dotnet"); + dotnetRoot = Path.Combine(repoArtifactsDir!, "bin", "redist", configuration, "dotnet"); hostNotFoundReason = "Is 'redist.csproj' built?"; } else if (!string.IsNullOrEmpty(dotnetInstallDirFromEnvironment)) @@ -264,7 +264,7 @@ public static ToolsetInfo Create(string repoRoot, string repoArtifactsDir, strin } else { - if (TryResolveCommand("dotnet", out string pathToDotnet)) + if (TryResolveCommand("dotnet", out string? pathToDotnet)) { dotnetRoot = Path.GetDirectoryName(pathToDotnet); } @@ -289,7 +289,7 @@ public static ToolsetInfo Create(string repoRoot, string repoArtifactsDir, strin } else if (commandLine.UseFullFrameworkMSBuild) { - if (TryResolveCommand("MSBuild", out string pathToMSBuild)) + if (TryResolveCommand("MSBuild", out string? pathToMSBuild)) { ret.FullFrameworkMSBuildPath = pathToMSBuild; } @@ -307,7 +307,7 @@ public static ToolsetInfo Create(string repoRoot, string repoArtifactsDir, strin else if (repoRoot != null && ret.ShouldUseFullFrameworkMSBuild) { // Find path to Microsoft.NET.Build.Extensions for full framework - string sdksPath = Path.Combine(repoArtifactsDir, "bin", configuration, "Sdks"); + string sdksPath = Path.Combine(repoArtifactsDir!, "bin", configuration, "Sdks"); var buildExtensionsSdkPath = Path.Combine(sdksPath, "Microsoft.NET.Build.Extensions"); ret.MicrosoftNETBuildExtensionsPathOverride = Path.Combine(buildExtensionsSdkPath, "msbuildExtensions", "Microsoft", "Microsoft.NET.Build.Extensions"); } @@ -317,7 +317,7 @@ public static ToolsetInfo Create(string repoRoot, string repoArtifactsDir, strin if (repoRoot != null) { // Find path to MSBuildSdkResolver for full framework - ret.SdkResolverPath = Path.Combine(repoArtifactsDir, "bin", "Microsoft.DotNet.MSBuildSdkResolver", configuration, "net472", "SdkResolvers"); + ret.SdkResolverPath = Path.Combine(repoArtifactsDir!, "bin", "Microsoft.DotNet.MSBuildSdkResolver", configuration, "net472", "SdkResolvers"); } else if (!string.IsNullOrWhiteSpace(commandLine.MsbuildAdditionalSdkResolverFolder)) { @@ -325,7 +325,7 @@ public static ToolsetInfo Create(string repoRoot, string repoArtifactsDir, strin } else if (Environment.GetEnvironmentVariable("DOTNET_SDK_TEST_MSBUILDSDKRESOLVER_FOLDER") != null) { - ret.SdkResolverPath = Path.Combine(Environment.GetEnvironmentVariable("DOTNET_SDK_TEST_MSBUILDSDKRESOLVER_FOLDER"), configuration, "net472", "SdkResolvers"); + ret.SdkResolverPath = Path.Combine(Environment.GetEnvironmentVariable("DOTNET_SDK_TEST_MSBUILDSDKRESOLVER_FOLDER")!, configuration, "net472", "SdkResolvers"); } else { @@ -335,7 +335,7 @@ public static ToolsetInfo Create(string repoRoot, string repoArtifactsDir, strin if (repoRoot != null) { - ret.CliHomePath = Path.Combine(repoArtifactsDir, "tmp", configuration); + ret.CliHomePath = Path.Combine(repoArtifactsDir!, "tmp", configuration); } return ret; @@ -347,7 +347,7 @@ public static ToolsetInfo Create(string repoRoot, string repoArtifactsDir, strin /// The command to resolve. /// The full path to the command /// when command can be resolved, otherwise. - public static bool TryResolveCommand(string command, out string fullExePath) + public static bool TryResolveCommand(string command, out string? fullExePath) { fullExePath = null; char pathSplitChar; @@ -356,7 +356,7 @@ public static bool TryResolveCommand(string command, out string fullExePath) { pathSplitChar = ';'; extensions = extensions - .Concat(Environment.GetEnvironmentVariable("PATHEXT").Split(pathSplitChar)) + .Concat(Environment.GetEnvironmentVariable("PATHEXT")?.Split(pathSplitChar)!) .ToArray(); } else @@ -364,8 +364,8 @@ public static bool TryResolveCommand(string command, out string fullExePath) pathSplitChar = ':'; } - var paths = Environment.GetEnvironmentVariable("PATH").Split(pathSplitChar); - string result = extensions.SelectMany(ext => paths.Select(p => Path.Combine(p, command + ext))) + var paths = Environment.GetEnvironmentVariable("PATH")?.Split(pathSplitChar); + string? result = extensions.SelectMany(ext => paths?.Select(p => Path.Combine(p, command + ext))!) .FirstOrDefault(File.Exists); if (result == null) @@ -383,8 +383,8 @@ private bool UsingFullMSBuildWithoutExtensionsTargets() { return false; } - string fullMSBuildDirectory = Path.GetDirectoryName(FullFrameworkMSBuildPath); - string extensionsImportAfterPath = Path.Combine(fullMSBuildDirectory, "..", "Microsoft.Common.targets", "ImportAfter", "Microsoft.NET.Build.Extensions.targets"); + string? fullMSBuildDirectory = Path.GetDirectoryName(FullFrameworkMSBuildPath); + string extensionsImportAfterPath = Path.Combine(fullMSBuildDirectory!, "..", "Microsoft.Common.targets", "ImportAfter", "Microsoft.NET.Build.Extensions.targets"); return !File.Exists(extensionsImportAfterPath); } @@ -392,7 +392,7 @@ private bool UsingFullMSBuildWithoutExtensionsTargets() => typeof(ToolsetInfo).Assembly .GetCustomAttributes() .Where(a => a.Key.EndsWith("PackageVersion")) - .Select(a => (a.Key, a.Value)); + .Select(a => (a.Key, a.Value))!; private static readonly Lazy _NewtonsoftJsonPackageVersion = new Lazy(() => GetPackageVersionProperties().Single(p => p.versionPropertyName == "NewtonsoftJsonPackageVersion").version); diff --git a/test/Microsoft.NET.TestFramework/Utilities/BufferedReporter.cs b/test/Microsoft.NET.TestFramework/Utilities/BufferedReporter.cs index 0412faff72a0..c54607a9f03f 100644 --- a/test/Microsoft.NET.TestFramework/Utilities/BufferedReporter.cs +++ b/test/Microsoft.NET.TestFramework/Utilities/BufferedReporter.cs @@ -1,8 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#nullable enable - using Microsoft.DotNet.Cli.Utils; namespace Microsoft.NET.TestFramework.Utilities diff --git a/test/Microsoft.NET.TestFramework/Utilities/FileThumbPrint.cs b/test/Microsoft.NET.TestFramework/Utilities/FileThumbPrint.cs index c4732f95f55a..8f188989dce9 100644 --- a/test/Microsoft.NET.TestFramework/Utilities/FileThumbPrint.cs +++ b/test/Microsoft.NET.TestFramework/Utilities/FileThumbPrint.cs @@ -53,11 +53,11 @@ public static List CreateFolderThumbprint(TestAsset testAsset, s return thumbprintLookup; } - public bool Equals(FileThumbPrint other) + public bool Equals(FileThumbPrint? other) { return - string.Equals(Path, other.Path, StringComparison.Ordinal) && - LastWriteTimeUtc == other.LastWriteTimeUtc && + string.Equals(Path, other?.Path, StringComparison.Ordinal) && + LastWriteTimeUtc == other?.LastWriteTimeUtc && string.Equals(Hash, other.Hash, StringComparison.Ordinal); } diff --git a/test/Microsoft.NET.TestFramework/Utilities/PeReaderUtils.cs b/test/Microsoft.NET.TestFramework/Utilities/PeReaderUtils.cs index 9fa32ece5b05..b5e803d9ebc7 100644 --- a/test/Microsoft.NET.TestFramework/Utilities/PeReaderUtils.cs +++ b/test/Microsoft.NET.TestFramework/Utilities/PeReaderUtils.cs @@ -16,7 +16,7 @@ public static bool IsCrossgened(this PEReader peReader) if (peReader.HasMetadata) { // 4 is the magic numbers that is set in the CLR header's flags when crossgened. - isCrossgened = ((int)peReader.PEHeaders.CorHeader.Flags & CROSSGEN_FLAG) == CROSSGEN_FLAG; + isCrossgened = ((int)peReader.PEHeaders.CorHeader?.Flags! & CROSSGEN_FLAG) == CROSSGEN_FLAG; } return isCrossgened; @@ -48,7 +48,7 @@ public static bool IsCetCompatible(string filePath) } } - public static string GetAssemblyAttributeValue(string assemblyPath, string attributeName) + public static string? GetAssemblyAttributeValue(string assemblyPath, string attributeName) { if (!File.Exists(assemblyPath)) { @@ -142,7 +142,7 @@ private static List GetFixedStringArguments(MetadataReader reader, Custo if (signatureTypeCode == SignatureTypeCode.String) { // Custom attribute constructor must take only strings - arguments.Add(valueReader.ReadSerializedString()); + arguments.Add(valueReader.ReadSerializedString()!); } } diff --git a/test/Microsoft.NET.TestFramework/Utilities/ProjectModification.cs b/test/Microsoft.NET.TestFramework/Utilities/ProjectModification.cs index a16381686348..d71ea0f8f312 100644 --- a/test/Microsoft.NET.TestFramework/Utilities/ProjectModification.cs +++ b/test/Microsoft.NET.TestFramework/Utilities/ProjectModification.cs @@ -7,7 +7,7 @@ public static class ProjectModification { public static void AddDisplayMessageToProject(XDocument project, string beforeTargets) { - XNamespace ns = project.Root.Name.Namespace; + XNamespace ns = project.Root!.Name.Namespace; XElement target = new(ns + "Target", new XAttribute("Name", "DisplayMessages"), new XAttribute("BeforeTargets", beforeTargets)); diff --git a/test/Microsoft.NET.TestFramework/XunitLoggerProvider.cs b/test/Microsoft.NET.TestFramework/XunitLoggerProvider.cs index 3b5823e8f7a5..db5c634269f7 100644 --- a/test/Microsoft.NET.TestFramework/XunitLoggerProvider.cs +++ b/test/Microsoft.NET.TestFramework/XunitLoggerProvider.cs @@ -2,8 +2,6 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. // -#nullable enable - using Microsoft.Extensions.Logging; using ILogger = Microsoft.Extensions.Logging.ILogger; using LogLevel = Microsoft.Extensions.Logging.LogLevel; diff --git a/test/Microsoft.TemplateEngine.Cli.UnitTests/BaseTest.cs b/test/Microsoft.TemplateEngine.Cli.UnitTests/BaseTest.cs index beb78ef98f07..de60f481d2f2 100644 --- a/test/Microsoft.TemplateEngine.Cli.UnitTests/BaseTest.cs +++ b/test/Microsoft.TemplateEngine.Cli.UnitTests/BaseTest.cs @@ -8,7 +8,7 @@ public abstract class BaseTest /// /// Gets a path to the folder with dotnet new test assets. /// - public static string DotnetNewTestAssets { get; } = VerifyExists(Path.Combine(TestContext.Current.TestAssetsDirectory, "TestPackages", "dotnet-new")); + public static string DotnetNewTestAssets { get; } = VerifyExists(Path.Combine(TestContext.Current?.TestAssetsDirectory!, "TestPackages", "dotnet-new")); /// /// Gets a path to the folder with dotnet new test NuGet template packages. @@ -55,7 +55,7 @@ private static string VerifyExists(string folder) private static string GetAndVerifyRepoRoot() { - string repoRoot = Path.GetFullPath(Path.Combine(TestContext.Current.TestAssetsDirectory, "..", "..")); + string repoRoot = Path.GetFullPath(Path.Combine(TestContext.Current?.TestAssetsDirectory!, "..", "..")); if (!Directory.Exists(repoRoot)) { Assert.Fail($"The repo root cannot be evaluated."); diff --git a/test/Microsoft.Win32.Msi.Manual.Tests/Microsoft.Win32.Msi.Manual.Tests.csproj b/test/Microsoft.Win32.Msi.Manual.Tests/Microsoft.Win32.Msi.Manual.Tests.csproj index 3fcce4907598..96730f505a2d 100644 --- a/test/Microsoft.Win32.Msi.Manual.Tests/Microsoft.Win32.Msi.Manual.Tests.csproj +++ b/test/Microsoft.Win32.Msi.Manual.Tests/Microsoft.Win32.Msi.Manual.Tests.csproj @@ -8,6 +8,7 @@ false false true + enable diff --git a/test/Microsoft.Win32.Msi.Manual.Tests/Program.cs b/test/Microsoft.Win32.Msi.Manual.Tests/Program.cs index d475409f56d8..eaf303a54ae8 100644 --- a/test/Microsoft.Win32.Msi.Manual.Tests/Program.cs +++ b/test/Microsoft.Win32.Msi.Manual.Tests/Program.cs @@ -17,7 +17,7 @@ public class Program int ProgressBarTop; bool ForwardProgress; bool ActionDataEnabled; - string CurrentAction; + string? CurrentAction; bool ProgressDone; int ActionDataStep; @@ -104,7 +104,7 @@ void ClearLine() Console.SetCursorPosition(0, top); } - void OnActionData(object sender, ActionDataEventArgs e) + void OnActionData(object? sender, ActionDataEventArgs e) { if (ActionDataEnabled) { @@ -115,7 +115,7 @@ void OnActionData(object sender, ActionDataEventArgs e) e.Result = DialogResult.IDOK; } - void OnActionStart(object send, ActionStartEventArgs e) + void OnActionStart(object? send, ActionStartEventArgs e) { if (ActionDataEnabled) { @@ -134,7 +134,7 @@ void OnActionStart(object send, ActionStartEventArgs e) e.Result = DialogResult.IDOK; } - void OnProgress(object send, ProgressEventArgs e) + void OnProgress(object? send, ProgressEventArgs e) { e.Result = DialogResult.IDOK; diff --git a/test/dotnet-new.Tests/BaseIntegrationTest.cs b/test/dotnet-new.Tests/BaseIntegrationTest.cs index c8103db5b29b..21c67c59059c 100644 --- a/test/dotnet-new.Tests/BaseIntegrationTest.cs +++ b/test/dotnet-new.Tests/BaseIntegrationTest.cs @@ -14,7 +14,7 @@ public BaseIntegrationTest(ITestOutputHelper log) : base(log) /// /// Gets a path to the folder with dotnet new test assets. /// - public static string DotnetNewTestAssets { get; } = VerifyExists(Path.Combine(TestContext.Current.TestAssetsDirectory, "TestPackages", "dotnet-new")); + public static string DotnetNewTestAssets { get; } = VerifyExists(Path.Combine(TestContext.Current?.TestAssetsDirectory!, "TestPackages", "dotnet-new")); /// /// Gets a path to the folder with dotnet new test NuGet template packages. @@ -178,7 +178,7 @@ private static string VerifyFileExists(string file) private static string GetAndVerifyRepoRoot() { - string repoRoot = Path.GetFullPath(Path.Combine(TestContext.Current.TestAssetsDirectory, "..", "..")); + string repoRoot = Path.GetFullPath(Path.Combine(TestContext.Current?.TestAssetsDirectory!, "..", "..")); if (!Directory.Exists(repoRoot)) { Assert.Fail($"The repo root cannot be evaluated."); diff --git a/test/dotnet-new.Tests/CommonTemplatesTests.cs b/test/dotnet-new.Tests/CommonTemplatesTests.cs index 2b1941764e5c..37105c27eac6 100644 --- a/test/dotnet-new.Tests/CommonTemplatesTests.cs +++ b/test/dotnet-new.Tests/CommonTemplatesTests.cs @@ -48,8 +48,8 @@ public CommonTemplatesTests(SharedHomeDirectory fixture, ITestOutputHelper log) [InlineData("MSBuild Directory.Build.targets file", "buildtargets", new[] { "--inherit" })] public async Task AllCommonItemsCreate(string expectedTemplateName, string templateShortName, string[]? args) { - Dictionary environmentUnderTest = new() { ["DOTNET_NOLOGO"] = false.ToString() }; - TestContext.Current.AddTestEnvironmentVariables(environmentUnderTest); + Dictionary environmentUnderTest = new() { ["DOTNET_NOLOGO"] = false.ToString() }; + TestContext.Current?.AddTestEnvironmentVariables(environmentUnderTest); string itemName = expectedTemplateName.Replace(' ', '-').Replace('.', '-'); @@ -61,11 +61,11 @@ public async Task AllCommonItemsCreate(string expectedTemplateName, string templ VerifyCommandOutput = true, VerificationExcludePatterns = new[] { "*/stderr.txt", "*\\stderr.txt" }, SettingsDirectory = _fixture.HomeDirectory, - DotnetExecutablePath = TestContext.Current.ToolsetUnderTest.DotNetHostPath, + DotnetExecutablePath = TestContext.Current?.ToolsetUnderTest?.DotNetHostPath, DoNotPrependTemplateNameToScenarioName = true, UniqueFor = expectedTemplateName.Equals("NuGet Config") ? UniqueForOption.OsPlatform : null, } - .WithCustomEnvironment(environmentUnderTest) + .WithCustomEnvironment(environmentUnderTest!) .WithCustomScrubbers( ScrubbersDefinition.Empty .AddScrubber(sb => sb.UnixifyNewlines(), "out") @@ -193,7 +193,7 @@ public async Task AotVariants(string name, string language) string finalProjectName = Path.Combine(projectDir, $"{projName}.{extension}"); Dictionary environmentUnderTest = new() { ["DOTNET_NOLOGO"] = false.ToString() }; - TestContext.Current.AddTestEnvironmentVariables(environmentUnderTest); + TestContext.Current?.AddTestEnvironmentVariables(environmentUnderTest!); TemplateVerifierOptions options = new TemplateVerifierOptions(templateName: name) { @@ -206,7 +206,7 @@ public async Task AotVariants(string name, string language) DoNotAppendTemplateArgsToScenarioName = true, ScenarioName = language.Replace('#', 's').ToLower(), VerificationExcludePatterns = new[] { "*/stderr.txt", "*\\stderr.txt" }, - DotnetExecutablePath = TestContext.Current.ToolsetUnderTest.DotNetHostPath, + DotnetExecutablePath = TestContext.Current?.ToolsetUnderTest?.DotNetHostPath, } .WithCustomEnvironment(environmentUnderTest) .WithCustomScrubbers( @@ -388,7 +388,7 @@ public async Task FeaturesSupport( string finalProjectName = Path.Combine(projectDir, $"{projName}.{extension}"); Dictionary environmentUnderTest = new() { ["DOTNET_NOLOGO"] = false.ToString() }; - TestContext.Current.AddTestEnvironmentVariables(environmentUnderTest); + TestContext.Current?.AddTestEnvironmentVariables(environmentUnderTest!); TemplateVerifierOptions options = new TemplateVerifierOptions(templateName: name) { @@ -405,7 +405,7 @@ public async Task FeaturesSupport( + '#' + (language == null ? "cs" : language.Replace('#', 's').ToLower()) + (langVersion == null ? "#NoLangVer" : (langVersionUnsupported ? "#UnsuportedLangVer" : null)), VerificationExcludePatterns = new[] { "*/stderr.txt", "*\\stderr.txt" }, - DotnetExecutablePath = TestContext.Current.ToolsetUnderTest.DotNetHostPath, + DotnetExecutablePath = TestContext.Current?.ToolsetUnderTest?.DotNetHostPath, } .WithCustomEnvironment(environmentUnderTest) .WithCustomScrubbers( diff --git a/test/dotnet-new.Tests/DotnetClassTemplateTests.cs b/test/dotnet-new.Tests/DotnetClassTemplateTests.cs index 3e2385687284..d2944a3e0d59 100644 --- a/test/dotnet-new.Tests/DotnetClassTemplateTests.cs +++ b/test/dotnet-new.Tests/DotnetClassTemplateTests.cs @@ -47,8 +47,8 @@ public async Task DotnetCSharpClassTemplatesTest( string targetFramework = "") { // prevents logging a welcome message from sdk installation - Dictionary environmentUnderTest = new() { ["DOTNET_NOLOGO"] = false.ToString() }; - TestContext.Current.AddTestEnvironmentVariables(environmentUnderTest); + Dictionary environmentUnderTest = new() { ["DOTNET_NOLOGO"] = false.ToString() }; + TestContext.Current?.AddTestEnvironmentVariables(environmentUnderTest); string folderName = GetFolderName(templateShortName, langVersion, targetFramework); string workingDir = CreateTemporaryFolder($"{nameof(DotnetCSharpClassTemplatesTest)}.{folderName}"); @@ -68,14 +68,14 @@ public async Task DotnetCSharpClassTemplatesTest( "*project.*.*" }, SettingsDirectory = _fixture.HomeDirectory, - DotnetExecutablePath = TestContext.Current.ToolsetUnderTest.DotNetHostPath, + DotnetExecutablePath = TestContext.Current?.ToolsetUnderTest?.DotNetHostPath, DoNotAppendTemplateArgsToScenarioName = true, DoNotPrependTemplateNameToScenarioName = true, ScenarioName = folderName, OutputDirectory = workingDir, EnsureEmptyOutputDirectory = false } - .WithCustomEnvironment(environmentUnderTest) + .WithCustomEnvironment(environmentUnderTest!) .WithCustomScrubbers( ScrubbersDefinition.Empty .AddScrubber((path, content) => @@ -128,7 +128,7 @@ public async Task DotnetVisualBasicClassTemplatesTest( { // prevents logging a welcome message from sdk installation Dictionary environmentUnderTest = new() { ["DOTNET_NOLOGO"] = false.ToString() }; - TestContext.Current.AddTestEnvironmentVariables(environmentUnderTest); + TestContext.Current?.AddTestEnvironmentVariables(environmentUnderTest!); string folderName = GetFolderName(templateShortName, langVersion, targetFramework); string workingDir = CreateTemporaryFolder($"{nameof(DotnetVisualBasicClassTemplatesTest)}.{folderName}"); @@ -148,7 +148,7 @@ public async Task DotnetVisualBasicClassTemplatesTest( "*project.*.*" }, SettingsDirectory = _fixture.HomeDirectory, - DotnetExecutablePath = TestContext.Current.ToolsetUnderTest.DotNetHostPath, + DotnetExecutablePath = TestContext.Current?.ToolsetUnderTest?.DotNetHostPath, DoNotAppendTemplateArgsToScenarioName = true, DoNotPrependTemplateNameToScenarioName = true, ScenarioName = folderName, diff --git a/test/dotnet-new.Tests/TemplateEngineSamplesTest.cs b/test/dotnet-new.Tests/TemplateEngineSamplesTest.cs index 4b9b0710a2b5..aee6340c94c5 100644 --- a/test/dotnet-new.Tests/TemplateEngineSamplesTest.cs +++ b/test/dotnet-new.Tests/TemplateEngineSamplesTest.cs @@ -45,8 +45,8 @@ public async Task TemplateEngineSamplesProjectTest( string caseDescription) { _log.LogInformation($"Template with {caseDescription}"); - Dictionary environmentUnderTest = new() { ["DOTNET_NOLOGO"] = false.ToString() }; - TestContext.Current.AddTestEnvironmentVariables(environmentUnderTest); + Dictionary environmentUnderTest = new() { ["DOTNET_NOLOGO"] = false.ToString() }; + TestContext.Current?.AddTestEnvironmentVariables(environmentUnderTest); FileExtensions.AddTextExtension(".cshtml"); TemplateVerifierOptions options = new TemplateVerifierOptions(templateName: shortName) @@ -56,11 +56,11 @@ public async Task TemplateEngineSamplesProjectTest( SnapshotsDirectory = "Approvals", SettingsDirectory = _sharedHome.HomeDirectory, DoNotAppendTemplateArgsToScenarioName = true, - DotnetExecutablePath = TestContext.Current.ToolsetUnderTest.DotNetHostPath, + DotnetExecutablePath = TestContext.Current?.ToolsetUnderTest?.DotNetHostPath, DoNotPrependCallerMethodNameToScenarioName = true, ScenarioName = $"{folderName.Substring(folderName.IndexOf("-") + 1)}{GetScenarioName(arguments)}" } - .WithCustomEnvironment(environmentUnderTest) + .WithCustomEnvironment(environmentUnderTest!) .WithCustomScrubbers( ScrubbersDefinition.Empty .AddScrubber(sb => sb.Replace(DateTime.Now.ToString("MM/dd/yyyy"), "**/**/****"))); diff --git a/test/dotnet-new.Tests/Utilities.cs b/test/dotnet-new.Tests/Utilities.cs index a02ee235aaa0..c8950f4d2938 100644 --- a/test/dotnet-new.Tests/Utilities.cs +++ b/test/dotnet-new.Tests/Utilities.cs @@ -15,7 +15,7 @@ internal static class Utilities /// internal static string GetTestExecutionTempFolder() { - return Path.Combine(TestContext.Current.TestExecutionDirectory, "dotnet-new.IntegrationTests"); + return Path.Combine(TestContext.Current?.TestExecutionDirectory!, "dotnet-new.IntegrationTests"); } /// diff --git a/test/dotnet-watch.Tests/HotReload/RuntimeProcessLauncherTests.cs b/test/dotnet-watch.Tests/HotReload/RuntimeProcessLauncherTests.cs index 6c6c7a8c0e3f..a66bacd17c8a 100644 --- a/test/dotnet-watch.Tests/HotReload/RuntimeProcessLauncherTests.cs +++ b/test/dotnet-watch.Tests/HotReload/RuntimeProcessLauncherTests.cs @@ -42,7 +42,7 @@ public async Task UpdateAndRudeEdit(TriggerEvent trigger) var program = Program.TryCreate( TestOptions.GetCommandLineOptions(["--verbose", "--non-interactive", "--project", hostProject]), console, - TestOptions.GetEnvironmentOptions(workingDirectory, TestContext.Current.ToolsetUnderTest.DotNetHostPath), + TestOptions.GetEnvironmentOptions(workingDirectory, TestContext.Current?.ToolsetUnderTest?.DotNetHostPath), reporter, out var errorCode); @@ -254,7 +254,7 @@ public async Task UpdateAppliedToNewProcesses(bool sharedOutput) var program = Program.TryCreate( TestOptions.GetCommandLineOptions(["--verbose", "--non-interactive", "--project", hostProject]), console, - TestOptions.GetEnvironmentOptions(workingDirectory, TestContext.Current.ToolsetUnderTest.DotNetHostPath), + TestOptions.GetEnvironmentOptions(workingDirectory, TestContext.Current?.ToolsetUnderTest?.DotNetHostPath), reporter, out var errorCode); @@ -393,7 +393,7 @@ public async Task HostRestart(UpdateLocation updateLocation) var program = Program.TryCreate( TestOptions.GetCommandLineOptions(["--verbose", "--project", hostProject]), console, - TestOptions.GetEnvironmentOptions(workingDirectory, TestContext.Current.ToolsetUnderTest.DotNetHostPath), + TestOptions.GetEnvironmentOptions(workingDirectory, TestContext.Current?.ToolsetUnderTest?.DotNetHostPath), reporter, out var errorCode); diff --git a/test/dotnet.Tests/dotnet-new/SdkInfoProviderTests.cs b/test/dotnet.Tests/dotnet-new/SdkInfoProviderTests.cs index 21ca583c9e66..68e9a0186f4d 100644 --- a/test/dotnet.Tests/dotnet-new/SdkInfoProviderTests.cs +++ b/test/dotnet.Tests/dotnet-new/SdkInfoProviderTests.cs @@ -13,7 +13,7 @@ public class SdkInfoProviderTests [Fact] public async Task GetInstalledVersionsAsync_ShouldContainCurrentVersion() { - string dotnetRootUnderTest = TestContext.Current.ToolsetUnderTest.DotNetRoot; + string? dotnetRootUnderTest = TestContext.Current?.ToolsetUnderTest?.DotNetRoot; string? pathOrig = Environment.GetEnvironmentVariable("PATH"); Environment.SetEnvironmentVariable("PATH", dotnetRootUnderTest + Path.PathSeparator + pathOrig); From 5e233e44bdb2da87700abf0d0264762e32520f3d Mon Sep 17 00:00:00 2001 From: Jason Zhai Date: Wed, 20 Nov 2024 00:18:57 -0800 Subject: [PATCH 2/6] Refactor Current, TestExecutionDirectory, and TestAssetsDirectory for null safety based on PR feedback --- test/Common/Program.cs | 2 +- .../EndToEndTests.cs | 4 +- .../CreateNewImageToolTaskTests.cs | 2 +- .../PackageTests.cs | 4 +- .../ProjectInitializer.cs | 2 +- .../TestSettings.cs | 2 +- .../ToolsetUtils.cs | 4 +- .../WorkloadPackGroupTests.cs | 6 +- .../CoreMSBuildAndWindowsOnlyFactAttribute.cs | 2 +- ...oreMSBuildAndWindowsOnlyTheoryAttribute.cs | 2 +- .../CoreMSBuildOnlyFactAttribute.cs | 2 +- .../CoreMSBuildOnlyTheoryAttribute.cs | 2 +- .../FullMSBuildOnlyFactAttribute.cs | 2 +- .../FullMSBuildOnlyTheoryAttribute.cs | 2 +- .../RequiresMSBuildVersionTheoryAttribute.cs | 6 +- .../Commands/DotnetCommand.cs | 4 +- .../Commands/GetValuesCommand.cs | 2 +- .../Commands/MSBuildCommand.cs | 2 +- .../Commands/MSBuildVersionCommand.cs | 4 +- .../Commands/NuGetExeRestoreCommand.cs | 8 +-- .../Commands/RunExeCommand.cs | 2 +- test/Microsoft.NET.TestFramework/SdkTest.cs | 2 +- .../TestAssetsManager.cs | 10 +-- .../TestContext.cs | 68 ++++++++++++++----- .../TestDirectory.cs | 2 +- .../ToolsetInfo.cs | 6 +- .../BaseTest.cs | 4 +- test/dotnet-new.Tests/BaseIntegrationTest.cs | 4 +- test/dotnet-new.Tests/CommonTemplatesTests.cs | 12 ++-- .../DotnetClassTemplateTests.cs | 8 +-- .../TemplateEngineSamplesTest.cs | 4 +- test/dotnet-new.Tests/Utilities.cs | 2 +- .../HotReload/RuntimeProcessLauncherTests.cs | 6 +- .../dotnet-new/SdkInfoProviderTests.cs | 2 +- 34 files changed, 114 insertions(+), 82 deletions(-) diff --git a/test/Common/Program.cs b/test/Common/Program.cs index 10bdde6027f5..6f4c02966ccd 100644 --- a/test/Common/Program.cs +++ b/test/Common/Program.cs @@ -66,7 +66,7 @@ private static int ShowSdkInfo() { var log = new StringTestLogger(); var command = new DotnetCommand(log, "--info"); - var testDirectory = TestDirectory.Create(Path.Combine(TestContext.Current?.TestExecutionDirectory!, "sdkinfo")); + var testDirectory = TestDirectory.Create(Path.Combine(TestContext.Current.TestExecutionDirectory, "sdkinfo")); command.WorkingDirectory = testDirectory.Path; diff --git a/test/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs b/test/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs index 271553662518..2098a1f550e7 100644 --- a/test/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs +++ b/test/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs @@ -324,7 +324,7 @@ public async Task EndToEnd_NoAPI_ProjectType(string projectType, bool addPackage if (addPackageReference) { - File.Copy(Path.Combine(TestContext.Current?.TestExecutionDirectory!, "NuGet.config"), Path.Combine(newProjectDir.FullName, "NuGet.config")); + File.Copy(Path.Combine(TestContext.Current.TestExecutionDirectory, "NuGet.config"), Path.Combine(newProjectDir.FullName, "NuGet.config")); (string? packagePath, string? packageVersion) = ToolsetUtils.GetContainersPackagePath(); @@ -495,7 +495,7 @@ public void EndToEnd_NoAPI_Console() .Execute() .Should().Pass(); - File.Copy(Path.Combine(TestContext.Current?.TestExecutionDirectory!, "NuGet.config"), Path.Combine(newProjectDir.FullName, "NuGet.config")); + File.Copy(Path.Combine(TestContext.Current.TestExecutionDirectory, "NuGet.config"), Path.Combine(newProjectDir.FullName, "NuGet.config")); (string? packagePath, string? packageVersion) = ToolsetUtils.GetContainersPackagePath(); diff --git a/test/Microsoft.NET.Build.Containers.IntegrationTests/FullFramework/CreateNewImageToolTaskTests.cs b/test/Microsoft.NET.Build.Containers.IntegrationTests/FullFramework/CreateNewImageToolTaskTests.cs index 9537fd9cbb29..cfffd3a8ca47 100644 --- a/test/Microsoft.NET.Build.Containers.IntegrationTests/FullFramework/CreateNewImageToolTaskTests.cs +++ b/test/Microsoft.NET.Build.Containers.IntegrationTests/FullFramework/CreateNewImageToolTaskTests.cs @@ -596,6 +596,6 @@ public void GenerateCommandLineCommands_LabelGeneration() private static string GetPathToContainerize() { - return Path.Combine(TestContext.Current?.TestExecutionDirectory!, "Container", "containerize"); + return Path.Combine(TestContext.Current.TestExecutionDirectory, "Container", "containerize"); } } diff --git a/test/Microsoft.NET.Build.Containers.IntegrationTests/PackageTests.cs b/test/Microsoft.NET.Build.Containers.IntegrationTests/PackageTests.cs index fe2ac53f7a38..90ba98aff3a7 100644 --- a/test/Microsoft.NET.Build.Containers.IntegrationTests/PackageTests.cs +++ b/test/Microsoft.NET.Build.Containers.IntegrationTests/PackageTests.cs @@ -23,7 +23,7 @@ public void SanityTest_ContainerizeDependencies() "..\\..\\Cli\\Microsoft.DotNet.Cli.Utils\\Microsoft.DotNet.Cli.Utils.csproj" }; - string projectFilePath = Path.Combine(TestContext.Current?.TestExecutionDirectory!, "Container", "ProjectFiles", "containerize.csproj"); + string projectFilePath = Path.Combine(TestContext.Current.TestExecutionDirectory, "Container", "ProjectFiles", "containerize.csproj"); XDocument project = XDocument.Load(projectFilePath); XNamespace ns = project.Root?.Name.Namespace ?? throw new InvalidOperationException("Project file is empty"); @@ -52,7 +52,7 @@ public void SanityTest_NET_Build_ContainersDependencies() "..\\..\\Cli\\Microsoft.DotNet.Cli.Utils\\Microsoft.DotNet.Cli.Utils.csproj" }; - string projectFilePath = Path.Combine(TestContext.Current?.TestExecutionDirectory!, "Container", "ProjectFiles", "Microsoft.NET.Build.Containers.csproj"); + string projectFilePath = Path.Combine(TestContext.Current.TestExecutionDirectory, "Container", "ProjectFiles", "Microsoft.NET.Build.Containers.csproj"); XDocument project = XDocument.Load(projectFilePath); XNamespace ns = project.Root?.Name.Namespace ?? throw new InvalidOperationException("Project file is empty"); diff --git a/test/Microsoft.NET.Build.Containers.IntegrationTests/ProjectInitializer.cs b/test/Microsoft.NET.Build.Containers.IntegrationTests/ProjectInitializer.cs index 620265ab896d..adfc8b03920b 100644 --- a/test/Microsoft.NET.Build.Containers.IntegrationTests/ProjectInitializer.cs +++ b/test/Microsoft.NET.Build.Containers.IntegrationTests/ProjectInitializer.cs @@ -14,7 +14,7 @@ public sealed class ProjectInitializer static ProjectInitializer() { - var artifactPackagingDirectory = Path.Combine(TestContext.Current?.TestExecutionDirectory!, "Container", "packaging"); + var artifactPackagingDirectory = Path.Combine(TestContext.Current.TestExecutionDirectory, "Container", "packaging"); var targetsFile = Path.Combine(artifactPackagingDirectory, "Microsoft.NET.Build.Containers.targets"); var propsFile = Path.ChangeExtension(targetsFile, ".props"); _combinedTargetsLocation = CombineFiles(propsFile, targetsFile); diff --git a/test/Microsoft.NET.Build.Containers.IntegrationTests/TestSettings.cs b/test/Microsoft.NET.Build.Containers.IntegrationTests/TestSettings.cs index 1e27c8ec47e6..2c518db5cda5 100644 --- a/test/Microsoft.NET.Build.Containers.IntegrationTests/TestSettings.cs +++ b/test/Microsoft.NET.Build.Containers.IntegrationTests/TestSettings.cs @@ -23,7 +23,7 @@ internal static string? TestArtifactsDirectory { if (_testArtifactsDir == null) { - string tmpDir = Path.Combine(TestContext.Current?.TestExecutionDirectory!, "ContainersTests", DateTime.Now.ToString("yyyyMMddHHmmssfff", CultureInfo.InvariantCulture)); + string tmpDir = Path.Combine(TestContext.Current.TestExecutionDirectory, "ContainersTests", DateTime.Now.ToString("yyyyMMddHHmmssfff", CultureInfo.InvariantCulture)); if (!Directory.Exists(tmpDir)) { Directory.CreateDirectory(tmpDir); diff --git a/test/Microsoft.NET.Build.Containers.IntegrationTests/ToolsetUtils.cs b/test/Microsoft.NET.Build.Containers.IntegrationTests/ToolsetUtils.cs index d078218def58..96d24ca85b3e 100644 --- a/test/Microsoft.NET.Build.Containers.IntegrationTests/ToolsetUtils.cs +++ b/test/Microsoft.NET.Build.Containers.IntegrationTests/ToolsetUtils.cs @@ -22,12 +22,12 @@ internal static string GetRuntimeGraphFilePath() /// internal static (string PackagePath, string PackageVersion) GetContainersPackagePath() { - string packageDir = Path.Combine(TestContext.Current?.TestExecutionDirectory!, "Container", "package"); + string packageDir = Path.Combine(TestContext.Current.TestExecutionDirectory, "Container", "package"); //until the package is stabilized, the package version matches TestContext.Current.ToolsetUnderTest.SdkVersion //after the package is stabilized, the package version doesn't have -prefix (-dev, -ci) anymore //so one of those is expected - string[] expectedPackageVersions = new[] { TestContext.Current?.ToolsetUnderTest?.SdkVersion, TestContext.Current?.ToolsetUnderTest?.SdkVersion?.Split('-')[0] }!; + string[] expectedPackageVersions = new[] { TestContext.Current.ToolsetUnderTest?.SdkVersion, TestContext.Current.ToolsetUnderTest?.SdkVersion?.Split('-')[0] }!; foreach (string expectedVersion in expectedPackageVersions) { diff --git a/test/Microsoft.NET.Sdk.WorkloadManifestReader.Tests/WorkloadPackGroupTests.cs b/test/Microsoft.NET.Sdk.WorkloadManifestReader.Tests/WorkloadPackGroupTests.cs index b0558f4ae577..06682a1323ef 100644 --- a/test/Microsoft.NET.Sdk.WorkloadManifestReader.Tests/WorkloadPackGroupTests.cs +++ b/test/Microsoft.NET.Sdk.WorkloadManifestReader.Tests/WorkloadPackGroupTests.cs @@ -82,7 +82,7 @@ public void TestGetPackGroups() public void TestGetManifestFeatureBands() { var manifestProvider = CreateManifestProvider(); - var workloadResolver = WorkloadResolver.CreateForTests(manifestProvider, TestContext.Current?.ToolsetUnderTest?.DotNetRoot!); + var workloadResolver = WorkloadResolver.CreateForTests(manifestProvider, TestContext.Current.ToolsetUnderTest?.DotNetRoot!); foreach (var manifestInfo in workloadResolver.GetInstalledManifests()) { @@ -93,7 +93,7 @@ public void TestGetManifestFeatureBands() SdkDirectoryWorkloadManifestProvider CreateManifestProvider() { - return new(TestContext.Current?.ToolsetUnderTest?.DotNetRoot!, TestContext.Current?.ToolsetUnderTest?.SdkVersion!, userProfileDir: null, globalJsonPath: null); + return new(TestContext.Current.ToolsetUnderTest?.DotNetRoot!, TestContext.Current.ToolsetUnderTest?.SdkVersion!, userProfileDir: null, globalJsonPath: null); } public IEnumerable GetManifests(SdkDirectoryWorkloadManifestProvider? manifestProvider = null) @@ -149,7 +149,7 @@ List GetPackGroups() var manifestProvider = CreateManifestProvider(); var manifests = GetManifests(manifestProvider); - var workloadResolver = WorkloadResolver.CreateForTests(manifestProvider, TestContext.Current?.ToolsetUnderTest?.DotNetRoot!); + var workloadResolver = WorkloadResolver.CreateForTests(manifestProvider, TestContext.Current.ToolsetUnderTest?.DotNetRoot!); foreach (var manifest in manifests) { diff --git a/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildAndWindowsOnlyFactAttribute.cs b/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildAndWindowsOnlyFactAttribute.cs index 9a5a6009926f..6698c4f1061f 100644 --- a/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildAndWindowsOnlyFactAttribute.cs +++ b/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildAndWindowsOnlyFactAttribute.cs @@ -7,7 +7,7 @@ public class CoreMSBuildAndWindowsOnlyFactAttribute : FactAttribute { public CoreMSBuildAndWindowsOnlyFactAttribute() { - if (TestContext.Current?.ToolsetUnderTest != null && TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild || !RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + if (TestContext.Current.ToolsetUnderTest != null && TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild || !RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { Skip = "This test requires Core MSBuild and Windows to run"; } diff --git a/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildAndWindowsOnlyTheoryAttribute.cs b/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildAndWindowsOnlyTheoryAttribute.cs index d583e21137a6..fdb9cb85008b 100644 --- a/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildAndWindowsOnlyTheoryAttribute.cs +++ b/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildAndWindowsOnlyTheoryAttribute.cs @@ -7,7 +7,7 @@ public class CoreMSBuildAndWindowsOnlyTheoryAttribute : TheoryAttribute { public CoreMSBuildAndWindowsOnlyTheoryAttribute() { - if (TestContext.Current?.ToolsetUnderTest != null && TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild || !RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + if (TestContext.Current.ToolsetUnderTest != null && TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild || !RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { Skip = "This test requires Core MSBuild and Windows to run"; } diff --git a/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildOnlyFactAttribute.cs b/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildOnlyFactAttribute.cs index b2540b5e84bf..bd8e9b2f950e 100644 --- a/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildOnlyFactAttribute.cs +++ b/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildOnlyFactAttribute.cs @@ -7,7 +7,7 @@ public class CoreMSBuildOnlyFactAttribute : FactAttribute { public CoreMSBuildOnlyFactAttribute() { - if (TestContext.Current?.ToolsetUnderTest != null && TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild) + if (TestContext.Current.ToolsetUnderTest != null && TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild) { Skip = "This test requires Core MSBuild to run"; } diff --git a/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildOnlyTheoryAttribute.cs b/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildOnlyTheoryAttribute.cs index 0398d893c79f..3e48a042689f 100644 --- a/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildOnlyTheoryAttribute.cs +++ b/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildOnlyTheoryAttribute.cs @@ -7,7 +7,7 @@ public class CoreMSBuildOnlyTheoryAttribute : TheoryAttribute { public CoreMSBuildOnlyTheoryAttribute() { - if (TestContext.Current?.ToolsetUnderTest != null && TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild) + if (TestContext.Current.ToolsetUnderTest != null && TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild) { Skip = "This test requires Core MSBuild to run"; } diff --git a/test/Microsoft.NET.TestFramework/Attributes/FullMSBuildOnlyFactAttribute.cs b/test/Microsoft.NET.TestFramework/Attributes/FullMSBuildOnlyFactAttribute.cs index ff50120ac14c..aa18a0c083bc 100644 --- a/test/Microsoft.NET.TestFramework/Attributes/FullMSBuildOnlyFactAttribute.cs +++ b/test/Microsoft.NET.TestFramework/Attributes/FullMSBuildOnlyFactAttribute.cs @@ -7,7 +7,7 @@ public class FullMSBuildOnlyFactAttribute : FactAttribute { public FullMSBuildOnlyFactAttribute() { - if (TestContext.Current?.ToolsetUnderTest != null && !TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild) + if (TestContext.Current.ToolsetUnderTest != null && !TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild) { Skip = "This test requires Full MSBuild to run"; } diff --git a/test/Microsoft.NET.TestFramework/Attributes/FullMSBuildOnlyTheoryAttribute.cs b/test/Microsoft.NET.TestFramework/Attributes/FullMSBuildOnlyTheoryAttribute.cs index 2b6d559753e9..06dd65cc0296 100644 --- a/test/Microsoft.NET.TestFramework/Attributes/FullMSBuildOnlyTheoryAttribute.cs +++ b/test/Microsoft.NET.TestFramework/Attributes/FullMSBuildOnlyTheoryAttribute.cs @@ -7,7 +7,7 @@ public class FullMSBuildOnlyTheoryAttribute : TheoryAttribute { public FullMSBuildOnlyTheoryAttribute() { - if (TestContext.Current?.ToolsetUnderTest != null && !TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild) + if (TestContext.Current.ToolsetUnderTest != null && !TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild) { Skip = "This test requires Full MSBuild to run"; } diff --git a/test/Microsoft.NET.TestFramework/Attributes/RequiresMSBuildVersionTheoryAttribute.cs b/test/Microsoft.NET.TestFramework/Attributes/RequiresMSBuildVersionTheoryAttribute.cs index 084b3ac7102d..0443fcae294d 100644 --- a/test/Microsoft.NET.TestFramework/Attributes/RequiresMSBuildVersionTheoryAttribute.cs +++ b/test/Microsoft.NET.TestFramework/Attributes/RequiresMSBuildVersionTheoryAttribute.cs @@ -17,9 +17,9 @@ public RequiresMSBuildVersionTheoryAttribute(string version) public static void CheckForRequiredMSBuildVersion(FactAttribute attribute, string version) { - if (!Version.TryParse(TestContext.Current?.ToolsetUnderTest?.MSBuildVersion, out Version? msbuildVersion)) + if (!Version.TryParse(TestContext.Current.ToolsetUnderTest?.MSBuildVersion, out Version? msbuildVersion)) { - attribute.Skip = $"Failed to determine the version of MSBuild ({TestContext.Current?.ToolsetUnderTest?.MSBuildVersion})."; + attribute.Skip = $"Failed to determine the version of MSBuild ({TestContext.Current.ToolsetUnderTest?.MSBuildVersion})."; return; } if (!Version.TryParse(version, out Version? requiredVersion)) @@ -29,7 +29,7 @@ public static void CheckForRequiredMSBuildVersion(FactAttribute attribute, strin } if (requiredVersion > msbuildVersion) { - attribute.Skip = $"This test requires MSBuild version {version} to run (using {TestContext.Current?.ToolsetUnderTest?.MSBuildVersion})."; + attribute.Skip = $"This test requires MSBuild version {version} to run (using {TestContext.Current.ToolsetUnderTest?.MSBuildVersion})."; } } } diff --git a/test/Microsoft.NET.TestFramework/Commands/DotnetCommand.cs b/test/Microsoft.NET.TestFramework/Commands/DotnetCommand.cs index 114ddbc21ce3..f2cf6774bd86 100644 --- a/test/Microsoft.NET.TestFramework/Commands/DotnetCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/DotnetCommand.cs @@ -14,11 +14,11 @@ protected override SdkCommandSpec CreateCommand(IEnumerable args) { var sdkCommandSpec = new SdkCommandSpec() { - FileName = TestContext.Current?.ToolsetUnderTest?.DotNetHostPath, + FileName = TestContext.Current.ToolsetUnderTest?.DotNetHostPath, Arguments = args.ToList(), WorkingDirectory = WorkingDirectory }; - TestContext.Current?.AddTestEnvironmentVariables(sdkCommandSpec.Environment!); + TestContext.Current.AddTestEnvironmentVariables(sdkCommandSpec.Environment!); return sdkCommandSpec; } } diff --git a/test/Microsoft.NET.TestFramework/Commands/GetValuesCommand.cs b/test/Microsoft.NET.TestFramework/Commands/GetValuesCommand.cs index 209c9fc39a61..28f387197ab7 100644 --- a/test/Microsoft.NET.TestFramework/Commands/GetValuesCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/GetValuesCommand.cs @@ -134,7 +134,7 @@ protected override SdkCommandSpec CreateCommand(IEnumerable args) var outputDirectory = GetValuesOutputDirectory(_targetFramework); outputDirectory.Create(); - return TestContext.Current?.ToolsetUnderTest?.CreateCommandForTarget(TargetName, newArgs)!; + return TestContext.Current.ToolsetUnderTest?.CreateCommandForTarget(TargetName, newArgs)!; } public List GetValues() diff --git a/test/Microsoft.NET.TestFramework/Commands/MSBuildCommand.cs b/test/Microsoft.NET.TestFramework/Commands/MSBuildCommand.cs index bc6ce4eb26bd..0fddbd5b38ff 100644 --- a/test/Microsoft.NET.TestFramework/Commands/MSBuildCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/MSBuildCommand.cs @@ -152,7 +152,7 @@ protected override SdkCommandSpec CreateCommand(IEnumerable args) var newArgs = args.ToList(); newArgs.Insert(0, FullPathProjectFile); - return TestContext.Current?.ToolsetUnderTest?.CreateCommandForTarget(Target, newArgs)!; + return TestContext.Current.ToolsetUnderTest?.CreateCommandForTarget(Target, newArgs)!; } } } diff --git a/test/Microsoft.NET.TestFramework/Commands/MSBuildVersionCommand.cs b/test/Microsoft.NET.TestFramework/Commands/MSBuildVersionCommand.cs index 377d0bc2f4f2..1bbb1fb6919a 100644 --- a/test/Microsoft.NET.TestFramework/Commands/MSBuildVersionCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/MSBuildVersionCommand.cs @@ -9,7 +9,7 @@ public MSBuildVersionCommand(ITestOutputHelper log) : base(log) { } protected override SdkCommandSpec CreateCommand(IEnumerable args) { - if (TestContext.Current?.ToolsetUnderTest != null && TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild) + if (TestContext.Current.ToolsetUnderTest != null && TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild) { return new SdkCommandSpec() { @@ -22,7 +22,7 @@ protected override SdkCommandSpec CreateCommand(IEnumerable args) { return new SdkCommandSpec() { - FileName = TestContext.Current?.ToolsetUnderTest?.DotNetHostPath, + FileName = TestContext.Current.ToolsetUnderTest?.DotNetHostPath, Arguments = { "msbuild", "-version" }, WorkingDirectory = WorkingDirectory }; diff --git a/test/Microsoft.NET.TestFramework/Commands/NuGetExeRestoreCommand.cs b/test/Microsoft.NET.TestFramework/Commands/NuGetExeRestoreCommand.cs index a68fd4056862..86d13ac78b64 100644 --- a/test/Microsoft.NET.TestFramework/Commands/NuGetExeRestoreCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/NuGetExeRestoreCommand.cs @@ -31,16 +31,16 @@ protected override SdkCommandSpec CreateCommand(IEnumerable args) newArgs.Add(FullPathProjectFile); newArgs.Add("-PackagesDirectory"); - newArgs.Add(PackagesDirectory ?? TestContext.Current?.NuGetCachePath ?? string.Empty); + newArgs.Add(PackagesDirectory ?? TestContext.Current.NuGetCachePath ?? string.Empty); newArgs.AddRange(args); - if (string.IsNullOrEmpty(TestContext.Current?.NuGetExePath)) + if (string.IsNullOrEmpty(TestContext.Current.NuGetExePath)) { throw new InvalidOperationException("Path to nuget.exe not set"); } - var nugetExePath = TestContext.Current?.NuGetExePath; + var nugetExePath = TestContext.Current.NuGetExePath; if (!string.IsNullOrEmpty(NuGetExeVersion)) { nugetExePath = Path.Combine(Path.GetDirectoryName(nugetExePath)!, NuGetExeVersion, "nuget.exe"); @@ -72,7 +72,7 @@ protected override SdkCommandSpec CreateCommand(IEnumerable args) Arguments = newArgs }; - TestContext.Current?.AddTestEnvironmentVariables(ret.Environment!); + TestContext.Current.AddTestEnvironmentVariables(ret.Environment!); return ret; } diff --git a/test/Microsoft.NET.TestFramework/Commands/RunExeCommand.cs b/test/Microsoft.NET.TestFramework/Commands/RunExeCommand.cs index 22c89740f650..7e1a6cdf4c2a 100644 --- a/test/Microsoft.NET.TestFramework/Commands/RunExeCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/RunExeCommand.cs @@ -26,7 +26,7 @@ protected override SdkCommandSpec CreateCommand(IEnumerable args) Arguments = args.ToList(), WorkingDirectory = WorkingDirectory, }; - TestContext.Current?.AddTestEnvironmentVariables(sdkCommandSpec.Environment!); + TestContext.Current.AddTestEnvironmentVariables(sdkCommandSpec.Environment!); return sdkCommandSpec; } } diff --git a/test/Microsoft.NET.TestFramework/SdkTest.cs b/test/Microsoft.NET.TestFramework/SdkTest.cs index 6dca47557f74..c508ec7ba4a8 100644 --- a/test/Microsoft.NET.TestFramework/SdkTest.cs +++ b/test/Microsoft.NET.TestFramework/SdkTest.cs @@ -7,7 +7,7 @@ public abstract class SdkTest { protected TestAssetsManager _testAssetsManager; - protected bool? UsingFullFrameworkMSBuild => TestContext.Current?.ToolsetUnderTest?.ShouldUseFullFrameworkMSBuild; + protected bool? UsingFullFrameworkMSBuild => TestContext.Current.ToolsetUnderTest?.ShouldUseFullFrameworkMSBuild; protected ITestOutputHelper Log { get; } diff --git a/test/Microsoft.NET.TestFramework/TestAssetsManager.cs b/test/Microsoft.NET.TestFramework/TestAssetsManager.cs index e62ebe9295c1..45153b5b03df 100644 --- a/test/Microsoft.NET.TestFramework/TestAssetsManager.cs +++ b/test/Microsoft.NET.TestFramework/TestAssetsManager.cs @@ -16,7 +16,7 @@ public class TestAssetsManager public TestAssetsManager(ITestOutputHelper log) { - var testAssetsDirectory = TestContext.Current?.TestAssetsDirectory!; + var testAssetsDirectory = TestContext.Current.TestAssetsDirectory; Log = log; if (!Directory.Exists(testAssetsDirectory)) @@ -42,7 +42,7 @@ public TestAsset CopyTestAsset( testDestinationDirectory ??= GetTestDestinationDirectoryPath(testProjectName, callingMethod + "_" + fileName, identifier, allowCopyIfPresent); TestDestinationDirectories.Add(testDestinationDirectory); - var testAsset = new TestAsset(testProjectDirectory, testDestinationDirectory, TestContext.Current?.SdkVersion, Log); + var testAsset = new TestAsset(testProjectDirectory, testDestinationDirectory, TestContext.Current.SdkVersion, Log); return testAsset; } @@ -125,7 +125,7 @@ private TestAsset CreateTestProjectsInDirectory( string testDestinationDirectory, string targetExtension = ".csproj") { - var testAsset = new TestAsset(testDestinationDirectory, TestContext.Current?.SdkVersion, Log); + var testAsset = new TestAsset(testDestinationDirectory, TestContext.Current.SdkVersion, Log); Stack projectStack = new(testProjects); HashSet createdProjects = new(); @@ -151,7 +151,7 @@ private TestAsset CreateTestProjectsInDirectory( public TestDirectory CreateTestDirectory([CallerMemberName] string? testName = null, string? identifier = null) { string dir = GetTestDestinationDirectoryPath(testName, testName, identifier ?? string.Empty); - return new TestDirectory(dir, TestContext.Current?.SdkVersion); + return new TestDirectory(dir, TestContext.Current.SdkVersion); } public string GetAndValidateTestProjectDirectory(string testProjectName, string testAssetSubdirectory = "") @@ -176,7 +176,7 @@ public static string GetTestDestinationDirectoryPath( string? identifier, bool allowCopyIfPresent = false) { - string? baseDirectory = TestContext.Current?.TestExecutionDirectory; + string? baseDirectory = TestContext.Current.TestExecutionDirectory; var directoryName = new StringBuilder(callingMethodAndFileName).Append(identifier); if (testProjectName != callingMethodAndFileName) diff --git a/test/Microsoft.NET.TestFramework/TestContext.cs b/test/Microsoft.NET.TestFramework/TestContext.cs index 89f370097455..b43c736dd128 100644 --- a/test/Microsoft.NET.TestFramework/TestContext.cs +++ b/test/Microsoft.NET.TestFramework/TestContext.cs @@ -9,9 +9,41 @@ namespace Microsoft.NET.TestFramework public class TestContext { // Generally the folder the test DLL is in - public string? TestExecutionDirectory { get; set; } + private string? _testExecutionDirectory; - public string? TestAssetsDirectory { get; set; } + public string TestExecutionDirectory + { + get + { + if (_testExecutionDirectory == null) + { + throw new InvalidOperationException("TestExecutionDirectory should never be null."); + } + return _testExecutionDirectory; + } + set + { + _testExecutionDirectory = value; + } + } + + private string? _testAssetsDirectory; + + public string TestAssetsDirectory + { + get + { + if (_testAssetsDirectory == null) + { + throw new InvalidOperationException("TestAssetsDirectory should never be null."); + } + return _testAssetsDirectory; + } + set + { + _testAssetsDirectory = value; + } + } public string? TestPackages { get; set; } @@ -31,7 +63,7 @@ public class TestContext private static TestContext? _current; - public static TestContext? Current + public static TestContext Current { get { @@ -41,7 +73,7 @@ public static TestContext? Current // (ie when using test explorer or another runner) Initialize(TestCommandLine.Parse(Array.Empty())); } - return _current; + return _current ?? throw new InvalidOperationException("TestContext.Current should never be null."); } set { @@ -53,7 +85,7 @@ public static TestContext? Current public static string GetRuntimeGraphFilePath() { - string dotnetRoot = TestContext.Current?.ToolsetUnderTest?.DotNetRoot!; + string dotnetRoot = TestContext.Current.ToolsetUnderTest?.DotNetRoot!; DirectoryInfo sdksDir = new(Path.Combine(dotnetRoot, "sdk")); @@ -111,11 +143,11 @@ public static void Initialize(TestCommandLine commandLine) // variable instead of packing the test, and installing it as a global tool. runAsTool = true; - testContext.TestAssetsDirectory = FindFolderInTree(Path.Combine("test", "TestAssets"), AppContext.BaseDirectory); + testContext.TestAssetsDirectory = FindFolderInTree(Path.Combine("test", "TestAssets"), AppContext.BaseDirectory)!; } else if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("DOTNET_SDK_TEST_ASSETS_DIRECTORY"))) { - testContext.TestAssetsDirectory = Environment.GetEnvironmentVariable("DOTNET_SDK_TEST_ASSETS_DIRECTORY"); + testContext.TestAssetsDirectory = Environment.GetEnvironmentVariable("DOTNET_SDK_TEST_ASSETS_DIRECTORY")!; } string? repoRoot = null; @@ -136,11 +168,11 @@ public static void Initialize(TestCommandLine commandLine) if (!string.IsNullOrEmpty(commandLine.TestExecutionDirectory)) { - testContext.TestExecutionDirectory = commandLine.TestExecutionDirectory; + testContext.TestExecutionDirectory = commandLine.TestExecutionDirectory!; } else if (Environment.GetEnvironmentVariable("DOTNET_SDK_TEST_EXECUTION_DIRECTORY") != null) { - testContext.TestExecutionDirectory = Environment.GetEnvironmentVariable("DOTNET_SDK_TEST_EXECUTION_DIRECTORY"); + testContext.TestExecutionDirectory = Environment.GetEnvironmentVariable("DOTNET_SDK_TEST_EXECUTION_DIRECTORY")!; } else if (runAsTool) { @@ -150,10 +182,10 @@ public static void Initialize(TestCommandLine commandLine) { testContext.TestExecutionDirectory = (Path.Combine(FindFolderInTree("artifacts", AppContext.BaseDirectory)!, "tmp", repoConfiguration)); - testContext.TestAssetsDirectory = FindFolderInTree(Path.Combine("test", "TestAssets"), AppContext.BaseDirectory); + testContext.TestAssetsDirectory = FindFolderInTree(Path.Combine("test", "TestAssets"), AppContext.BaseDirectory)!; } - Directory.CreateDirectory(testContext.TestExecutionDirectory!); + Directory.CreateDirectory(testContext.TestExecutionDirectory); string? artifactsDir = Environment.GetEnvironmentVariable("DOTNET_SDK_ARTIFACTS_DIR"); if (string.IsNullOrEmpty(artifactsDir) && !string.IsNullOrEmpty(repoRoot)) @@ -167,7 +199,7 @@ public static void Initialize(TestCommandLine commandLine) } else { - testContext.TestGlobalPackagesFolder = Path.Combine(testContext.TestExecutionDirectory!, ".nuget", "packages"); + testContext.TestGlobalPackagesFolder = Path.Combine(testContext.TestExecutionDirectory, ".nuget", "packages"); } if (repoRoot != null) @@ -180,11 +212,11 @@ public static void Initialize(TestCommandLine commandLine) } else if (runAsTool) { - testContext.NuGetFallbackFolder = Path.Combine(testContext.TestExecutionDirectory!, ".nuget", "NuGetFallbackFolder"); - testContext.NuGetExePath = Path.Combine(testContext.TestExecutionDirectory!, ".nuget", $"nuget{Constants.ExeSuffix}"); - testContext.NuGetCachePath = Path.Combine(testContext.TestExecutionDirectory!, ".nuget", "packages"); + testContext.NuGetFallbackFolder = Path.Combine(testContext.TestExecutionDirectory, ".nuget", "NuGetFallbackFolder"); + testContext.NuGetExePath = Path.Combine(testContext.TestExecutionDirectory, ".nuget", $"nuget{Constants.ExeSuffix}"); + testContext.NuGetCachePath = Path.Combine(testContext.TestExecutionDirectory, ".nuget", "packages"); - var testPackages = Path.Combine(testContext.TestExecutionDirectory!, "Testpackages"); + var testPackages = Path.Combine(testContext.TestExecutionDirectory, "Testpackages"); if (Directory.Exists(testPackages)) { testContext.TestPackages = testPackages; @@ -193,13 +225,13 @@ public static void Initialize(TestCommandLine commandLine) else { var nugetFolder = FindFolderInTree(".nuget", AppContext.BaseDirectory, false) - ?? Path.Combine(testContext.TestExecutionDirectory!, ".nuget"); + ?? Path.Combine(testContext.TestExecutionDirectory, ".nuget"); testContext.NuGetFallbackFolder = Path.Combine(nugetFolder, "NuGetFallbackFolder"); testContext.NuGetExePath = Path.Combine(nugetFolder, $"nuget{Constants.ExeSuffix}"); testContext.NuGetCachePath = Path.Combine(nugetFolder, "packages"); - var testPackages = Path.Combine(testContext.TestExecutionDirectory!, "Testpackages"); + var testPackages = Path.Combine(testContext.TestExecutionDirectory, "Testpackages"); if (Directory.Exists(testPackages)) { testContext.TestPackages = testPackages; diff --git a/test/Microsoft.NET.TestFramework/TestDirectory.cs b/test/Microsoft.NET.TestFramework/TestDirectory.cs index 4aa88e5c6d28..5ec95f511bcb 100644 --- a/test/Microsoft.NET.TestFramework/TestDirectory.cs +++ b/test/Microsoft.NET.TestFramework/TestDirectory.cs @@ -19,7 +19,7 @@ internal TestDirectory(string path, string? sdkVersion) public static TestDirectory Create(string path) { - return new TestDirectory(path, TestContext.Current?.SdkVersion); + return new TestDirectory(path, TestContext.Current.SdkVersion); } public string Path { get; private set; } diff --git a/test/Microsoft.NET.TestFramework/ToolsetInfo.cs b/test/Microsoft.NET.TestFramework/ToolsetInfo.cs index 02fc97cf6cc3..fc442523f3ad 100644 --- a/test/Microsoft.NET.TestFramework/ToolsetInfo.cs +++ b/test/Microsoft.NET.TestFramework/ToolsetInfo.cs @@ -90,7 +90,7 @@ private void InitSdkVersion() var logger = new StringTestLogger(); var command = new DotnetCommand(logger, "--version") { - WorkingDirectory = TestContext.Current?.TestExecutionDirectory + WorkingDirectory = TestContext.Current.TestExecutionDirectory }; var result = command.Execute(); @@ -113,7 +113,7 @@ private void InitMSBuildVersion() var logger = new StringTestLogger(); var command = new MSBuildVersionCommand(logger) { - WorkingDirectory = TestContext.Current?.TestExecutionDirectory + WorkingDirectory = TestContext.Current.TestExecutionDirectory }; var result = command.Execute(); @@ -229,7 +229,7 @@ private SdkCommandSpec CreateCommand(params string[] args) ret.Arguments = newArgs; } - TestContext.Current?.AddTestEnvironmentVariables(ret.Environment!); + TestContext.Current.AddTestEnvironmentVariables(ret.Environment!); return ret; } diff --git a/test/Microsoft.TemplateEngine.Cli.UnitTests/BaseTest.cs b/test/Microsoft.TemplateEngine.Cli.UnitTests/BaseTest.cs index de60f481d2f2..beb78ef98f07 100644 --- a/test/Microsoft.TemplateEngine.Cli.UnitTests/BaseTest.cs +++ b/test/Microsoft.TemplateEngine.Cli.UnitTests/BaseTest.cs @@ -8,7 +8,7 @@ public abstract class BaseTest /// /// Gets a path to the folder with dotnet new test assets. /// - public static string DotnetNewTestAssets { get; } = VerifyExists(Path.Combine(TestContext.Current?.TestAssetsDirectory!, "TestPackages", "dotnet-new")); + public static string DotnetNewTestAssets { get; } = VerifyExists(Path.Combine(TestContext.Current.TestAssetsDirectory, "TestPackages", "dotnet-new")); /// /// Gets a path to the folder with dotnet new test NuGet template packages. @@ -55,7 +55,7 @@ private static string VerifyExists(string folder) private static string GetAndVerifyRepoRoot() { - string repoRoot = Path.GetFullPath(Path.Combine(TestContext.Current?.TestAssetsDirectory!, "..", "..")); + string repoRoot = Path.GetFullPath(Path.Combine(TestContext.Current.TestAssetsDirectory, "..", "..")); if (!Directory.Exists(repoRoot)) { Assert.Fail($"The repo root cannot be evaluated."); diff --git a/test/dotnet-new.Tests/BaseIntegrationTest.cs b/test/dotnet-new.Tests/BaseIntegrationTest.cs index 21c67c59059c..c8103db5b29b 100644 --- a/test/dotnet-new.Tests/BaseIntegrationTest.cs +++ b/test/dotnet-new.Tests/BaseIntegrationTest.cs @@ -14,7 +14,7 @@ public BaseIntegrationTest(ITestOutputHelper log) : base(log) /// /// Gets a path to the folder with dotnet new test assets. /// - public static string DotnetNewTestAssets { get; } = VerifyExists(Path.Combine(TestContext.Current?.TestAssetsDirectory!, "TestPackages", "dotnet-new")); + public static string DotnetNewTestAssets { get; } = VerifyExists(Path.Combine(TestContext.Current.TestAssetsDirectory, "TestPackages", "dotnet-new")); /// /// Gets a path to the folder with dotnet new test NuGet template packages. @@ -178,7 +178,7 @@ private static string VerifyFileExists(string file) private static string GetAndVerifyRepoRoot() { - string repoRoot = Path.GetFullPath(Path.Combine(TestContext.Current?.TestAssetsDirectory!, "..", "..")); + string repoRoot = Path.GetFullPath(Path.Combine(TestContext.Current.TestAssetsDirectory, "..", "..")); if (!Directory.Exists(repoRoot)) { Assert.Fail($"The repo root cannot be evaluated."); diff --git a/test/dotnet-new.Tests/CommonTemplatesTests.cs b/test/dotnet-new.Tests/CommonTemplatesTests.cs index 37105c27eac6..8b8aa8c69a0c 100644 --- a/test/dotnet-new.Tests/CommonTemplatesTests.cs +++ b/test/dotnet-new.Tests/CommonTemplatesTests.cs @@ -49,7 +49,7 @@ public CommonTemplatesTests(SharedHomeDirectory fixture, ITestOutputHelper log) public async Task AllCommonItemsCreate(string expectedTemplateName, string templateShortName, string[]? args) { Dictionary environmentUnderTest = new() { ["DOTNET_NOLOGO"] = false.ToString() }; - TestContext.Current?.AddTestEnvironmentVariables(environmentUnderTest); + TestContext.Current.AddTestEnvironmentVariables(environmentUnderTest); string itemName = expectedTemplateName.Replace(' ', '-').Replace('.', '-'); @@ -61,7 +61,7 @@ public async Task AllCommonItemsCreate(string expectedTemplateName, string templ VerifyCommandOutput = true, VerificationExcludePatterns = new[] { "*/stderr.txt", "*\\stderr.txt" }, SettingsDirectory = _fixture.HomeDirectory, - DotnetExecutablePath = TestContext.Current?.ToolsetUnderTest?.DotNetHostPath, + DotnetExecutablePath = TestContext.Current.ToolsetUnderTest?.DotNetHostPath, DoNotPrependTemplateNameToScenarioName = true, UniqueFor = expectedTemplateName.Equals("NuGet Config") ? UniqueForOption.OsPlatform : null, } @@ -193,7 +193,7 @@ public async Task AotVariants(string name, string language) string finalProjectName = Path.Combine(projectDir, $"{projName}.{extension}"); Dictionary environmentUnderTest = new() { ["DOTNET_NOLOGO"] = false.ToString() }; - TestContext.Current?.AddTestEnvironmentVariables(environmentUnderTest!); + TestContext.Current.AddTestEnvironmentVariables(environmentUnderTest!); TemplateVerifierOptions options = new TemplateVerifierOptions(templateName: name) { @@ -206,7 +206,7 @@ public async Task AotVariants(string name, string language) DoNotAppendTemplateArgsToScenarioName = true, ScenarioName = language.Replace('#', 's').ToLower(), VerificationExcludePatterns = new[] { "*/stderr.txt", "*\\stderr.txt" }, - DotnetExecutablePath = TestContext.Current?.ToolsetUnderTest?.DotNetHostPath, + DotnetExecutablePath = TestContext.Current.ToolsetUnderTest?.DotNetHostPath, } .WithCustomEnvironment(environmentUnderTest) .WithCustomScrubbers( @@ -388,7 +388,7 @@ public async Task FeaturesSupport( string finalProjectName = Path.Combine(projectDir, $"{projName}.{extension}"); Dictionary environmentUnderTest = new() { ["DOTNET_NOLOGO"] = false.ToString() }; - TestContext.Current?.AddTestEnvironmentVariables(environmentUnderTest!); + TestContext.Current.AddTestEnvironmentVariables(environmentUnderTest!); TemplateVerifierOptions options = new TemplateVerifierOptions(templateName: name) { @@ -405,7 +405,7 @@ public async Task FeaturesSupport( + '#' + (language == null ? "cs" : language.Replace('#', 's').ToLower()) + (langVersion == null ? "#NoLangVer" : (langVersionUnsupported ? "#UnsuportedLangVer" : null)), VerificationExcludePatterns = new[] { "*/stderr.txt", "*\\stderr.txt" }, - DotnetExecutablePath = TestContext.Current?.ToolsetUnderTest?.DotNetHostPath, + DotnetExecutablePath = TestContext.Current.ToolsetUnderTest?.DotNetHostPath, } .WithCustomEnvironment(environmentUnderTest) .WithCustomScrubbers( diff --git a/test/dotnet-new.Tests/DotnetClassTemplateTests.cs b/test/dotnet-new.Tests/DotnetClassTemplateTests.cs index d2944a3e0d59..812e7f8720a7 100644 --- a/test/dotnet-new.Tests/DotnetClassTemplateTests.cs +++ b/test/dotnet-new.Tests/DotnetClassTemplateTests.cs @@ -48,7 +48,7 @@ public async Task DotnetCSharpClassTemplatesTest( { // prevents logging a welcome message from sdk installation Dictionary environmentUnderTest = new() { ["DOTNET_NOLOGO"] = false.ToString() }; - TestContext.Current?.AddTestEnvironmentVariables(environmentUnderTest); + TestContext.Current.AddTestEnvironmentVariables(environmentUnderTest); string folderName = GetFolderName(templateShortName, langVersion, targetFramework); string workingDir = CreateTemporaryFolder($"{nameof(DotnetCSharpClassTemplatesTest)}.{folderName}"); @@ -68,7 +68,7 @@ public async Task DotnetCSharpClassTemplatesTest( "*project.*.*" }, SettingsDirectory = _fixture.HomeDirectory, - DotnetExecutablePath = TestContext.Current?.ToolsetUnderTest?.DotNetHostPath, + DotnetExecutablePath = TestContext.Current.ToolsetUnderTest?.DotNetHostPath, DoNotAppendTemplateArgsToScenarioName = true, DoNotPrependTemplateNameToScenarioName = true, ScenarioName = folderName, @@ -128,7 +128,7 @@ public async Task DotnetVisualBasicClassTemplatesTest( { // prevents logging a welcome message from sdk installation Dictionary environmentUnderTest = new() { ["DOTNET_NOLOGO"] = false.ToString() }; - TestContext.Current?.AddTestEnvironmentVariables(environmentUnderTest!); + TestContext.Current.AddTestEnvironmentVariables(environmentUnderTest!); string folderName = GetFolderName(templateShortName, langVersion, targetFramework); string workingDir = CreateTemporaryFolder($"{nameof(DotnetVisualBasicClassTemplatesTest)}.{folderName}"); @@ -148,7 +148,7 @@ public async Task DotnetVisualBasicClassTemplatesTest( "*project.*.*" }, SettingsDirectory = _fixture.HomeDirectory, - DotnetExecutablePath = TestContext.Current?.ToolsetUnderTest?.DotNetHostPath, + DotnetExecutablePath = TestContext.Current.ToolsetUnderTest?.DotNetHostPath, DoNotAppendTemplateArgsToScenarioName = true, DoNotPrependTemplateNameToScenarioName = true, ScenarioName = folderName, diff --git a/test/dotnet-new.Tests/TemplateEngineSamplesTest.cs b/test/dotnet-new.Tests/TemplateEngineSamplesTest.cs index aee6340c94c5..525a3e7ff848 100644 --- a/test/dotnet-new.Tests/TemplateEngineSamplesTest.cs +++ b/test/dotnet-new.Tests/TemplateEngineSamplesTest.cs @@ -46,7 +46,7 @@ public async Task TemplateEngineSamplesProjectTest( { _log.LogInformation($"Template with {caseDescription}"); Dictionary environmentUnderTest = new() { ["DOTNET_NOLOGO"] = false.ToString() }; - TestContext.Current?.AddTestEnvironmentVariables(environmentUnderTest); + TestContext.Current.AddTestEnvironmentVariables(environmentUnderTest); FileExtensions.AddTextExtension(".cshtml"); TemplateVerifierOptions options = new TemplateVerifierOptions(templateName: shortName) @@ -56,7 +56,7 @@ public async Task TemplateEngineSamplesProjectTest( SnapshotsDirectory = "Approvals", SettingsDirectory = _sharedHome.HomeDirectory, DoNotAppendTemplateArgsToScenarioName = true, - DotnetExecutablePath = TestContext.Current?.ToolsetUnderTest?.DotNetHostPath, + DotnetExecutablePath = TestContext.Current.ToolsetUnderTest?.DotNetHostPath, DoNotPrependCallerMethodNameToScenarioName = true, ScenarioName = $"{folderName.Substring(folderName.IndexOf("-") + 1)}{GetScenarioName(arguments)}" } diff --git a/test/dotnet-new.Tests/Utilities.cs b/test/dotnet-new.Tests/Utilities.cs index c8950f4d2938..a02ee235aaa0 100644 --- a/test/dotnet-new.Tests/Utilities.cs +++ b/test/dotnet-new.Tests/Utilities.cs @@ -15,7 +15,7 @@ internal static class Utilities /// internal static string GetTestExecutionTempFolder() { - return Path.Combine(TestContext.Current?.TestExecutionDirectory!, "dotnet-new.IntegrationTests"); + return Path.Combine(TestContext.Current.TestExecutionDirectory, "dotnet-new.IntegrationTests"); } /// diff --git a/test/dotnet-watch.Tests/HotReload/RuntimeProcessLauncherTests.cs b/test/dotnet-watch.Tests/HotReload/RuntimeProcessLauncherTests.cs index a66bacd17c8a..b251ce1a4606 100644 --- a/test/dotnet-watch.Tests/HotReload/RuntimeProcessLauncherTests.cs +++ b/test/dotnet-watch.Tests/HotReload/RuntimeProcessLauncherTests.cs @@ -42,7 +42,7 @@ public async Task UpdateAndRudeEdit(TriggerEvent trigger) var program = Program.TryCreate( TestOptions.GetCommandLineOptions(["--verbose", "--non-interactive", "--project", hostProject]), console, - TestOptions.GetEnvironmentOptions(workingDirectory, TestContext.Current?.ToolsetUnderTest?.DotNetHostPath), + TestOptions.GetEnvironmentOptions(workingDirectory, TestContext.Current.ToolsetUnderTest?.DotNetHostPath), reporter, out var errorCode); @@ -254,7 +254,7 @@ public async Task UpdateAppliedToNewProcesses(bool sharedOutput) var program = Program.TryCreate( TestOptions.GetCommandLineOptions(["--verbose", "--non-interactive", "--project", hostProject]), console, - TestOptions.GetEnvironmentOptions(workingDirectory, TestContext.Current?.ToolsetUnderTest?.DotNetHostPath), + TestOptions.GetEnvironmentOptions(workingDirectory, TestContext.Current.ToolsetUnderTest?.DotNetHostPath), reporter, out var errorCode); @@ -393,7 +393,7 @@ public async Task HostRestart(UpdateLocation updateLocation) var program = Program.TryCreate( TestOptions.GetCommandLineOptions(["--verbose", "--project", hostProject]), console, - TestOptions.GetEnvironmentOptions(workingDirectory, TestContext.Current?.ToolsetUnderTest?.DotNetHostPath), + TestOptions.GetEnvironmentOptions(workingDirectory, TestContext.Current.ToolsetUnderTest?.DotNetHostPath), reporter, out var errorCode); diff --git a/test/dotnet.Tests/dotnet-new/SdkInfoProviderTests.cs b/test/dotnet.Tests/dotnet-new/SdkInfoProviderTests.cs index 68e9a0186f4d..58e495d1c26e 100644 --- a/test/dotnet.Tests/dotnet-new/SdkInfoProviderTests.cs +++ b/test/dotnet.Tests/dotnet-new/SdkInfoProviderTests.cs @@ -13,7 +13,7 @@ public class SdkInfoProviderTests [Fact] public async Task GetInstalledVersionsAsync_ShouldContainCurrentVersion() { - string? dotnetRootUnderTest = TestContext.Current?.ToolsetUnderTest?.DotNetRoot; + string? dotnetRootUnderTest = TestContext.Current.ToolsetUnderTest?.DotNetRoot; string? pathOrig = Environment.GetEnvironmentVariable("PATH"); Environment.SetEnvironmentVariable("PATH", dotnetRootUnderTest + Path.PathSeparator + pathOrig); From 596d00cefbc346fecf08c3fa6a22c7c4040e2a5c Mon Sep 17 00:00:00 2001 From: Jason Zhai Date: Sun, 8 Dec 2024 22:51:14 -0800 Subject: [PATCH 3/6] Refactor code to remove '!' operators --- test/Common/Program.cs | 2 +- test/HelixTasks/CreateLocalHelixTestLayout.cs | 9 ++- ...omCreateXUnitWorkItemsWithTestExclusion.cs | 9 ++- .../TarGzFileCreateFromDirectory.cs | 4 +- .../CreateNewImageTests.cs | 4 +- .../EndToEndTests.cs | 14 ++-- .../PackageTests.cs | 2 +- .../ProjectInitializer.cs | 2 +- .../TestSettings.cs | 2 +- .../ToolsetUtils.cs | 8 +-- .../TransientTestFolder.cs | 2 +- .../WorkloadPackGroupTests.cs | 6 +- .../AspNetSdkTest.cs | 5 +- .../Assertions/CommandResultAssertions.cs | 4 +- .../Assertions/DirectoryInfoAssertions.cs | 2 +- .../CoreMSBuildAndWindowsOnlyFactAttribute.cs | 2 +- ...oreMSBuildAndWindowsOnlyTheoryAttribute.cs | 2 +- .../CoreMSBuildOnlyFactAttribute.cs | 2 +- .../CoreMSBuildOnlyTheoryAttribute.cs | 2 +- .../FullMSBuildOnlyFactAttribute.cs | 2 +- .../FullMSBuildOnlyTheoryAttribute.cs | 2 +- .../RequiresMSBuildVersionTheoryAttribute.cs | 6 +- .../Commands/AddReferenceCommand.cs | 2 +- .../Commands/ComposeStoreCommand.cs | 9 +-- .../Commands/DotnetBuildCommand.cs | 4 +- .../Commands/DotnetCommand.cs | 4 +- .../Commands/DotnetPublishCommand.cs | 2 +- .../Commands/GetValuesCommand.cs | 11 ++- .../Commands/ListPackageCommand.cs | 2 +- .../Commands/MSBuildCommand.cs | 8 +-- .../Commands/MSBuildVersionCommand.cs | 4 +- .../Commands/NuGetExeRestoreCommand.cs | 10 +-- .../Commands/PublishCommand.cs | 4 +- .../Commands/RemoveReferenceCommand.cs | 2 +- .../Commands/RunExeCommand.cs | 2 +- .../ConflictResolutionAssets.cs | 2 +- .../Mock/FileSystemMockBuilder.cs | 26 +++---- .../OutputPathCalculator.cs | 52 ++++++++------ .../ProjectConstruction/TestProject.cs | 46 +++++++----- test/Microsoft.NET.TestFramework/TestAsset.cs | 71 ++++++++++++------- .../TestAssetsManager.cs | 4 +- .../TestCommandLine.cs | 37 +++++----- .../TestContext.cs | 65 ++++++++++++----- .../TestPackageReference.cs | 2 +- .../ToolsetInfo.cs | 70 ++++++++++-------- .../Utilities/PeReaderUtils.cs | 6 +- .../Utilities/ProjectModification.cs | 6 +- test/dotnet-new.Tests/CommonTemplatesTests.cs | 8 +-- .../DotnetClassTemplateTests.cs | 6 +- .../TemplateEngineSamplesTest.cs | 4 +- 50 files changed, 333 insertions(+), 229 deletions(-) diff --git a/test/Common/Program.cs b/test/Common/Program.cs index 6f4c02966ccd..e1cd32e1c617 100644 --- a/test/Common/Program.cs +++ b/test/Common/Program.cs @@ -10,7 +10,7 @@ partial class Program public static int Main(string[] args) { var testCommandLine = TestCommandLine.HandleCommandLine(args); - var newArgs = testCommandLine.RemainingArgs?.ToList()!; + List newArgs = testCommandLine.RemainingArgs?.ToList() ?? new List(); // Help argument needs to be the first one to xunit, so don't insert assembly location in that case if (testCommandLine.ShouldShowHelp) diff --git a/test/HelixTasks/CreateLocalHelixTestLayout.cs b/test/HelixTasks/CreateLocalHelixTestLayout.cs index cb8b158f7b55..ebdc35745f64 100644 --- a/test/HelixTasks/CreateLocalHelixTestLayout.cs +++ b/test/HelixTasks/CreateLocalHelixTestLayout.cs @@ -15,11 +15,16 @@ public sealed class CreateLocalHelixTestLayout : Build.Utilities.Task public override bool Execute() { - foreach (var payload in HelixCorrelationPayload!) + if (HelixCorrelationPayload is null) + { + return false; + }; + + foreach (var payload in HelixCorrelationPayload) { var copyfrom = new DirectoryInfo(payload.GetMetadata("PayloadDirectory")); var relativeDestinationPathOnHelix = payload.GetMetadata("Destination"); - var destination = new DirectoryInfo(Path.Combine(TestOutputDirectory!, relativeDestinationPathOnHelix)); + var destination = new DirectoryInfo(Path.Combine(TestOutputDirectory ?? string.Empty, relativeDestinationPathOnHelix)); if (Directory.Exists(destination.FullName)) { diff --git a/test/HelixTasks/SDKCustomCreateXUnitWorkItemsWithTestExclusion.cs b/test/HelixTasks/SDKCustomCreateXUnitWorkItemsWithTestExclusion.cs index 9472f0b8a638..1a05492abc35 100644 --- a/test/HelixTasks/SDKCustomCreateXUnitWorkItemsWithTestExclusion.cs +++ b/test/HelixTasks/SDKCustomCreateXUnitWorkItemsWithTestExclusion.cs @@ -71,8 +71,13 @@ public override bool Execute() /// private async Task ExecuteAsync() { - XUnitWorkItems = (await Task.WhenAll(XUnitProjects?.Select(PrepareWorkItem)!)) - .SelectMany(i => i!) + if(XUnitProjects is null) + { + return; + } + + XUnitWorkItems = (await Task.WhenAll(XUnitProjects.Select(PrepareWorkItem))) + .SelectMany(i => i ?? new()) .Where(wi => wi != null) .ToArray(); return; diff --git a/test/HelixTasks/TarGzFileCreateFromDirectory.cs b/test/HelixTasks/TarGzFileCreateFromDirectory.cs index da8c7fb7d8b4..7a6d7d5df391 100644 --- a/test/HelixTasks/TarGzFileCreateFromDirectory.cs +++ b/test/HelixTasks/TarGzFileCreateFromDirectory.cs @@ -69,7 +69,7 @@ protected override bool ValidateParameters() retVal = false; } } - if (SourceDirectory != null) + if (SourceDirectory is not null) { SourceDirectory = Path.GetFullPath(SourceDirectory); @@ -115,7 +115,7 @@ protected override string GenerateCommandLineCommands() private string GetSourceSpecification() { - if (IncludeBaseDirectory && SourceDirectory != null) + if (SourceDirectory is not null && IncludeBaseDirectory) { var parentDirectory = Directory.GetParent(SourceDirectory)?.Parent?.FullName; diff --git a/test/Microsoft.NET.Build.Containers.IntegrationTests/CreateNewImageTests.cs b/test/Microsoft.NET.Build.Containers.IntegrationTests/CreateNewImageTests.cs index ec9c460fc748..43b766d65c1a 100644 --- a/test/Microsoft.NET.Build.Containers.IntegrationTests/CreateNewImageTests.cs +++ b/test/Microsoft.NET.Build.Containers.IntegrationTests/CreateNewImageTests.cs @@ -246,7 +246,7 @@ public async System.Threading.Tasks.Task CreateNewImage_RootlessBaseImage() await registry.PushAsync(builtImage, sourceReference, destinationReference, cancellationToken: default).ConfigureAwait(false); // Build an application image on top of the rootless base runtime image. - DirectoryInfo newProjectDir = new(Path.Combine(TestSettings.TestArtifactsDirectory!, nameof(CreateNewImage_RootlessBaseImage))); + DirectoryInfo newProjectDir = new(Path.Combine(TestSettings.TestArtifactsDirectory, nameof(CreateNewImage_RootlessBaseImage))); if (newProjectDir.Exists) { @@ -307,7 +307,7 @@ private static (IBuildEngine buildEngine, List errors) SetupBuildEngine return (buildEngine, errors); } - private static string GetTestDirectoryName([CallerMemberName] string testName = "DefaultTest") => Path.Combine(TestSettings.TestArtifactsDirectory!, testName + "_" + DateTime.Now.ToString("yyyyMMddHHmmss")); + private static string GetTestDirectoryName([CallerMemberName] string testName = "DefaultTest") => Path.Combine(TestSettings.TestArtifactsDirectory, testName + "_" + DateTime.Now.ToString("yyyyMMddHHmmss")); private static string FormatBuildMessages(List messages) => string.Join("\r\n", messages); } diff --git a/test/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs b/test/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs index 2098a1f550e7..340723c0e1c5 100644 --- a/test/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs +++ b/test/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs @@ -153,7 +153,7 @@ public async Task ApiEndToEndWithArchiveWritingAndLoad() BuiltImage builtImage = imageBuilder.Build(); // Write the image to disk - var archiveFile = Path.Combine(TestSettings.TestArtifactsDirectory!, + var archiveFile = Path.Combine(TestSettings.TestArtifactsDirectory, nameof(ApiEndToEndWithArchiveWritingAndLoad), "app.tar.gz"); var sourceReference = new SourceImageReference(registry, DockerRegistryManager.RuntimeBaseImage, DockerRegistryManager.Net9PreviewImageTag); var destinationReference = new DestinationImageReference(new ArchiveFileRegistry(archiveFile), NewImageName(), new[] { "latest", "1.0" }); @@ -178,7 +178,7 @@ public async Task ApiEndToEndWithArchiveWritingAndLoad() private string BuildLocalApp([CallerMemberName] string testName = "TestName", string tfm = ToolsetInfo.CurrentTargetFramework, string rid = "linux-x64") { - string workingDirectory = Path.Combine(TestSettings.TestArtifactsDirectory!, testName); + string workingDirectory = Path.Combine(TestSettings.TestArtifactsDirectory, testName); DirectoryInfo d = new(Path.Combine(workingDirectory, "MinimalTestApp")); if (d.Exists) @@ -213,7 +213,7 @@ private string BuildLocalApp([CallerMemberName] string testName = "TestName", st public async Task EndToEnd_MultiProjectSolution() { ILogger logger = _loggerFactory.CreateLogger(nameof(EndToEnd_MultiProjectSolution)); - DirectoryInfo newSolutionDir = new(Path.Combine(TestSettings.TestArtifactsDirectory!, $"CreateNewImageTest_EndToEnd_MultiProjectSolution")); + DirectoryInfo newSolutionDir = new(Path.Combine(TestSettings.TestArtifactsDirectory, $"CreateNewImageTest_EndToEnd_MultiProjectSolution")); if (newSolutionDir.Exists) { @@ -298,8 +298,8 @@ public async Task EndToEnd_MultiProjectSolution() [InlineData("worker", true)] public async Task EndToEnd_NoAPI_ProjectType(string projectType, bool addPackageReference) { - DirectoryInfo newProjectDir = new(Path.Combine(TestSettings.TestArtifactsDirectory!, $"CreateNewImageTest_{projectType}_{addPackageReference}")); - DirectoryInfo privateNuGetAssets = new(Path.Combine(TestSettings.TestArtifactsDirectory!, "ContainerNuGet")); + DirectoryInfo newProjectDir = new(Path.Combine(TestSettings.TestArtifactsDirectory, $"CreateNewImageTest_{projectType}_{addPackageReference}")); + DirectoryInfo privateNuGetAssets = new(Path.Combine(TestSettings.TestArtifactsDirectory, "ContainerNuGet")); if (newProjectDir.Exists) { @@ -471,8 +471,8 @@ public async Task EndToEnd_NoAPI_ProjectType(string projectType, bool addPackage [DockerAvailableFact(Skip = "https://github.com/dotnet/sdk/issues/42850")] public void EndToEnd_NoAPI_Console() { - DirectoryInfo newProjectDir = new(Path.Combine(TestSettings.TestArtifactsDirectory!, "CreateNewImageTest")); - DirectoryInfo privateNuGetAssets = new(Path.Combine(TestSettings.TestArtifactsDirectory!, "ContainerNuGet")); + DirectoryInfo newProjectDir = new(Path.Combine(TestSettings.TestArtifactsDirectory, "CreateNewImageTest")); + DirectoryInfo privateNuGetAssets = new(Path.Combine(TestSettings.TestArtifactsDirectory, "ContainerNuGet")); if (newProjectDir.Exists) { diff --git a/test/Microsoft.NET.Build.Containers.IntegrationTests/PackageTests.cs b/test/Microsoft.NET.Build.Containers.IntegrationTests/PackageTests.cs index 90ba98aff3a7..3aa57a3a7670 100644 --- a/test/Microsoft.NET.Build.Containers.IntegrationTests/PackageTests.cs +++ b/test/Microsoft.NET.Build.Containers.IntegrationTests/PackageTests.cs @@ -132,7 +132,7 @@ public void PackageContentTest() }; (string? packageFilePath, string? packageVersion) = ToolsetUtils.GetContainersPackagePath(); - using ZipArchive archive = new(File.OpenRead(packageFilePath), ZipArchiveMode.Read, false); + using ZipArchive archive = new(File.OpenRead(packageFilePath ?? string.Empty), ZipArchiveMode.Read, false); IEnumerable actualEntries = archive.Entries .Select(e => e.FullName) diff --git a/test/Microsoft.NET.Build.Containers.IntegrationTests/ProjectInitializer.cs b/test/Microsoft.NET.Build.Containers.IntegrationTests/ProjectInitializer.cs index adfc8b03920b..89f1f16f2b4c 100644 --- a/test/Microsoft.NET.Build.Containers.IntegrationTests/ProjectInitializer.cs +++ b/test/Microsoft.NET.Build.Containers.IntegrationTests/ProjectInitializer.cs @@ -27,7 +27,7 @@ private static string CombineFiles(string propsFile, string targetsFile) var combinedContent = new List(); combinedContent.AddRange(propsContent[..^1]); combinedContent.AddRange(targetsContent[1..]); - var tempTargetLocation = Path.Combine(TestSettings.TestArtifactsDirectory!, "Containers", "Microsoft.NET.Build.Containers.targets"); + var tempTargetLocation = Path.Combine(TestSettings.TestArtifactsDirectory, "Containers", "Microsoft.NET.Build.Containers.targets"); string? directoryName = Path.GetDirectoryName(tempTargetLocation); Assert.NotNull(directoryName); Directory.CreateDirectory(directoryName); diff --git a/test/Microsoft.NET.Build.Containers.IntegrationTests/TestSettings.cs b/test/Microsoft.NET.Build.Containers.IntegrationTests/TestSettings.cs index 2c518db5cda5..7240fff45640 100644 --- a/test/Microsoft.NET.Build.Containers.IntegrationTests/TestSettings.cs +++ b/test/Microsoft.NET.Build.Containers.IntegrationTests/TestSettings.cs @@ -13,7 +13,7 @@ internal static class TestSettings /// /// Gets temporary location for test artifacts. /// - internal static string? TestArtifactsDirectory + internal static string TestArtifactsDirectory { get { diff --git a/test/Microsoft.NET.Build.Containers.IntegrationTests/ToolsetUtils.cs b/test/Microsoft.NET.Build.Containers.IntegrationTests/ToolsetUtils.cs index 96d24ca85b3e..58fda7c074e7 100644 --- a/test/Microsoft.NET.Build.Containers.IntegrationTests/ToolsetUtils.cs +++ b/test/Microsoft.NET.Build.Containers.IntegrationTests/ToolsetUtils.cs @@ -20,18 +20,18 @@ internal static string GetRuntimeGraphFilePath() /// Gets path to built Microsoft.NET.Build.Containers.*.nupkg prepared for tests. /// /// - internal static (string PackagePath, string PackageVersion) GetContainersPackagePath() + internal static (string? PackagePath, string? PackageVersion) GetContainersPackagePath() { string packageDir = Path.Combine(TestContext.Current.TestExecutionDirectory, "Container", "package"); //until the package is stabilized, the package version matches TestContext.Current.ToolsetUnderTest.SdkVersion //after the package is stabilized, the package version doesn't have -prefix (-dev, -ci) anymore //so one of those is expected - string[] expectedPackageVersions = new[] { TestContext.Current.ToolsetUnderTest?.SdkVersion, TestContext.Current.ToolsetUnderTest?.SdkVersion?.Split('-')[0] }!; + string?[] expectedPackageVersions = new[] { TestContext.Current.ToolsetUnderTest?.SdkVersion, TestContext.Current.ToolsetUnderTest?.SdkVersion?.Split('-')[0] }; - foreach (string expectedVersion in expectedPackageVersions) + foreach (string? expectedVersion in expectedPackageVersions) { - string fullFileName = Path.Combine(packageDir, $"Microsoft.NET.Build.Containers.{expectedVersion}.nupkg"); + string? fullFileName = Path.Combine(packageDir, $"Microsoft.NET.Build.Containers.{expectedVersion}.nupkg"); if (File.Exists(fullFileName)) { return (fullFileName, expectedVersion); diff --git a/test/Microsoft.NET.Build.Containers.IntegrationTests/TransientTestFolder.cs b/test/Microsoft.NET.Build.Containers.IntegrationTests/TransientTestFolder.cs index 65e191945dcb..b2deedcf065f 100644 --- a/test/Microsoft.NET.Build.Containers.IntegrationTests/TransientTestFolder.cs +++ b/test/Microsoft.NET.Build.Containers.IntegrationTests/TransientTestFolder.cs @@ -10,7 +10,7 @@ namespace Microsoft.NET.Build.Containers.IntegrationTests; /// internal sealed class TransientTestFolder : IDisposable { - public readonly string Path = Combine(TestSettings.TestArtifactsDirectory!, GetRandomFileName()); + public readonly string Path = Combine(TestSettings.TestArtifactsDirectory, GetRandomFileName()); public readonly DirectoryInfo DirectoryInfo; public TransientTestFolder() diff --git a/test/Microsoft.NET.Sdk.WorkloadManifestReader.Tests/WorkloadPackGroupTests.cs b/test/Microsoft.NET.Sdk.WorkloadManifestReader.Tests/WorkloadPackGroupTests.cs index 06682a1323ef..8dce2302d652 100644 --- a/test/Microsoft.NET.Sdk.WorkloadManifestReader.Tests/WorkloadPackGroupTests.cs +++ b/test/Microsoft.NET.Sdk.WorkloadManifestReader.Tests/WorkloadPackGroupTests.cs @@ -82,7 +82,7 @@ public void TestGetPackGroups() public void TestGetManifestFeatureBands() { var manifestProvider = CreateManifestProvider(); - var workloadResolver = WorkloadResolver.CreateForTests(manifestProvider, TestContext.Current.ToolsetUnderTest?.DotNetRoot!); + var workloadResolver = WorkloadResolver.CreateForTests(manifestProvider, TestContext.Current.ToolsetUnderTest.DotNetRoot); foreach (var manifestInfo in workloadResolver.GetInstalledManifests()) { @@ -93,7 +93,7 @@ public void TestGetManifestFeatureBands() SdkDirectoryWorkloadManifestProvider CreateManifestProvider() { - return new(TestContext.Current.ToolsetUnderTest?.DotNetRoot!, TestContext.Current.ToolsetUnderTest?.SdkVersion!, userProfileDir: null, globalJsonPath: null); + return new(TestContext.Current.ToolsetUnderTest.DotNetRoot, TestContext.Current.ToolsetUnderTest.SdkVersion, userProfileDir: null, globalJsonPath: null); } public IEnumerable GetManifests(SdkDirectoryWorkloadManifestProvider? manifestProvider = null) @@ -149,7 +149,7 @@ List GetPackGroups() var manifestProvider = CreateManifestProvider(); var manifests = GetManifests(manifestProvider); - var workloadResolver = WorkloadResolver.CreateForTests(manifestProvider, TestContext.Current.ToolsetUnderTest?.DotNetRoot!); + var workloadResolver = WorkloadResolver.CreateForTests(manifestProvider, TestContext.Current.ToolsetUnderTest.DotNetRoot); foreach (var manifest in manifests) { diff --git a/test/Microsoft.NET.TestFramework/AspNetSdkTest.cs b/test/Microsoft.NET.TestFramework/AspNetSdkTest.cs index 3ea210276603..41a4819463e1 100644 --- a/test/Microsoft.NET.TestFramework/AspNetSdkTest.cs +++ b/test/Microsoft.NET.TestFramework/AspNetSdkTest.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System; using System.Diagnostics; using System.Reflection; using System.Runtime.CompilerServices; @@ -134,7 +135,7 @@ protected virtual CommandResult ExecuteCommand(TestCommand command, params strin if (_generateMSbuildLogs) { var i = 0; - for (i = 0; File.Exists(Path.Combine(command.WorkingDirectory!, $"msbuild{i}.binlog")) && i < 20; i++) { } + for (i = 0; command.WorkingDirectory is not null && File.Exists(Path.Combine(command.WorkingDirectory, $"msbuild{i}.binlog")) && i < 20; i++) { } var log = $"msbuild{i}.binlog"; return command.Execute([$"/bl:{log}", .. arguments]); @@ -152,7 +153,7 @@ protected virtual CommandResult ExecuteCommandWithoutRestore(MSBuildCommand comm if (_generateMSbuildLogs) { var i = 0; - for (i = 0; File.Exists(Path.Combine(command.WorkingDirectory!, $"msbuild{i}.binlog")) && i < 20; i++) { } + for (i = 0; command.WorkingDirectory is not null && File.Exists(Path.Combine(command.WorkingDirectory, $"msbuild{i}.binlog")) && i < 20; i++) { } var log = $"msbuild{i}.binlog"; return command.ExecuteWithoutRestore([$"/bl:{log}", .. arguments]); } diff --git a/test/Microsoft.NET.TestFramework/Assertions/CommandResultAssertions.cs b/test/Microsoft.NET.TestFramework/Assertions/CommandResultAssertions.cs index 30bdcf8cd9bd..abd700a429af 100644 --- a/test/Microsoft.NET.TestFramework/Assertions/CommandResultAssertions.cs +++ b/test/Microsoft.NET.TestFramework/Assertions/CommandResultAssertions.cs @@ -222,7 +222,7 @@ public AndConstraint NuPkgContainsPatterns(string nupkg foreach (var pattern in filePatterns) { var directory = Path.GetDirectoryName(pattern); - var path = Path.Combine(unzipped, directory!); + var path = Path.Combine(unzipped, directory ?? string.Empty); var searchPattern = Path.GetFileName(pattern); var condition = Directory.GetFiles(path, searchPattern).Length < 1; @@ -240,7 +240,7 @@ public AndConstraint NuPkgDoesNotContainPatterns(string foreach (var pattern in filePatterns) { var directory = Path.GetDirectoryName(pattern); - var path = Path.Combine(unzipped, directory!); + var path = Path.Combine(unzipped, directory ?? string.Empty); var searchPattern = Path.GetFileName(pattern); var condition = Directory.Exists(path) && Directory.GetFiles(path, searchPattern).Length > 0; diff --git a/test/Microsoft.NET.TestFramework/Assertions/DirectoryInfoAssertions.cs b/test/Microsoft.NET.TestFramework/Assertions/DirectoryInfoAssertions.cs index 37f6439a4dae..af759b019d41 100644 --- a/test/Microsoft.NET.TestFramework/Assertions/DirectoryInfoAssertions.cs +++ b/test/Microsoft.NET.TestFramework/Assertions/DirectoryInfoAssertions.cs @@ -99,7 +99,7 @@ public AndConstraint HaveDirectory(string expectedDir) Execute.Assertion.ForCondition(dir != null) .FailWith("Expected directory {0} cannot be found inside directory {1}.", expectedDir, _dirInfo.FullName); - return new AndConstraint(new DirectoryInfoAssertions(dir!)); + return new AndConstraint(new DirectoryInfoAssertions(dir ?? new DirectoryInfo(string.Empty))); } public AndConstraint OnlyHaveFiles(IEnumerable expectedFiles, SearchOption searchOption = SearchOption.AllDirectories) diff --git a/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildAndWindowsOnlyFactAttribute.cs b/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildAndWindowsOnlyFactAttribute.cs index 6698c4f1061f..d7f9a4994525 100644 --- a/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildAndWindowsOnlyFactAttribute.cs +++ b/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildAndWindowsOnlyFactAttribute.cs @@ -7,7 +7,7 @@ public class CoreMSBuildAndWindowsOnlyFactAttribute : FactAttribute { public CoreMSBuildAndWindowsOnlyFactAttribute() { - if (TestContext.Current.ToolsetUnderTest != null && TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild || !RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + if (TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild || !RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { Skip = "This test requires Core MSBuild and Windows to run"; } diff --git a/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildAndWindowsOnlyTheoryAttribute.cs b/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildAndWindowsOnlyTheoryAttribute.cs index fdb9cb85008b..ecf9e48bf808 100644 --- a/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildAndWindowsOnlyTheoryAttribute.cs +++ b/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildAndWindowsOnlyTheoryAttribute.cs @@ -7,7 +7,7 @@ public class CoreMSBuildAndWindowsOnlyTheoryAttribute : TheoryAttribute { public CoreMSBuildAndWindowsOnlyTheoryAttribute() { - if (TestContext.Current.ToolsetUnderTest != null && TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild || !RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + if (TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild || !RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { Skip = "This test requires Core MSBuild and Windows to run"; } diff --git a/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildOnlyFactAttribute.cs b/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildOnlyFactAttribute.cs index bd8e9b2f950e..c09394700ae8 100644 --- a/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildOnlyFactAttribute.cs +++ b/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildOnlyFactAttribute.cs @@ -7,7 +7,7 @@ public class CoreMSBuildOnlyFactAttribute : FactAttribute { public CoreMSBuildOnlyFactAttribute() { - if (TestContext.Current.ToolsetUnderTest != null && TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild) + if (TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild) { Skip = "This test requires Core MSBuild to run"; } diff --git a/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildOnlyTheoryAttribute.cs b/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildOnlyTheoryAttribute.cs index 3e48a042689f..a756d6e95a88 100644 --- a/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildOnlyTheoryAttribute.cs +++ b/test/Microsoft.NET.TestFramework/Attributes/CoreMSBuildOnlyTheoryAttribute.cs @@ -7,7 +7,7 @@ public class CoreMSBuildOnlyTheoryAttribute : TheoryAttribute { public CoreMSBuildOnlyTheoryAttribute() { - if (TestContext.Current.ToolsetUnderTest != null && TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild) + if (TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild) { Skip = "This test requires Core MSBuild to run"; } diff --git a/test/Microsoft.NET.TestFramework/Attributes/FullMSBuildOnlyFactAttribute.cs b/test/Microsoft.NET.TestFramework/Attributes/FullMSBuildOnlyFactAttribute.cs index aa18a0c083bc..b7a4c9961925 100644 --- a/test/Microsoft.NET.TestFramework/Attributes/FullMSBuildOnlyFactAttribute.cs +++ b/test/Microsoft.NET.TestFramework/Attributes/FullMSBuildOnlyFactAttribute.cs @@ -7,7 +7,7 @@ public class FullMSBuildOnlyFactAttribute : FactAttribute { public FullMSBuildOnlyFactAttribute() { - if (TestContext.Current.ToolsetUnderTest != null && !TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild) + if (!TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild) { Skip = "This test requires Full MSBuild to run"; } diff --git a/test/Microsoft.NET.TestFramework/Attributes/FullMSBuildOnlyTheoryAttribute.cs b/test/Microsoft.NET.TestFramework/Attributes/FullMSBuildOnlyTheoryAttribute.cs index 06dd65cc0296..09abd77f8dfd 100644 --- a/test/Microsoft.NET.TestFramework/Attributes/FullMSBuildOnlyTheoryAttribute.cs +++ b/test/Microsoft.NET.TestFramework/Attributes/FullMSBuildOnlyTheoryAttribute.cs @@ -7,7 +7,7 @@ public class FullMSBuildOnlyTheoryAttribute : TheoryAttribute { public FullMSBuildOnlyTheoryAttribute() { - if (TestContext.Current.ToolsetUnderTest != null && !TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild) + if (!TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild) { Skip = "This test requires Full MSBuild to run"; } diff --git a/test/Microsoft.NET.TestFramework/Attributes/RequiresMSBuildVersionTheoryAttribute.cs b/test/Microsoft.NET.TestFramework/Attributes/RequiresMSBuildVersionTheoryAttribute.cs index 0443fcae294d..86bc24729b77 100644 --- a/test/Microsoft.NET.TestFramework/Attributes/RequiresMSBuildVersionTheoryAttribute.cs +++ b/test/Microsoft.NET.TestFramework/Attributes/RequiresMSBuildVersionTheoryAttribute.cs @@ -17,9 +17,9 @@ public RequiresMSBuildVersionTheoryAttribute(string version) public static void CheckForRequiredMSBuildVersion(FactAttribute attribute, string version) { - if (!Version.TryParse(TestContext.Current.ToolsetUnderTest?.MSBuildVersion, out Version? msbuildVersion)) + if (!Version.TryParse(TestContext.Current.ToolsetUnderTest.MSBuildVersion, out Version? msbuildVersion)) { - attribute.Skip = $"Failed to determine the version of MSBuild ({TestContext.Current.ToolsetUnderTest?.MSBuildVersion})."; + attribute.Skip = $"Failed to determine the version of MSBuild ({TestContext.Current.ToolsetUnderTest.MSBuildVersion})."; return; } if (!Version.TryParse(version, out Version? requiredVersion)) @@ -29,7 +29,7 @@ public static void CheckForRequiredMSBuildVersion(FactAttribute attribute, strin } if (requiredVersion > msbuildVersion) { - attribute.Skip = $"This test requires MSBuild version {version} to run (using {TestContext.Current.ToolsetUnderTest?.MSBuildVersion})."; + attribute.Skip = $"This test requires MSBuild version {version} to run (using {TestContext.Current.ToolsetUnderTest.MSBuildVersion})."; } } } diff --git a/test/Microsoft.NET.TestFramework/Commands/AddReferenceCommand.cs b/test/Microsoft.NET.TestFramework/Commands/AddReferenceCommand.cs index e4c8db860714..f362655c3666 100644 --- a/test/Microsoft.NET.TestFramework/Commands/AddReferenceCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/AddReferenceCommand.cs @@ -17,7 +17,7 @@ public override CommandResult Execute(IEnumerable args) { List newArgs = new(); newArgs.Add("add"); - if (_projectName != null && !string.IsNullOrEmpty(_projectName)) + if (_projectName is not null && !string.IsNullOrEmpty(_projectName)) { newArgs.Add(_projectName); } diff --git a/test/Microsoft.NET.TestFramework/Commands/ComposeStoreCommand.cs b/test/Microsoft.NET.TestFramework/Commands/ComposeStoreCommand.cs index c6e16960fb95..bbeaf4b22896 100644 --- a/test/Microsoft.NET.TestFramework/Commands/ComposeStoreCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/ComposeStoreCommand.cs @@ -19,7 +19,7 @@ public ComposeStoreCommand(ITestOutputHelper log, string projectPath, string? re { } - public override DirectoryInfo GetOutputDirectory(string? targetFramework = "netcoreapp1.0", string configuration = "Debug", string? runtimeIdentifier = "", string? platformIdentifier = "") + public override DirectoryInfo GetOutputDirectory(string? targetFramework = "netcoreapp1.0", string? configuration = "Debug", string? runtimeIdentifier = "", string? platformIdentifier = "") { string output = Path.Combine(ProjectRootPath, "bin", BuildRelativeOutputPath(targetFramework, configuration, runtimeIdentifier, platformIdentifier)); return new DirectoryInfo(output); @@ -30,14 +30,15 @@ public string GetPublishedAppPath(string appName) return Path.Combine(GetOutputDirectory().FullName, $"{appName}.dll"); } - private string BuildRelativeOutputPath(string? targetFramework, string configuration, string? runtimeIdentifier, string? platformIdentifier) + private string BuildRelativeOutputPath(string? targetFramework, string? configuration, string? runtimeIdentifier, string? platformIdentifier) { if (runtimeIdentifier?.Length == 0) { runtimeIdentifier = RuntimeInformation.RuntimeIdentifier; } - string? arch = runtimeIdentifier?.Substring(runtimeIdentifier.LastIndexOf("-") + 1); - return Path.Combine(platformIdentifier!, configuration, arch!, targetFramework!, PublishSubfolderName); + + string arch = runtimeIdentifier?.Substring(runtimeIdentifier.LastIndexOf("-") + 1) ?? string.Empty; + return Path.Combine(platformIdentifier ?? string.Empty, configuration ?? string.Empty, arch, targetFramework ?? string.Empty, PublishSubfolderName); } } } diff --git a/test/Microsoft.NET.TestFramework/Commands/DotnetBuildCommand.cs b/test/Microsoft.NET.TestFramework/Commands/DotnetBuildCommand.cs index c3c2fbd6c9da..8c5cce77583d 100644 --- a/test/Microsoft.NET.TestFramework/Commands/DotnetBuildCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/DotnetBuildCommand.cs @@ -13,9 +13,9 @@ public DotnetBuildCommand(ITestOutputHelper log, params string[] args) : base(lo public DotnetBuildCommand(TestAsset testAsset, params string[] args) : this(testAsset.Log, args) { - if (testAsset.TestProject != null) + if (testAsset.TestProject != null && testAsset.TestProject.Name is not null) { - WorkingDirectory = Path.Combine(testAsset.TestRoot, testAsset.TestProject.Name!); + WorkingDirectory = Path.Combine(testAsset.TestRoot, testAsset.TestProject.Name); } else { diff --git a/test/Microsoft.NET.TestFramework/Commands/DotnetCommand.cs b/test/Microsoft.NET.TestFramework/Commands/DotnetCommand.cs index f2cf6774bd86..bde70471e2a3 100644 --- a/test/Microsoft.NET.TestFramework/Commands/DotnetCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/DotnetCommand.cs @@ -14,11 +14,11 @@ protected override SdkCommandSpec CreateCommand(IEnumerable args) { var sdkCommandSpec = new SdkCommandSpec() { - FileName = TestContext.Current.ToolsetUnderTest?.DotNetHostPath, + FileName = TestContext.Current.ToolsetUnderTest.DotNetHostPath, Arguments = args.ToList(), WorkingDirectory = WorkingDirectory }; - TestContext.Current.AddTestEnvironmentVariables(sdkCommandSpec.Environment!); + TestContext.Current.AddTestEnvironmentVariables(sdkCommandSpec.Environment); return sdkCommandSpec; } } diff --git a/test/Microsoft.NET.TestFramework/Commands/DotnetPublishCommand.cs b/test/Microsoft.NET.TestFramework/Commands/DotnetPublishCommand.cs index b5dc4c2fa115..52744143f9b4 100644 --- a/test/Microsoft.NET.TestFramework/Commands/DotnetPublishCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/DotnetPublishCommand.cs @@ -16,7 +16,7 @@ public DotnetPublishCommand(ITestOutputHelper log, params string[] args) : base( protected override SdkCommandSpec CreateCommand(IEnumerable args) { List newArgs = new(args); - if (_runtime != null && !string.IsNullOrEmpty(_runtime)) + if (_runtime is not null && !string.IsNullOrEmpty(_runtime)) { newArgs.Add("-r"); newArgs.Add(_runtime); diff --git a/test/Microsoft.NET.TestFramework/Commands/GetValuesCommand.cs b/test/Microsoft.NET.TestFramework/Commands/GetValuesCommand.cs index 28f387197ab7..956c6eea16e4 100644 --- a/test/Microsoft.NET.TestFramework/Commands/GetValuesCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/GetValuesCommand.cs @@ -46,7 +46,7 @@ public GetValuesCommand(TestAsset testAsset, string? targetFramework = null) : base(testAsset, "WriteValuesToFile", relativePathToProject: null) { - _targetFramework = targetFramework ?? OutputPathCalculator.FromProject(ProjectFile, testAsset).TargetFramework!; + _targetFramework = targetFramework ?? OutputPathCalculator.FromProject(ProjectFile, testAsset).TargetFramework ?? string.Empty; _valueName = valueName; _valueType = valueType; @@ -67,7 +67,12 @@ protected override SdkCommandSpec CreateCommand(IEnumerable args) var project = XDocument.Load(ProjectFile); - var ns = project.Root!.Name.Namespace; + if(project.Root is null) + { + throw new InvalidOperationException($"The project file '{ProjectFile}' does not have a root element."); + } + + var ns = project.Root.Name.Namespace; string linesAttribute; if (_valueType == ValueType.Property) @@ -134,7 +139,7 @@ protected override SdkCommandSpec CreateCommand(IEnumerable args) var outputDirectory = GetValuesOutputDirectory(_targetFramework); outputDirectory.Create(); - return TestContext.Current.ToolsetUnderTest?.CreateCommandForTarget(TargetName, newArgs)!; + return TestContext.Current.ToolsetUnderTest.CreateCommandForTarget(TargetName, newArgs); } public List GetValues() diff --git a/test/Microsoft.NET.TestFramework/Commands/ListPackageCommand.cs b/test/Microsoft.NET.TestFramework/Commands/ListPackageCommand.cs index d84b5422f54b..efb15f9ba2f6 100644 --- a/test/Microsoft.NET.TestFramework/Commands/ListPackageCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/ListPackageCommand.cs @@ -17,7 +17,7 @@ public override CommandResult Execute(IEnumerable args) { List newArgs = new(); newArgs.Add("list"); - if (_projectName != null && !string.IsNullOrEmpty(_projectName)) + if (_projectName is not null && !string.IsNullOrEmpty(_projectName)) { newArgs.Add(_projectName); } diff --git a/test/Microsoft.NET.TestFramework/Commands/MSBuildCommand.cs b/test/Microsoft.NET.TestFramework/Commands/MSBuildCommand.cs index 0fddbd5b38ff..bcc8da92bd38 100644 --- a/test/Microsoft.NET.TestFramework/Commands/MSBuildCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/MSBuildCommand.cs @@ -70,9 +70,9 @@ internal static string FindProjectFile(ref string projectRootPath, string? relat public virtual DirectoryInfo GetOutputDirectory(string? targetFramework = null, string configuration = "Debug", string? runtimeIdentifier = null, string? platform = null) { - if (TestAsset != null) + if (TestAsset != null && platform is not null) { - return new DirectoryInfo(OutputPathCalculator.FromProject(ProjectFile, TestAsset).GetOutputDirectory(targetFramework!, configuration, runtimeIdentifier!, platform!)); + return new DirectoryInfo(OutputPathCalculator.FromProject(ProjectFile, TestAsset).GetOutputDirectory(targetFramework, configuration, runtimeIdentifier, platform)); } platform ??= string.Empty; @@ -88,7 +88,7 @@ public virtual DirectoryInfo GetIntermediateDirectory(string? targetFramework = { if (TestAsset != null) { - return new DirectoryInfo(OutputPathCalculator.FromProject(ProjectFile, TestAsset).GetIntermediateDirectory(targetFramework!, configuration, runtimeIdentifier!)); + return new DirectoryInfo(OutputPathCalculator.FromProject(ProjectFile, TestAsset).GetIntermediateDirectory(targetFramework, configuration, runtimeIdentifier)); } targetFramework = targetFramework ?? string.Empty; @@ -152,7 +152,7 @@ protected override SdkCommandSpec CreateCommand(IEnumerable args) var newArgs = args.ToList(); newArgs.Insert(0, FullPathProjectFile); - return TestContext.Current.ToolsetUnderTest?.CreateCommandForTarget(Target, newArgs)!; + return TestContext.Current.ToolsetUnderTest.CreateCommandForTarget(Target, newArgs); } } } diff --git a/test/Microsoft.NET.TestFramework/Commands/MSBuildVersionCommand.cs b/test/Microsoft.NET.TestFramework/Commands/MSBuildVersionCommand.cs index 1bbb1fb6919a..178daab62b70 100644 --- a/test/Microsoft.NET.TestFramework/Commands/MSBuildVersionCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/MSBuildVersionCommand.cs @@ -9,7 +9,7 @@ public MSBuildVersionCommand(ITestOutputHelper log) : base(log) { } protected override SdkCommandSpec CreateCommand(IEnumerable args) { - if (TestContext.Current.ToolsetUnderTest != null && TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild) + if (TestContext.Current.ToolsetUnderTest.ShouldUseFullFrameworkMSBuild) { return new SdkCommandSpec() { @@ -22,7 +22,7 @@ protected override SdkCommandSpec CreateCommand(IEnumerable args) { return new SdkCommandSpec() { - FileName = TestContext.Current.ToolsetUnderTest?.DotNetHostPath, + FileName = TestContext.Current.ToolsetUnderTest.DotNetHostPath, Arguments = { "msbuild", "-version" }, WorkingDirectory = WorkingDirectory }; diff --git a/test/Microsoft.NET.TestFramework/Commands/NuGetExeRestoreCommand.cs b/test/Microsoft.NET.TestFramework/Commands/NuGetExeRestoreCommand.cs index 86d13ac78b64..a31545b142ab 100644 --- a/test/Microsoft.NET.TestFramework/Commands/NuGetExeRestoreCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/NuGetExeRestoreCommand.cs @@ -43,15 +43,15 @@ protected override SdkCommandSpec CreateCommand(IEnumerable args) var nugetExePath = TestContext.Current.NuGetExePath; if (!string.IsNullOrEmpty(NuGetExeVersion)) { - nugetExePath = Path.Combine(Path.GetDirectoryName(nugetExePath)!, NuGetExeVersion, "nuget.exe"); + nugetExePath = Path.Combine(Path.GetDirectoryName(nugetExePath) ?? string.Empty, NuGetExeVersion, "nuget.exe"); } if (!File.Exists(nugetExePath)) { - string? directory = Path.GetDirectoryName(nugetExePath); + string directory = Path.GetDirectoryName(nugetExePath) ?? string.Empty; if (!Directory.Exists(directory)) { - Directory.CreateDirectory(directory!); + Directory.CreateDirectory(directory); } string url = string.IsNullOrEmpty(NuGetExeVersion) ? @@ -60,7 +60,7 @@ protected override SdkCommandSpec CreateCommand(IEnumerable args) using (var client = new System.Net.Http.HttpClient()) using (var response = client.GetAsync(url).ConfigureAwait(false).GetAwaiter().GetResult()) - using (var fs = new FileStream(nugetExePath!, FileMode.CreateNew)) + using (var fs = new FileStream(nugetExePath, FileMode.CreateNew)) { response.Content.CopyToAsync(fs).ConfigureAwait(false).GetAwaiter().GetResult(); } @@ -72,7 +72,7 @@ protected override SdkCommandSpec CreateCommand(IEnumerable args) Arguments = newArgs }; - TestContext.Current.AddTestEnvironmentVariables(ret.Environment!); + TestContext.Current.AddTestEnvironmentVariables(ret.Environment); return ret; } diff --git a/test/Microsoft.NET.TestFramework/Commands/PublishCommand.cs b/test/Microsoft.NET.TestFramework/Commands/PublishCommand.cs index bd560ba4ea6d..b9c19be1007a 100644 --- a/test/Microsoft.NET.TestFramework/Commands/PublishCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/PublishCommand.cs @@ -24,9 +24,9 @@ public PublishCommand(TestAsset testAsset, string? relativePathToProject = null) public override DirectoryInfo GetOutputDirectory(string? targetFramework = null, string configuration = "Debug", string? runtimeIdentifier = "", string? platformIdentifier = "") { - if (TestAsset != null) + if (TestAsset != null && targetFramework is not null) { - return new DirectoryInfo(OutputPathCalculator.FromProject(ProjectFile, TestAsset).GetPublishDirectory(targetFramework!, configuration, runtimeIdentifier!, platformIdentifier!)); + return new DirectoryInfo(OutputPathCalculator.FromProject(ProjectFile, TestAsset).GetPublishDirectory(targetFramework, configuration, runtimeIdentifier, platformIdentifier)); } if (string.IsNullOrEmpty(targetFramework)) diff --git a/test/Microsoft.NET.TestFramework/Commands/RemoveReferenceCommand.cs b/test/Microsoft.NET.TestFramework/Commands/RemoveReferenceCommand.cs index a3ac184a53dc..31b1310835f3 100644 --- a/test/Microsoft.NET.TestFramework/Commands/RemoveReferenceCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/RemoveReferenceCommand.cs @@ -17,7 +17,7 @@ public override CommandResult Execute(IEnumerable args) { List newArgs = new(); newArgs.Add("remove"); - if (_projectName != null && !string.IsNullOrEmpty(_projectName)) + if (_projectName is not null && !string.IsNullOrEmpty(_projectName)) { newArgs.Add(_projectName); } diff --git a/test/Microsoft.NET.TestFramework/Commands/RunExeCommand.cs b/test/Microsoft.NET.TestFramework/Commands/RunExeCommand.cs index 7e1a6cdf4c2a..97751154a6a4 100644 --- a/test/Microsoft.NET.TestFramework/Commands/RunExeCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/RunExeCommand.cs @@ -26,7 +26,7 @@ protected override SdkCommandSpec CreateCommand(IEnumerable args) Arguments = args.ToList(), WorkingDirectory = WorkingDirectory, }; - TestContext.Current.AddTestEnvironmentVariables(sdkCommandSpec.Environment!); + TestContext.Current.AddTestEnvironmentVariables(sdkCommandSpec.Environment); return sdkCommandSpec; } } diff --git a/test/Microsoft.NET.TestFramework/ConflictResolutionAssets.cs b/test/Microsoft.NET.TestFramework/ConflictResolutionAssets.cs index 5b74e3c20aa5..50820e00bf83 100644 --- a/test/Microsoft.NET.TestFramework/ConflictResolutionAssets.cs +++ b/test/Microsoft.NET.TestFramework/ConflictResolutionAssets.cs @@ -61,7 +61,7 @@ public static IEnumerable> ConflictResolutionDependencies foreach (var dependency in netStandardDependencies.Elements("dependency")) { - yield return Tuple.Create(dependency.Attribute("id")?.Value, dependency.Attribute("version")?.Value)!; + yield return Tuple.Create(dependency.Attribute("id")?.Value ?? string.Empty, dependency.Attribute("version")?.Value ?? string.Empty); } yield return Tuple.Create("System.Diagnostics.TraceSource", "4.0.0"); diff --git a/test/Microsoft.NET.TestFramework/Mock/FileSystemMockBuilder.cs b/test/Microsoft.NET.TestFramework/Mock/FileSystemMockBuilder.cs index 1c0f27015798..944ce7dc8d34 100644 --- a/test/Microsoft.NET.TestFramework/Mock/FileSystemMockBuilder.cs +++ b/test/Microsoft.NET.TestFramework/Mock/FileSystemMockBuilder.cs @@ -24,7 +24,7 @@ public static FileSystemMockBuilder Create() public FileSystemMockBuilder AddFile(string name, string content = "") { - _actions.Add(() => _mockFileSystemModel?.CreateDirectory(Path.GetDirectoryName(name)!)); + _actions.Add(() => _mockFileSystemModel?.CreateDirectory(Path.GetDirectoryName(name) ?? string.Empty)); _actions.Add(() => _mockFileSystemModel?.CreateFile(name, content)); return this; } @@ -220,7 +220,7 @@ public void CreateFile(string path, string content) Debug.Assert(directoryNode != null, nameof(directoryNode) + " != null"); - return predicate(directoryNode?.Subs!); + return predicate(directoryNode?.Subs ?? new()); } public DirectoryNode GetParentOfDirectoryNode(string path) @@ -517,7 +517,7 @@ public bool Exists(string path) { if (path == null) throw new ArgumentNullException(nameof(path)); - if (_files != null && _files.TryGetNodeParent(path, out DirectoryNode current)) + if (_files is not null && _files.TryGetNodeParent(path, out DirectoryNode current)) { PathModel pathModel = new(path); @@ -541,7 +541,7 @@ public ITemporaryDirectory CreateTemporaryDirectory() return _files?.EnumerateDirectory(path, subs => subs.Where(s => s.Value is DirectoryNode) - .Select(s => Path.Combine(path, s.Key)))!; + .Select(s => Path.Combine(path, s.Key))); } public IEnumerable? EnumerateFiles(string path) @@ -550,7 +550,7 @@ public ITemporaryDirectory CreateTemporaryDirectory() return _files?.EnumerateDirectory(path, subs => subs.Where(s => s.Value is FileNode) - .Select(s => Path.Combine(path, s.Key)))!; + .Select(s => Path.Combine(path, s.Key))); } public IEnumerable? EnumerateFileSystemEntries(string path) @@ -558,12 +558,12 @@ public ITemporaryDirectory CreateTemporaryDirectory() if (path == null) throw new ArgumentNullException(nameof(path)); return _files?.EnumerateDirectory(path, - subs => subs.Select(s => Path.Combine(path, s.Key)))!; + subs => subs.Select(s => Path.Combine(path, s.Key))); } - public string GetCurrentDirectory() + public string? GetCurrentDirectory() { - return _files?.WorkingDirectory!; + return _files?.WorkingDirectory; } public void CreateDirectory(string? path) @@ -590,7 +590,7 @@ public void Delete(string path, bool recursive) } else { - if (EnumerateFiles(path)!.Any()) + if (EnumerateFiles(path)?.Any() ?? false) { throw new IOException("Directory not empty"); } @@ -618,7 +618,7 @@ public void Move(string source, string destination) IFileSystemTreeNode? sourceNode = sourceParent?.Subs[parentPathModel.FileOrDirectoryName()]; - if (_files != null && _files.TryGetNodeParent(destination, out DirectoryNode current) && current != null) + if (_files is not null && _files.TryGetNodeParent(destination, out DirectoryNode current) && current != null) { PathModel destinationPathModel = new(destination); @@ -632,8 +632,10 @@ public void Move(string source, string destination) throw new IOException($"Cannot create {destination} because a file or" + " directory with the same name already exists"); } - - sourceNode = current.Subs.GetOrAdd(destinationPathModel.FileOrDirectoryName(), sourceNode!); + if(sourceNode is not null) + { + sourceNode = current.Subs.GetOrAdd(destinationPathModel.FileOrDirectoryName(), sourceNode); + } sourceParent?.Subs.TryRemove(parentPathModel.FileOrDirectoryName(), out _); } else diff --git a/test/Microsoft.NET.TestFramework/OutputPathCalculator.cs b/test/Microsoft.NET.TestFramework/OutputPathCalculator.cs index ebd156612efc..26b60fe70b55 100644 --- a/test/Microsoft.NET.TestFramework/OutputPathCalculator.cs +++ b/test/Microsoft.NET.TestFramework/OutputPathCalculator.cs @@ -31,9 +31,9 @@ public static OutputPathCalculator FromProject(string? projectPath, TestProject? } // Support passing in the root test path and looking in subfolder specified by testProject - if (projectPath == null && testProject != null) + if (projectPath == null && testProject != null && testProject.Name is not null && originalProjectPath is not null) { - projectPath = Path.Combine(originalProjectPath!, testProject.Name!); + projectPath = Path.Combine(originalProjectPath, testProject.Name); if (!File.Exists(projectPath) && Directory.Exists(projectPath)) { @@ -75,17 +75,23 @@ public static OutputPathCalculator FromProject(string? projectPath, TestProject? { throw new InvalidOperationException("Couldn't find Directory.Build.props for test project " + projectPath); } - calculator.ArtifactsPath = Path.Combine(Path.GetDirectoryName(directoryBuildPropsFile)!, "artifacts"); + calculator.ArtifactsPath = Path.Combine(Path.GetDirectoryName(directoryBuildPropsFile) ?? string.Empty, "artifacts"); } else { - calculator.ArtifactsPath = Path.Combine(Path.GetDirectoryName(projectPath)!, "artifacts"); + calculator.ArtifactsPath = Path.Combine(Path.GetDirectoryName(projectPath) ?? string.Empty, "artifacts"); } } else { var project = XDocument.Load(projectPath); - var ns = project.Root!.Name.Namespace; + + if (project.Root is null) + { + throw new InvalidOperationException($"The project file '{projectPath}' does not have a root element."); + } + + var ns = project.Root.Name.Namespace; var useArtifactsOutputElement = project.Root.Elements(ns + "PropertyGroup").Elements(ns + "UseArtifactsOutput").FirstOrDefault(); if (useArtifactsOutputElement != null) @@ -94,7 +100,7 @@ public static OutputPathCalculator FromProject(string? projectPath, TestProject? if (calculator.UseArtifactsOutput) { calculator.IncludeProjectNameInArtifactsPaths = false; - calculator.ArtifactsPath = Path.Combine(Path.GetDirectoryName(projectPath)!, "artifacts"); + calculator.ArtifactsPath = Path.Combine(Path.GetDirectoryName(projectPath) ?? string.Empty, "artifacts"); } } @@ -120,7 +126,11 @@ public static OutputPathCalculator FromProject(string? projectPath, TestProject? if (directoryBuildPropsFile != null) { var dbp = XDocument.Load(directoryBuildPropsFile); - var dbpns = dbp.Root!.Name.Namespace; + if (dbp.Root is null) + { + throw new InvalidOperationException($"The project file '{directoryBuildPropsFile}' does not have a root element."); + } + var dbpns = dbp.Root.Name.Namespace; var dbpUsesArtifacts = dbp.Root.Elements(dbpns + "PropertyGroup").Elements(dbpns + "UseArtifactsOutput").FirstOrDefault(); if (dbpUsesArtifacts != null) @@ -130,7 +140,7 @@ public static OutputPathCalculator FromProject(string? projectPath, TestProject? if (calculator.UseArtifactsOutput) { calculator.IncludeProjectNameInArtifactsPaths = true; - calculator.ArtifactsPath = Path.Combine(Path.GetDirectoryName(directoryBuildPropsFile)!, "artifacts"); + calculator.ArtifactsPath = Path.Combine(Path.GetDirectoryName(directoryBuildPropsFile) ?? string.Empty, "artifacts"); } } } @@ -179,11 +189,11 @@ public string GetOutputDirectory(string? targetFramework = null, string configur if (IncludeProjectNameInArtifactsPaths) { - return Path.Combine(ArtifactsPath!, "bin", Path.GetFileNameWithoutExtension(ProjectPath)!, pivot); + return Path.Combine(ArtifactsPath ?? string.Empty, "bin", Path.GetFileNameWithoutExtension(ProjectPath) ?? string.Empty, pivot); } else { - return Path.Combine(ArtifactsPath!, "bin", pivot); + return Path.Combine(ArtifactsPath ?? string.Empty, "bin", pivot); } } else @@ -195,18 +205,18 @@ public string GetOutputDirectory(string? targetFramework = null, string configur if (IsSdkProject) { - string output = Path.Combine(Path.GetDirectoryName(ProjectPath)!, "bin", platform, configuration, targetFramework, runtimeIdentifier); + string output = Path.Combine(Path.GetDirectoryName(ProjectPath) ?? string.Empty, "bin", platform, configuration, targetFramework, runtimeIdentifier); return output; } else { - string output = Path.Combine(Path.GetDirectoryName(ProjectPath)!, "bin", platform, configuration); + string output = Path.Combine(Path.GetDirectoryName(ProjectPath) ?? string.Empty, "bin", platform, configuration); return output; } } } - public string GetPublishDirectory(string? targetFramework = null, string configuration = "Debug", string? runtimeIdentifier = "", string platform = "") + public string GetPublishDirectory(string? targetFramework = null, string configuration = "Debug", string? runtimeIdentifier = "", string? platform = "") { if (UseArtifactsOutput) { @@ -226,11 +236,11 @@ public string GetPublishDirectory(string? targetFramework = null, string configu if (IncludeProjectNameInArtifactsPaths) { - return Path.Combine(ArtifactsPath!, "publish", Path.GetFileNameWithoutExtension(ProjectPath)!, pivot); + return Path.Combine(ArtifactsPath ?? string.Empty, "publish", Path.GetFileNameWithoutExtension(ProjectPath)!, pivot); } else { - return Path.Combine(ArtifactsPath!, "publish", pivot); + return Path.Combine(ArtifactsPath ?? string.Empty, "publish", pivot); } } else @@ -240,7 +250,7 @@ public string GetPublishDirectory(string? targetFramework = null, string configu runtimeIdentifier ??= RuntimeIdentifier ?? string.Empty; platform ??= string.Empty; - string output = Path.Combine(Path.GetDirectoryName(ProjectPath)!, "bin", platform, configuration, targetFramework, runtimeIdentifier, "publish"); + string output = Path.Combine(Path.GetDirectoryName(ProjectPath) ?? string.Empty, "bin", platform, configuration, targetFramework, runtimeIdentifier, "publish"); return output; } } @@ -265,11 +275,11 @@ public string GetIntermediateDirectory(string? targetFramework = null, string co if (IncludeProjectNameInArtifactsPaths) { - return Path.Combine(ArtifactsPath!, "obj", Path.GetFileNameWithoutExtension(ProjectPath)!, pivot); + return Path.Combine(ArtifactsPath ?? string.Empty, "obj", Path.GetFileNameWithoutExtension(ProjectPath) ?? string.Empty, pivot); } else { - return Path.Combine(ArtifactsPath!, "obj", pivot); + return Path.Combine(ArtifactsPath ?? string.Empty, "obj", pivot); } } @@ -278,7 +288,7 @@ public string GetIntermediateDirectory(string? targetFramework = null, string co configuration = configuration ?? string.Empty; runtimeIdentifier = runtimeIdentifier ?? RuntimeIdentifier ?? string.Empty; - string output = Path.Combine(Path.GetDirectoryName(ProjectPath)!, "obj", configuration, targetFramework, runtimeIdentifier); + string output = Path.Combine(Path.GetDirectoryName(ProjectPath) ?? string.Empty, "obj", configuration, targetFramework, runtimeIdentifier); return output; } @@ -286,11 +296,11 @@ public string GetPackageDirectory(string configuration = "Debug") { if (UseArtifactsOutput) { - return Path.Combine(ArtifactsPath!, "package", configuration.ToLowerInvariant()); + return Path.Combine(ArtifactsPath ?? string.Empty, "package", configuration.ToLowerInvariant()); } else { - return Path.Combine(Path.GetDirectoryName(ProjectPath)!, "bin", configuration); + return Path.Combine(Path.GetDirectoryName(ProjectPath) ?? string.Empty, "bin", configuration); } } } diff --git a/test/Microsoft.NET.TestFramework/ProjectConstruction/TestProject.cs b/test/Microsoft.NET.TestFramework/ProjectConstruction/TestProject.cs index a24d6ed50e60..91b9f562f1a5 100644 --- a/test/Microsoft.NET.TestFramework/ProjectConstruction/TestProject.cs +++ b/test/Microsoft.NET.TestFramework/ProjectConstruction/TestProject.cs @@ -127,7 +127,7 @@ public bool BuildsOnNonWindows internal void Create(TestAsset targetTestAsset, string testProjectsSourceFolder, string targetExtension = ".csproj") { - string targetFolder = Path.Combine(targetTestAsset.Path, Name!); + string targetFolder = Path.Combine(targetTestAsset.Path, Name ?? string.Empty); Directory.CreateDirectory(targetFolder); string targetProjectPath = Path.Combine(targetFolder, Name + targetExtension); @@ -144,7 +144,7 @@ internal void Create(TestAsset targetTestAsset, string testProjectsSourceFolder, } // Copy any additional files from template - foreach (var file in Directory.GetFiles(Path.GetDirectoryName(sourceProject)!)) + foreach (var file in Directory.GetFiles(Path.GetDirectoryName(sourceProject) ?? string.Empty)) { if (file != sourceProject) { @@ -154,26 +154,30 @@ internal void Create(TestAsset targetTestAsset, string testProjectsSourceFolder, var projectXml = XDocument.Load(sourceProject); - var ns = projectXml.Root!.Name.Namespace; + if (projectXml.Root is null) + { + throw new InvalidOperationException($"The project file '{sourceProject}' does not have a root element."); + } + var ns = projectXml.Root.Name.Namespace; - if (ProjectSdk != null) + if (projectXml.Root.Attribute("Sdk") is not null && ProjectSdk != null) { projectXml.Root.Attribute("Sdk")!.Value = ProjectSdk; } var propertyGroup = projectXml.Root?.Elements(ns + "PropertyGroup").First(); - var packageReferenceItemGroup = projectXml.Root!.Elements(ns + "ItemGroup") + var packageReferenceItemGroup = projectXml.Root?.Elements(ns + "ItemGroup") .FirstOrDefault(itemGroup => itemGroup.Elements(ns + "PackageReference").Count() > 0); if (packageReferenceItemGroup == null) { - packageReferenceItemGroup = projectXml.Root.Elements(ns + "ItemGroup") + packageReferenceItemGroup = projectXml.Root?.Elements(ns + "ItemGroup") .FirstOrDefault(); } if (packageReferenceItemGroup == null) { packageReferenceItemGroup = new XElement(ns + "ItemGroup"); - projectXml.Root.Add(packageReferenceItemGroup); + projectXml.Root?.Add(packageReferenceItemGroup); } foreach (TestPackageReference packageReference in PackageReferences) @@ -238,11 +242,17 @@ internal void Create(TestAsset targetTestAsset, string testProjectsSourceFolder, // To construct an accurate PCL project file, we must modify the import of the CSharp targets; // building/testing the SDK requires a VSDev command prompt which sets 'VSINSTALLDIR' - var importGroup = projectXml.Root.Elements(ns + "Import").Last(); - importGroup.Attribute("Project")!.Value = "$(VSINSTALLDIR)\\MSBuild\\Microsoft\\Portable\\$(TargetFrameworkVersion)\\Microsoft.Portable.CSharp.targets"; + var importGroup = projectXml.Root?.Elements(ns + "Import").Last(); + if(importGroup?.Attribute("Project") is not null) + { + importGroup.Attribute("Project")!.Value = "$(VSINSTALLDIR)\\MSBuild\\Microsoft\\Portable\\$(TargetFrameworkVersion)\\Microsoft.Portable.CSharp.targets"; + } } - propertyGroup?.Element(ns + "TargetFrameworkVersion")?.SetValue(TargetFrameworkVersion!); + if(TargetFrameworkVersion is not null) + { + propertyGroup?.Element(ns + "TargetFrameworkVersion")?.SetValue(TargetFrameworkVersion); + } } foreach (var additionalProperty in AdditionalProperties) @@ -254,11 +264,11 @@ internal void Create(TestAsset targetTestAsset, string testProjectsSourceFolder, { foreach (var additionalItem in AdditionalItems) { - var additionalItemGroup = projectXml.Root.Elements(ns + "ItemGroup").FirstOrDefault(); + var additionalItemGroup = projectXml.Root?.Elements(ns + "ItemGroup").FirstOrDefault(); if (additionalItemGroup == null) { additionalItemGroup = new XElement(ns + "ItemGroup"); - projectXml.Root.Add(packageReferenceItemGroup); + projectXml.Root?.Add(packageReferenceItemGroup); } var item = new XElement(ns + additionalItem.Key); foreach (var attribute in additionalItem.Value) @@ -283,7 +293,7 @@ internal void Create(TestAsset targetTestAsset, string testProjectsSourceFolder, if (ReferencedProjects.Any()) { - var projectReferenceItemGroup = projectXml.Root.Elements(ns + "ItemGroup") + var projectReferenceItemGroup = projectXml.Root?.Elements(ns + "ItemGroup") .FirstOrDefault(itemGroup => itemGroup.Elements(ns + "ProjectReference").Count() > 0); if (projectReferenceItemGroup == null) { @@ -299,7 +309,7 @@ internal void Create(TestAsset targetTestAsset, string testProjectsSourceFolder, if (References.Any()) { - var referenceItemGroup = projectXml.Root.Elements(ns + "ItemGroup") + var referenceItemGroup = projectXml.Root?.Elements(ns + "ItemGroup") .FirstOrDefault(itemGroup => itemGroup.Elements(ns + "Reference").Count() > 0); if (referenceItemGroup == null) { @@ -317,7 +327,7 @@ internal void Create(TestAsset targetTestAsset, string testProjectsSourceFolder, if (FrameworkReferences.Any()) { var frameworkReferenceItemGroup = new XElement(ns + "ItemGroup"); - projectXml.Root.Add(frameworkReferenceItemGroup); + projectXml.Root?.Add(frameworkReferenceItemGroup); foreach (var frameworkReference in FrameworkReferences) { frameworkReferenceItemGroup.Add(new XElement(ns + "FrameworkReference", @@ -343,7 +353,7 @@ internal void Create(TestAsset targetTestAsset, string testProjectsSourceFolder, } target.Add(copyElement); - projectXml.Root.Add(target); + projectXml.Root?.Add(target); } } @@ -490,7 +500,7 @@ public Dictionary GetPropertyValues(string testRoot, string? tar { var propertyValues = new Dictionary(); - string intermediateOutputPath = Path.Combine(testRoot, Name!, "obj", configuration, targetFramework ?? TargetFrameworks); + string intermediateOutputPath = Path.Combine(testRoot, Name ?? string.Empty, "obj", configuration, targetFramework ?? TargetFrameworks); foreach (var line in File.ReadAllLines(Path.Combine(intermediateOutputPath, "PropertyValues.txt"))) { @@ -514,7 +524,7 @@ public static bool ReferenceAssembliesAreInstalled(TargetDotNetFrameworkVersion private OutputPathCalculator GetOutputPathCalculator(string testRoot) { - return OutputPathCalculator.FromProject(Path.Combine(testRoot, Name!, Name + ".csproj"), this); + return OutputPathCalculator.FromProject(Path.Combine(testRoot, Name ?? string.Empty, Name + ".csproj"), this); } public string GetOutputDirectory(string testRoot, string? targetFramework = null, string configuration = "Debug", string runtimeIdentifier = "") diff --git a/test/Microsoft.NET.TestFramework/TestAsset.cs b/test/Microsoft.NET.TestFramework/TestAsset.cs index 98e5265e2ccd..062d69680301 100644 --- a/test/Microsoft.NET.TestFramework/TestAsset.cs +++ b/test/Microsoft.NET.TestFramework/TestAsset.cs @@ -64,15 +64,15 @@ public TestAsset WithSource() { _projectFiles = new List(); - var sourceDirs = Directory.GetDirectories(_testAssetRoot!, "*", SearchOption.AllDirectories) + var sourceDirs = Directory.GetDirectories(_testAssetRoot ?? string.Empty, "*", SearchOption.AllDirectories) .Where(dir => !IsBinOrObjFolder(dir)); foreach (string sourceDir in sourceDirs) { - Directory.CreateDirectory(sourceDir.Replace(_testAssetRoot!, Path)); + Directory.CreateDirectory(sourceDir.Replace(_testAssetRoot ?? string.Empty, Path)); } - var sourceFiles = Directory.GetFiles(_testAssetRoot!, "*.*", SearchOption.AllDirectories) + var sourceFiles = Directory.GetFiles(_testAssetRoot ?? string.Empty, "*.*", SearchOption.AllDirectories) .Where(file => { return !IsInBinOrObjFolder(file); @@ -80,7 +80,7 @@ public TestAsset WithSource() foreach (string srcFile in sourceFiles) { - string destFile = srcFile.Replace(_testAssetRoot!, Path); + string destFile = srcFile.Replace(_testAssetRoot ?? string.Empty, Path); if (System.IO.Path.GetFileName(srcFile).EndsWith("proj") || System.IO.Path.GetFileName(srcFile).EndsWith("xml")) { @@ -115,10 +115,13 @@ public TestAsset UpdateProjProperty(string propertyName, string variableName, st return WithProjectChanges( p => { - var ns = p.Root!.Name.Namespace; - var getNode = p.Root.Elements(ns + "PropertyGroup").Elements(ns + propertyName).FirstOrDefault(); - getNode ??= p.Root.Elements(ns + "PropertyGroup").Elements(ns + $"{propertyName}s").FirstOrDefault(); - getNode?.SetValue(getNode?.Value.Replace(variableName, targetValue)!); + if (p.Root is not null) + { + var ns = p.Root.Name.Namespace; + var getNode = p.Root.Elements(ns + "PropertyGroup").Elements(ns + propertyName).FirstOrDefault(); + getNode ??= p.Root.Elements(ns + "PropertyGroup").Elements(ns + $"{propertyName}s").FirstOrDefault(); + getNode?.SetValue(getNode?.Value.Replace(variableName, targetValue) ?? string.Empty); + } }); } @@ -128,17 +131,24 @@ public TestAsset ReplacePackageVersionVariable(string targetName, string targetV return WithProjectChanges(project => { - var ns = project.Root!.Name.Namespace; - foreach (var PropertyName in PropertyNames) + if (project.Root is not null) { - var packageReferencesToUpdate = - project.Root.Descendants(ns + PropertyName) - .Where(p => p.Attribute("Version") != null && p.Attribute("Version")!.Value.Equals($"$({targetName})", StringComparison.OrdinalIgnoreCase)); - foreach (var packageReference in packageReferencesToUpdate) + var ns = project.Root!.Name.Namespace; + foreach (var PropertyName in PropertyNames) { - packageReference.Attribute("Version")!.Value = targetValue; + var packageReferencesToUpdate = + project.Root.Descendants(ns + PropertyName) + .Where(p => p.Attribute("Version") != null && p.Attribute("Version")!.Value.Equals($"$({targetName})", StringComparison.OrdinalIgnoreCase)); + foreach (var packageReference in packageReferencesToUpdate) + { + if(packageReference.Attribute("Version") is not null) + { + packageReference.Attribute("Version")!.Value = targetValue; + } + } } } + }); } @@ -151,8 +161,11 @@ public TestAsset WithTargetFramework(string targetFramework, string? projectName return WithProjectChanges( p => { - var ns = p.Root!.Name.Namespace; - p.Root.Elements(ns + "PropertyGroup").Elements(ns + "TargetFramework").Single().SetValue(targetFramework); + if (p.Root is not null) + { + var ns = p.Root.Name.Namespace; + p.Root.Elements(ns + "PropertyGroup").Elements(ns + "TargetFramework").Single().SetValue(targetFramework); + } }, projectName); } @@ -166,11 +179,14 @@ public TestAsset WithTargetFrameworks(string targetFrameworks, string? projectNa return WithProjectChanges( p => { - var ns = p.Root!.Name.Namespace; - var propertyGroup = p.Root.Elements(ns + "PropertyGroup").First(); - propertyGroup.Elements(ns + "TargetFramework").SingleOrDefault()?.Remove(); - propertyGroup.Elements(ns + "TargetFrameworks").SingleOrDefault()?.Remove(); - propertyGroup.Add(new XElement(ns + "TargetFrameworks", targetFrameworks)); + if (p.Root is not null) + { + var ns = p.Root.Name.Namespace; + var propertyGroup = p.Root.Elements(ns + "PropertyGroup").First(); + propertyGroup.Elements(ns + "TargetFramework").SingleOrDefault()?.Remove(); + propertyGroup.Elements(ns + "TargetFrameworks").SingleOrDefault()?.Remove(); + propertyGroup.Add(new XElement(ns + "TargetFrameworks", targetFrameworks)); + } }, projectName); } @@ -193,13 +209,16 @@ private TestAsset WithProjectChanges(Action actionOnProject, string? { if (!string.IsNullOrEmpty(projectName)) { - if (projectName != null && !projectName.Equals(System.IO.Path.GetFileNameWithoutExtension(path), StringComparison.OrdinalIgnoreCase)) + if (projectName is not null && !projectName.Equals(System.IO.Path.GetFileNameWithoutExtension(path), StringComparison.OrdinalIgnoreCase)) { return; } } - - var ns = project.Root!.Name.Namespace; + if (project.Root is null) + { + throw new InvalidOperationException($"The project file '{projectName}' does not have a root element."); + } + var ns = project.Root.Name.Namespace; actionOnProject(project); }); } @@ -227,7 +246,7 @@ public TestAsset WithProjectChanges(Action xmlAction) } } return this; - + } public RestoreCommand GetRestoreCommand(ITestOutputHelper log, string relativePath = "") diff --git a/test/Microsoft.NET.TestFramework/TestAssetsManager.cs b/test/Microsoft.NET.TestFramework/TestAssetsManager.cs index 45153b5b03df..2546ab0a5709 100644 --- a/test/Microsoft.NET.TestFramework/TestAssetsManager.cs +++ b/test/Microsoft.NET.TestFramework/TestAssetsManager.cs @@ -8,7 +8,7 @@ namespace Microsoft.NET.TestFramework { public class TestAssetsManager { - public string TestAssetsRoot { get; private set; } + public string TestAssetsRoot { get; private set; } private List TestDestinationDirectories { get; } = new List(); @@ -199,7 +199,7 @@ public static string GetTestDestinationDirectoryPath( .AppendFormat("{0:X2}", hash[3]); } - var directoryPath = Path.Combine(baseDirectory!, directoryName.ToString()); + var directoryPath = Path.Combine(baseDirectory, directoryName.ToString()); #if CI_BUILD if (!allowCopyIfPresent && Directory.Exists(directoryPath)) { diff --git a/test/Microsoft.NET.TestFramework/TestCommandLine.cs b/test/Microsoft.NET.TestFramework/TestCommandLine.cs index 38bd22202a91..ab94559bb7aa 100644 --- a/test/Microsoft.NET.TestFramework/TestCommandLine.cs +++ b/test/Microsoft.NET.TestFramework/TestCommandLine.cs @@ -151,29 +151,32 @@ public List GetXunitArgsFromTestConfig() foreach (var testConfigFile in TestConfigFiles) { var testConfig = XDocument.Load(testConfigFile); - foreach (var item in testConfig.Root!.Elements()) + if (testConfig.Root is not null) { - if (item.Name.LocalName.Equals("TestList", StringComparison.OrdinalIgnoreCase)) + foreach (var item in testConfig.Root.Elements()) { - testLists.Add(TestList.Parse(item)); - } - else if (item.Name.LocalName.Equals("SkippedTests", StringComparison.OrdinalIgnoreCase)) - { - var skippedGroup = TestList.Parse(item); - testsToSkip.AddRange(skippedGroup.TestSpecifiers); - } - else - { - if (bool.TryParse(item.Attribute("Skip")?.Value ?? string.Empty, out bool shouldSkip) && - shouldSkip) + if (item.Name.LocalName.Equals("TestList", StringComparison.OrdinalIgnoreCase)) + { + testLists.Add(TestList.Parse(item)); + } + else if (item.Name.LocalName.Equals("SkippedTests", StringComparison.OrdinalIgnoreCase)) + { + var skippedGroup = TestList.Parse(item); + testsToSkip.AddRange(skippedGroup.TestSpecifiers); + } + else { - testsToSkip.Add(TestSpecifier.Parse(item)); + if (bool.TryParse(item.Attribute("Skip")?.Value ?? string.Empty, out bool shouldSkip) && + shouldSkip) + { + testsToSkip.Add(TestSpecifier.Parse(item)); + } } } } } - foreach (var testList in testLists.Where(g => TestListsToRun.Contains(g.Name!))) + foreach (var testList in testLists.Where(g => TestListsToRun.Contains(g.Name ?? string.Empty))) { foreach (var testSpec in testList.TestSpecifiers) { @@ -193,7 +196,7 @@ public List GetXunitArgsFromTestConfig() { throw new ArgumentException("Unrecognized test specifier type: " + testSpec.Type); } - ret.Add(testSpec.Specifier!); + ret.Add(testSpec.Specifier ?? string.Empty); } } @@ -215,7 +218,7 @@ public List GetXunitArgsFromTestConfig() { throw new ArgumentException("Unrecognized test specifier type: " + testSpec.Type); } - ret.Add(testSpec.Specifier!); + ret.Add(testSpec.Specifier ?? string.Empty); } return ret; diff --git a/test/Microsoft.NET.TestFramework/TestContext.cs b/test/Microsoft.NET.TestFramework/TestContext.cs index b43c736dd128..682412fc0b7a 100644 --- a/test/Microsoft.NET.TestFramework/TestContext.cs +++ b/test/Microsoft.NET.TestFramework/TestContext.cs @@ -59,7 +59,23 @@ public string TestAssetsDirectory public string? SdkVersion { get; set; } - public ToolsetInfo? ToolsetUnderTest { get; set; } + private ToolsetInfo? _toolsetUnderTest; + + public ToolsetInfo ToolsetUnderTest + { + get + { + if (_toolsetUnderTest == null) + { + throw new InvalidOperationException("ToolsetUnderTest should never be null."); + } + return _toolsetUnderTest; + } + set + { + _toolsetUnderTest = value; + } + } private static TestContext? _current; @@ -85,7 +101,7 @@ public static TestContext Current public static string GetRuntimeGraphFilePath() { - string dotnetRoot = TestContext.Current.ToolsetUnderTest?.DotNetRoot!; + string dotnetRoot = TestContext.Current.ToolsetUnderTest.DotNetRoot; DirectoryInfo sdksDir = new(Path.Combine(dotnetRoot, "sdk")); @@ -94,12 +110,15 @@ public static string GetRuntimeGraphFilePath() return lastWrittenSdk.GetFiles("RuntimeIdentifierGraph.json").Single().FullName; } - public void AddTestEnvironmentVariables(IDictionary environment) + public void AddTestEnvironmentVariables(IDictionary environment) { environment["DOTNET_MULTILEVEL_LOOKUP"] = "0"; // Set NUGET_PACKAGES environment variable to match value from build.ps1 - environment["NUGET_PACKAGES"] = NuGetCachePath; + if(NuGetCachePath is not null) + { + environment["NUGET_PACKAGES"] = NuGetCachePath; + } environment["GenerateResourceMSBuildArchitecture"] = "CurrentArchitecture"; environment["GenerateResourceMSBuildRuntime"] = "CurrentRuntime"; @@ -107,7 +126,7 @@ public void AddTestEnvironmentVariables(IDictionary environment // Prevent test MSBuild nodes from persisting environment["MSBUILDDISABLENODEREUSE"] = "1"; - ToolsetUnderTest?.AddTestEnvironmentVariables(environment); + ToolsetUnderTest.AddTestEnvironmentVariables(environment); } @@ -142,10 +161,13 @@ public static void Initialize(TestCommandLine commandLine) // This allows testing most of the "tests as global tool" behavior by setting an environment // variable instead of packing the test, and installing it as a global tool. runAsTool = true; - - testContext.TestAssetsDirectory = FindFolderInTree(Path.Combine("test", "TestAssets"), AppContext.BaseDirectory)!; + string? FindFolder = FindFolderInTree(Path.Combine("test", "TestAssets"), AppContext.BaseDirectory); + if (FindFolder is not null) + { + testContext.TestAssetsDirectory = FindFolder; + } } - else if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("DOTNET_SDK_TEST_ASSETS_DIRECTORY"))) + else if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("DOTNET_SDK_TEST_ASSETS_DIRECTORY")) && Environment.GetEnvironmentVariable("DOTNET_SDK_TEST_ASSETS_DIRECTORY") is not null) { testContext.TestAssetsDirectory = Environment.GetEnvironmentVariable("DOTNET_SDK_TEST_ASSETS_DIRECTORY")!; } @@ -166,9 +188,9 @@ public static void Initialize(TestCommandLine commandLine) repoRoot = GetRepoRoot(); } - if (!string.IsNullOrEmpty(commandLine.TestExecutionDirectory)) + if (!string.IsNullOrEmpty(commandLine.TestExecutionDirectory) && commandLine.TestExecutionDirectory is not null) { - testContext.TestExecutionDirectory = commandLine.TestExecutionDirectory!; + testContext.TestExecutionDirectory = commandLine.TestExecutionDirectory; } else if (Environment.GetEnvironmentVariable("DOTNET_SDK_TEST_EXECUTION_DIRECTORY") != null) { @@ -180,9 +202,16 @@ public static void Initialize(TestCommandLine commandLine) } else { - testContext.TestExecutionDirectory = (Path.Combine(FindFolderInTree("artifacts", AppContext.BaseDirectory)!, "tmp", repoConfiguration)); - - testContext.TestAssetsDirectory = FindFolderInTree(Path.Combine("test", "TestAssets"), AppContext.BaseDirectory)!; + string? FindFolder1 = FindFolderInTree("artifacts", AppContext.BaseDirectory); + string? FindFolder2 = FindFolderInTree(Path.Combine("test", "TestAssets"), AppContext.BaseDirectory); + if (FindFolder1 is not null) + { + testContext.TestExecutionDirectory = Path.Combine(FindFolder1, "tmp", repoConfiguration); + } + if (FindFolder2 is not null) + { + testContext.TestAssetsDirectory = FindFolder2; + } } Directory.CreateDirectory(testContext.TestExecutionDirectory); @@ -202,13 +231,13 @@ public static void Initialize(TestCommandLine commandLine) testContext.TestGlobalPackagesFolder = Path.Combine(testContext.TestExecutionDirectory, ".nuget", "packages"); } - if (repoRoot != null) + if (repoRoot != null && artifactsDir is not null) { - testContext.NuGetFallbackFolder = Path.Combine(artifactsDir!, ".nuget", "NuGetFallbackFolder"); - testContext.NuGetExePath = Path.Combine(artifactsDir!, ".nuget", $"nuget{Constants.ExeSuffix}"); - testContext.NuGetCachePath = Path.Combine(artifactsDir!, ".nuget", "packages"); + testContext.NuGetFallbackFolder = Path.Combine(artifactsDir, ".nuget", "NuGetFallbackFolder"); + testContext.NuGetExePath = Path.Combine(artifactsDir, ".nuget", $"nuget{Constants.ExeSuffix}"); + testContext.NuGetCachePath = Path.Combine(artifactsDir, ".nuget", "packages"); - testContext.TestPackages = Path.Combine(artifactsDir!, "tmp", repoConfiguration, "testpackages"); + testContext.TestPackages = Path.Combine(artifactsDir, "tmp", repoConfiguration, "testpackages"); } else if (runAsTool) { diff --git a/test/Microsoft.NET.TestFramework/TestPackageReference.cs b/test/Microsoft.NET.TestFramework/TestPackageReference.cs index d97e5ec16c5d..073b76ce3dfe 100644 --- a/test/Microsoft.NET.TestFramework/TestPackageReference.cs +++ b/test/Microsoft.NET.TestFramework/TestPackageReference.cs @@ -25,7 +25,7 @@ public TestPackageReference(string id, string? version = null, string? nupkgPath public bool UpdatePackageReference { get; private set; } public bool NuGetPackageExists() { - return File.Exists(Path.Combine(NupkgPath!, string.Concat(ID + "." + Version + ".nupkg"))); + return File.Exists(Path.Combine(NupkgPath ?? string.Empty, string.Concat(ID + "." + Version + ".nupkg"))); } } } diff --git a/test/Microsoft.NET.TestFramework/ToolsetInfo.cs b/test/Microsoft.NET.TestFramework/ToolsetInfo.cs index fc442523f3ad..c2174722c544 100644 --- a/test/Microsoft.NET.TestFramework/ToolsetInfo.cs +++ b/test/Microsoft.NET.TestFramework/ToolsetInfo.cs @@ -18,11 +18,11 @@ public class ToolsetInfo public const string LatestMacRuntimeIdentifier = "osx"; public const string LatestRuntimeIdentifiers = $"{LatestWinRuntimeIdentifier}-x64;{LatestWinRuntimeIdentifier}-x86;osx-x64;{LatestMacRuntimeIdentifier}-x64;{LatestLinuxRuntimeIdentifier}-x64;linux-musl-x64"; - public string? DotNetRoot { get; } + public string DotNetRoot { get; } public string DotNetHostPath { get; } private string? _sdkVersion; - public string? SdkVersion + public string SdkVersion { get { @@ -32,7 +32,7 @@ public string? SdkVersion // for the TestContext to finish being initialize InitSdkVersion(); } - return _sdkVersion; + return _sdkVersion ?? throw new InvalidOperationException("SdkVersion should never be null."); ; } } @@ -68,13 +68,13 @@ public string? MSBuildVersion public string? SdkResolverPath { get; set; } - public ToolsetInfo(string? dotNetRoot) + public ToolsetInfo(string dotNetRoot) { DotNetRoot = dotNetRoot; - DotNetHostPath = Path.Combine(dotNetRoot!, $"dotnet{Constants.ExeSuffix}"); + DotNetHostPath = Path.Combine(dotNetRoot, $"dotnet{Constants.ExeSuffix}"); - _sdkFolderUnderTest = new Lazy(() => Path.Combine(DotNetRoot!, "sdk", SdkVersion!)); + _sdkFolderUnderTest = new Lazy(() => Path.Combine(DotNetRoot, "sdk", SdkVersion)); _sdksPath = new Lazy(() => Path.Combine(SdkFolderUnderTest, "Sdks")); } @@ -136,25 +136,32 @@ private void InitMSBuildVersion() { if (ShouldUseFullFrameworkMSBuild) { + string? msbuildRoot = null; var msbuildBinPath = Path.GetDirectoryName(FullFrameworkMSBuildPath); - var msbuildRoot = Directory.GetParent(msbuildBinPath!)?.Parent?.FullName; - return Path.Combine(msbuildRoot!, @"Microsoft\Microsoft.NET.Build.Extensions"); + if(msbuildBinPath is not null) + { + msbuildRoot = Directory.GetParent(msbuildBinPath)?.Parent?.FullName; + } + return Path.Combine(msbuildRoot ?? string.Empty, @"Microsoft\Microsoft.NET.Build.Extensions"); } else { - return Path.Combine(DotNetRoot!, "sdk", SdkVersion!, @"Microsoft\Microsoft.NET.Build.Extensions"); + return Path.Combine(DotNetRoot, "sdk", SdkVersion, @"Microsoft\Microsoft.NET.Build.Extensions"); } } } - public void AddTestEnvironmentVariables(IDictionary environment) + public void AddTestEnvironmentVariables(IDictionary environment) { if (ShouldUseFullFrameworkMSBuild) { - string sdksPath = Path.Combine(DotNetRoot!, "sdk", SdkVersion!, "Sdks"); + string sdksPath = Path.Combine(DotNetRoot, "sdk", SdkVersion, "Sdks"); // Use stage 2 MSBuild SDK resolver - environment["MSBUILDADDITIONALSDKRESOLVERSFOLDER"] = SdkResolverPath; + if(SdkResolverPath is not null) + { + environment["MSBUILDADDITIONALSDKRESOLVERSFOLDER"] = SdkResolverPath; + } // Avoid using stage 0 dotnet install dir environment["DOTNET_MSBUILD_SDK_RESOLVER_CLI_DIR"] = ""; @@ -165,7 +172,10 @@ public void AddTestEnvironmentVariables(IDictionary environment if (!string.IsNullOrEmpty(MicrosoftNETBuildExtensionsPathOverride)) { var microsoftNETBuildExtensionsPath = GetMicrosoftNETBuildExtensionsPath(); - environment["MicrosoftNETBuildExtensionsTargets"] = Path.Combine(microsoftNETBuildExtensionsPath!, "Microsoft.NET.Build.Extensions.targets"); + if (microsoftNETBuildExtensionsPath is not null) + { + environment["MicrosoftNETBuildExtensionsTargets"] = Path.Combine(microsoftNETBuildExtensionsPath, "Microsoft.NET.Build.Extensions.targets"); + } if (UsingFullMSBuildWithoutExtensionsTargets()) { @@ -185,7 +195,7 @@ public void AddTestEnvironmentVariables(IDictionary environment environment.Add("DOTNET_ROOT(x86)", DotNetRoot); } - if (!string.IsNullOrEmpty(CliHomePath)) + if (CliHomePath is not null && !string.IsNullOrEmpty(CliHomePath)) { environment.Add("DOTNET_CLI_HOME", CliHomePath); } @@ -229,13 +239,13 @@ private SdkCommandSpec CreateCommand(params string[] args) ret.Arguments = newArgs; } - TestContext.Current.AddTestEnvironmentVariables(ret.Environment!); + TestContext.Current.AddTestEnvironmentVariables(ret.Environment); return ret; } private static string GetDotnetHostPath(string? dotnetRoot) - => Path.Combine(dotnetRoot!, "dotnet" + Constants.ExeSuffix); + => Path.Combine(dotnetRoot ?? string.Empty, "dotnet" + Constants.ExeSuffix); public static ToolsetInfo Create(string? repoRoot, string? repoArtifactsDir, string configuration, TestCommandLine commandLine) { @@ -252,9 +262,9 @@ public static ToolsetInfo Create(string? repoRoot, string? repoArtifactsDir, str dotnetRoot = Path.GetDirectoryName(commandLine.DotnetHostPath); hostNotFoundReason = "Command line argument -dotnetPath is incorrect."; } - else if (repoRoot != null) + else if (repoRoot != null && repoArtifactsDir is not null) { - dotnetRoot = Path.Combine(repoArtifactsDir!, "bin", "redist", configuration, "dotnet"); + dotnetRoot = Path.Combine(repoArtifactsDir, "bin", "redist", configuration, "dotnet"); hostNotFoundReason = "Is 'redist.csproj' built?"; } else if (!string.IsNullOrEmpty(dotnetInstallDirFromEnvironment)) @@ -276,7 +286,7 @@ public static ToolsetInfo Create(string? repoRoot, string? repoArtifactsDir, str } var dotnetHost = GetDotnetHostPath(dotnetRoot); - if (!File.Exists(dotnetHost)) + if (dotnetRoot is null || !File.Exists(dotnetHost)) { throw new FileNotFoundException($"Host '{dotnetHost}' not found. {hostNotFoundReason}"); } @@ -304,20 +314,20 @@ public static ToolsetInfo Create(string? repoRoot, string? repoArtifactsDir, str { ret.MicrosoftNETBuildExtensionsPathOverride = Path.GetDirectoryName(microsoftNETBuildExtensionsTargetsFromEnvironment); } - else if (repoRoot != null && ret.ShouldUseFullFrameworkMSBuild) + else if (repoRoot != null && ret.ShouldUseFullFrameworkMSBuild && repoArtifactsDir is not null) { // Find path to Microsoft.NET.Build.Extensions for full framework - string sdksPath = Path.Combine(repoArtifactsDir!, "bin", configuration, "Sdks"); + string sdksPath = Path.Combine(repoArtifactsDir, "bin", configuration, "Sdks"); var buildExtensionsSdkPath = Path.Combine(sdksPath, "Microsoft.NET.Build.Extensions"); ret.MicrosoftNETBuildExtensionsPathOverride = Path.Combine(buildExtensionsSdkPath, "msbuildExtensions", "Microsoft", "Microsoft.NET.Build.Extensions"); } if (ret.ShouldUseFullFrameworkMSBuild) { - if (repoRoot != null) + if (repoRoot != null && repoArtifactsDir is not null) { // Find path to MSBuildSdkResolver for full framework - ret.SdkResolverPath = Path.Combine(repoArtifactsDir!, "bin", "Microsoft.DotNet.MSBuildSdkResolver", configuration, "net472", "SdkResolvers"); + ret.SdkResolverPath = Path.Combine(repoArtifactsDir, "bin", "Microsoft.DotNet.MSBuildSdkResolver", configuration, "net472", "SdkResolvers"); } else if (!string.IsNullOrWhiteSpace(commandLine.MsbuildAdditionalSdkResolverFolder)) { @@ -333,9 +343,9 @@ public static ToolsetInfo Create(string? repoRoot, string? repoArtifactsDir, str } } - if (repoRoot != null) + if (repoRoot != null && repoArtifactsDir is not null) { - ret.CliHomePath = Path.Combine(repoArtifactsDir!, "tmp", configuration); + ret.CliHomePath = Path.Combine(repoArtifactsDir, "tmp", configuration); } return ret; @@ -356,7 +366,7 @@ public static bool TryResolveCommand(string command, out string? fullExePath) { pathSplitChar = ';'; extensions = extensions - .Concat(Environment.GetEnvironmentVariable("PATHEXT")?.Split(pathSplitChar)!) + .Concat(Environment.GetEnvironmentVariable("PATHEXT")?.Split(pathSplitChar) ?? Array.Empty()) .ToArray(); } else @@ -365,7 +375,7 @@ public static bool TryResolveCommand(string command, out string? fullExePath) } var paths = Environment.GetEnvironmentVariable("PATH")?.Split(pathSplitChar); - string? result = extensions.SelectMany(ext => paths?.Select(p => Path.Combine(p, command + ext))!) + string? result = extensions.SelectMany(ext => paths?.Select(p => Path.Combine(p, command + ext)) ?? Array.Empty()) .FirstOrDefault(File.Exists); if (result == null) @@ -384,15 +394,15 @@ private bool UsingFullMSBuildWithoutExtensionsTargets() return false; } string? fullMSBuildDirectory = Path.GetDirectoryName(FullFrameworkMSBuildPath); - string extensionsImportAfterPath = Path.Combine(fullMSBuildDirectory!, "..", "Microsoft.Common.targets", "ImportAfter", "Microsoft.NET.Build.Extensions.targets"); + string extensionsImportAfterPath = Path.Combine(fullMSBuildDirectory ?? string.Empty, "..", "Microsoft.Common.targets", "ImportAfter", "Microsoft.NET.Build.Extensions.targets"); return !File.Exists(extensionsImportAfterPath); } internal static IEnumerable<(string versionPropertyName, string version)> GetPackageVersionProperties() => typeof(ToolsetInfo).Assembly .GetCustomAttributes() - .Where(a => a.Key.EndsWith("PackageVersion")) - .Select(a => (a.Key, a.Value))!; + .Where(a => a.Key is not null && a.Key.EndsWith("PackageVersion")) + .Select(a => (a.Key ?? string.Empty, a.Value ?? string.Empty)); private static readonly Lazy _NewtonsoftJsonPackageVersion = new Lazy(() => GetPackageVersionProperties().Single(p => p.versionPropertyName == "NewtonsoftJsonPackageVersion").version); diff --git a/test/Microsoft.NET.TestFramework/Utilities/PeReaderUtils.cs b/test/Microsoft.NET.TestFramework/Utilities/PeReaderUtils.cs index b5e803d9ebc7..2ae00a9eec78 100644 --- a/test/Microsoft.NET.TestFramework/Utilities/PeReaderUtils.cs +++ b/test/Microsoft.NET.TestFramework/Utilities/PeReaderUtils.cs @@ -13,10 +13,10 @@ public static bool IsCrossgened(this PEReader peReader) const int CROSSGEN_FLAG = 4; bool isCrossgened = false; - if (peReader.HasMetadata) + if (peReader.HasMetadata && peReader.PEHeaders.CorHeader is not null) { // 4 is the magic numbers that is set in the CLR header's flags when crossgened. - isCrossgened = ((int)peReader.PEHeaders.CorHeader?.Flags! & CROSSGEN_FLAG) == CROSSGEN_FLAG; + isCrossgened = ((int)peReader.PEHeaders.CorHeader.Flags & CROSSGEN_FLAG) == CROSSGEN_FLAG; } return isCrossgened; @@ -142,7 +142,7 @@ private static List GetFixedStringArguments(MetadataReader reader, Custo if (signatureTypeCode == SignatureTypeCode.String) { // Custom attribute constructor must take only strings - arguments.Add(valueReader.ReadSerializedString()!); + arguments.Add(valueReader.ReadSerializedString() ?? string.Empty); } } diff --git a/test/Microsoft.NET.TestFramework/Utilities/ProjectModification.cs b/test/Microsoft.NET.TestFramework/Utilities/ProjectModification.cs index d71ea0f8f312..7e3e9181568e 100644 --- a/test/Microsoft.NET.TestFramework/Utilities/ProjectModification.cs +++ b/test/Microsoft.NET.TestFramework/Utilities/ProjectModification.cs @@ -7,7 +7,11 @@ public static class ProjectModification { public static void AddDisplayMessageToProject(XDocument project, string beforeTargets) { - XNamespace ns = project.Root!.Name.Namespace; + if (project.Root is null) + { + throw new InvalidOperationException($"The project file '{project}' does not have a root element."); + } + XNamespace ns = project.Root.Name.Namespace; XElement target = new(ns + "Target", new XAttribute("Name", "DisplayMessages"), new XAttribute("BeforeTargets", beforeTargets)); diff --git a/test/dotnet-new.Tests/CommonTemplatesTests.cs b/test/dotnet-new.Tests/CommonTemplatesTests.cs index 8b8aa8c69a0c..27bd96e1cfea 100644 --- a/test/dotnet-new.Tests/CommonTemplatesTests.cs +++ b/test/dotnet-new.Tests/CommonTemplatesTests.cs @@ -48,7 +48,7 @@ public CommonTemplatesTests(SharedHomeDirectory fixture, ITestOutputHelper log) [InlineData("MSBuild Directory.Build.targets file", "buildtargets", new[] { "--inherit" })] public async Task AllCommonItemsCreate(string expectedTemplateName, string templateShortName, string[]? args) { - Dictionary environmentUnderTest = new() { ["DOTNET_NOLOGO"] = false.ToString() }; + Dictionary environmentUnderTest = new() { ["DOTNET_NOLOGO"] = false.ToString() }; TestContext.Current.AddTestEnvironmentVariables(environmentUnderTest); string itemName = expectedTemplateName.Replace(' ', '-').Replace('.', '-'); @@ -65,7 +65,7 @@ public async Task AllCommonItemsCreate(string expectedTemplateName, string templ DoNotPrependTemplateNameToScenarioName = true, UniqueFor = expectedTemplateName.Equals("NuGet Config") ? UniqueForOption.OsPlatform : null, } - .WithCustomEnvironment(environmentUnderTest!) + .WithCustomEnvironment(environmentUnderTest) .WithCustomScrubbers( ScrubbersDefinition.Empty .AddScrubber(sb => sb.UnixifyNewlines(), "out") @@ -193,7 +193,7 @@ public async Task AotVariants(string name, string language) string finalProjectName = Path.Combine(projectDir, $"{projName}.{extension}"); Dictionary environmentUnderTest = new() { ["DOTNET_NOLOGO"] = false.ToString() }; - TestContext.Current.AddTestEnvironmentVariables(environmentUnderTest!); + TestContext.Current.AddTestEnvironmentVariables(environmentUnderTest); TemplateVerifierOptions options = new TemplateVerifierOptions(templateName: name) { @@ -388,7 +388,7 @@ public async Task FeaturesSupport( string finalProjectName = Path.Combine(projectDir, $"{projName}.{extension}"); Dictionary environmentUnderTest = new() { ["DOTNET_NOLOGO"] = false.ToString() }; - TestContext.Current.AddTestEnvironmentVariables(environmentUnderTest!); + TestContext.Current.AddTestEnvironmentVariables(environmentUnderTest); TemplateVerifierOptions options = new TemplateVerifierOptions(templateName: name) { diff --git a/test/dotnet-new.Tests/DotnetClassTemplateTests.cs b/test/dotnet-new.Tests/DotnetClassTemplateTests.cs index 812e7f8720a7..1d1817c5f359 100644 --- a/test/dotnet-new.Tests/DotnetClassTemplateTests.cs +++ b/test/dotnet-new.Tests/DotnetClassTemplateTests.cs @@ -47,7 +47,7 @@ public async Task DotnetCSharpClassTemplatesTest( string targetFramework = "") { // prevents logging a welcome message from sdk installation - Dictionary environmentUnderTest = new() { ["DOTNET_NOLOGO"] = false.ToString() }; + Dictionary environmentUnderTest = new() { ["DOTNET_NOLOGO"] = false.ToString() }; TestContext.Current.AddTestEnvironmentVariables(environmentUnderTest); string folderName = GetFolderName(templateShortName, langVersion, targetFramework); @@ -75,7 +75,7 @@ public async Task DotnetCSharpClassTemplatesTest( OutputDirectory = workingDir, EnsureEmptyOutputDirectory = false } - .WithCustomEnvironment(environmentUnderTest!) + .WithCustomEnvironment(environmentUnderTest) .WithCustomScrubbers( ScrubbersDefinition.Empty .AddScrubber((path, content) => @@ -128,7 +128,7 @@ public async Task DotnetVisualBasicClassTemplatesTest( { // prevents logging a welcome message from sdk installation Dictionary environmentUnderTest = new() { ["DOTNET_NOLOGO"] = false.ToString() }; - TestContext.Current.AddTestEnvironmentVariables(environmentUnderTest!); + TestContext.Current.AddTestEnvironmentVariables(environmentUnderTest); string folderName = GetFolderName(templateShortName, langVersion, targetFramework); string workingDir = CreateTemporaryFolder($"{nameof(DotnetVisualBasicClassTemplatesTest)}.{folderName}"); diff --git a/test/dotnet-new.Tests/TemplateEngineSamplesTest.cs b/test/dotnet-new.Tests/TemplateEngineSamplesTest.cs index 525a3e7ff848..2783789100df 100644 --- a/test/dotnet-new.Tests/TemplateEngineSamplesTest.cs +++ b/test/dotnet-new.Tests/TemplateEngineSamplesTest.cs @@ -45,7 +45,7 @@ public async Task TemplateEngineSamplesProjectTest( string caseDescription) { _log.LogInformation($"Template with {caseDescription}"); - Dictionary environmentUnderTest = new() { ["DOTNET_NOLOGO"] = false.ToString() }; + Dictionary environmentUnderTest = new() { ["DOTNET_NOLOGO"] = false.ToString() }; TestContext.Current.AddTestEnvironmentVariables(environmentUnderTest); FileExtensions.AddTextExtension(".cshtml"); @@ -60,7 +60,7 @@ public async Task TemplateEngineSamplesProjectTest( DoNotPrependCallerMethodNameToScenarioName = true, ScenarioName = $"{folderName.Substring(folderName.IndexOf("-") + 1)}{GetScenarioName(arguments)}" } - .WithCustomEnvironment(environmentUnderTest!) + .WithCustomEnvironment(environmentUnderTest) .WithCustomScrubbers( ScrubbersDefinition.Empty .AddScrubber(sb => sb.Replace(DateTime.Now.ToString("MM/dd/yyyy"), "**/**/****"))); From 12ee19961b559d223062a10255d8aef2cd9a7334 Mon Sep 17 00:00:00 2001 From: Jason Zhai Date: Sun, 8 Dec 2024 23:50:27 -0800 Subject: [PATCH 4/6] Remove '!' operators --- .../EndToEndTests.cs | 8 ++++---- .../Microsoft.NET.TestFramework/Commands/DotnetCommand.cs | 4 ++-- test/Microsoft.NET.TestFramework/OutputPathCalculator.cs | 2 +- test/Microsoft.NET.TestFramework/TestAsset.cs | 4 ++-- test/Microsoft.NET.TestFramework/TestAssetsManager.cs | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/test/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs b/test/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs index 340723c0e1c5..7d00e74e20a2 100644 --- a/test/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs +++ b/test/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs @@ -328,14 +328,14 @@ public async Task EndToEnd_NoAPI_ProjectType(string projectType, bool addPackage (string? packagePath, string? packageVersion) = ToolsetUtils.GetContainersPackagePath(); - new DotnetCommand(_testOutput, "nuget", "add", "source", Path.GetDirectoryName(packagePath), "--name", "local-temp") + new DotnetCommand(_testOutput, "nuget", "add", "source", Path.GetDirectoryName(packagePath) ?? string.Empty, "--name", "local-temp") .WithEnvironmentVariable("NUGET_PACKAGES", privateNuGetAssets.FullName) .WithWorkingDirectory(newProjectDir.FullName) .Execute() .Should().Pass(); // Add package to the project - new DotnetCommand(_testOutput, "add", "package", "Microsoft.NET.Build.Containers", "-f", ToolsetInfo.CurrentTargetFramework, "-v", packageVersion) + new DotnetCommand(_testOutput, "add", "package", "Microsoft.NET.Build.Containers", "-f", ToolsetInfo.CurrentTargetFramework, "-v", packageVersion ?? string.Empty) .WithEnvironmentVariable("NUGET_PACKAGES", privateNuGetAssets.FullName) .WithWorkingDirectory(newProjectDir.FullName) .Execute() @@ -499,14 +499,14 @@ public void EndToEnd_NoAPI_Console() (string? packagePath, string? packageVersion) = ToolsetUtils.GetContainersPackagePath(); - new DotnetCommand(_testOutput, "nuget", "add", "source", Path.GetDirectoryName(packagePath), "--name", "local-temp") + new DotnetCommand(_testOutput, "nuget", "add", "source", Path.GetDirectoryName(packagePath) ?? string.Empty, "--name", "local-temp") .WithEnvironmentVariable("NUGET_PACKAGES", privateNuGetAssets.FullName) .WithWorkingDirectory(newProjectDir.FullName) .Execute() .Should().Pass(); // Add package to the project - new DotnetCommand(_testOutput, "add", "package", "Microsoft.NET.Build.Containers", "-f", ToolsetInfo.CurrentTargetFramework, "-v", packageVersion) + new DotnetCommand(_testOutput, "add", "package", "Microsoft.NET.Build.Containers", "-f", ToolsetInfo.CurrentTargetFramework, "-v", packageVersion ?? string.Empty) .WithEnvironmentVariable("NUGET_PACKAGES", privateNuGetAssets.FullName) .WithWorkingDirectory(newProjectDir.FullName) .Execute() diff --git a/test/Microsoft.NET.TestFramework/Commands/DotnetCommand.cs b/test/Microsoft.NET.TestFramework/Commands/DotnetCommand.cs index bde70471e2a3..8122ca293148 100644 --- a/test/Microsoft.NET.TestFramework/Commands/DotnetCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/DotnetCommand.cs @@ -5,9 +5,9 @@ namespace Microsoft.NET.TestFramework.Commands { public class DotnetCommand : TestCommand { - public DotnetCommand(ITestOutputHelper log, params string?[] args) : base(log) + public DotnetCommand(ITestOutputHelper log, params string[] args) : base(log) { - Arguments.AddRange(args!); + Arguments.AddRange(args); } protected override SdkCommandSpec CreateCommand(IEnumerable args) diff --git a/test/Microsoft.NET.TestFramework/OutputPathCalculator.cs b/test/Microsoft.NET.TestFramework/OutputPathCalculator.cs index 26b60fe70b55..86642807956f 100644 --- a/test/Microsoft.NET.TestFramework/OutputPathCalculator.cs +++ b/test/Microsoft.NET.TestFramework/OutputPathCalculator.cs @@ -236,7 +236,7 @@ public string GetPublishDirectory(string? targetFramework = null, string configu if (IncludeProjectNameInArtifactsPaths) { - return Path.Combine(ArtifactsPath ?? string.Empty, "publish", Path.GetFileNameWithoutExtension(ProjectPath)!, pivot); + return Path.Combine(ArtifactsPath ?? string.Empty, "publish", Path.GetFileNameWithoutExtension(ProjectPath) ?? string.Empty, pivot); } else { diff --git a/test/Microsoft.NET.TestFramework/TestAsset.cs b/test/Microsoft.NET.TestFramework/TestAsset.cs index 062d69680301..1e28e46931e9 100644 --- a/test/Microsoft.NET.TestFramework/TestAsset.cs +++ b/test/Microsoft.NET.TestFramework/TestAsset.cs @@ -133,7 +133,7 @@ public TestAsset ReplacePackageVersionVariable(string targetName, string targetV { if (project.Root is not null) { - var ns = project.Root!.Name.Namespace; + var ns = project.Root.Name.Namespace; foreach (var PropertyName in PropertyNames) { var packageReferencesToUpdate = @@ -234,7 +234,7 @@ public TestAsset WithProjectChanges(Action xmlAction) { FindProjectFiles(); } - foreach (var projectFile in _projectFiles!) + foreach (var projectFile in _projectFiles ?? new()) { var project = XDocument.Load(projectFile); diff --git a/test/Microsoft.NET.TestFramework/TestAssetsManager.cs b/test/Microsoft.NET.TestFramework/TestAssetsManager.cs index 2546ab0a5709..bcff9b50598c 100644 --- a/test/Microsoft.NET.TestFramework/TestAssetsManager.cs +++ b/test/Microsoft.NET.TestFramework/TestAssetsManager.cs @@ -110,7 +110,7 @@ public TestAsset CreateTestProjects( foreach (var testProject in testProjects) { - new DotnetCommand(Log, "sln", "add", testProject.Name) + new DotnetCommand(Log, "sln", "add", testProject.Name?? string.Empty) .WithWorkingDirectory(testDestinationDirectory) .Execute() .Should() From 2709f62e1cac47409a220212763f0eee067e2133 Mon Sep 17 00:00:00 2001 From: Jason Zhai Date: Thu, 12 Dec 2024 22:25:28 -0800 Subject: [PATCH 5/6] Fix build failed due to enabling nullable --- .../Assertions/CommandResultAssertions.cs | 28 +++++++++---------- .../Commands/MSBuildCommand.cs | 2 +- .../Commands/PublishCommand.cs | 2 +- .../NuGetTransientErrorDetector.cs | 4 +-- .../OutputPathCalculator.cs | 2 +- .../ProjectConstruction/TestProject.cs | 8 +++--- .../TestAssetsManager.cs | 5 +++- .../ToolsetInfo.cs | 4 +-- .../DotnetNewArgumentsTests.cs | 2 +- test/dotnet-new.Tests/DotnetNewSearchTests.cs | 2 +- .../MsBuildFileSetFactoryTest.cs | 4 +-- 11 files changed, 33 insertions(+), 30 deletions(-) diff --git a/test/Microsoft.NET.TestFramework/Assertions/CommandResultAssertions.cs b/test/Microsoft.NET.TestFramework/Assertions/CommandResultAssertions.cs index abd700a429af..a7220a60eb89 100644 --- a/test/Microsoft.NET.TestFramework/Assertions/CommandResultAssertions.cs +++ b/test/Microsoft.NET.TestFramework/Assertions/CommandResultAssertions.cs @@ -47,19 +47,19 @@ public AndConstraint HaveStdOut() public AndConstraint HaveStdOut(string expectedOutput) { - Execute.Assertion.ForCondition(_commandResult.StdOut.Equals(expectedOutput, StringComparison.Ordinal)) + Execute.Assertion.ForCondition(_commandResult.StdOut is not null &&_commandResult.StdOut.Equals(expectedOutput, StringComparison.Ordinal)) .FailWith(AppendDiagnosticsTo($"Command did not output with Expected Output. Expected: {expectedOutput}")); return new AndConstraint(this); } public AndConstraint HaveStdOutContaining(string pattern) { - Execute.Assertion.ForCondition(_commandResult.StdOut.Contains(pattern)) + Execute.Assertion.ForCondition(_commandResult.StdOut is not null && _commandResult.StdOut.Contains(pattern)) .FailWith(AppendDiagnosticsTo($"The command output did not contain expected result: {pattern}{Environment.NewLine}")); return new AndConstraint(this); } - public AndConstraint HaveStdOutContaining(Func predicate, string description = "") + public AndConstraint HaveStdOutContaining(Func predicate, string description = "") { Execute.Assertion.ForCondition(predicate(_commandResult.StdOut)) .FailWith(AppendDiagnosticsTo($"The command output did not contain expected result: {description} {Environment.NewLine}")); @@ -68,7 +68,7 @@ public AndConstraint HaveStdOutContaining(Func NotHaveStdOutContaining(string pattern, string[]? ignoredPatterns = null) { - string filteredStdOut = _commandResult.StdOut; + string filteredStdOut = _commandResult.StdOut ?? string.Empty; if (ignoredPatterns != null && ignoredPatterns.Length > 0) { foreach (var ignoredPattern in ignoredPatterns) @@ -88,7 +88,7 @@ public AndConstraint NotHaveStdOutContaining(string pat public AndConstraint HaveStdOutContainingIgnoreSpaces(string pattern) { - string commandResultNoSpaces = _commandResult.StdOut.Replace(" ", ""); + string commandResultNoSpaces = _commandResult.StdOut?.Replace(" ", "") ?? string.Empty; Execute.Assertion .ForCondition(commandResultNoSpaces.Contains(pattern)) @@ -99,21 +99,21 @@ public AndConstraint HaveStdOutContainingIgnoreSpaces(s public AndConstraint HaveStdOutContainingIgnoreCase(string pattern) { - Execute.Assertion.ForCondition(_commandResult.StdOut.IndexOf(pattern, StringComparison.OrdinalIgnoreCase) >= 0) + Execute.Assertion.ForCondition(_commandResult.StdOut is not null && _commandResult.StdOut.IndexOf(pattern, StringComparison.OrdinalIgnoreCase) >= 0) .FailWith(AppendDiagnosticsTo($"The command output did not contain expected result (ignoring case): {pattern}{Environment.NewLine}")); return new AndConstraint(this); } public AndConstraint HaveStdOutMatching(string pattern, RegexOptions options = RegexOptions.None) { - Execute.Assertion.ForCondition(Regex.Match(_commandResult.StdOut, pattern, options).Success) + Execute.Assertion.ForCondition(Regex.Match(_commandResult.StdOut ?? string.Empty, pattern, options).Success) .FailWith(AppendDiagnosticsTo($"Matching the command output failed. Pattern: {pattern}{Environment.NewLine}")); return new AndConstraint(this); } public AndConstraint NotHaveStdOutMatching(string pattern, RegexOptions options = RegexOptions.None) { - Execute.Assertion.ForCondition(!Regex.Match(_commandResult.StdOut, pattern, options).Success) + Execute.Assertion.ForCondition(!Regex.Match(_commandResult.StdOut ?? string.Empty, pattern, options).Success) .FailWith(AppendDiagnosticsTo($"The command output matched a pattern it should not have. Pattern: {pattern}{Environment.NewLine}")); return new AndConstraint(this); } @@ -127,22 +127,22 @@ public AndConstraint HaveStdErr() public AndConstraint HaveStdErr(string expectedOutput) { - Execute.Assertion.ForCondition(_commandResult.StdErr.Equals(expectedOutput, StringComparison.Ordinal)) + Execute.Assertion.ForCondition(_commandResult.StdErr is not null && _commandResult.StdErr.Equals(expectedOutput, StringComparison.Ordinal)) .FailWith(AppendDiagnosticsTo($"Command did not output the expected output to StdErr.{Environment.NewLine}Expected: {expectedOutput}{Environment.NewLine}Actual: {_commandResult.StdErr}")); return new AndConstraint(this); } public AndConstraint HaveStdErrContaining(string pattern) { - Execute.Assertion.ForCondition(_commandResult.StdErr.Contains(pattern)) + Execute.Assertion.ForCondition(_commandResult.StdErr is not null && _commandResult.StdErr.Contains(pattern)) .FailWith(AppendDiagnosticsTo($"The command error output did not contain expected result: {pattern}{Environment.NewLine}")); return new AndConstraint(this); } public AndConstraint HaveStdErrContainingOnce(string pattern) { - var lines = _commandResult.StdErr.Split(new[] { Environment.NewLine }, StringSplitOptions.None); - var matchingLines = lines.Where(line => line.Contains(pattern)).Count(); + var lines = _commandResult.StdErr?.Split(new[] { Environment.NewLine }, StringSplitOptions.None); + var matchingLines = lines?.Where(line => line.Contains(pattern)).Count(); Execute.Assertion.ForCondition(matchingLines == 0) .FailWith(AppendDiagnosticsTo($"The command error output did not contain expected result: {pattern}{Environment.NewLine}")); Execute.Assertion.ForCondition(matchingLines != 1) @@ -152,14 +152,14 @@ public AndConstraint HaveStdErrContainingOnce(string pa public AndConstraint NotHaveStdErrContaining(string pattern) { - Execute.Assertion.ForCondition(!_commandResult.StdErr.Contains(pattern)) + Execute.Assertion.ForCondition(_commandResult.StdErr is not null && !_commandResult.StdErr.Contains(pattern)) .FailWith(AppendDiagnosticsTo($"The command error output contained a result it should not have contained: {pattern}{Environment.NewLine}")); return new AndConstraint(this); } public AndConstraint HaveStdErrMatching(string pattern, RegexOptions options = RegexOptions.None) { - Execute.Assertion.ForCondition(Regex.Match(_commandResult.StdErr, pattern, options).Success) + Execute.Assertion.ForCondition(Regex.Match(_commandResult.StdErr ?? string.Empty, pattern, options).Success) .FailWith(AppendDiagnosticsTo($"Matching the command error output failed. Pattern: {pattern}{Environment.NewLine}")); return new AndConstraint(this); } diff --git a/test/Microsoft.NET.TestFramework/Commands/MSBuildCommand.cs b/test/Microsoft.NET.TestFramework/Commands/MSBuildCommand.cs index bcc8da92bd38..9ccfb366f631 100644 --- a/test/Microsoft.NET.TestFramework/Commands/MSBuildCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/MSBuildCommand.cs @@ -70,7 +70,7 @@ internal static string FindProjectFile(ref string projectRootPath, string? relat public virtual DirectoryInfo GetOutputDirectory(string? targetFramework = null, string configuration = "Debug", string? runtimeIdentifier = null, string? platform = null) { - if (TestAsset != null && platform is not null) + if (TestAsset != null) { return new DirectoryInfo(OutputPathCalculator.FromProject(ProjectFile, TestAsset).GetOutputDirectory(targetFramework, configuration, runtimeIdentifier, platform)); } diff --git a/test/Microsoft.NET.TestFramework/Commands/PublishCommand.cs b/test/Microsoft.NET.TestFramework/Commands/PublishCommand.cs index b9c19be1007a..d03d788fb37d 100644 --- a/test/Microsoft.NET.TestFramework/Commands/PublishCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/PublishCommand.cs @@ -24,7 +24,7 @@ public PublishCommand(TestAsset testAsset, string? relativePathToProject = null) public override DirectoryInfo GetOutputDirectory(string? targetFramework = null, string configuration = "Debug", string? runtimeIdentifier = "", string? platformIdentifier = "") { - if (TestAsset != null && targetFramework is not null) + if (TestAsset != null) { return new DirectoryInfo(OutputPathCalculator.FromProject(ProjectFile, TestAsset).GetPublishDirectory(targetFramework, configuration, runtimeIdentifier, platformIdentifier)); } diff --git a/test/Microsoft.NET.TestFramework/NuGetTransientErrorDetector.cs b/test/Microsoft.NET.TestFramework/NuGetTransientErrorDetector.cs index 444a353636fe..d87b966eb9e0 100644 --- a/test/Microsoft.NET.TestFramework/NuGetTransientErrorDetector.cs +++ b/test/Microsoft.NET.TestFramework/NuGetTransientErrorDetector.cs @@ -13,9 +13,9 @@ public static class NuGetTransientErrorDetector "An error occurred while sending the request" }; - public static bool IsTransientError(string errorMessage) + public static bool IsTransientError(string? errorMessage) { - return errorMessage.Contains("NuGet.targets") && _errorSubstrings.Any(errorMessage.Contains); + return errorMessage is not null && errorMessage.Contains("NuGet.targets") && _errorSubstrings.Any(errorMessage.Contains); } } } diff --git a/test/Microsoft.NET.TestFramework/OutputPathCalculator.cs b/test/Microsoft.NET.TestFramework/OutputPathCalculator.cs index 86642807956f..a01f24a4d349 100644 --- a/test/Microsoft.NET.TestFramework/OutputPathCalculator.cs +++ b/test/Microsoft.NET.TestFramework/OutputPathCalculator.cs @@ -169,7 +169,7 @@ public bool IsMultiTargeted() return !string.IsNullOrEmpty(TargetFrameworks); } - public string GetOutputDirectory(string? targetFramework = null, string configuration = "Debug", string? runtimeIdentifier = "", string platform = "") + public string GetOutputDirectory(string? targetFramework = null, string configuration = "Debug", string? runtimeIdentifier = "", string? platform = "") { if (UseArtifactsOutput) { diff --git a/test/Microsoft.NET.TestFramework/ProjectConstruction/TestProject.cs b/test/Microsoft.NET.TestFramework/ProjectConstruction/TestProject.cs index 91b9f562f1a5..5eff2d08acc5 100644 --- a/test/Microsoft.NET.TestFramework/ProjectConstruction/TestProject.cs +++ b/test/Microsoft.NET.TestFramework/ProjectConstruction/TestProject.cs @@ -55,7 +55,7 @@ public TestProject([CallerMemberName] string? name = null) public bool UseArtifactsOutput { get; set; } - public List ReferencedProjects { get; } = new List(); + public List ReferencedProjects { get; } = new List(); public List References { get; } = new List(); @@ -303,7 +303,7 @@ internal void Create(TestAsset targetTestAsset, string testProjectsSourceFolder, foreach (var referencedProject in ReferencedProjects) { projectReferenceItemGroup.Add(new XElement(ns + "ProjectReference", - new XAttribute("Include", $"../{referencedProject.Name}/{referencedProject.Name}.csproj"))); + new XAttribute("Include", $"../{referencedProject?.Name}/{referencedProject?.Name}.csproj"))); } } @@ -419,7 +419,7 @@ static void Main(string[] args) foreach (var dependency in ReferencedProjects) { - string? safeDependencyName = dependency.Name?.Replace('.', '_'); + string? safeDependencyName = dependency?.Name?.Replace('.', '_'); source += $" Console.WriteLine({safeDependencyName}.{safeDependencyName}Class.Name);" + Environment.NewLine; source += $" Console.WriteLine({safeDependencyName}.{safeDependencyName}Class.List);" + Environment.NewLine; @@ -448,7 +448,7 @@ public class {safeThisName}Class "; foreach (var dependency in ReferencedProjects) { - string? safeDependencyName = dependency.Name?.Replace('.', '_'); + string? safeDependencyName = dependency?.Name?.Replace('.', '_'); source += $" public string {safeDependencyName}Name {{ get {{ return {safeDependencyName}.{safeDependencyName}Class.Name; }} }}" + Environment.NewLine; source += $" public List {safeDependencyName}List {{ get {{ return {safeDependencyName}.{safeDependencyName}Class.List; }} }}" + Environment.NewLine; diff --git a/test/Microsoft.NET.TestFramework/TestAssetsManager.cs b/test/Microsoft.NET.TestFramework/TestAssetsManager.cs index cf3f2fbbc65b..c3061e48eb3c 100644 --- a/test/Microsoft.NET.TestFramework/TestAssetsManager.cs +++ b/test/Microsoft.NET.TestFramework/TestAssetsManager.cs @@ -140,7 +140,10 @@ private TestAsset CreateTestProjectsInDirectory( foreach (var referencedProject in project.ReferencedProjects) { - projectStack.Push(referencedProject); + if(referencedProject is not null) + { + projectStack.Push(referencedProject); + } } } } diff --git a/test/Microsoft.NET.TestFramework/ToolsetInfo.cs b/test/Microsoft.NET.TestFramework/ToolsetInfo.cs index c2174722c544..37c948f719f7 100644 --- a/test/Microsoft.NET.TestFramework/ToolsetInfo.cs +++ b/test/Microsoft.NET.TestFramework/ToolsetInfo.cs @@ -100,7 +100,7 @@ private void InitSdkVersion() throw new Exception("Failed to get dotnet version" + Environment.NewLine + logger.ToString()); } - _sdkVersion = result.StdOut.Trim(); + _sdkVersion = result.StdOut?.Trim(); } finally { @@ -123,7 +123,7 @@ private void InitMSBuildVersion() throw new Exception("Failed to get msbuild version" + Environment.NewLine + logger.ToString()); } - _msbuildVersion = result.StdOut.Split().Last(); + _msbuildVersion = result.StdOut?.Split().Last(); } public string? GetMicrosoftNETBuildExtensionsPath() diff --git a/test/dotnet-new.Tests/DotnetNewArgumentsTests.cs b/test/dotnet-new.Tests/DotnetNewArgumentsTests.cs index 6f8ae07434be..71dbd7b20e97 100644 --- a/test/dotnet-new.Tests/DotnetNewArgumentsTests.cs +++ b/test/dotnet-new.Tests/DotnetNewArgumentsTests.cs @@ -25,7 +25,7 @@ public void ShowsDetailedOutputOnMissedRequiredParam() .Should() .ExitWith(127) .And.HaveStdErrContaining("Required argument missing for option: '-v'") - .And.HaveStdOutContaining(dotnetNewHelpOutput.StdOut); + .And.HaveStdOutContaining(dotnetNewHelpOutput.StdOut ?? string.Empty); } } } diff --git a/test/dotnet-new.Tests/DotnetNewSearchTests.cs b/test/dotnet-new.Tests/DotnetNewSearchTests.cs index 9596ac84b58a..da8369cab4c7 100644 --- a/test/dotnet-new.Tests/DotnetNewSearchTests.cs +++ b/test/dotnet-new.Tests/DotnetNewSearchTests.cs @@ -97,7 +97,7 @@ public void ExamplePrefersMicrosoftPackage(string testCase) IEnumerable> microsoftPackages = tableOutput.Where(row => row[2] == "Microsoft" && row[3].StartsWith("Microsoft", StringComparison.OrdinalIgnoreCase)); IEnumerable installationCommands = microsoftPackages.Select(package => $"new install {package[3].Split(" /")[0]}").ToList(); - bool ContainsOneOfInstallationCommands(string output) => installationCommands.Any(command => output.Contains(command)); + bool ContainsOneOfInstallationCommands(string? output) => installationCommands.Any(command => output is not null && output.Contains(command)); commandResult.Should().HaveStdOutContaining(ContainsOneOfInstallationCommands, "Checks if the output contains one of the expected installation commands"); } diff --git a/test/dotnet-watch.Tests/MsBuildFileSetFactoryTest.cs b/test/dotnet-watch.Tests/MsBuildFileSetFactoryTest.cs index 41b03dbd8876..ca3acea03d11 100644 --- a/test/dotnet-watch.Tests/MsBuildFileSetFactoryTest.cs +++ b/test/dotnet-watch.Tests/MsBuildFileSetFactoryTest.cs @@ -422,7 +422,7 @@ private async Task Evaluate(string projectPath) return result; } - private static string GetTestProjectPath(TestAsset target) => Path.Combine(GetTestProjectDirectory(target), target.TestProject.Name + ".csproj"); + private static string GetTestProjectPath(TestAsset target) => Path.Combine(GetTestProjectDirectory(target), target.TestProject?.Name + ".csproj"); private static string WriteFile(TestAsset testAsset, string name, string contents = "") { @@ -443,6 +443,6 @@ private static string WriteFile(TestDirectory testAsset, string name, string con } private static string GetTestProjectDirectory(TestAsset testAsset) - => Path.Combine(testAsset.Path, testAsset.TestProject.Name); + => Path.Combine(testAsset.Path, testAsset.TestProject?.Name ?? string.Empty); } } From 73049a763d6e7446059d1d069d7e7d4cb72e1610 Mon Sep 17 00:00:00 2001 From: Jason Zhai Date: Sun, 15 Dec 2024 22:14:24 -0800 Subject: [PATCH 6/6] Update the code based on the feedback --- test/HelixTasks/TarGzFileCreateFromDirectory.cs | 2 +- .../Commands/AddReferenceCommand.cs | 2 +- .../Commands/DotnetPublishCommand.cs | 2 +- .../Commands/ListPackageCommand.cs | 2 +- .../Commands/ListReferenceCommand.cs | 2 +- .../Commands/RemoveReferenceCommand.cs | 2 +- .../ProjectConstruction/TestProject.cs | 5 +++-- test/Microsoft.NET.TestFramework/TestAsset.cs | 9 +++++---- test/Microsoft.NET.TestFramework/TestAssetsManager.cs | 2 +- test/Microsoft.NET.TestFramework/ToolsetInfo.cs | 2 +- 10 files changed, 16 insertions(+), 14 deletions(-) diff --git a/test/HelixTasks/TarGzFileCreateFromDirectory.cs b/test/HelixTasks/TarGzFileCreateFromDirectory.cs index 7a6d7d5df391..8629f92e6b23 100644 --- a/test/HelixTasks/TarGzFileCreateFromDirectory.cs +++ b/test/HelixTasks/TarGzFileCreateFromDirectory.cs @@ -33,7 +33,7 @@ public sealed class TarGzFileCreateFromDirectory : ToolTask /// /// An item group of regular expressions for content to exclude from the archive. /// - public ITaskItem[]? ExcludePatterns { get; set; } + public ITaskItem[]? ExcludePatterns { get; set; } public bool IgnoreExitCode { get; set; } diff --git a/test/Microsoft.NET.TestFramework/Commands/AddReferenceCommand.cs b/test/Microsoft.NET.TestFramework/Commands/AddReferenceCommand.cs index f362655c3666..65347841c7e0 100644 --- a/test/Microsoft.NET.TestFramework/Commands/AddReferenceCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/AddReferenceCommand.cs @@ -17,7 +17,7 @@ public override CommandResult Execute(IEnumerable args) { List newArgs = new(); newArgs.Add("add"); - if (_projectName is not null && !string.IsNullOrEmpty(_projectName)) + if (!string.IsNullOrEmpty(_projectName)) { newArgs.Add(_projectName); } diff --git a/test/Microsoft.NET.TestFramework/Commands/DotnetPublishCommand.cs b/test/Microsoft.NET.TestFramework/Commands/DotnetPublishCommand.cs index 52744143f9b4..3d97bfa509a7 100644 --- a/test/Microsoft.NET.TestFramework/Commands/DotnetPublishCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/DotnetPublishCommand.cs @@ -16,7 +16,7 @@ public DotnetPublishCommand(ITestOutputHelper log, params string[] args) : base( protected override SdkCommandSpec CreateCommand(IEnumerable args) { List newArgs = new(args); - if (_runtime is not null && !string.IsNullOrEmpty(_runtime)) + if (!string.IsNullOrEmpty(_runtime)) { newArgs.Add("-r"); newArgs.Add(_runtime); diff --git a/test/Microsoft.NET.TestFramework/Commands/ListPackageCommand.cs b/test/Microsoft.NET.TestFramework/Commands/ListPackageCommand.cs index efb15f9ba2f6..cf0a6717daf2 100644 --- a/test/Microsoft.NET.TestFramework/Commands/ListPackageCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/ListPackageCommand.cs @@ -17,7 +17,7 @@ public override CommandResult Execute(IEnumerable args) { List newArgs = new(); newArgs.Add("list"); - if (_projectName is not null && !string.IsNullOrEmpty(_projectName)) + if (!string.IsNullOrEmpty(_projectName)) { newArgs.Add(_projectName); } diff --git a/test/Microsoft.NET.TestFramework/Commands/ListReferenceCommand.cs b/test/Microsoft.NET.TestFramework/Commands/ListReferenceCommand.cs index 77b938abbc11..5414b6ea08bd 100644 --- a/test/Microsoft.NET.TestFramework/Commands/ListReferenceCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/ListReferenceCommand.cs @@ -17,7 +17,7 @@ public override CommandResult Execute(IEnumerable args) { List newArgs = new(); newArgs.Add("list"); - if (_projectName != null && !string.IsNullOrEmpty(_projectName)) + if (!string.IsNullOrEmpty(_projectName)) { newArgs.Add(_projectName); } diff --git a/test/Microsoft.NET.TestFramework/Commands/RemoveReferenceCommand.cs b/test/Microsoft.NET.TestFramework/Commands/RemoveReferenceCommand.cs index 31b1310835f3..2017398215de 100644 --- a/test/Microsoft.NET.TestFramework/Commands/RemoveReferenceCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/RemoveReferenceCommand.cs @@ -17,7 +17,7 @@ public override CommandResult Execute(IEnumerable args) { List newArgs = new(); newArgs.Add("remove"); - if (_projectName is not null && !string.IsNullOrEmpty(_projectName)) + if (!string.IsNullOrEmpty(_projectName)) { newArgs.Add(_projectName); } diff --git a/test/Microsoft.NET.TestFramework/ProjectConstruction/TestProject.cs b/test/Microsoft.NET.TestFramework/ProjectConstruction/TestProject.cs index 5eff2d08acc5..0c329ae25b30 100644 --- a/test/Microsoft.NET.TestFramework/ProjectConstruction/TestProject.cs +++ b/test/Microsoft.NET.TestFramework/ProjectConstruction/TestProject.cs @@ -160,9 +160,10 @@ internal void Create(TestAsset targetTestAsset, string testProjectsSourceFolder, } var ns = projectXml.Root.Name.Namespace; - if (projectXml.Root.Attribute("Sdk") is not null && ProjectSdk != null) + var sdkAttribute = projectXml.Root.Attribute("Sdk"); + if (sdkAttribute is not null && ProjectSdk != null) { - projectXml.Root.Attribute("Sdk")!.Value = ProjectSdk; + sdkAttribute.Value = ProjectSdk; } var propertyGroup = projectXml.Root?.Elements(ns + "PropertyGroup").First(); diff --git a/test/Microsoft.NET.TestFramework/TestAsset.cs b/test/Microsoft.NET.TestFramework/TestAsset.cs index 1e28e46931e9..b26334ad21a6 100644 --- a/test/Microsoft.NET.TestFramework/TestAsset.cs +++ b/test/Microsoft.NET.TestFramework/TestAsset.cs @@ -138,12 +138,13 @@ public TestAsset ReplacePackageVersionVariable(string targetName, string targetV { var packageReferencesToUpdate = project.Root.Descendants(ns + PropertyName) - .Where(p => p.Attribute("Version") != null && p.Attribute("Version")!.Value.Equals($"$({targetName})", StringComparison.OrdinalIgnoreCase)); - foreach (var packageReference in packageReferencesToUpdate) + .Select(p => p.Attribute("Version")) + .Where(va => va is not null && va.Value.Equals($"$({targetName})", StringComparison.OrdinalIgnoreCase)); + foreach (var versionAttribute in packageReferencesToUpdate) { - if(packageReference.Attribute("Version") is not null) + if(versionAttribute is not null) { - packageReference.Attribute("Version")!.Value = targetValue; + versionAttribute.Value = targetValue; } } } diff --git a/test/Microsoft.NET.TestFramework/TestAssetsManager.cs b/test/Microsoft.NET.TestFramework/TestAssetsManager.cs index c3061e48eb3c..7d7c78c9a5fa 100644 --- a/test/Microsoft.NET.TestFramework/TestAssetsManager.cs +++ b/test/Microsoft.NET.TestFramework/TestAssetsManager.cs @@ -110,7 +110,7 @@ public TestAsset CreateTestProjects( foreach (var testProject in testProjects) { - new DotnetCommand(Log, "sln", "add", testProject.Name?? string.Empty) + new DotnetCommand(Log, "sln", "add", testProject.Name ?? string.Empty) .WithWorkingDirectory(testDestinationDirectory) .Execute() .Should() diff --git a/test/Microsoft.NET.TestFramework/ToolsetInfo.cs b/test/Microsoft.NET.TestFramework/ToolsetInfo.cs index 37c948f719f7..52a9165ad6c2 100644 --- a/test/Microsoft.NET.TestFramework/ToolsetInfo.cs +++ b/test/Microsoft.NET.TestFramework/ToolsetInfo.cs @@ -195,7 +195,7 @@ public void AddTestEnvironmentVariables(IDictionary environment) environment.Add("DOTNET_ROOT(x86)", DotNetRoot); } - if (CliHomePath is not null && !string.IsNullOrEmpty(CliHomePath)) + if (!string.IsNullOrEmpty(CliHomePath)) { environment.Add("DOTNET_CLI_HOME", CliHomePath); }