Skip to content

Commit

Permalink
Allow passing fantomas-config.json without any path prefix. Fixes fsp…
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed May 15, 2020
1 parent 622e2b0 commit 6ecc4c1
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/Fantomas.CoreGlobalTool.Tests/ByteOrderMarkTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ let private getInitialBytes file =
[<Test>]
let ``byte-order mark should be preserved, 795``() =
use fileFixture = new TemporaryFileCodeSample(Source, true)
let exitCode = runFantomasTool fileFixture.Filename
let (exitCode,_) = runFantomasTool fileFixture.Filename
exitCode |> should equal 0

let expectedPreamble = Encoding.UTF8.GetPreamble()
Expand All @@ -29,7 +29,7 @@ let ``byte-order mark should be preserved, 795``() =
let ``preserve byte-order from original file`` () =
use inputFixture = new TemporaryFileCodeSample(Source, true)
use outputFixture = new OutputFile()
let exitCode =
let (exitCode,_) =
sprintf "--out %s %s" outputFixture.Filename inputFixture.Filename
|> runFantomasTool

Expand Down
10 changes: 5 additions & 5 deletions src/Fantomas.CoreGlobalTool.Tests/CheckTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ let b= a + 123
let WithErrors = """le a 2"""

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

[<Test>]
let ``formatted files should report exit code 0``() =
use fileFixture = new TemporaryFileCodeSample(CorrectlyFormated)
let exitCode = checkCode fileFixture.Filename
use fileFixture = new TemporaryFileCodeSample(CorrectlyFormatted)
let (exitCode,_) = checkCode fileFixture.Filename
exitCode |> should equal 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
let (exitCode,_) = checkCode fileFixture.Filename
exitCode |> should equal 99
22 changes: 22 additions & 0 deletions src/Fantomas.CoreGlobalTool.Tests/ConfigTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Fantomas.CoreGlobalTool.Tests.ConfigTests

open NUnit.Framework
open FsUnit
open Fantomas.CoreGlobalTool.Tests.TestHelpers
open Fantomas.FormatConfig

[<Test>]
let ``config file in working directory should not require relative prefix, 821`` () =
use fileFixture =
new TemporaryFileCodeSample("let a = 9")

use configFixture =
new ConfigurationFile({ FormatConfig.Default with
IndentSpaceNum = 2 })

let (exitCode, output) =
runFantomasTool (sprintf "--config fantomas-config.json %s" fileFixture.Filename)

exitCode |> should equal 0
output
|> should startWith (sprintf "Processing %s" fileFixture.Filename)
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<Compile Include="TestHelpers.fs" />
<Compile Include="CheckTests.fs" />
<Compile Include="ByteOrderMarkTests.fs" />
<Compile Include="ConfigTests.fs" />
</ItemGroup>
<Import Project="..\..\.paket\Paket.Restore.targets" />
</Project>
16 changes: 15 additions & 1 deletion src/Fantomas.CoreGlobalTool.Tests/TestHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ open System
open System.Diagnostics
open System.IO
open System.Text
open Fantomas

type TemporaryFileCodeSample internal (codeSnippet: string, ?hasByteOrderMark: bool) =
let hasByteOrderMark = defaultArg hasByteOrderMark false
Expand All @@ -24,6 +25,16 @@ type OutputFile internal () =
if File.Exists(filename) then
File.Delete(filename)

type ConfigurationFile internal (config: Fantomas.FormatConfig.FormatConfig) =
let filename = Path.Join(Path.GetTempPath(), "fantomas-config.json")
let content = Config.configToJson config
do File.WriteAllText(filename, content)
member _.Filename: string = filename
interface IDisposable with
member this.Dispose(): unit =
if File.Exists(filename) then
File.Delete(filename)

let runFantomasTool arguments =
let pwd = Path.GetDirectoryName(typeof<TemporaryFileCodeSample>.Assembly.Location)
let configuration =
Expand All @@ -42,9 +53,12 @@ let runFantomasTool arguments =
p.StartInfo.UseShellExecute <- false
p.StartInfo.FileName <- @"dotnet"
p.StartInfo.Arguments <- sprintf "%s %s" fantomasDll arguments
p.StartInfo.WorkingDirectory <- Path.GetTempPath()
p.StartInfo.RedirectStandardOutput <- true
p.Start() |> ignore
let output = p.StandardOutput.ReadToEnd()
p.WaitForExit()
p.ExitCode
(p.ExitCode, output)

let checkCode file =
let arguments = sprintf "--check \"%s\"" file
Expand Down
17 changes: 1 addition & 16 deletions src/Fantomas.Tests/FormatConfigJsonConfigurationFileTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,8 @@ open Fantomas.Tests.TestHelper
[<Literal>]
let private configFileName ="fantomas-config.json"

let private configToJson config =
Reflection.getRecordFields config
|> Array.choose (fun (k,v) ->
match v with
| :? System.Boolean as b ->
sprintf "\"%s\":%s" k (if b then "true " else "false")
|> Some
| :? System.Int32 as i ->
sprintf " \"%s\":%d" k i
|> Some
| _ -> None
)
|> String.concat ",\n "
|> sprintf "{ %s }"

let private mkConfig subFolder config =
let json = configToJson config
let json = Config.configToJson config
mkConfigFromContent configFileName subFolder json

let private mkConfigFromJson subFolder json =
Expand Down
11 changes: 9 additions & 2 deletions src/Fantomas/ConfigFile.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module internal Fantomas.ConfigFile

open System
open System.IO
open Fantomas.FormatConfig
open Fantomas.Version
Expand Down Expand Up @@ -28,8 +29,14 @@ let rec findConfigurationFiles fileOrFolder : string list =
|> List.collect findConfigInFolder
elif File.Exists(fileOrFolder) then
let parentFolder =
Directory.GetParent(Path.GetDirectoryName(fileOrFolder))
|> Option.ofObj
if String.IsNullOrWhiteSpace(Path.GetDirectoryName(fileOrFolder)) then
Directory.GetCurrentDirectory()
|> DirectoryInfo
|> Some
else
Directory.GetParent(Path.GetDirectoryName(fileOrFolder))
|> Option.ofObj

match parentFolder with
| Some pf -> findConfigurationFiles pf.FullName @ [fileOrFolder]
| None -> [fileOrFolder]
Expand Down
16 changes: 16 additions & 0 deletions src/Fantomas/Utils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,19 @@ module Reflection =
let names = FSharpType.GetRecordFields (x.GetType()) |> Seq.map (fun x -> x.Name)
let values = FSharpValue.GetRecordFields x
Seq.zip names values |> Seq.toArray

module Config =
let configToJson config =
Reflection.getRecordFields config
|> Array.choose (fun (k,v) ->
match v with
| :? System.Boolean as b ->
sprintf "\"%s\":%s" k (if b then "true " else "false")
|> Some
| :? System.Int32 as i ->
sprintf " \"%s\":%d" k i
|> Some
| _ -> None
)
|> String.concat ",\n "
|> sprintf "{ %s }"

0 comments on commit 6ecc4c1

Please sign in to comment.