From cf1743e05fe5ed1f3515a8f09fc9d65b2437a454 Mon Sep 17 00:00:00 2001 From: Artem Pyanykh Date: Sun, 30 Oct 2022 13:41:08 +0000 Subject: [PATCH] fix: Do not fail on empty glob patterns --- Marksman/GitIgnore.fs | 23 +++++++++------ Marksman/Marksman.fsproj | 64 ++++++++++++++++++++-------------------- Tests/GitIgnoreTest.fs | 33 +++++++++++---------- 3 files changed, 64 insertions(+), 56 deletions(-) diff --git a/Marksman/GitIgnore.fs b/Marksman/GitIgnore.fs index 5320847..839656b 100644 --- a/Marksman/GitIgnore.fs +++ b/Marksman/GitIgnore.fs @@ -1,23 +1,28 @@ module Marksman.GitIgnore +open System open System.IO open GlobExpressions +open Marksman.Misc type GlobPattern = | Include of Glob | Exclude of Glob let patternToGlob (pat: string) : array = - let firstSlashIdx = pat.IndexOf('/') - let isAbsolute = firstSlashIdx <> pat.Length - 1 - let isDir = pat[pat.Length - 1] = '/' - let pat = if pat.StartsWith("/") then pat.Substring(1) else pat - let pat = if isAbsolute then pat else "**/" + pat - - if isDir then - [| Glob(pat + "**"); Glob(pat.Substring(0, pat.Length - 1)) |] + if String.IsNullOrWhiteSpace(pat) then + [||] else - [| Glob(pat) |] + let firstSlashIdx = pat.IndexOf('/') + let isAbsolute = firstSlashIdx <> pat.Length - 1 + let isDir = pat[pat.Length - 1] = '/' + let pat = if pat.StartsWith("/") then pat.Substring(1) else pat + let pat = if isAbsolute then pat else "**/" + pat + + if isDir then + [| Glob(pat + "**"); Glob(pat.Substring(0, pat.Length - 1)) |] + else + [| Glob(pat) |] let mkGlobPattern (pat: string) : array = if pat.StartsWith("#") then diff --git a/Marksman/Marksman.fsproj b/Marksman/Marksman.fsproj index a634028..7b3ce9b 100644 --- a/Marksman/Marksman.fsproj +++ b/Marksman/Marksman.fsproj @@ -9,7 +9,7 @@ - + $(VersionString) @@ -20,43 +20,43 @@ dotnet fsharplint -f msbuild lint --lint-config $(MSBuildProjectDirectory)/../fsharplint.json $(MSBuildProjectFullPath) - + - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + - - - - - + + + + + - - - + + + - - + + diff --git a/Tests/GitIgnoreTest.fs b/Tests/GitIgnoreTest.fs index ca0e4b9..4275fb6 100644 --- a/Tests/GitIgnoreTest.fs +++ b/Tests/GitIgnoreTest.fs @@ -5,6 +5,9 @@ open Xunit open Marksman.GitIgnore +[] +let patternToGlob_Empty () = Assert.Equal([||], patternToGlob "") + [] let absGlob_Unix () = if not (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) then @@ -15,7 +18,7 @@ let absGlob_Unix () = let notIgnored = "/Users/john/notes/real.md" GlobMatcher.ignores glob notIgnored |> Assert.False - + [] let relGlob_Unix_1 () = if not (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) then @@ -23,16 +26,16 @@ let relGlob_Unix_1 () = let glob = GlobMatcher.mk root [| "node_modules/" |] let ignored = "/Users/john/notes/node_modules" GlobMatcher.ignores glob ignored |> Assert.True - + let ignored = "/Users/john/notes/node_modules/" GlobMatcher.ignores glob ignored |> Assert.True - + let ignored = "/Users/john/notes/node_modules/foo.md" GlobMatcher.ignores glob ignored |> Assert.True let notIgnored = "/Users/john/notes/real.md" GlobMatcher.ignores glob notIgnored |> Assert.False - + [] let relGlob_Unix_2 () = if not (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) then @@ -40,10 +43,10 @@ let relGlob_Unix_2 () = let glob = GlobMatcher.mk root [| "node_modules/" |] let ignored = "/Users/john/notes/sub/node_modules" GlobMatcher.ignores glob ignored |> Assert.True - + let ignored = "/Users/john/notes/sub/node_modules/" GlobMatcher.ignores glob ignored |> Assert.True - + let ignored = "/Users/john/notes/sub/node_modules/foo.md" GlobMatcher.ignores glob ignored |> Assert.True @@ -71,38 +74,38 @@ let absGlob_Win () = let notIgnored = "C:\\notes\\real.md" GlobMatcher.ignores glob notIgnored |> Assert.False - - + + [] let relGlob_Win_1 () = if RuntimeInformation.IsOSPlatform(OSPlatform.Windows) then let root = "C:\\notes" let glob = GlobMatcher.mk root [| "node_modules/" |] - + let ignored = "C:\\notes\\node_modules" GlobMatcher.ignores glob ignored |> Assert.True - + let ignored = "C:\\notes\\node_modules\\" GlobMatcher.ignores glob ignored |> Assert.True - + let ignored = "C:\\notes\\node_modules\\foo.md" GlobMatcher.ignores glob ignored |> Assert.True let notIgnored = "C:\\notes\\real.md" GlobMatcher.ignores glob notIgnored |> Assert.False - + [] let relGlob_Win_2 () = if RuntimeInformation.IsOSPlatform(OSPlatform.Windows) then let root = "C:\\notes" let glob = GlobMatcher.mk root [| "node_modules/" |] - + let ignored = "C:\\notes\\sub\\node_modules" GlobMatcher.ignores glob ignored |> Assert.True - + let ignored = "C:\\notes\\sub\\node_modules\\" GlobMatcher.ignores glob ignored |> Assert.True - + let ignored = "C:\\notes\\sub\\node_modules\\foo.md" GlobMatcher.ignores glob ignored |> Assert.True