Skip to content

Commit

Permalink
Renamed file in --check mode. Fixes fsprojects#869
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed May 29, 2020
1 parent 1a53522 commit 2cc227a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
12 changes: 12 additions & 0 deletions src/Fantomas.CoreGlobalTool.Tests/CheckTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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

[<Test>]
let ``check with Program.fs file`` () =
let codeSnippet = """[<EntryPoint>]
let main _ = 0
"""

use fileFixture =
new TemporaryFileCodeSample(codeSnippet, fileName = "Program")

let (exitCode, _) = checkCode fileFixture.Filename
exitCode |> should equal 0
12 changes: 10 additions & 2 deletions src/Fantomas.CoreGlobalTool.Tests/TestHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
16 changes: 10 additions & 6 deletions src/Fantomas/FakeHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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 =
Expand Down

0 comments on commit 2cc227a

Please sign in to comment.