This repository has been archived by the owner on Oct 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Redesign option parsing for executables. Fix #1578
- Loading branch information
1 parent
f340cf1
commit 0fc1c72
Showing
4 changed files
with
120 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
module OptionsSpec where | ||
|
||
import Prelude hiding (unzip) | ||
import Data.List.NonEmpty(unzip) | ||
import Test.Hspec | ||
import Options.Applicative | ||
import Haskell.Ide.Engine.Options(GlobalOpts(..), RunMode(..), ProjectLoadingOpts(..), optionParser) | ||
import System.Exit(ExitCode(..)) | ||
import Data.List(isPrefixOf) | ||
|
||
main :: IO () | ||
main = hspec spec | ||
|
||
spec :: Spec | ||
spec = do | ||
let defaultGlobalOptions = GlobalOpts False Nothing Nothing False Nothing False (ProjectLoadingMode $ ProjectLoadingOpts False []) | ||
let getParseFailure (Failure x) = Just (renderFailure x "hie") | ||
getParseFailure _ = Nothing | ||
let sut = optionParser | ||
let parserInfo = info sut mempty | ||
let parserPrefs = prefs mempty | ||
let runSut :: [String] -> ParserResult GlobalOpts | ||
runSut = execParserPure parserPrefs parserInfo | ||
|
||
describe "cmd option parsing" $ do | ||
describe "compiler flag" $ do | ||
let input = ["--compiler"] | ||
let result = runSut input | ||
let (maybeMessage, maybeStatusCode) = unzip $ getParseFailure result | ||
|
||
it "should return ghc version" $ | ||
maybeMessage `shouldSatisfy` any ("ghc" `isPrefixOf`) | ||
it "should return exit code 0" $ | ||
maybeStatusCode `shouldBe` Just ExitSuccess | ||
|
||
describe "numeric version flag" $ do | ||
let input = ["--numeric-version"] | ||
let result = runSut input | ||
let (maybeMessage, maybeStatusCode) = unzip $ getParseFailure result | ||
|
||
it "should return version" $ | ||
maybeMessage `shouldBe` Just "1.1" | ||
it "shoud return exit code 0" $ | ||
maybeStatusCode `shouldBe` Just ExitSuccess | ||
|
||
describe "not providing arguments" $ do | ||
let input = [] | ||
let result = runSut input | ||
let maybeGlobalOptions = getParseResult result | ||
|
||
it "should result in default options" $ | ||
maybeGlobalOptions `shouldBe` Just defaultGlobalOptions | ||
|
||
describe "lsp flag" $ do | ||
let input = ["--lsp"] | ||
let result = runSut input | ||
let maybeGlobalOptions = getParseResult result | ||
|
||
it "should result in default lsp options" $ | ||
maybeGlobalOptions `shouldBe` Just (GlobalOpts False Nothing Nothing False Nothing False LspMode) | ||
|
||
describe "providing two unmatching arguments" $ do | ||
let input = ["--lsp", "--dry-run"] | ||
let result = runSut input | ||
let (maybeMessage, maybeStatusCode) = unzip $ getParseFailure result | ||
|
||
it "should return expected error message" $ | ||
maybeMessage `shouldSatisfy` any ("Invalid option `--dry-run'" `isPrefixOf`) | ||
it "should return error exit code 1" $ | ||
maybeStatusCode `shouldBe` Just (ExitFailure 1) |