Skip to content

Commit

Permalink
Refactor FormatDocumentResponse to DU.
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Aug 1, 2021
1 parent 3f6a6b6 commit dec961f
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 60 deletions.
14 changes: 12 additions & 2 deletions src/Fantomas.Client/Contracts.fs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@ type FormatDocumentRequest =
/// File path will be used to identify the .editorconfig options
/// Unless the configuration is passed
FilePath: string
/// Determines the underlying F# ParsingOptions
IsLastFile: bool
/// Overrides the found .editorconfig.
Config: IReadOnlyDictionary<string, string> option }

type FormatDocumentResponse = { Formatted: string }
[<RequireQualifiedAccess>]
type FormatDocumentResponse =
| Formatted of filename: string * formattedContent: string
| Unchanged of filename: string
| Error of filename: string * formattingError: Exception
| IgnoredFile of filename: string

type FormatSelectionRequest =
{ SourceCode: string
Expand All @@ -52,7 +59,10 @@ and FormatSelectionRange =
EndColumn = endColumn }
end

type FormatSelectionResponse = { Formatted: string }
[<RequireQualifiedAccess>]
type FormatSelectionResponse =
| Formatted of filename: string * formattedContent: string
| Error of filename: string * formattingError: Exception

type FantomasOption = { Type: string; DefaultValue: string }

Expand Down
172 changes: 137 additions & 35 deletions src/Fantomas.CoreGlobalTool.Tests/DaemonTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,79 @@ let private assertFormatted (actual: string) (expected: string) : unit =
String.normalizeNewLine actual
|> should equal (String.normalizeNewLine expected)

let mutable client: FantomasService = Unchecked.defaultof<FantomasService>

[<SetUp>]
let ``create client`` () =
let processStart = getFantomasToolStartInfo "--daemon"
client <- new LSPFantomasService(processStart)

[<TearDown>]
let ``dispose client`` () = client.Dispose()

[<Test>]
let ``compare the version with the public api`` () =
async {
let processStart = getFantomasToolStartInfo "--daemon"
use client = new LSPFantomasService(processStart)
let! { Version = version } = (client :> FantomasService).VersionAsync()
let! { Version = version } = client.VersionAsync()

version
|> should equal (CodeFormatter.GetVersion())
}

[<Test>]
let ``format document`` () =
let ``format implementation file`` () =
async {
let processStart = getFantomasToolStartInfo "--daemon"
use client = new LSPFantomasService(processStart)
let sourceCode = "module Foobar"
use codeFile = new TemporaryFileCodeSample(sourceCode)

let request =
{ SourceCode = sourceCode
FilePath = codeFile.Filename
IsLastFile = false
Config = None }

let! response =
(client :> FantomasService)
.FormatDocumentAsync(request)
let! response = client.FormatDocumentAsync(request)

match response with
| FormatDocumentResponse.Formatted (_, formatted) ->
assertFormatted
formatted
"module Foobar
"
| otherResponse -> Assert.Fail $"Unexpected response %A{otherResponse}"
}

[<Test>]
let ``format signature file`` () =
async {
let sourceCode = "module Foobar\n\nval meh : int"

use codeFile =
new TemporaryFileCodeSample(sourceCode, extension = "fsi")

let request =
{ SourceCode = sourceCode
FilePath = codeFile.Filename
IsLastFile = false
Config = None }

let! response = client.FormatDocumentAsync(request)

assertFormatted
response.Formatted
"module Foobar
match response with
| FormatDocumentResponse.Formatted (_, formatted) ->
assertFormatted
formatted
"module Foobar
val meh : int
"
| otherResponse -> Assert.Fail $"Unexpected response %A{otherResponse}"
}


[<Test>]
let ``format document respecting .editorconfig file`` () =
async {
let processStart = getFantomasToolStartInfo "--daemon"
use client = new LSPFantomasService(processStart)
let sourceCode = "module Foo\n\nlet a = //\n 4"
use codeFile = new TemporaryFileCodeSample(sourceCode)

Expand All @@ -59,26 +93,26 @@ let ``format document respecting .editorconfig file`` () =
let request =
{ SourceCode = sourceCode
FilePath = codeFile.Filename
IsLastFile = false
Config = None }

let! response =
(client :> FantomasService)
.FormatDocumentAsync(request)
let! response = client.FormatDocumentAsync(request)

assertFormatted
response.Formatted
"module Foo
match response with
| FormatDocumentResponse.Formatted (_, formatted) ->
assertFormatted
formatted
"module Foo
let a = //
4
"
| otherResponse -> Assert.Fail $"Unexpected response %A{otherResponse}"
}

