-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable Nullable Reference Types in Test Projects #44862
Conversation
@@ -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 | |||
/// </summary> | |||
[Required] | |||
public ITaskItem[] XUnitProjects { get; set; } | |||
public ITaskItem[]? XUnitProjects { get; set; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rainersigwald Have ya'll enabled nullable for any msbuild tasks? Is it possible for these to be null or does MSBuild force them to have a value always? Do they have to be marked Required to not be null?
test/Common/Program.cs
Outdated
@@ -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")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if rather than having the ! here we should have the property getter for TestExecutionDirectory throw if it's null (as it really should never be null). That would potentially remove a lot of these !s that you've added by doing it in one place.
49f596d
to
1676070
Compare
1676070
to
1a475e9
Compare
… null safety based on PR feedback
5d498be
to
8e1c5db
Compare
…i/enable-nullable
9f34fd2
to
2709f62
Compare
@MiYanni @marcpopMSFT would you be able to review these changes? Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall, looks good. Just a few adjustments to be made based on the comments I left. Otherwise, good to go.
|
||
if (!Directory.Exists(SourceDirectory)) | ||
{ | ||
Log.LogError($"SourceDirectory '{SourceDirectory} does not exist."); | ||
Log.LogError($"SourceDirectory '{SourceDirectory}' does not exist."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good find.
@@ -33,7 +33,7 @@ public sealed class TarGzFileCreateFromDirectory : ToolTask | |||
/// <summary> | |||
/// An item group of regular expressions for content to exclude from the archive. | |||
/// </summary> | |||
public ITaskItem[] ExcludePatterns { get; set; } | |||
public ITaskItem[]? ExcludePatterns { get; set; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: trailing space
@@ -17,7 +17,7 @@ public override CommandResult Execute(IEnumerable<string> args) | |||
{ | |||
List<string> newArgs = new(); | |||
newArgs.Add("add"); | |||
if (!string.IsNullOrEmpty(_projectName)) | |||
if (_projectName is not null && !string.IsNullOrEmpty(_projectName)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We've updated the build to no longer warn for the .NET Framework compilation for string.IsNullOrEmpty
so this added check is no longer necessary. See: #45299
@@ -16,7 +16,7 @@ public DotnetPublishCommand(ITestOutputHelper log, params string[] args) : base( | |||
protected override SdkCommandSpec CreateCommand(IEnumerable<string> args) | |||
{ | |||
List<string> newArgs = new(args); | |||
if (!string.IsNullOrEmpty(_runtime)) | |||
if (_runtime is not null && !string.IsNullOrEmpty(_runtime)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No longer necessary.
@@ -17,7 +17,7 @@ public override CommandResult Execute(IEnumerable<string> args) | |||
{ | |||
List<string> newArgs = new(); | |||
newArgs.Add("list"); | |||
if (!string.IsNullOrEmpty(_projectName)) | |||
if (_projectName is not null && !string.IsNullOrEmpty(_projectName)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No longer necessary.
@@ -17,7 +17,7 @@ public override CommandResult Execute(IEnumerable<string> args) | |||
{ | |||
List<string> newArgs = new(); | |||
newArgs.Add("remove"); | |||
if (!string.IsNullOrEmpty(_projectName)) | |||
if (_projectName is not null && !string.IsNullOrEmpty(_projectName)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No longer necessary.
if (ProjectSdk != null) | ||
if (projectXml.Root.Attribute("Sdk") is not null && ProjectSdk != null) | ||
{ | ||
projectXml.Root.Attribute("Sdk").Value = ProjectSdk; | ||
projectXml.Root.Attribute("Sdk")!.Value = ProjectSdk; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be better to do something like this. Not sure it will work exactly like this since I'm not sure of the types used here. Might need to play around with it.
if (ProjectSdk != null) | |
if (projectXml.Root.Attribute("Sdk") is not null && ProjectSdk != null) | |
{ | |
projectXml.Root.Attribute("Sdk").Value = ProjectSdk; | |
projectXml.Root.Attribute("Sdk")!.Value = ProjectSdk; | |
} | |
var sdkAttribute = projectXml.Root.Attribute("Sdk"); | |
if (sdkAttribute is not null && ProjectSdk != null) | |
{ | |
sdkAttribute.Value = ProjectSdk; | |
} |
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; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to the comment above. You might need to set the type on versionAttributesToUpdate
to be explicitly not null for it to resolve in the foreach properly. This avoids checking it is not null twice, since you already filter out the nulls in the Where.
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; | |
} | |
} | |
var versionAttributesToUpdate = | |
project.Root.Descendants(ns + PropertyName) | |
.Select(p => p.Attribute("Version")) | |
.Where(va => va is not null && va.Value.Equals($"$({targetName})", StringComparison.OrdinalIgnoreCase)); | |
foreach (var versionAttribute in versionAttributesToUpdate) | |
{ | |
versionAttribute.Value = targetValue; | |
} |
@@ -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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: missing space
@@ -185,7 +195,7 @@ public void AddTestEnvironmentVariables(IDictionary<string, string> environment) | |||
environment.Add("DOTNET_ROOT(x86)", DotNetRoot); | |||
} | |||
|
|||
if (!string.IsNullOrEmpty(CliHomePath)) | |||
if (CliHomePath is not null && !string.IsNullOrEmpty(CliHomePath)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No longer necessary.
/azp run dotnet-sdk-public-ci,sdk-source-build,sdk-unified-build |
Azure Pipelines successfully started running 3 pipeline(s). |
This PR addresses enabling nullable reference types for the following test projects: