Skip to content

Commit

Permalink
Renamed file in --check mode (#874)
Browse files Browse the repository at this point in the history
* Renamed file in --check mode. Fixes #869

* Fixed failing check test

* Removed unused value

* Fix failing check test.

* Update to alpha-007
  • Loading branch information
nojaf authored Jun 1, 2020
1 parent 5cda99e commit 8824f4a
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 18 deletions.
2 changes: 1 addition & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
### 4.0.0-alpha-006 - 05/2020
### 4.0.0-alpha-007 - 06/2020
* WIP for [#705](https://github.com/fsprojects/fantomas/issues/705)

### 3.3.0 - 02/2020
Expand Down
16 changes: 14 additions & 2 deletions src/Fantomas.CoreGlobalTool.Tests/CheckTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let b= a + 123
"""

[<Literal>]
let WithErrors = """le a 2"""
let WithErrors = """let a ="""

[<Literal>]
let CorrectlyFormatted = """module A
Expand All @@ -28,11 +28,23 @@ let ``formatted files should report exit code 0``() =
[<Test>]
let ``invalid files should report exit code 1``() =
use fileFixture = new TemporaryFileCodeSample(WithErrors)
let (exitCode,_) = checkCode fileFixture.Filename
let (exitCode, _) = checkCode fileFixture.Filename
exitCode |> should equal 1

[<Test>]
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
<GenerateProgramFile>false</GenerateProgramFile>
<Version>4.0.0-alpha-006</Version>
<Version>4.0.0-alpha-007</Version>
<NoWarn>FS0988</NoWarn>
</PropertyGroup>
<ItemGroup>
Expand Down
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
2 changes: 1 addition & 1 deletion src/Fantomas.CoreGlobalTool/Fantomas.CoreGlobalTool.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
<ToolCommandName>fantomas</ToolCommandName>
<PackAsTool>True</PackAsTool>
<Version>4.0.0-alpha-006</Version>
<Version>4.0.0-alpha-007</Version>
<AssemblyName>fantomas-tool</AssemblyName>
</PropertyGroup>
<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions src/Fantomas.Tests/CheckTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ let b= a + 123
"""

[<Literal>]
let WithErrors = """le a 2"""
let WithErrors = """let a"""

[<Literal>]
let CorrectlyFormated = """module A
let CorrectlyFormatted = """module A
"""

[<Test>]
let ``formatted files should report no changes``() =
use fileFixture = new TemporaryFileCodeSample(CorrectlyFormated)
use fileFixture = new TemporaryFileCodeSample(CorrectlyFormatted)

let result =
fileFixture.Filename
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas.Tests/Fantomas.Tests.fsproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\netfx.props" />
<PropertyGroup>
<Version>4.0.0-alpha-006</Version>
<Version>4.0.0-alpha-007</Version>
<NoWarn>FS0988</NoWarn>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
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
2 changes: 1 addition & 1 deletion src/Fantomas/Fantomas.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Import Project="..\netfx.props" />
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>4.0.0-alpha-006</Version>
<Version>4.0.0-alpha-007</Version>
<Description>Source code formatter for F#</Description>
</PropertyGroup>
<ItemGroup>
Expand Down

0 comments on commit 8824f4a

Please sign in to comment.