diff --git a/src/Fantomas.CoreGlobalTool.Tests/CheckTests.fs b/src/Fantomas.CoreGlobalTool.Tests/CheckTests.fs index e92c9f9164..312fe9289f 100644 --- a/src/Fantomas.CoreGlobalTool.Tests/CheckTests.fs +++ b/src/Fantomas.CoreGlobalTool.Tests/CheckTests.fs @@ -36,3 +36,15 @@ let ``files that need formatting should report exit code 99``() = use fileFixture = new TemporaryFileCodeSample(NeedsFormatting) let (exitCode,_) = checkCode fileFixture.Filename exitCode |> should equal 99 + +[] +let ``check with Program.fs file`` () = + let codeSnippet = """[] +let main _ = 0 +""" + + use fileFixture = + new TemporaryFileCodeSample(codeSnippet, fileName = "Program") + + let (exitCode, _) = checkCode fileFixture.Filename + exitCode |> should equal 0 diff --git a/src/Fantomas.CoreGlobalTool.Tests/TestHelpers.fs b/src/Fantomas.CoreGlobalTool.Tests/TestHelpers.fs index b01dfef303..c648a18e55 100644 --- a/src/Fantomas.CoreGlobalTool.Tests/TestHelpers.fs +++ b/src/Fantomas.CoreGlobalTool.Tests/TestHelpers.fs @@ -6,9 +6,17 @@ open System.IO open System.Text open Fantomas -type TemporaryFileCodeSample internal (codeSnippet: string, ?hasByteOrderMark: bool) = +type TemporaryFileCodeSample internal (codeSnippet: string, ?hasByteOrderMark: bool, ?fileName: string) = let hasByteOrderMark = defaultArg hasByteOrderMark false - let filename = Path.Join(Path.GetTempPath(), Guid.NewGuid().ToString() + ".fs") + + let filename = + let name = + match fileName with + | Some fn -> fn + | None -> Guid.NewGuid().ToString() + + Path.Join(Path.GetTempPath(), sprintf "%s.fs" name) + do (if hasByteOrderMark then File.WriteAllText(filename, codeSnippet, Encoding.UTF8) else File.WriteAllText(filename, codeSnippet)) diff --git a/src/Fantomas/FakeHelpers.fs b/src/Fantomas/FakeHelpers.fs index 3e1ef40943..db01fd8365 100644 --- a/src/Fantomas/FakeHelpers.fs +++ b/src/Fantomas/FakeHelpers.fs @@ -43,13 +43,17 @@ let createParsingOptionsFromFile fileName = let formatContentAsync config (file: string) (originalContent: string) = async { try + let fileName = + if Path.GetExtension(file) = ".fsi" then "tmp.fsi" else "tmp.fsx" + let! formattedContent = - CodeFormatter.FormatDocumentAsync(file, SourceOrigin.SourceString originalContent, config, - createParsingOptionsFromFile file ,sharedChecker.Value) + CodeFormatter.FormatDocumentAsync(fileName, SourceOrigin.SourceString originalContent, config, + createParsingOptionsFromFile fileName ,sharedChecker.Value) + if originalContent <> formattedContent then let! isValid = - CodeFormatter.IsValidFSharpCodeAsync(file, (SourceOrigin.SourceString(formattedContent)), - createParsingOptionsFromFile file, sharedChecker.Value) + CodeFormatter.IsValidFSharpCodeAsync(fileName, (SourceOrigin.SourceString(formattedContent)), + createParsingOptionsFromFile fileName, sharedChecker.Value) if not isValid then raise <| FormatException "Formatted content is not valid F# code" @@ -63,8 +67,8 @@ let formatContentAsync config (file: string) (originalContent: string) = let formatFileAsync config (file : string) = let originalContent = File.ReadAllText file async { - let! formated = originalContent |> formatContentAsync config file - return formated + let! formatted = originalContent |> formatContentAsync config file + return formatted } let formatFilesAsync config files =