diff --git a/src/Fantomas.Tests/FSharpScriptTests.fs b/src/Fantomas.Tests/FSharpScriptTests.fs index 36be61888d..498a7144ca 100644 --- a/src/Fantomas.Tests/FSharpScriptTests.fs +++ b/src/Fantomas.Tests/FSharpScriptTests.fs @@ -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 [] let ``source _directory keyword should not be replace with actual path`` () = @@ -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 @@ -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 @@ -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 diff --git a/src/Fantomas.Tests/Fantomas.Tests.fsproj b/src/Fantomas.Tests/Fantomas.Tests.fsproj index efb069bc80..338bb82d5b 100644 --- a/src/Fantomas.Tests/Fantomas.Tests.fsproj +++ b/src/Fantomas.Tests/Fantomas.Tests.fsproj @@ -50,12 +50,12 @@ - + diff --git a/src/Fantomas.Tests/FormatConfigEditorConfigurationFileTests.fs b/src/Fantomas.Tests/FormatConfigEditorConfigurationFileTests.fs index 875de2a5eb..9773f316f4 100644 --- a/src/Fantomas.Tests/FormatConfigEditorConfigurationFileTests.fs +++ b/src/Fantomas.Tests/FormatConfigEditorConfigurationFileTests.fs @@ -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 @@ -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) @@ -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 @@ -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 @@ -95,10 +105,13 @@ type FSharpFile internal (rootFolderName: string, ?fsharpFileExtension: string, [] 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 @@ -152,14 +165,17 @@ let ``parent config should not be taking into account when child is root`` () = [] 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 @@ -193,8 +209,10 @@ fsharp_max_function_binding_width=40 [] 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" @@ -207,8 +225,10 @@ let ``non existing file should return defaults for readConfiguration`` () = [] 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"