Skip to content

Commit

Permalink
Fix .fantomasignore support (fsprojects#1920)
Browse files Browse the repository at this point in the history
* Fix .fantomasignore support

- Don't call MAB.DotIgnore.IgnoreList.IsIgnored() with absolute paths
- Remove path prefix (".\" or: "./") when calling MAB.DotIgnore.IgnoreList.IsIgnored()

* add unit test 'ignore specific file in subfolder'

- extend TestHelpers.TemporaryFileCodeSample to support multiple sub folders
  otherwise we would call --recurse in Path.GetTempPath(), which might contain unrelated .fs files
- adjust other tests

* Add back the original subFolder ctr parameter alongside the new subFolders parameter.

- Internally use an array and let subfolders take precedence over subfolder

* Remove now superfluous blank lines

* Call Directory.Delete() with the topmost subfolder and recursive = true

Co-authored-by: David Schaefer <david.schaefer@rhein-spree.com>
  • Loading branch information
dawedawe and David Schaefer authored Oct 24, 2021
1 parent 0f59676 commit 3032b53
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
18 changes: 18 additions & 0 deletions src/Fantomas.CoreGlobalTool.Tests/IgnoreFilesTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@ let ``ignore specific file`` () =

output |> should contain "was ignored"

[<Test>]
let ``ignore specific file in subfolder`` () =
let fileName = "A"
let sub1 = System.Guid.NewGuid().ToString("N")
let sub2 = System.Guid.NewGuid().ToString("N")
let subFolders = [| sub1; sub2 |]

use inputFixture =
new TemporaryFileCodeSample(Source, fileName = fileName, subFolders = subFolders)

use ignoreFixture =
new FantomasIgnoreFile(sprintf "%s/%s/A.fs" sub1 sub2)

let { ExitCode = exitCode; Output = output } =
runFantomasTool (sprintf "--recurse --check .%c%s" Path.DirectorySeparatorChar sub1)

exitCode |> should equal 0

[<Test>]
let ``don't ignore other files`` () =
let fileName = "B"
Expand Down
22 changes: 16 additions & 6 deletions src/Fantomas.CoreGlobalTool.Tests/TestHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,29 @@ type TemporaryFileCodeSample
codeSnippet: string,
?hasByteOrderMark: bool,
?fileName: string,
?subFolder: string
?subFolder: string,
?subFolders: string array
) =
let hasByteOrderMark = defaultArg hasByteOrderMark false

let internalSubFolders =
match subFolders with
| Some sf -> Some sf
| None ->
match subFolder with
| Some sf -> Array.singleton sf |> Some
| None -> None

let filename =
let name =
match fileName with
| Some fn -> fn
| None -> Guid.NewGuid().ToString()

match subFolder with
match internalSubFolders with
| Some sf ->
let tempFolder = Path.Join(Path.GetTempPath(), sf)
let tempFolder =
Path.Join(Path.GetTempPath(), Path.Join(sf))

if not (Directory.Exists(tempFolder)) then
Directory.CreateDirectory(tempFolder) |> ignore
Expand All @@ -44,11 +54,11 @@ type TemporaryFileCodeSample
member this.Dispose() : unit =
File.Delete(filename)

subFolder
internalSubFolders
|> Option.iter
(fun sf ->
Path.Join(Path.GetTempPath(), sf)
|> Directory.Delete)
let path = Path.Join(Path.GetTempPath(), sf.[0])
Directory.Delete(path, true))

type OutputFile internal () =
let filename =
Expand Down
13 changes: 11 additions & 2 deletions src/Fantomas.Extras/IgnoreFile.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,22 @@ module IgnoreFile =
let path = getIgnoreFilePath ()
File.Exists path |> not

let private relativePathPrefix =
sprintf ".%c" Path.DirectorySeparatorChar

let private removeRelativePathPrefix (path: string) =
if path.StartsWith(relativePathPrefix) then
path.Substring(2)
else
path

let isIgnoredFile (file: string) =
if hasNoIgnoreFile () then
false
else
try
let fullPath = Path.GetFullPath(file)
ignores.Value.IsIgnored(fullPath, false)
let path = removeRelativePathPrefix file
ignores.Value.IsIgnored(path, false)
with
| ex ->
printfn "%A" ex
Expand Down

0 comments on commit 3032b53

Please sign in to comment.