[<Test>]
let ``custom configuration has precedence over .editorconfig file`` () =
async {
let processStart = getFantomasToolStartInfo "--daemon"
use client = new LSPFantomasService(processStart)
let sourceCode = "module Foo\n\nlet a = //\n 4"
use codeFile = new TemporaryFileCodeSample(sourceCode)

Expand All @@ -88,19 +122,86 @@ let ``custom configuration has precedence over .editorconfig file`` () =
let request =
{ SourceCode = sourceCode
FilePath = codeFile.Filename
IsLastFile = false
Config = Some(readOnlyDict [ "indent_size", "4" ]) }

let! response =
(client :> FantomasService)
.FormatDocumentAsync(request)
let! response = client.FormatDocumentAsync(request)

assertFormatted
response.Formatted
"module Foo
match response with
| FormatDocumentResponse.Formatted (_, formatted) ->
assertFormatted
formatted
"module Foo
let a = //
4
"
| otherResponse -> Assert.Fail $"Unexpected response %A{otherResponse}"
}

[<Test>]
let ``already formatted file returns unchanged`` () =
async {
let sourceCode = "let a = x\n"

use codeFile =
new TemporaryFileCodeSample(sourceCode, extension = "fsx")

let request =
{ SourceCode = sourceCode
FilePath = codeFile.Filename
IsLastFile = true
Config = Some(readOnlyDict [ "end_of_line", "lf" ]) }

let! response = client.FormatDocumentAsync(request)

match response with
| FormatDocumentResponse.Unchanged fileName -> fileName |> should equal codeFile.Filename
| otherResponse -> Assert.Fail $"Unexpected response %A{otherResponse}"
}

[<Test>]
let ``ignored file returns ignored`` () =
async {
let sourceCode = "let a = x\n"

use codeFile =
new TemporaryFileCodeSample(sourceCode, extension = "fsx")

use _ignoreFile = new FantomasIgnoreFile("*.fsx")

let request =
{ SourceCode = sourceCode
FilePath = codeFile.Filename
IsLastFile = true
Config = None }

let! response = client.FormatDocumentAsync(request)

match response with
| FormatDocumentResponse.IgnoredFile fileName -> fileName |> should equal codeFile.Filename
| otherResponse -> Assert.Fail $"Unexpected response %A{otherResponse}"
}

[<Test>]
let ``format invalid code`` () =
async {
let sourceCode = "module Foobar\n\nlet ziggy ="
use codeFile = new TemporaryFileCodeSample(sourceCode)

let request =
{ SourceCode = sourceCode
FilePath = codeFile.Filename
IsLastFile = false
Config = None }

let! response = client.FormatDocumentAsync(request)

match response with
| FormatDocumentResponse.Error (fileName, error) ->
fileName |> should equal codeFile.Filename
StringAssert.StartsWith("Parsing failed with errors:", error.Message)
| otherResponse -> Assert.Fail $"Unexpected response %A{otherResponse}"
}

[<Test>]
Expand All @@ -113,8 +214,6 @@ let x = 4
let y = 5
"""

let processStart = getFantomasToolStartInfo "--daemon"
use client = new LSPFantomasService(processStart)
use _codeFile = new TemporaryFileCodeSample(sourceCode)

let request: FormatSelectionRequest =
Expand All @@ -125,14 +224,16 @@ let y = 5
Config = None
Range = range }

let! response =
(client :> FantomasService)
.FormatSelectionAsync(request)
let! response = client.FormatSelectionAsync(request)

assertFormatted response.Formatted "let x = 4\n"
match response with
| FormatSelectionResponse.Formatted (fileName, formatted) ->
fileName |> should equal "tmp.fsx"
assertFormatted formatted "let x = 4\n"
| otherResponse -> Assert.Fail $"Unexpected response %A{otherResponse}"
}


(*
[<Test>]
let ``find fantomas tool from working directory`` () =
async {
Expand Down Expand Up @@ -161,3 +262,4 @@ let ``find fantomas tool from working directory`` () =
let formattedCode = formattedResponse
()
}
*)
9 changes: 6 additions & 3 deletions src/Fantomas.CoreGlobalTool.Tests/TestHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ type TemporaryFileCodeSample
codeSnippet: string,
?hasByteOrderMark: bool,
?fileName: string,
?subFolder: string
?subFolder: string,
?extension: string
) =
let hasByteOrderMark = defaultArg hasByteOrderMark false

Expand All @@ -22,15 +23,17 @@ type TemporaryFileCodeSample
| Some fn -> fn
| None -> Guid.NewGuid().ToString()

let extension = Option.defaultValue "fs" extension

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

if not (Directory.Exists(tempFolder)) then
Directory.CreateDirectory(tempFolder) |> ignore

Path.Join(tempFolder, sprintf "%s.fs" name)
| None -> Path.Join(Path.GetTempPath(), sprintf "%s.fs" name)
Path.Join(tempFolder, sprintf "%s.%s" name extension)
| None -> Path.Join(Path.GetTempPath(), sprintf "%s.%s" name extension)

do
(if hasByteOrderMark then
Expand Down
Loading

0 comments on commit dec961f

Please sign in to comment.