Skip to content

Commit

Permalink
Refactor FSharpScriptTests to use helpers from FormatConfigEditorConf…
Browse files Browse the repository at this point in the history
…igurationFileTests. (#1847)
  • Loading branch information
nojaf authored Jul 31, 2021
1 parent 2fcb03a commit d03a398
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 38 deletions.
50 changes: 23 additions & 27 deletions src/Fantomas.Tests/FSharpScriptTests.fs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
module Fantomas.Tests.FSharpScriptTests

open Fantomas
open Fantomas.Extras
open System.IO
open NUnit.Framework
open FsUnit
open Fantomas
open Fantomas.Extras
open Fantomas.Tests.TestHelper
open System.IO
open Fantomas.Tests.FormatConfigEditorConfigurationFileTests

[<Test>]
let ``source _directory keyword should not be replace with actual path`` () =
Expand All @@ -31,16 +32,17 @@ let ``e2e script test with keyword __source__directory__`` () =
#load ".paket/load/net471/main.group.fsx"
"""

let file =
Path.Combine(Path.GetTempPath(), System.Guid.NewGuid().ToString("N") + ".fsx")
let rootFolderName = tempName ()

File.WriteAllText(file, source)
use fsharpScript =
new FSharpFile(rootFolderName, fsharpFileExtension = ".fsx", content = source)

let! formattedFiles = FakeHelpers.formatCode [ file ]
let! formattedFiles = FakeHelpers.formatCode [ fsharpScript.FSharpFile ]

let formattedSource =
File.ReadAllText(fsharpScript.FSharpFile)

let formattedSource = File.ReadAllText(file)
Array.length formattedFiles == 1
File.Delete(file)

formattedSource
|> String.normalizeNewLine
Expand All @@ -61,29 +63,24 @@ let ``fantomas removes module and namespace if it is only 1 word`` () =
type Counter = int
"""

let file =
Path.Combine(Path.GetTempPath(), System.Guid.NewGuid().ToString("N") + ".fsx")
let rootFolderName = tempName ()

File.WriteAllText(file, source)
use fsharpScript =
new FSharpFile(rootFolderName, fsharpFileExtension = ".fsx", content = source)

let fantomasConfig =
{ FormatConfig.FormatConfig.Default with
StrictMode = true
IndentSize = 2
SpaceBeforeColon = false }
|> EditorConfig.configToEditorConfig

let editorConfigPath =
Path.Combine(Path.GetTempPath(), ".editorconfig")

File.WriteAllText(editorConfigPath, fantomasConfig)
use _editorConfig =
new ConfigurationFile(fantomasConfig, rootFolderName)

let! formattedFiles = FakeHelpers.formatCode [ file ]
let! formattedFiles = FakeHelpers.formatCode [ fsharpScript.FSharpFile ]

let formattedSource = File.ReadAllText(file)
let formattedSource = File.ReadAllText fsharpScript.FSharpFile
Array.length formattedFiles == 1
File.Delete(file)
File.Delete(editorConfigPath)

formattedSource
|> String.normalizeNewLine
Expand All @@ -107,16 +104,15 @@ let ``number in the filename should not end up in the module name`` () =
| _ -> printfn "x is %s" x
"""

let file =
Path.Combine(Path.GetTempPath(), "60Seconds.fsx")
let rootFolderName = tempName ()

File.WriteAllText(file, source)
use fsharpScript =
new FSharpFile(rootFolderName, fileName = "60Seconds.fsx", content = source)

async {
let! formattedFiles = FakeHelpers.formatCode [| file |]
let formattedSource = File.ReadAllText(file)
let! formattedFiles = FakeHelpers.formatCode [| fsharpScript.FSharpFile |]
let formattedSource = File.ReadAllText fsharpScript.FSharpFile
Array.length formattedFiles == 1
File.Delete(file)

formattedSource
|> String.normalizeNewLine
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas.Tests/Fantomas.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@
<Compile Include="DynamicOperatorTests.fs" />
<Compile Include="SynExprSetTests.fs" />
<Compile Include="SynConstTests.fs" />
<Compile Include="FSharpScriptTests.fs" />
<Compile Include="TupleTests.fs" />
<Compile Include="ElmishTests.fs" />
<Compile Include="LambdaTests.fs" />
<Compile Include="IfThenElseTests.fs" />
<Compile Include="FormatConfigEditorConfigurationFileTests.fs" />
<Compile Include="FSharpScriptTests.fs" />
<Compile Include="CheckTests.fs" />
<Compile Include="ContextTests.fs" />
<Compile Include="SpaceBeforeSemicolonTests.fs" />
Expand Down
40 changes: 30 additions & 10 deletions src/Fantomas.Tests/FormatConfigEditorConfigurationFileTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ open NUnit.Framework
open System.IO

let private defaultConfig = FormatConfig.Default
let private tempName () = Guid.NewGuid().ToString("N")
let tempName () = Guid.NewGuid().ToString("N")

type ConfigurationFile
internal
Expand Down Expand Up @@ -60,7 +60,15 @@ type ConfigurationFile
if Directory.Exists(rootDir) then
Directory.Delete(rootDir, true)

type FSharpFile internal (rootFolderName: string, ?fsharpFileExtension: string, ?subFolder: string) =
type FSharpFile
internal
(
rootFolderName: string,
?fsharpFileExtension: string,
?subFolder: string,
?content: string,
?fileName: string
) =
let rootDir =
Path.Join(Path.GetTempPath(), rootFolderName)

Expand All @@ -71,7 +79,8 @@ type FSharpFile internal (rootFolderName: string, ?fsharpFileExtension: string,
let extension =
Option.defaultValue ".fs" fsharpFileExtension

let fsharpFile = sprintf "%s%s" (tempName ()) extension
let fsharpFile =
Option.defaultValue (sprintf "%s%s" (tempName ()) extension) fileName

let fsharpFilePath =
match subFolder with
Expand All @@ -84,7 +93,8 @@ type FSharpFile internal (rootFolderName: string, ?fsharpFileExtension: string,
Path.Join(rootDir, sf, fsharpFile)
| None -> Path.Join(rootDir, fsharpFile)

do File.WriteAllText(fsharpFilePath, String.empty)
let content = Option.defaultValue String.empty content
do File.WriteAllText(fsharpFilePath, content)

member __.FSharpFile: string = fsharpFilePath

Expand All @@ -95,10 +105,13 @@ type FSharpFile internal (rootFolderName: string, ?fsharpFileExtension: string,

[<Test>]
let ``single configuration file`` () =
let rootFolderName = tempName ()

use configFixture =
new ConfigurationFile(defaultConfig, tempName ())
new ConfigurationFile(defaultConfig, rootFolderName)

use fsharpFile = new FSharpFile(".fs")
use fsharpFile =
new FSharpFile(rootFolderName, fsharpFileExtension = ".fs")

let config =
EditorConfig.readConfiguration fsharpFile.FSharpFile
Expand Down Expand Up @@ -152,14 +165,17 @@ let ``parent config should not be taking into account when child is root`` () =

[<Test>]
let ``configuration file should not affect file extension`` () =
let rootFolder = tempName ()

use configFixture =
new ConfigurationFile(
{ defaultConfig with
MaxLineLength = 90 },
tempName ()
rootFolder
)

use fsharpFile = new FSharpFile(".fsx")
use fsharpFile =
new FSharpFile(rootFolder, fsharpFileExtension = ".fsx")

let config =
EditorConfig.readConfiguration fsharpFile.FSharpFile
Expand Down Expand Up @@ -193,8 +209,10 @@ fsharp_max_function_binding_width=40

[<Test>]
let ``non existing file should return defaults for readConfiguration`` () =
let rootDir = tempName ()

use configFixture =
new ConfigurationFile(defaultConfig, tempName ())
new ConfigurationFile(defaultConfig, rootDir)

let config =
EditorConfig.readConfiguration "bogus.fs"
Expand All @@ -207,8 +225,10 @@ let ``non existing file should return defaults for readConfiguration`` () =

[<Test>]
let ``non existing file should return None for tryReadConfiguration`` () =
let rootDir = tempName ()

use configFixture =
new ConfigurationFile(defaultConfig, tempName ())
new ConfigurationFile(defaultConfig, rootDir)

let config =
EditorConfig.tryReadConfiguration "bogus.fs"
Expand Down

0 comments on commit d03a398

Please sign in to comment.