Skip to content

Commit

Permalink
Enable nullables on SettingsMigrator (#3744)
Browse files Browse the repository at this point in the history
  • Loading branch information
Evangelink committed Jun 13, 2022
1 parent fd9d907 commit 39ae335
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 45 deletions.
37 changes: 21 additions & 16 deletions src/SettingsMigrator/Migrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -91,7 +91,7 @@ public void Migrate(string oldFilePath, string newFilePath)
/// <param name="newRunSettingsPath">Path to new runsettings.</param>
private void MigrateRunSettings(string oldRunSettingsPath, string newRunSettingsPath)
{
string testSettingsPath = null;
string? testSettingsPath = null;
using XmlTextReader reader = new(oldRunSettingsPath);
reader.Namespaces = false;

Expand All @@ -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))
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -252,10 +252,15 @@ private void RemoveEmbeddedTestSettings(XmlDocument newXmlDoc)
/// <param name="parallelTestCount">parallelTestCount</param>
/// <param name="hostProcessPlatform">hostProcessPlatform</param>
/// <param name="newXmlDoc">newXmlDoc</param>
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;
}
Expand Down Expand Up @@ -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);
Expand Down
42 changes: 42 additions & 0 deletions src/SettingsMigrator/NullableHelpers.cs
Original file line number Diff line number Diff line change
@@ -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.
// <auto-generated>
// 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.
// </auto-generated>

#nullable enable

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

namespace SettingsMigrator;

internal static class StringUtils
{
/// <inheritdoc cref="string.IsNullOrEmpty(string)"/>
[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);

/// <inheritdoc cref="string.IsNullOrWhiteSpace(string)"/>
[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
{
/// <inheritdoc cref="Debug.Assert(bool)"/>
[Conditional("DEBUG")]
public static void Assert([DoesNotReturnIf(false)] bool b)
=> Debug.Assert(b);

/// <inheritdoc cref="Debug.Assert(bool, string)"/>
[Conditional("DEBUG")]
public static void Assert([DoesNotReturnIf(false)] bool b, string message)
=> Debug.Assert(b, message);
}
45 changes: 45 additions & 0 deletions src/SettingsMigrator/NullableHelpers.tt
Original file line number Diff line number Diff line change
@@ -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.
// <auto-generated>
// 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.
// </auto-generated>

#nullable enable

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

namespace <#= System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint") #>;

internal static class StringUtils
{
/// <inheritdoc cref="string.IsNullOrEmpty(string)"/>
[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);

/// <inheritdoc cref="string.IsNullOrWhiteSpace(string)"/>
[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
{
/// <inheritdoc cref="Debug.Assert(bool)"/>
[Conditional("DEBUG")]
public static void Assert([DoesNotReturnIf(false)] bool b)
=> Debug.Assert(b);

/// <inheritdoc cref="Debug.Assert(bool, string)"/>
[Conditional("DEBUG")]
public static void Assert([DoesNotReturnIf(false)] bool b, string message)
=> Debug.Assert(b, message);
}
6 changes: 2 additions & 4 deletions src/SettingsMigrator/PathResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using System;
using System.IO;

#nullable disable

namespace Microsoft.VisualStudio.TestPlatform.SettingsMigrator;

/// <summary>
Expand All @@ -20,9 +18,9 @@ public class PathResolver
/// </summary>
/// <param name="args">User inputs</param>
/// <returns>New file path to create</returns>
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;
Expand Down
8 changes: 4 additions & 4 deletions src/SettingsMigrator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand Down
23 changes: 12 additions & 11 deletions src/SettingsMigrator/PublicAPI/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
@@ -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
static Microsoft.VisualStudio.TestPlatform.SettingsMigrator.Program.Main(string![]! args) -> int
17 changes: 17 additions & 0 deletions src/SettingsMigrator/SettingsMigrator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup>
<Compile Update="NullableHelpers.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>NullableHelpers.tt</DependentUpon>
</Compile>
<Compile Update="Resources\Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
Expand All @@ -44,5 +49,17 @@
<AdditionalFiles Include="PublicAPI/PublicAPI.Shipped.txt" />
<AdditionalFiles Include="PublicAPI/PublicAPI.Unshipped.txt" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\shared\NullableAttributes.cs" Link="NullableAttributes.cs" />
</ItemGroup>
<ItemGroup>
<None Update="NullableHelpers.tt">
<LastGenOutput>NullableHelpers.cs</LastGenOutput>
<Generator>TextTemplatingFileGenerator</Generator>
</None>
</ItemGroup>
<ItemGroup>
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
</ItemGroup>
<Import Project="$(TestPlatformRoot)scripts\build\TestPlatform.targets" />
</Project>
18 changes: 8 additions & 10 deletions src/SettingsMigrator/TestSettingsNodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,26 @@

using System.Xml;

#nullable disable

namespace Microsoft.VisualStudio.TestPlatform.SettingsMigrator;

/// <summary>
/// Contains the test settings nodes that need to be converted.
/// </summary>
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; }
}

0 comments on commit 39ae335

Please sign in to comment.