Skip to content

Commit 2709f62

Browse files
committed
Fix build failed due to enabling nullable
1 parent 8b0dee8 commit 2709f62

File tree

11 files changed

+33
-30
lines changed

11 files changed

+33
-30
lines changed

test/Microsoft.NET.TestFramework/Assertions/CommandResultAssertions.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,19 @@ public AndConstraint<CommandResultAssertions> HaveStdOut()
4747

4848
public AndConstraint<CommandResultAssertions> HaveStdOut(string expectedOutput)
4949
{
50-
Execute.Assertion.ForCondition(_commandResult.StdOut.Equals(expectedOutput, StringComparison.Ordinal))
50+
Execute.Assertion.ForCondition(_commandResult.StdOut is not null &&_commandResult.StdOut.Equals(expectedOutput, StringComparison.Ordinal))
5151
.FailWith(AppendDiagnosticsTo($"Command did not output with Expected Output. Expected: {expectedOutput}"));
5252
return new AndConstraint<CommandResultAssertions>(this);
5353
}
5454

5555
public AndConstraint<CommandResultAssertions> HaveStdOutContaining(string pattern)
5656
{
57-
Execute.Assertion.ForCondition(_commandResult.StdOut.Contains(pattern))
57+
Execute.Assertion.ForCondition(_commandResult.StdOut is not null && _commandResult.StdOut.Contains(pattern))
5858
.FailWith(AppendDiagnosticsTo($"The command output did not contain expected result: {pattern}{Environment.NewLine}"));
5959
return new AndConstraint<CommandResultAssertions>(this);
6060
}
6161

