Skip to content

Commit

Permalink
[Group 1] Enable nullable annotations for `Microsoft.Extensions.FileS…
Browse files Browse the repository at this point in the history
…ystemGlobbing` (#57398)

* Annotate src

* Annotate ref

* Null check for directoryInfo

* Nullcheck for PatternMatchingResult.Files

* ParentDirectory cannot be null

! need to be addressed

* Update PatternBuilder.cs

* Use expression body in segments

* Update ref

* DisableImplicitAssemblyReferences

* Fix doc

* TestMatchingGroup nonnull

* ParentDirectory can be null

* Update TestGetHashCodeWithNull

* Update FilePatternMatchTests.cs

* Update FilePatternMatchTests.cs

* Remove TestGetHashCodeWithNull

* InMemoryFileInfo.ParentDirectory is not null
  • Loading branch information
maxkoshevoi authored Oct 6, 2021
1 parent be91858 commit 1c30027
Show file tree
Hide file tree
Showing 26 changed files with 116 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ public partial struct FilePatternMatch : System.IEquatable<Microsoft.Extensions.
{
private object _dummy;
private int _dummyPrimitive;
public FilePatternMatch(string path, string stem) { throw null; }
public FilePatternMatch(string path, string? stem) { throw null; }
public readonly string Path { get { throw null; } }
public readonly string Stem { get { throw null; } }
public readonly string? Stem { get { throw null; } }
public bool Equals(Microsoft.Extensions.FileSystemGlobbing.FilePatternMatch other) { throw null; }
public override bool Equals(object obj) { throw null; }
public override bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] object? obj) { throw null; }
public override int GetHashCode() { throw null; }
}
public partial class InMemoryDirectoryInfo : Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoBase
{
public InMemoryDirectoryInfo(string rootDir, System.Collections.Generic.IEnumerable<string> files) { }
public InMemoryDirectoryInfo(string rootDir, System.Collections.Generic.IEnumerable<string>? files) { }
public override string FullName { get { throw null; } }
public override string Name { get { throw null; } }
public override Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoBase ParentDirectory { get { throw null; } }
public override Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoBase? ParentDirectory { get { throw null; } }
public override System.Collections.Generic.IEnumerable<Microsoft.Extensions.FileSystemGlobbing.Abstractions.FileSystemInfoBase> EnumerateFileSystemInfos() { throw null; }
public override Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoBase GetDirectory(string path) { throw null; }
public override Microsoft.Extensions.FileSystemGlobbing.Abstractions.FileInfoBase GetFile(string path) { throw null; }
public override Microsoft.Extensions.FileSystemGlobbing.Abstractions.FileInfoBase? GetFile(string path) { throw null; }
}
public partial class Matcher
{
Expand All @@ -40,9 +40,9 @@ public static partial class MatcherExtensions
public static void AddExcludePatterns(this Microsoft.Extensions.FileSystemGlobbing.Matcher matcher, params System.Collections.Generic.IEnumerable<string>[] excludePatternsGroups) { }
public static void AddIncludePatterns(this Microsoft.Extensions.FileSystemGlobbing.Matcher matcher, params System.Collections.Generic.IEnumerable<string>[] includePatternsGroups) { }
public static System.Collections.Generic.IEnumerable<string> GetResultsInFullPath(this Microsoft.Extensions.FileSystemGlobbing.Matcher matcher, string directoryPath) { throw null; }
public static Microsoft.Extensions.FileSystemGlobbing.PatternMatchingResult Match(this Microsoft.Extensions.FileSystemGlobbing.Matcher matcher, System.Collections.Generic.IEnumerable<string> files) { throw null; }
public static Microsoft.Extensions.FileSystemGlobbing.PatternMatchingResult Match(this Microsoft.Extensions.FileSystemGlobbing.Matcher matcher, System.Collections.Generic.IEnumerable<string>? files) { throw null; }
public static Microsoft.Extensions.FileSystemGlobbing.PatternMatchingResult Match(this Microsoft.Extensions.FileSystemGlobbing.Matcher matcher, string file) { throw null; }
public static Microsoft.Extensions.FileSystemGlobbing.PatternMatchingResult Match(this Microsoft.Extensions.FileSystemGlobbing.Matcher matcher, string rootDir, System.Collections.Generic.IEnumerable<string> files) { throw null; }
public static Microsoft.Extensions.FileSystemGlobbing.PatternMatchingResult Match(this Microsoft.Extensions.FileSystemGlobbing.Matcher matcher, string rootDir, System.Collections.Generic.IEnumerable<string>? files) { throw null; }
public static Microsoft.Extensions.FileSystemGlobbing.PatternMatchingResult Match(this Microsoft.Extensions.FileSystemGlobbing.Matcher matcher, string rootDir, string file) { throw null; }
}
public partial class PatternMatchingResult
Expand All @@ -59,17 +59,17 @@ public abstract partial class DirectoryInfoBase : Microsoft.Extensions.FileSyste
{
protected DirectoryInfoBase() { }
public abstract System.Collections.Generic.IEnumerable<Microsoft.Extensions.FileSystemGlobbing.Abstractions.FileSystemInfoBase> EnumerateFileSystemInfos();
public abstract Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoBase GetDirectory(string path);
public abstract Microsoft.Extensions.FileSystemGlobbing.Abstractions.FileInfoBase GetFile(string path);
public abstract Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoBase? GetDirectory(string path);
public abstract Microsoft.Extensions.FileSystemGlobbing.Abstractions.FileInfoBase? GetFile(string path);
}
public partial class DirectoryInfoWrapper : Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoBase
{
public DirectoryInfoWrapper(System.IO.DirectoryInfo directoryInfo) { }
public override string FullName { get { throw null; } }
public override string Name { get { throw null; } }
public override Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoBase ParentDirectory { get { throw null; } }
public override Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoBase? ParentDirectory { get { throw null; } }
public override System.Collections.Generic.IEnumerable<Microsoft.Extensions.FileSystemGlobbing.Abstractions.FileSystemInfoBase> EnumerateFileSystemInfos() { throw null; }
public override Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoBase GetDirectory(string name) { throw null; }
public override Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoBase? GetDirectory(string name) { throw null; }
public override Microsoft.Extensions.FileSystemGlobbing.Abstractions.FileInfoBase GetFile(string name) { throw null; }
}
public abstract partial class FileInfoBase : Microsoft.Extensions.FileSystemGlobbing.Abstractions.FileSystemInfoBase
Expand All @@ -81,14 +81,14 @@ public partial class FileInfoWrapper : Microsoft.Extensions.FileSystemGlobbing.A
public FileInfoWrapper(System.IO.FileInfo fileInfo) { }
public override string FullName { get { throw null; } }
public override string Name { get { throw null; } }
public override Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoBase ParentDirectory { get { throw null; } }
public override Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoBase? ParentDirectory { get { throw null; } }
}
public abstract partial class FileSystemInfoBase
{
protected FileSystemInfoBase() { }
public abstract string FullName { get; }
public abstract string Name { get; }
public abstract Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoBase ParentDirectory { get; }
public abstract Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoBase? ParentDirectory { get; }
}
}
namespace Microsoft.Extensions.FileSystemGlobbing.Internal
Expand Down Expand Up @@ -133,8 +133,8 @@ public partial struct PatternTestResult
private int _dummyPrimitive;
public static readonly Microsoft.Extensions.FileSystemGlobbing.Internal.PatternTestResult Failed;
public readonly bool IsSuccessful { get { throw null; } }
public readonly string Stem { get { throw null; } }
public static Microsoft.Extensions.FileSystemGlobbing.Internal.PatternTestResult Success(string stem) { throw null; }
public readonly string? Stem { get { throw null; } }
public static Microsoft.Extensions.FileSystemGlobbing.Internal.PatternTestResult Success(string? stem) { throw null; }
}
}
namespace Microsoft.Extensions.FileSystemGlobbing.Internal.PathSegments
Expand All @@ -150,7 +150,7 @@ public partial class LiteralPathSegment : Microsoft.Extensions.FileSystemGlobbin
public LiteralPathSegment(string value, System.StringComparison comparisonType) { }
public bool CanProduceStem { get { throw null; } }
public string Value { get { throw null; } }
public override bool Equals(object obj) { throw null; }
public override bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] object? obj) { throw null; }
public override int GetHashCode() { throw null; }
public bool Match(string value) { throw null; }
}
Expand Down Expand Up @@ -195,7 +195,7 @@ public partial struct FrameData
public bool InStem;
public bool IsNotApplicable;
public int SegmentIndex;
public string Stem { get { throw null; } }
public string? Stem { get { throw null; } }
public System.Collections.Generic.IList<string> StemItems { get { throw null; } }
}
}
Expand Down Expand Up @@ -232,7 +232,7 @@ public partial struct FrameData
public System.Collections.Generic.IList<Microsoft.Extensions.FileSystemGlobbing.Internal.IPathSegment> SegmentGroup;
public int SegmentGroupIndex;
public int SegmentIndex;
public string Stem { get { throw null; } }
public string? Stem { get { throw null; } }
public System.Collections.Generic.IList<string> StemItems { get { throw null; } }
}
}
Expand All @@ -249,12 +249,12 @@ public override void Declare(System.Action<Microsoft.Extensions.FileSystemGlobbi
}
public abstract partial class PatternContext<TFrame> : Microsoft.Extensions.FileSystemGlobbing.Internal.IPatternContext
{
protected TFrame Frame;
protected TFrame? Frame;
protected PatternContext() { }
public virtual void Declare(System.Action<Microsoft.Extensions.FileSystemGlobbing.Internal.IPathSegment, bool> declare) { }
protected bool IsStackEmpty() { throw null; }
public virtual void PopDirectory() { }
protected void PushDataFrame(TFrame frame) { }
protected void PushDataFrame(TFrame? frame) { }
public abstract void PushDirectory(Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoBase directory);
public abstract bool Test(Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoBase directory);
public abstract Microsoft.Extensions.FileSystemGlobbing.Internal.PatternTestResult Test(Microsoft.Extensions.FileSystemGlobbing.Abstractions.FileInfoBase file);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Include="Microsoft.Extensions.FileSystemGlobbing.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ public abstract class DirectoryInfoBase : FileSystemInfoBase
/// </summary>
/// <param name="path">The directory name</param>
/// <returns>Instance of <see cref="DirectoryInfoBase" /> even if directory does not exist</returns>
public abstract DirectoryInfoBase GetDirectory(string path);
public abstract DirectoryInfoBase? GetDirectory(string path);

/// <summary>
/// Returns an instance of <see cref="FileInfoBase" /> that represents a file in the directory
/// </summary>
/// <param name="path">The file name</param>
/// <returns>Instance of <see cref="FileInfoBase" /> even if file does not exist</returns>
public abstract FileInfoBase GetFile(string path);
public abstract FileInfoBase? GetFile(string path);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ public override IEnumerable<FileSystemInfoBase> EnumerateFileSystemInfos()

foreach (FileSystemInfo fileSystemInfo in fileSystemInfos)
{
var directoryInfo = fileSystemInfo as DirectoryInfo;
if (directoryInfo != null)
if (fileSystemInfo is DirectoryInfo directoryInfo)
{
yield return new DirectoryInfoWrapper(directoryInfo);
}
Expand All @@ -68,7 +67,7 @@ public override IEnumerable<FileSystemInfoBase> EnumerateFileSystemInfos()
/// </remarks>
/// <param name="name">The directory name</param>
/// <returns>The directory</returns>
public override DirectoryInfoBase GetDirectory(string name)
public override DirectoryInfoBase? GetDirectory(string name)
{
bool isParentPath = string.Equals(name, "..", StringComparison.Ordinal);

Expand Down Expand Up @@ -120,7 +119,7 @@ public override FileInfoBase GetFile(string name)
/// <remarks>
/// Equals the value of <seealso cref="System.IO.DirectoryInfo.Parent" />.
/// </remarks>
public override DirectoryInfoBase ParentDirectory
=> new DirectoryInfoWrapper(_directoryInfo.Parent);
public override DirectoryInfoBase? ParentDirectory
=> new DirectoryInfoWrapper(_directoryInfo.Parent!);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.IO;

namespace Microsoft.Extensions.FileSystemGlobbing.Abstractions
Expand All @@ -18,7 +19,7 @@ public class FileInfoWrapper : FileInfoBase
/// <param name="fileInfo">The <see cref="System.IO.FileInfo" /></param>
public FileInfoWrapper(FileInfo fileInfo)
{
_fileInfo = fileInfo;
_fileInfo = fileInfo ?? throw new ArgumentNullException(nameof(fileInfo));
}

/// <summary>
Expand All @@ -43,6 +44,7 @@ public FileInfoWrapper(FileInfo fileInfo)
/// <remarks>
/// Equals the value of <see cref="System.IO.FileInfo.Directory" />.
/// </remarks>
public override DirectoryInfoBase ParentDirectory => new DirectoryInfoWrapper(_fileInfo.Directory);
public override DirectoryInfoBase? ParentDirectory
=> new DirectoryInfoWrapper(_fileInfo.Directory!);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ public abstract class FileSystemInfoBase
/// <summary>
/// The parent directory for the current file or directory
/// </summary>
public abstract DirectoryInfoBase ParentDirectory { get; }
public abstract DirectoryInfoBase? ParentDirectory { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Numerics.Hashing;

namespace Microsoft.Extensions.FileSystemGlobbing
Expand All @@ -27,16 +28,16 @@ public struct FilePatternMatch : IEquatable<FilePatternMatch>
/// If the matcher searched for "src/Project/**/*.cs" and the pattern matcher found "src/Project/Interfaces/IFile.cs",
/// then <see cref="Stem" /> = "Interfaces/IFile.cs" and <see cref="Path" /> = "src/Project/Interfaces/IFile.cs".
/// </remarks>
public string Stem { get; }
public string? Stem { get; }

/// <summary>
/// Initializes new instance of <see cref="FilePatternMatch" />
/// </summary>
/// <param name="path">The path to the file matched, relative to the beginning of the matching search pattern.</param>
/// <param name="stem">The subpath to the file matched, relative to the first wildcard in the matching search pattern.</param>
public FilePatternMatch(string path, string stem)
public FilePatternMatch(string path, string? stem)
{
Path = path;
Path = path ?? throw new ArgumentNullException(nameof(path));
Stem = stem;
}

Expand All @@ -56,10 +57,8 @@ public bool Equals(FilePatternMatch other)
/// </summary>
/// <param name="obj">The object to be compared</param>
/// <returns>True when <see cref="Equals(FilePatternMatch)" /></returns>
public override bool Equals(object obj)
{
return Equals((FilePatternMatch) obj);
}
public override bool Equals([NotNullWhen(true)] object? obj) =>
obj is FilePatternMatch match && Equals(match);

/// <summary>
/// Gets a hash for the file pattern match.
Expand All @@ -68,7 +67,7 @@ public override bool Equals(object obj)
public override int GetHashCode() =>
HashHelpers.Combine(GetHashCode(Path), GetHashCode(Stem));

private static int GetHashCode(string value) =>
private static int GetHashCode(string? value) =>
value != null ? StringComparer.OrdinalIgnoreCase.GetHashCode(value) : 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,19 @@ public class InMemoryDirectoryInfo : DirectoryInfoBase
/// </summary>
/// <param name="rootDir">The root directory that this FileSystem will use.</param>
/// <param name="files">Collection of file names. If relative paths <paramref name="rootDir"/> will be prepended to the paths.</param>
public InMemoryDirectoryInfo(string rootDir, IEnumerable<string> files)
public InMemoryDirectoryInfo(string rootDir, IEnumerable<string>? files)
: this(rootDir, files, false)
{
}

private InMemoryDirectoryInfo(string rootDir, IEnumerable<string> files, bool normalized)
private InMemoryDirectoryInfo(string rootDir, IEnumerable<string>? files, bool normalized)
{
if (string.IsNullOrEmpty(rootDir))
{
throw new ArgumentNullException(nameof(rootDir));
}

if (files == null)
{
files = new List<string>();
}
files ??= new List<string>();

Name = Path.GetFileName(rootDir);
if (normalized)
Expand Down Expand Up @@ -69,13 +66,8 @@ private InMemoryDirectoryInfo(string rootDir, IEnumerable<string> files, bool no
public override string Name { get; }

/// <inheritdoc />
public override DirectoryInfoBase ParentDirectory
{
get
{
return new InMemoryDirectoryInfo(Path.GetDirectoryName(FullName), _files, true);
}
}
public override DirectoryInfoBase? ParentDirectory =>
new InMemoryDirectoryInfo(Path.GetDirectoryName(FullName)!, _files, true);

/// <inheritdoc />
public override IEnumerable<FileSystemInfoBase> EnumerateFileSystemInfos()
Expand All @@ -99,8 +91,7 @@ public override IEnumerable<FileSystemInfoBase> EnumerateFileSystemInfos()
else
{
string name = file.Substring(0, endSegment);
List<string> list;
if (!dict.TryGetValue(name, out list))
if (!dict.TryGetValue(name, out List<string>? list))
{
dict[name] = new List<string> { file };
}
Expand Down Expand Up @@ -145,7 +136,7 @@ public override DirectoryInfoBase GetDirectory(string path)
/// </summary>
/// <param name="path">The filename.</param>
/// <returns>Instance of <see cref="FileInfoBase"/> if the file exists, null otherwise.</returns>
public override FileInfoBase GetFile(string path)
public override FileInfoBase? GetFile(string path)
{
string normPath = Path.GetFullPath(path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar));
foreach (string file in _files)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ public InMemoryFileInfo(string file, InMemoryDirectoryInfo parent)

public override string Name { get; }

public override DirectoryInfoBase ParentDirectory
{
get
{
return _parent;
}
}
public override DirectoryInfoBase ParentDirectory => _parent;
}
}
Loading

0 comments on commit 1c30027

Please sign in to comment.