Skip to content

Commit f187449

Browse files
authored
Make FilePatternMatch.Stem non-nullable. (#118410)
- Add an ArgumentNullException if stem is null in Success method. - Add tests for null checks.
1 parent b267b85 commit f187449

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/FilePatternMatch.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,17 @@ public struct FilePatternMatch : IEquatable<FilePatternMatch>
2828
/// If the matcher searched for "src/Project/**/*.cs" and the pattern matcher found "src/Project/Interfaces/IFile.cs",
2929
/// then <see cref="Stem" /> = "Interfaces/IFile.cs" and <see cref="Path" /> = "src/Project/Interfaces/IFile.cs".
3030
/// </remarks>
31-
public string? Stem { get; }
31+
public string Stem { get; }
3232

3333
/// <summary>
3434
/// Initializes new instance of <see cref="FilePatternMatch" />
3535
/// </summary>
3636
/// <param name="path">The path to the file matched, relative to the beginning of the matching search pattern.</param>
3737
/// <param name="stem">The subpath to the file matched, relative to the first wildcard in the matching search pattern.</param>
38-
public FilePatternMatch(string path, string? stem)
38+
public FilePatternMatch(string path, string stem)
3939
{
4040
ArgumentNullException.ThrowIfNull(path);
41+
ArgumentNullException.ThrowIfNull(stem);
4142

4243
Path = path;
4344
Stem = stem;

src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Internal/PatternTestResult.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
5+
46
namespace Microsoft.Extensions.FileSystemGlobbing.Internal
57
{
68
/// <summary>
@@ -11,6 +13,7 @@ public struct PatternTestResult
1113
{
1214
public static readonly PatternTestResult Failed = new(isSuccessful: false, stem: null);
1315

16+
[MemberNotNullWhen(returnValue: true, nameof(Stem))]
1417
public bool IsSuccessful { get; }
1518
public string? Stem { get; }
1619

src/libraries/Microsoft.Extensions.FileSystemGlobbing/tests/FilePatternMatchTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System;
45
using Xunit;
56

67
namespace Microsoft.Extensions.FileSystemGlobbing.Tests
@@ -24,5 +25,17 @@ public void TestGetHashCode()
2425
FilePatternMatch matchCase2 = new FilePatternMatch("sub/sub2/bar/baz/three.txt", "Sub2/bar/baz/thrEE.txt");
2526
Assert.Equal(matchCase1.GetHashCode(), matchCase2.GetHashCode());
2627
}
28+
29+
[Fact]
30+
public void TestPathArgumentNullExceptions()
31+
{
32+
Assert.Throws<ArgumentNullException>(() => new FilePatternMatch(null, "sub2/bar/baz/three.txt"));
33+
}
34+
35+
[Fact]
36+
public void TestStemArgumentNullExceptions()
37+
{
38+
Assert.Throws<ArgumentNullException>(() => new FilePatternMatch("sub2/bar/baz/three.txt", null));
39+
}
2740
}
2841
}

0 commit comments

Comments
 (0)