62-
public AndConstraint<CommandResultAssertions> HaveStdOutContaining(Func<string, bool> predicate, string description = "")
62+
public AndConstraint<CommandResultAssertions> HaveStdOutContaining(Func<string?, bool> predicate, string description = "")
6363
{
6464
Execute.Assertion.ForCondition(predicate(_commandResult.StdOut))
6565
.FailWith(AppendDiagnosticsTo($"The command output did not contain expected result: {description} {Environment.NewLine}"));
@@ -68,7 +68,7 @@ public AndConstraint<CommandResultAssertions> HaveStdOutContaining(Func<string,
6868

6969
public AndConstraint<CommandResultAssertions> NotHaveStdOutContaining(string pattern, string[]? ignoredPatterns = null)
7070
{
71-
string filteredStdOut = _commandResult.StdOut;
71+
string filteredStdOut = _commandResult.StdOut ?? string.Empty;
7272
if (ignoredPatterns != null && ignoredPatterns.Length > 0)
7373
{
7474
foreach (var ignoredPattern in ignoredPatterns)
@@ -88,7 +88,7 @@ public AndConstraint<CommandResultAssertions> NotHaveStdOutContaining(string pat
8888

8989
public AndConstraint<CommandResultAssertions> HaveStdOutContainingIgnoreSpaces(string pattern)
9090
{
91-
string commandResultNoSpaces = _commandResult.StdOut.Replace(" ", "");
91+
string commandResultNoSpaces = _commandResult.StdOut?.Replace(" ", "") ?? string.Empty;
9292

9393
Execute.Assertion
9494
.ForCondition(commandResultNoSpaces.Contains(pattern))
@@ -99,21 +99,21 @@ public AndConstraint<CommandResultAssertions> HaveStdOutContainingIgnoreSpaces(s
9999

100100
public AndConstraint<CommandResultAssertions> HaveStdOutContainingIgnoreCase(string pattern)
101101
{
102-
Execute.Assertion.ForCondition(_commandResult.StdOut.IndexOf(pattern, StringComparison.OrdinalIgnoreCase) >= 0)
102+
Execute.Assertion.ForCondition(_commandResult.StdOut is not null && _commandResult.StdOut.IndexOf(pattern, StringComparison.OrdinalIgnoreCase) >= 0)
103103
.FailWith(AppendDiagnosticsTo($"The command output did not contain expected result (ignoring case): {pattern}{Environment.NewLine}"));
104104
return new AndConstraint<CommandResultAssertions>(this);
105105
}
106106

107107
public AndConstraint<CommandResultAssertions> HaveStdOutMatching(string pattern, RegexOptions options = RegexOptions.None)
108108
{
109-
Execute.Assertion.ForCondition(Regex.Match(_commandResult.StdOut, pattern, options).Success)
109+
Execute.Assertion.ForCondition(Regex.Match(_commandResult.StdOut ?? string.Empty, pattern, options).Success)
110110
.FailWith(AppendDiagnosticsTo($"Matching the command output failed. Pattern: {pattern}{Environment.NewLine}"));
111111
return new AndConstraint<CommandResultAssertions>(this);
112112
}
113113

114114
public AndConstraint<CommandResultAssertions> NotHaveStdOutMatching(string pattern, RegexOptions options = RegexOptions.None)
115115
{
116-
Execute.Assertion.ForCondition(!Regex.Match(_commandResult.StdOut, pattern, options).Success)
116+
Execute.Assertion.ForCondition(!Regex.Match(_commandResult.StdOut ?? string.Empty, pattern, options).Success)
117117
.FailWith(AppendDiagnosticsTo($"The command output matched a pattern it should not have. Pattern: {pattern}{Environment.NewLine}"));
118118
return new AndConstraint<CommandResultAssertions>(this);
119119
}
@@ -127,22 +127,22 @@ public AndConstraint<CommandResultAssertions> HaveStdErr()
127127

128128
public AndConstraint<CommandResultAssertions> HaveStdErr(string expectedOutput)
129129
{
130-
Execute.Assertion.ForCondition(_commandResult.StdErr.Equals(expectedOutput, StringComparison.Ordinal))
130+
Execute.Assertion.ForCondition(_commandResult.StdErr is not null && _commandResult.StdErr.Equals(expectedOutput, StringComparison.Ordinal))
131131
.FailWith(AppendDiagnosticsTo($"Command did not output the expected output to StdErr.{Environment.NewLine}Expected: {expectedOutput}{Environment.NewLine}Actual: {_commandResult.StdErr}"));
132132
return new AndConstraint<CommandResultAssertions>(this);
133133
}
134134

135135
public AndConstraint<CommandResultAssertions> HaveStdErrContaining(string pattern)
136136
{
137-
Execute.Assertion.ForCondition(_commandResult.StdErr.Contains(pattern))
137+
Execute.Assertion.ForCondition(_commandResult.StdErr is not null && _commandResult.StdErr.Contains(pattern))
138138
.FailWith(AppendDiagnosticsTo($"The command error output did not contain expected result: {pattern}{Environment.NewLine}"));
139139
return new AndConstraint<CommandResultAssertions>(this);
140140
}
141141

142142
public AndConstraint<CommandResultAssertions> HaveStdErrContainingOnce(string pattern)
143143
{
144-
var lines = _commandResult.StdErr.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
145-
var matchingLines = lines.Where(line => line.Contains(pattern)).Count();
144+
var lines = _commandResult.StdErr?.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
145+
var matchingLines = lines?.Where(line => line.Contains(pattern)).Count();
146146
Execute.Assertion.ForCondition(matchingLines == 0)
147147
.FailWith(AppendDiagnosticsTo($"The command error output did not contain expected result: {pattern}{Environment.NewLine}"));
148148
Execute.Assertion.ForCondition(matchingLines != 1)
@@ -152,14 +152,14 @@ public AndConstraint<CommandResultAssertions> HaveStdErrContainingOnce(string pa
152152

153153
public AndConstraint<CommandResultAssertions> NotHaveStdErrContaining(string pattern)
154154
{
155-
Execute.Assertion.ForCondition(!_commandResult.StdErr.Contains(pattern))
155+
Execute.Assertion.ForCondition(_commandResult.StdErr is not null && !_commandResult.StdErr.Contains(pattern))
156156
.FailWith(AppendDiagnosticsTo($"The command error output contained a result it should not have contained: {pattern}{Environment.NewLine}"));
157157
return new AndConstraint<CommandResultAssertions>(this);
158158
}
159159

160160
public AndConstraint<CommandResultAssertions> HaveStdErrMatching(string pattern, RegexOptions options = RegexOptions.None)
161161
{
162-
Execute.Assertion.ForCondition(Regex.Match(_commandResult.StdErr, pattern, options).Success)
162+
Execute.Assertion.ForCondition(Regex.Match(_commandResult.StdErr ?? string.Empty, pattern, options).Success)
163163
.FailWith(AppendDiagnosticsTo($"Matching the command error output failed. Pattern: {pattern}{Environment.NewLine}"));
164164
return new AndConstraint<CommandResultAssertions>(this);
165165
}

test/Microsoft.NET.TestFramework/Commands/MSBuildCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ internal static string FindProjectFile(ref string projectRootPath, string? relat
7070

7171
public virtual DirectoryInfo GetOutputDirectory(string? targetFramework = null, string configuration = "Debug", string? runtimeIdentifier = null, string? platform = null)
7272
{
73-
if (TestAsset != null && platform is not null)
73+
if (TestAsset != null)
7474
{
7575
return new DirectoryInfo(OutputPathCalculator.FromProject(ProjectFile, TestAsset).GetOutputDirectory(targetFramework, configuration, runtimeIdentifier, platform));
7676
}

test/Microsoft.NET.TestFramework/Commands/PublishCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public PublishCommand(TestAsset testAsset, string? relativePathToProject = null)
2424

2525
public override DirectoryInfo GetOutputDirectory(string? targetFramework = null, string configuration = "Debug", string? runtimeIdentifier = "", string? platformIdentifier = "")
2626
{
27-
if (TestAsset != null && targetFramework is not null)
27+
if (TestAsset != null)
2828
{
2929
return new DirectoryInfo(OutputPathCalculator.FromProject(ProjectFile, TestAsset).GetPublishDirectory(targetFramework, configuration, runtimeIdentifier, platformIdentifier));
3030
}

test/Microsoft.NET.TestFramework/NuGetTransientErrorDetector.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ public static class NuGetTransientErrorDetector
1313
"An error occurred while sending the request"
1414
};
1515

16-
public static bool IsTransientError(string errorMessage)
16+
public static bool IsTransientError(string? errorMessage)
1717
{
18-
return errorMessage.Contains("NuGet.targets") && _errorSubstrings.Any(errorMessage.Contains);
18+
return errorMessage is not null && errorMessage.Contains("NuGet.targets") && _errorSubstrings.Any(errorMessage.Contains);
1919
}
2020
}
2121
}

test/Microsoft.NET.TestFramework/OutputPathCalculator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public bool IsMultiTargeted()
169169
return !string.IsNullOrEmpty(TargetFrameworks);
170170
}
171171

172-
public string GetOutputDirectory(string? targetFramework = null, string configuration = "Debug", string? runtimeIdentifier = "", string platform = "")
172+
public string GetOutputDirectory(string? targetFramework = null, string configuration = "Debug", string? runtimeIdentifier = "", string? platform = "")
173173
{
174174
if (UseArtifactsOutput)
175175
{

test/Microsoft.NET.TestFramework/ProjectConstruction/TestProject.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public TestProject([CallerMemberName] string? name = null)
5555

5656
public bool UseArtifactsOutput { get; set; }
5757

58-
public List<TestProject> ReferencedProjects { get; } = new List<TestProject>();
58+
public List<TestProject?> ReferencedProjects { get; } = new List<TestProject?>();
5959

6060
public List<string> References { get; } = new List<string>();
6161

@@ -303,7 +303,7 @@ internal void Create(TestAsset targetTestAsset, string testProjectsSourceFolder,
303303
foreach (var referencedProject in ReferencedProjects)
304304
{
305305
projectReferenceItemGroup.Add(new XElement(ns + "ProjectReference",
306-
new XAttribute("Include", $"../{referencedProject.Name}/{referencedProject.Name}.csproj")));
306+
new XAttribute("Include", $"../{referencedProject?.Name}/{referencedProject?.Name}.csproj")));
307307
}
308308
}
309309

@@ -419,7 +419,7 @@ static void Main(string[] args)
419419

420420
foreach (var dependency in ReferencedProjects)
421421
{
422-
string? safeDependencyName = dependency.Name?.Replace('.', '_');
422+
string? safeDependencyName = dependency?.Name?.Replace('.', '_');
423423

424424
source += $" Console.WriteLine({safeDependencyName}.{safeDependencyName}Class.Name);" + Environment.NewLine;
425425
source += $" Console.WriteLine({safeDependencyName}.{safeDependencyName}Class.List);" + Environment.NewLine;
@@ -448,7 +448,7 @@ public class {safeThisName}Class
448448
";
449449
foreach (var dependency in ReferencedProjects)
450450
{
451-
string? safeDependencyName = dependency.Name?.Replace('.', '_');
451+
string? safeDependencyName = dependency?.Name?.Replace('.', '_');
452452

453453
source += $" public string {safeDependencyName}Name {{ get {{ return {safeDependencyName}.{safeDependencyName}Class.Name; }} }}" + Environment.NewLine;
454454
source += $" public List<string> {safeDependencyName}List {{ get {{ return {safeDependencyName}.{safeDependencyName}Class.List; }} }}" + Environment.NewLine;

test/Microsoft.NET.TestFramework/TestAssetsManager.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,10 @@ private TestAsset CreateTestProjectsInDirectory(
140140

141141
foreach (var referencedProject in project.ReferencedProjects)
142142
{
143-
projectStack.Push(referencedProject);
143+
if(referencedProject is not null)
144+
{
145+
projectStack.Push(referencedProject);
146+
}
144147
}
145148
}
146149
}

test/Microsoft.NET.TestFramework/ToolsetInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private void InitSdkVersion()
100100
throw new Exception("Failed to get dotnet version" + Environment.NewLine + logger.ToString());
101101
}
102102

103-
_sdkVersion = result.StdOut.Trim();
103+
_sdkVersion = result.StdOut?.Trim();
104104
}
105105
finally
106106
{
@@ -123,7 +123,7 @@ private void InitMSBuildVersion()
123123
throw new Exception("Failed to get msbuild version" + Environment.NewLine + logger.ToString());
124124
}
125125

126-
_msbuildVersion = result.StdOut.Split().Last();
126+
_msbuildVersion = result.StdOut?.Split().Last();
127127
}
128128

129129
public string? GetMicrosoftNETBuildExtensionsPath()

test/dotnet-new.Tests/DotnetNewArgumentsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void ShowsDetailedOutputOnMissedRequiredParam()
2525
.Should()
2626
.ExitWith(127)
2727
.And.HaveStdErrContaining("Required argument missing for option: '-v'")
28-
.And.HaveStdOutContaining(dotnetNewHelpOutput.StdOut);
28+
.And.HaveStdOutContaining(dotnetNewHelpOutput.StdOut ?? string.Empty);
2929
}
3030
}
3131
}

test/dotnet-new.Tests/DotnetNewSearchTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public void ExamplePrefersMicrosoftPackage(string testCase)
9797
IEnumerable<List<string>> microsoftPackages = tableOutput.Where(row => row[2] == "Microsoft" && row[3].StartsWith("Microsoft", StringComparison.OrdinalIgnoreCase));
9898
IEnumerable<string> installationCommands = microsoftPackages.Select(package => $"new install {package[3].Split(" /")[0]}").ToList();
9999

100-
bool ContainsOneOfInstallationCommands(string output) => installationCommands.Any(command => output.Contains(command));
100+
bool ContainsOneOfInstallationCommands(string? output) => installationCommands.Any(command => output is not null && output.Contains(command));
101101
commandResult.Should().HaveStdOutContaining(ContainsOneOfInstallationCommands, "Checks if the output contains one of the expected installation commands");
102102
}
103103

0 commit comments

Comments
 (0)