Skip to content

Commit

Permalink
Extend InputPath DU to improve error messages (#2384)
Browse files Browse the repository at this point in the history
* - Extend InputPath DU with cases NoFSharpFile, NotFound to improve error output
- Change exit code from 0 to 1 for --check unspecified
- add some tests

* For InputPath.Unspecified, give tip about --help

* Add CHANGELOG.md entry
  • Loading branch information
dawedawe authored Jul 26, 2022
1 parent 59293a1 commit f668062
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [Unreleased]

### Changed
* Show a better error message when the folder does not exist. [#2341](https://github.com/fsprojects/fantomas/issues/2341)

## [5.0.0-beta-002] - 2022-07-19

### Fixed
Expand Down
16 changes: 16 additions & 0 deletions src/Fantomas.Tests/Integration/CheckTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ let ``invalid files should report exit code 1`` () =
let { ExitCode = exitCode } = checkCode [ fileFixture.Filename ]
exitCode |> should equal 1

[<Test>]
let ``non-existing file should report exit code 1`` () =
let { ExitCode = exitCode } = checkCode [ "somenonexistingfile.fs" ]
exitCode |> should equal 1

[<Test>]
let ``unsupported file should report exit code 1`` () =
use fileFixture = new TemporaryFileCodeSample(CorrectlyFormatted, extension = "txt")
let { ExitCode = exitCode } = checkCode [ fileFixture.Filename ]
exitCode |> should equal 1

[<Test>]
let ``missing file should report exit code 1`` () =
let { ExitCode = exitCode } = checkCode []
exitCode |> should equal 1

[<Test>]
let ``files that need formatting should report exit code 99`` () =
use fileFixture = new TemporaryFileCodeSample(NeedsFormatting)
Expand Down
22 changes: 22 additions & 0 deletions src/Fantomas.Tests/Integration/ExitCodeTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,30 @@ open Fantomas.Tests.TestHelpers
[<Literal>]
let WithErrors = """let a ="""

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

[<Test>]
let ``invalid files should report exit code 1`` () =
use fileFixture = new TemporaryFileCodeSample(WithErrors)
let { ExitCode = exitCode } = formatCode [ fileFixture.Filename ]
exitCode |> should equal 1

[<Test>]
let ``non-existing file should report exit code 1`` () =
let { ExitCode = exitCode } = formatCode [ "somenonexistingfile.fs" ]
exitCode |> should equal 1

[<Test>]
let ``unsupported file should report exit code 1`` () =
use fileFixture = new TemporaryFileCodeSample(CorrectlyFormatted, extension = "txt")
let { ExitCode = exitCode } = formatCode [ fileFixture.Filename ]
exitCode |> should equal 1

[<Test>]
let ``missing file should report exit code 1`` () =
let { ExitCode = exitCode } = formatCode []
exitCode |> should equal 1
24 changes: 20 additions & 4 deletions src/Fantomas/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ type InputPath =
| File of string
| Folder of string
| Multiple of files: string list * folder: string list
| NoFSharpFile of string
| NotFound of string
| Unspecified

[<RequireQualifiedAccess>]
Expand Down Expand Up @@ -186,9 +188,15 @@ let runCheckCommand (recurse: bool) (inputPath: InputPath) : int =
if checkResult.HasErrors then 1 else 99

match inputPath with
| InputPath.NoFSharpFile s ->
eprintfn "Input path '%s' is unsupported file type" s
1
| InputPath.NotFound s ->
eprintfn "Input path '%s' not found" s
1
| InputPath.Unspecified _ ->
eprintfn "No input path provided. Nothing to do."
0
eprintfn "No input path provided. Call with --help for usage information."
1
| InputPath.File f when (IgnoreFile.isIgnoredFile (IgnoreFile.current.Force()) f) ->
printfn "'%s' was ignored" f
0
Expand Down Expand Up @@ -232,8 +240,10 @@ let main argv =
InputPath.Folder input
elif File.Exists input && isFSharpFile input then
InputPath.File input
elif File.Exists input then
InputPath.NoFSharpFile input
else
InputPath.Unspecified
InputPath.NotFound input
| Some inputs ->
let isFolder (path: string) = Path.GetExtension(path) = ""

Expand Down Expand Up @@ -353,8 +363,14 @@ let main argv =
else
try
match inputPath, outputPath with
| InputPath.NoFSharpFile s, _ ->
eprintfn "Input path '%s' is unsupported file type." s
exit 1
| InputPath.NotFound s, _ ->
eprintfn "Input path '%s' not found." s
exit 1
| InputPath.Unspecified, _ ->
eprintfn "Input path is missing..."
eprintfn "Input path is missing. Call with --help for usage information."
exit 1
| InputPath.File f, _ when (IgnoreFile.isIgnoredFile (IgnoreFile.current.Force()) f) ->
printfn "'%s' was ignored" f
Expand Down

0 comments on commit f668062

Please sign in to comment.