diff --git a/Directory.Packages.props b/Directory.Packages.props index 922483fc4..641e9276a 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -3,7 +3,6 @@ true - diff --git a/Source/Testably.Abstractions.Testing/Helpers/ChangeDescriptionExtensions.cs b/Source/Testably.Abstractions.Testing/Helpers/ChangeDescriptionExtensions.cs index 53b404beb..e45260567 100644 --- a/Source/Testably.Abstractions.Testing/Helpers/ChangeDescriptionExtensions.cs +++ b/Source/Testably.Abstractions.Testing/Helpers/ChangeDescriptionExtensions.cs @@ -1,5 +1,4 @@ -using DotNet.Globbing; -using System; +using System; using System.IO; using Testably.Abstractions.Testing.FileSystem; @@ -37,7 +36,7 @@ internal static bool Matches(this ChangeDescription changeDescription, return false; } - Glob? glob = Glob.Parse(globPattern, execute.GlobOptions); + GlobPattern glob = GlobPattern.Parse(globPattern, !execute.IsLinux); if (globPattern.IndexOfAny([ execute.Path.DirectorySeparatorChar, execute.Path.AltDirectorySeparatorChar, @@ -48,7 +47,7 @@ internal static bool Matches(this ChangeDescription changeDescription, return false; } } - else if (!glob.IsMatch(execute.Path.GetFileName(changeDescription.Name))) + else if (!glob.IsMatch(execute.Path.GetFileName(changeDescription.Name) ?? string.Empty)) { return false; } diff --git a/Source/Testably.Abstractions.Testing/Helpers/Execute.cs b/Source/Testably.Abstractions.Testing/Helpers/Execute.cs index c69717613..4201913d2 100644 --- a/Source/Testably.Abstractions.Testing/Helpers/Execute.cs +++ b/Source/Testably.Abstractions.Testing/Helpers/Execute.cs @@ -1,5 +1,4 @@ -using DotNet.Globbing; -using System; +using System; using System.IO; using System.Linq; using System.Runtime.InteropServices; @@ -8,24 +7,6 @@ namespace Testably.Abstractions.Testing.Helpers; internal sealed partial class Execute { - /// - /// The that use case sensitivity depending on the simulated OS. - /// - public GlobOptions GlobOptions - { - get - { - field ??= new GlobOptions - { - Evaluation = - { - CaseInsensitive = !IsLinux, - }, - }; - return field; - } - } - /// /// Flag indicating if the code runs on . /// diff --git a/Source/Testably.Abstractions.Testing/Helpers/GlobPattern.cs b/Source/Testably.Abstractions.Testing/Helpers/GlobPattern.cs new file mode 100644 index 000000000..43f6864a2 --- /dev/null +++ b/Source/Testably.Abstractions.Testing/Helpers/GlobPattern.cs @@ -0,0 +1,168 @@ +namespace Testably.Abstractions.Testing.Helpers; + +/// +/// A glob pattern supporting * and ? within a path segment, ** as a whole segment for any +/// number of directories and character classes like [abc], [a-z] or [!a-z].
+/// Both / and \ are treated as path separators, which neither *, ? nor character +/// classes match. +///
+internal sealed class GlobPattern +{ + private readonly bool _ignoreCase; + private readonly string _pattern; + + private GlobPattern(string pattern, bool ignoreCase) + { + _pattern = pattern; + _ignoreCase = ignoreCase; + } + + /// + /// Checks if the matches the glob pattern. + /// + public bool IsMatch(string text) + => IsMatch(0, text, 0); + + /// + /// Creates a from the given . + /// + public static GlobPattern Parse(string pattern, bool ignoreCase) + => new(pattern, ignoreCase); + + private bool CharEquals(char a, char b) + => a == b || (_ignoreCase && char.ToUpperInvariant(a) == char.ToUpperInvariant(b)); + + private bool IsDirectoryWildcard(int p) + => p + 1 < _pattern.Length && _pattern[p + 1] == '*' && + (p == 0 || IsSeparator(_pattern[p - 1])) && + (p + 2 == _pattern.Length || IsSeparator(_pattern[p + 2])); + + private bool IsMatch(int p, string text, int t) + { + while (p < _pattern.Length) + { + if (_pattern[p] == '*') + { + return IsDirectoryWildcard(p) + ? MatchesDirectoryWildcard(p, text, t) + : MatchesWildcard(p, text, t); + } + + if (t == text.Length || !MatchesNextCharacter(ref p, text[t])) + { + return false; + } + + t++; + } + + return t == text.Length; + } + + private static bool IsSeparator(char c) + => c is '/' or '\\'; + + private bool MatchesCharacter(char patternChar, char textChar) + { + if (IsSeparator(patternChar)) + { + return IsSeparator(textChar); + } + + if (patternChar == '?') + { + return !IsSeparator(textChar); + } + + return CharEquals(patternChar, textChar); + } + + private bool MatchesDirectoryWildcard(int p, string text, int t) + { + if (p + 2 == _pattern.Length) + { + return true; + } + + for (int i = t; i <= text.Length; i++) + { + if ((i == t || IsSeparator(text[i - 1])) && IsMatch(p + 3, text, i)) + { + return true; + } + } + + return false; + } + + private bool MatchesNextCharacter(ref int p, char textChar) + => _pattern[p] == '[' && TryMatchClass(ref p, textChar, out bool isClassMatch) + ? isClassMatch + : MatchesCharacter(_pattern[p++], textChar); + + private bool MatchesWildcard(int p, string text, int t) + { + while (p < _pattern.Length && _pattern[p] == '*') + { + p++; + } + + int segmentEnd = t; + while (segmentEnd < text.Length && !IsSeparator(text[segmentEnd])) + { + segmentEnd++; + } + + for (int i = t; i <= segmentEnd; i++) + { + if (IsMatch(p, text, i)) + { + return true; + } + } + + return false; + } + + private bool TryMatchClass(ref int p, char c, out bool isMatch) + { + int start = p + 1; + bool isNegated = start < _pattern.Length && _pattern[start] == '!'; + if (isNegated) + { + start++; + } + + int end = _pattern.IndexOf(']', start); + if (end <= start) + { + isMatch = false; + return false; + } + + bool isFound = false; + int i = start; + while (i < end && !isFound) + { + if (i + 2 < end && _pattern[i + 1] == '-') + { + isFound = IsInRange(c, _pattern[i], _pattern[i + 2]) || + (_ignoreCase && (IsInRange(char.ToUpperInvariant(c), _pattern[i], _pattern[i + 2]) || + IsInRange(char.ToLowerInvariant(c), _pattern[i], _pattern[i + 2]))); + i += 3; + } + else + { + isFound = CharEquals(_pattern[i], c); + i++; + } + } + + p = end + 1; + isMatch = isFound != isNegated && !IsSeparator(c); + return true; + + static bool IsInRange(char value, char from, char to) + => value >= from && value <= to; + } +} diff --git a/Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs b/Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs index f497fd2cf..5126bdb90 100644 --- a/Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs +++ b/Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs @@ -1,5 +1,4 @@ -using DotNet.Globbing; -using System; +using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; @@ -22,7 +21,7 @@ private readonly ConcurrentDictionary private readonly ConcurrentDictionary> _fileHandles = new(); - private readonly List<(Glob, bool, FileVersionInfoContainer)> + private readonly List<(GlobPattern, bool, FileVersionInfoContainer)> _fileVersions = new(); private readonly ConcurrentDictionary _drives = @@ -753,7 +752,7 @@ public override string ToString() internal void AddFileVersion(string globPattern, FileVersionInfoContainer container) { _fileVersions.Add(( - Glob.Parse(globPattern, _fileSystem.Execute.GlobOptions), + GlobPattern.Parse(globPattern, !_fileSystem.Execute.IsLinux), globPattern.IndexOfAny([ _fileSystem.Execute.Path.DirectorySeparatorChar, _fileSystem.Execute.Path.AltDirectorySeparatorChar, diff --git a/Source/Testably.Abstractions.Testing/Testably.Abstractions.Testing.csproj b/Source/Testably.Abstractions.Testing/Testably.Abstractions.Testing.csproj index 592577a8d..14c049fa1 100644 --- a/Source/Testably.Abstractions.Testing/Testably.Abstractions.Testing.csproj +++ b/Source/Testably.Abstractions.Testing/Testably.Abstractions.Testing.csproj @@ -22,7 +22,6 @@
- diff --git a/Tests/Testably.Abstractions.Testing.Tests/Helpers/GlobPatternTests.cs b/Tests/Testably.Abstractions.Testing.Tests/Helpers/GlobPatternTests.cs new file mode 100644 index 000000000..05138dc55 --- /dev/null +++ b/Tests/Testably.Abstractions.Testing.Tests/Helpers/GlobPatternTests.cs @@ -0,0 +1,128 @@ +using Testably.Abstractions.Testing.Helpers; + +namespace Testably.Abstractions.Testing.Tests.Helpers; + +public sealed class GlobPatternTests +{ + [Test] + [Arguments("*", "", true)] + [Arguments("*", "foo", true)] + [Arguments("*", "a/b", false)] + [Arguments("f*o", "foo", true)] + [Arguments("f*o", "fo", true)] + [Arguments("f*o", "fooa", false)] + [Arguments("*.txt", "a.txt", true)] + [Arguments("*.txt", "a.txt.bak", false)] + [Arguments("*o*o*", "foobar", true)] + [Arguments("*o*o*", "fobar", false)] + [Arguments("f?o", "foo", true)] + [Arguments("f?o", "fo", false)] + [Arguments("f?o", "f/o", false)] + [Arguments("foo", "foo", true)] + [Arguments("foo", "bar", false)] + [Arguments("foo", "foobar", false)] + [Arguments("", "", true)] + [Arguments("", "a", false)] + public async Task IsMatch_Wildcards_ShouldMatchWithinSegment( + string pattern, string text, bool expected) + { + bool result = GlobPattern.Parse(pattern, false).IsMatch(text); + + await That(result).IsEqualTo(expected); + } + + [Test] + [Arguments("a/*", "a/b", true)] + [Arguments("a/*", "a/b/c", false)] + [Arguments("a/b", "a\\b", true)] + [Arguments("a\\b", "a/b", true)] + [Arguments("a/b", "a/c", false)] + [Arguments("/a/b", "/a/b", true)] + [Arguments("C:/a/*.txt", "C:/a/b.txt", true)] + public async Task IsMatch_Separators_ShouldTreatSlashAndBackslashAsSeparator( + string pattern, string text, bool expected) + { + bool result = GlobPattern.Parse(pattern, false).IsMatch(text); + + await That(result).IsEqualTo(expected); + } + + [Test] + [Arguments("**", "a", true)] + [Arguments("**", "a/b", true)] + [Arguments("**/foo", "foo", true)] + [Arguments("**/foo", "a/b/foo", true)] + [Arguments("**/foo", "/a/foo", true)] + [Arguments("**/foo", "C:/a/foo", true)] + [Arguments("**/foo", "a/foobar", false)] + [Arguments("**/f*o", "C:/bar/foo", true)] + [Arguments("a/**/b", "a/b", true)] + [Arguments("a/**/b", "a/x/y/b", true)] + [Arguments("a/**/b", "a/x/c", false)] + [Arguments("a/**/b", "x/a/b", false)] + [Arguments("a/**", "a/b/c", true)] + [Arguments("a/**", "b/c", false)] + [Arguments("a/**", "a", false)] + [Arguments("a**b", "axb", true)] + [Arguments("a**b", "ax/yb", false)] + public async Task IsMatch_DirectoryWildcard_ShouldMatchAnyNumberOfDirectories( + string pattern, string text, bool expected) + { + bool result = GlobPattern.Parse(pattern, false).IsMatch(text); + + await That(result).IsEqualTo(expected); + } + + [Test] + [Arguments("[abc]x", "bx", true)] + [Arguments("[abc]x", "dx", false)] + [Arguments("[a-c]x", "bx", true)] + [Arguments("[a-c]x", "dx", false)] + [Arguments("[!abc]x", "dx", true)] + [Arguments("[!abc]x", "ax", false)] + [Arguments("[!a-c]x", "dx", true)] + [Arguments("[!a-c]x", "bx", false)] + [Arguments("a[!b]c", "a/c", false)] + [Arguments("a[!b]c", "a\\c", false)] + [Arguments("[a-cx-z]", "y", true)] + [Arguments("[a-cx-z]", "m", false)] + [Arguments("[*]", "*", true)] + [Arguments("[*]", "a", false)] + [Arguments("[?]", "?", true)] + [Arguments("[?]", "a", false)] + public async Task IsMatch_CharacterClasses_ShouldMatchSingleCharacter( + string pattern, string text, bool expected) + { + bool result = GlobPattern.Parse(pattern, false).IsMatch(text); + + await That(result).IsEqualTo(expected); + } + + [Test] + [Arguments("FOO", "foo", true)] + [Arguments("f*O", "FoO", true)] + [Arguments("[A-C]x", "bX", true)] + [Arguments("[abc]", "B", true)] + [Arguments("[!A-C]x", "bx", false)] + public async Task IsMatch_IgnoreCase_ShouldMatchCaseInsensitive( + string pattern, string text, bool expected) + { + bool result = GlobPattern.Parse(pattern, true).IsMatch(text); + + await That(result).IsEqualTo(expected); + } + + [Test] + [Arguments("FOO", "foo", false)] + [Arguments("f*O", "FoO", false)] + [Arguments("[A-C]x", "bx", false)] + [Arguments("[abc]", "B", false)] + [Arguments("[!A-C]x", "bx", true)] + public async Task IsMatch_CaseSensitive_ShouldMatchCaseSensitive( + string pattern, string text, bool expected) + { + bool result = GlobPattern.Parse(pattern, false).IsMatch(text); + + await That(result).IsEqualTo(expected); + } +}