diff --git a/src/SettingsMigrator/Migrator.cs b/src/SettingsMigrator/Migrator.cs index e1ef63d0eb..4911b71f00 100644 --- a/src/SettingsMigrator/Migrator.cs +++ b/src/SettingsMigrator/Migrator.cs @@ -6,9 +6,9 @@ using System.IO; using System.Xml; -using CommandLineResources = Microsoft.VisualStudio.TestPlatform.SettingsMigrator.Resources.Resources; +using SettingsMigrator; -#nullable disable +using CommandLineResources = Microsoft.VisualStudio.TestPlatform.SettingsMigrator.Resources.Resources; namespace Microsoft.VisualStudio.TestPlatform.SettingsMigrator; @@ -91,7 +91,7 @@ public void Migrate(string oldFilePath, string newFilePath) /// Path to new runsettings. private void MigrateRunSettings(string oldRunSettingsPath, string newRunSettingsPath) { - string testSettingsPath = null; + string? testSettingsPath = null; using XmlTextReader reader = new(oldRunSettingsPath); reader.Namespaces = false; @@ -106,7 +106,7 @@ private void MigrateRunSettings(string oldRunSettingsPath, string newRunSettings testSettingsPath = testSettingsNode.InnerText; } - if (!string.IsNullOrWhiteSpace(testSettingsPath)) + if (!testSettingsPath.IsNullOrWhiteSpace()) { // Expand path relative to runSettings location. if (!Path.IsPathRooted(testSettingsPath)) @@ -153,25 +153,25 @@ private void MigrateTestSettingsNodesToRunSettings(string testSettingsPath, XmlD { var testSettingsNodes = ReadTestSettingsNodes(testSettingsPath); - string testTimeout = null; + string? testTimeout = null; if (testSettingsNodes.Timeout != null && testSettingsNodes.Timeout.Attributes[TestTimeoutAttributeName] != null) { testTimeout = testSettingsNodes.Timeout.Attributes[TestTimeoutAttributeName].Value; } - string runTimeout = null; + string? runTimeout = null; if (testSettingsNodes.Timeout != null && testSettingsNodes.Timeout.Attributes[RunTimeoutAttributeName] != null) { runTimeout = testSettingsNodes.Timeout.Attributes[RunTimeoutAttributeName].Value; } - string parallelTestCount = null; + string? parallelTestCount = null; if (testSettingsNodes.Execution != null && testSettingsNodes.Execution.Attributes[ParallelTestCountAttributeName] != null) { parallelTestCount = testSettingsNodes.Execution.Attributes[ParallelTestCountAttributeName].Value; } - string hostProcessPlatform = null; + string? hostProcessPlatform = null; if (testSettingsNodes.Execution != null && testSettingsNodes.Execution.Attributes[HostProcessPlatformAttributeName] != null) { hostProcessPlatform = testSettingsNodes.Execution.Attributes[HostProcessPlatformAttributeName].Value; @@ -187,7 +187,7 @@ private void MigrateTestSettingsNodesToRunSettings(string testSettingsPath, XmlD AddLegacyNodes(testSettingsNodes, testTimeout, parallelTestCount, hostProcessPlatform, runSettingsXmlDoc); // TestSessionTimeout node. - if (!string.IsNullOrEmpty(runTimeout)) + if (!runTimeout.IsNullOrEmpty()) { AddRunTimeoutNode(runTimeout, runSettingsXmlDoc); } @@ -252,10 +252,15 @@ private void RemoveEmbeddedTestSettings(XmlDocument newXmlDoc) /// parallelTestCount /// hostProcessPlatform /// newXmlDoc - private void AddLegacyNodes(TestSettingsNodes testSettingsNodes, string testTimeout, string parallelTestCount, string hostProcessPlatform, XmlDocument newXmlDoc) + private void AddLegacyNodes(TestSettingsNodes testSettingsNodes, string? testTimeout, string? parallelTestCount, string? hostProcessPlatform, XmlDocument newXmlDoc) { - if (testSettingsNodes.Deployment == null && testSettingsNodes.Script == null && testSettingsNodes.UnitTestConfig == null && - string.IsNullOrEmpty(parallelTestCount) && string.IsNullOrEmpty(testTimeout) && string.IsNullOrEmpty(hostProcessPlatform) && testSettingsNodes.Hosts == null) + if (testSettingsNodes.Deployment == null + && testSettingsNodes.Script == null + && testSettingsNodes.UnitTestConfig == null + && parallelTestCount.IsNullOrEmpty() + && testTimeout.IsNullOrEmpty() + && hostProcessPlatform.IsNullOrEmpty() + && testSettingsNodes.Hosts == null) { return; } @@ -301,25 +306,25 @@ private void AddLegacyNodes(TestSettingsNodes testSettingsNodes, string testTime } // Execution node. - if (testSettingsNodes.UnitTestConfig != null || !string.IsNullOrEmpty(parallelTestCount) || !string.IsNullOrEmpty(testTimeout) || testSettingsNodes.Hosts != null) + if (testSettingsNodes.UnitTestConfig != null || !parallelTestCount.IsNullOrEmpty() || !testTimeout.IsNullOrEmpty() || testSettingsNodes.Hosts != null) { var newExecutionNode = newXmlDoc.CreateNode(XmlNodeType.Element, ExecutionNodeName, null); - if (!string.IsNullOrEmpty(parallelTestCount)) + if (!parallelTestCount.IsNullOrEmpty()) { var paralellAttribute = newXmlDoc.CreateAttribute(ParallelTestCountAttributeName); paralellAttribute.Value = parallelTestCount; newExecutionNode.Attributes.Append(paralellAttribute); } - if (!string.IsNullOrEmpty(hostProcessPlatform)) + if (!hostProcessPlatform.IsNullOrEmpty()) { var hostProcessPlatformAttribute = newXmlDoc.CreateAttribute(HostProcessPlatformAttributeName); hostProcessPlatformAttribute.Value = hostProcessPlatform; newExecutionNode.Attributes.Append(hostProcessPlatformAttribute); } - if (!string.IsNullOrEmpty(testTimeout)) + if (!testTimeout.IsNullOrEmpty()) { var newTimeoutsNode = newXmlDoc.CreateNode(XmlNodeType.Element, TimeoutsNodeName, null); var testtimeoutattribute = newXmlDoc.CreateAttribute(TestTimeoutAttributeName); diff --git a/src/SettingsMigrator/NullableHelpers.cs b/src/SettingsMigrator/NullableHelpers.cs new file mode 100644 index 0000000000..08dfce98e4 --- /dev/null +++ b/src/SettingsMigrator/NullableHelpers.cs @@ -0,0 +1,42 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// +// This code is auto-generated. Changes to this file will be lost! +// This T4 file is copied in various projects because inclusion as link or through shared project +// doesn't allow to generate the C# file locally. If some modification is required, please update +// all instances. +// + +#nullable enable + +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; + +namespace SettingsMigrator; + +internal static class StringUtils +{ + /// + [SuppressMessage("ApiDesign", "RS0030:Do not used banned APIs", Justification = "Replacement API to allow nullable hints for compiler")] + public static bool IsNullOrEmpty([NotNullWhen(returnValue: false)] this string? value) + => string.IsNullOrEmpty(value); + + /// + [SuppressMessage("ApiDesign", "RS0030:Do not used banned APIs", Justification = "Replacement API to allow nullable hints for compiler")] + public static bool IsNullOrWhiteSpace([NotNullWhen(returnValue: false)] this string? value) + => string.IsNullOrWhiteSpace(value); +} + +[SuppressMessage("ApiDesign", "RS0030:Do not used banned APIs", Justification = "Replacement API to allow nullable hints for compiler")] +internal static class TPDebug +{ + /// + [Conditional("DEBUG")] + public static void Assert([DoesNotReturnIf(false)] bool b) + => Debug.Assert(b); + + /// + [Conditional("DEBUG")] + public static void Assert([DoesNotReturnIf(false)] bool b, string message) + => Debug.Assert(b, message); +} diff --git a/src/SettingsMigrator/NullableHelpers.tt b/src/SettingsMigrator/NullableHelpers.tt new file mode 100644 index 0000000000..7e3d8e7270 --- /dev/null +++ b/src/SettingsMigrator/NullableHelpers.tt @@ -0,0 +1,45 @@ +<#@ template debug="true" hostspecific="true" language="C#" #> +<#@ output extension=".cs" #> +<#@ assembly name="System.Core" #> +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// +// This code is auto-generated. Changes to this file will be lost! +// This T4 file is copied in various projects because inclusion as link or through shared project +// doesn't allow to generate the C# file locally. If some modification is required, please update +// all instances. +// + +#nullable enable + +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; + +namespace <#= System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint") #>; + +internal static class StringUtils +{ + /// + [SuppressMessage("ApiDesign", "RS0030:Do not used banned APIs", Justification = "Replacement API to allow nullable hints for compiler")] + public static bool IsNullOrEmpty([NotNullWhen(returnValue: false)] this string? value) + => string.IsNullOrEmpty(value); + + /// + [SuppressMessage("ApiDesign", "RS0030:Do not used banned APIs", Justification = "Replacement API to allow nullable hints for compiler")] + public static bool IsNullOrWhiteSpace([NotNullWhen(returnValue: false)] this string? value) + => string.IsNullOrWhiteSpace(value); +} + +[SuppressMessage("ApiDesign", "RS0030:Do not used banned APIs", Justification = "Replacement API to allow nullable hints for compiler")] +internal static class TPDebug +{ + /// + [Conditional("DEBUG")] + public static void Assert([DoesNotReturnIf(false)] bool b) + => Debug.Assert(b); + + /// + [Conditional("DEBUG")] + public static void Assert([DoesNotReturnIf(false)] bool b, string message) + => Debug.Assert(b, message); +} diff --git a/src/SettingsMigrator/PathResolver.cs b/src/SettingsMigrator/PathResolver.cs index f4c234cdfc..4e627611dd 100644 --- a/src/SettingsMigrator/PathResolver.cs +++ b/src/SettingsMigrator/PathResolver.cs @@ -4,8 +4,6 @@ using System; using System.IO; -#nullable disable - namespace Microsoft.VisualStudio.TestPlatform.SettingsMigrator; /// @@ -20,9 +18,9 @@ public class PathResolver /// /// User inputs /// New file path to create - public string GetTargetPath(string[] args) + public string? GetTargetPath(string[] args) { - string newFilePath = null; + string? newFilePath = null; if (args.Length < 1 || !Path.IsPathRooted(args[0])) { return newFilePath; diff --git a/src/SettingsMigrator/Program.cs b/src/SettingsMigrator/Program.cs index 71003c2006..0a65b64b43 100644 --- a/src/SettingsMigrator/Program.cs +++ b/src/SettingsMigrator/Program.cs @@ -4,9 +4,9 @@ using System; using System.Globalization; -using CommandLineResources = Microsoft.VisualStudio.TestPlatform.SettingsMigrator.Resources.Resources; +using SettingsMigrator; -#nullable disable +using CommandLineResources = Microsoft.VisualStudio.TestPlatform.SettingsMigrator.Resources.Resources; namespace Microsoft.VisualStudio.TestPlatform.SettingsMigrator; @@ -23,9 +23,9 @@ public static class Program public static int Main(string[] args) { var pathResolver = new PathResolver(); - string newFilePath = pathResolver.GetTargetPath(args); + string? newFilePath = pathResolver.GetTargetPath(args); - if (!string.IsNullOrEmpty(newFilePath)) + if (!newFilePath.IsNullOrEmpty()) { string oldFilePath = args[0]; var migrator = new Migrator(); diff --git a/src/SettingsMigrator/PublicAPI/PublicAPI.Shipped.txt b/src/SettingsMigrator/PublicAPI/PublicAPI.Shipped.txt index 26ef994763..706bd9259e 100644 --- a/src/SettingsMigrator/PublicAPI/PublicAPI.Shipped.txt +++ b/src/SettingsMigrator/PublicAPI/PublicAPI.Shipped.txt @@ -1,26 +1,27 @@ +#nullable enable Microsoft.VisualStudio.TestPlatform.SettingsMigrator.Migrator -Microsoft.VisualStudio.TestPlatform.SettingsMigrator.Migrator.Migrate(string oldFilePath, string newFilePath) -> void +Microsoft.VisualStudio.TestPlatform.SettingsMigrator.Migrator.Migrate(string! oldFilePath, string! newFilePath) -> void Microsoft.VisualStudio.TestPlatform.SettingsMigrator.Migrator.Migrator() -> void Microsoft.VisualStudio.TestPlatform.SettingsMigrator.PathResolver -Microsoft.VisualStudio.TestPlatform.SettingsMigrator.PathResolver.GetTargetPath(string[] args) -> string +Microsoft.VisualStudio.TestPlatform.SettingsMigrator.PathResolver.GetTargetPath(string![]! args) -> string? Microsoft.VisualStudio.TestPlatform.SettingsMigrator.PathResolver.PathResolver() -> void Microsoft.VisualStudio.TestPlatform.SettingsMigrator.Program Microsoft.VisualStudio.TestPlatform.SettingsMigrator.TestSettingsNodes -Microsoft.VisualStudio.TestPlatform.SettingsMigrator.TestSettingsNodes.Datacollectors.get -> System.Xml.XmlNodeList +Microsoft.VisualStudio.TestPlatform.SettingsMigrator.TestSettingsNodes.Datacollectors.get -> System.Xml.XmlNodeList? Microsoft.VisualStudio.TestPlatform.SettingsMigrator.TestSettingsNodes.Datacollectors.set -> void -Microsoft.VisualStudio.TestPlatform.SettingsMigrator.TestSettingsNodes.Deployment.get -> System.Xml.XmlNode +Microsoft.VisualStudio.TestPlatform.SettingsMigrator.TestSettingsNodes.Deployment.get -> System.Xml.XmlNode? Microsoft.VisualStudio.TestPlatform.SettingsMigrator.TestSettingsNodes.Deployment.set -> void -Microsoft.VisualStudio.TestPlatform.SettingsMigrator.TestSettingsNodes.Execution.get -> System.Xml.XmlNode +Microsoft.VisualStudio.TestPlatform.SettingsMigrator.TestSettingsNodes.Execution.get -> System.Xml.XmlNode? Microsoft.VisualStudio.TestPlatform.SettingsMigrator.TestSettingsNodes.Execution.set -> void -Microsoft.VisualStudio.TestPlatform.SettingsMigrator.TestSettingsNodes.Hosts.get -> System.Xml.XmlNode +Microsoft.VisualStudio.TestPlatform.SettingsMigrator.TestSettingsNodes.Hosts.get -> System.Xml.XmlNode? Microsoft.VisualStudio.TestPlatform.SettingsMigrator.TestSettingsNodes.Hosts.set -> void -Microsoft.VisualStudio.TestPlatform.SettingsMigrator.TestSettingsNodes.Script.get -> System.Xml.XmlNode +Microsoft.VisualStudio.TestPlatform.SettingsMigrator.TestSettingsNodes.Script.get -> System.Xml.XmlNode? Microsoft.VisualStudio.TestPlatform.SettingsMigrator.TestSettingsNodes.Script.set -> void Microsoft.VisualStudio.TestPlatform.SettingsMigrator.TestSettingsNodes.TestSettingsNodes() -> void -Microsoft.VisualStudio.TestPlatform.SettingsMigrator.TestSettingsNodes.Timeout.get -> System.Xml.XmlNode +Microsoft.VisualStudio.TestPlatform.SettingsMigrator.TestSettingsNodes.Timeout.get -> System.Xml.XmlNode? Microsoft.VisualStudio.TestPlatform.SettingsMigrator.TestSettingsNodes.Timeout.set -> void -Microsoft.VisualStudio.TestPlatform.SettingsMigrator.TestSettingsNodes.UnitTestConfig.get -> System.Xml.XmlNode +Microsoft.VisualStudio.TestPlatform.SettingsMigrator.TestSettingsNodes.UnitTestConfig.get -> System.Xml.XmlNode? Microsoft.VisualStudio.TestPlatform.SettingsMigrator.TestSettingsNodes.UnitTestConfig.set -> void -Microsoft.VisualStudio.TestPlatform.SettingsMigrator.TestSettingsNodes.WebSettings.get -> System.Xml.XmlNode +Microsoft.VisualStudio.TestPlatform.SettingsMigrator.TestSettingsNodes.WebSettings.get -> System.Xml.XmlNode? Microsoft.VisualStudio.TestPlatform.SettingsMigrator.TestSettingsNodes.WebSettings.set -> void -static Microsoft.VisualStudio.TestPlatform.SettingsMigrator.Program.Main(string[] args) -> int \ No newline at end of file +static Microsoft.VisualStudio.TestPlatform.SettingsMigrator.Program.Main(string![]! args) -> int diff --git a/src/SettingsMigrator/SettingsMigrator.csproj b/src/SettingsMigrator/SettingsMigrator.csproj index 148506d5da..f28f01d98a 100644 --- a/src/SettingsMigrator/SettingsMigrator.csproj +++ b/src/SettingsMigrator/SettingsMigrator.csproj @@ -27,6 +27,11 @@ + + True + True + NullableHelpers.tt + True True @@ -44,5 +49,17 @@ + + + + + + NullableHelpers.cs + TextTemplatingFileGenerator + + + + + diff --git a/src/SettingsMigrator/TestSettingsNodes.cs b/src/SettingsMigrator/TestSettingsNodes.cs index c4b53487f6..d2b6ce1934 100644 --- a/src/SettingsMigrator/TestSettingsNodes.cs +++ b/src/SettingsMigrator/TestSettingsNodes.cs @@ -3,8 +3,6 @@ using System.Xml; -#nullable disable - namespace Microsoft.VisualStudio.TestPlatform.SettingsMigrator; /// @@ -12,19 +10,19 @@ namespace Microsoft.VisualStudio.TestPlatform.SettingsMigrator; /// public class TestSettingsNodes { - public XmlNode Deployment { get; set; } + public XmlNode? Deployment { get; set; } - public XmlNode Script { get; set; } + public XmlNode? Script { get; set; } - public XmlNode WebSettings { get; set; } + public XmlNode? WebSettings { get; set; } - public XmlNodeList Datacollectors { get; set; } + public XmlNodeList? Datacollectors { get; set; } - public XmlNode Timeout { get; set; } + public XmlNode? Timeout { get; set; } - public XmlNode UnitTestConfig { get; set; } + public XmlNode? UnitTestConfig { get; set; } - public XmlNode Hosts { get; set; } + public XmlNode? Hosts { get; set; } - public XmlNode Execution { get; set; } + public XmlNode? Execution { get; set; } }