Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Hspec options #118

Merged
merged 1 commit into from
Jul 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions hie.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cradle:
cabal:
component: sensei:test:spec
73 changes: 53 additions & 20 deletions src/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,63 @@ splitArgs args = case break (== "--") $ reverse args of
x : _ -> (dropEnd (length x) args, x)
[] -> (args, [])
where
isHspecArgs :: [String] -> Bool
isHspecArgs xs = case getOpt Permute options xs of
(_, [], []) -> True
(result, [], []) -> all (== Valid) result
_ -> False

dropEnd :: Int -> [a] -> [a]
dropEnd n = reverse . drop n . reverse

options :: [OptDescr ()]
options = map ($ "") [
Option [] ["help"] (NoArg ())
, Option "m" ["match"] (ReqArg (const ()) "PATTERN")
, Option [] ["skip"] (ReqArg (const ()) "PATTERN")
, Option [] ["color"] (NoArg ())
, Option [] ["no-color"] (NoArg ())
, Option "f" ["format"] (ReqArg (const ()) "FORMATTER")
, Option "o" ["out"] (ReqArg (const ()) "FILE")
, Option [] ["depth"] (ReqArg (const ()) "N")
, Option "a" ["qc-max-success"] (ReqArg (const ()) "N")
, Option [] ["qc-max-size"] (ReqArg (const ()) "N")
, Option [] ["qc-max-discard"] (ReqArg (const ()) "N")
, Option [] ["seed"] (ReqArg (const ()) "N")
, Option [] ["print-cpu-time"] (NoArg ())
, Option [] ["focused-only"] (NoArg ())
, Option [] ["dry-run"] (NoArg ())
, Option [] ["fail-fast"] (NoArg ())
, Option "r" ["rerun"] (NoArg ())
data Valid = Valid | Invalid
deriving (Eq, Show)

options :: [OptDescr Valid]
options = concat [
flag "color"
, flag "diff"
, flag "dry-run"
, flag "expert"
, flag "fail-fast"
, flag "fail-on=ITEMS"
, flag "focused-only"
, flag "pretty"
, flag "randomize"
, flag "strict"
, flag "times"
, flag "unicode"
, noArg "" "help"
, noArg "" "ignore-dot-hspec"
, noArg "" "print-cpu-time"
, noArg "" "rerun-all-on-success"
, noArg "r" "rerun"
, reqArg "" "depth" "N"
, reqArg "" "diff-command" "CMD"
, reqArg "" "diff-context" "N"
, reqArg "" "failure-report" "FILE"
, reqArg "" "jobs" "N"
, reqArg "" "qc-max-discard" "N"
, reqArg "" "qc-max-shrinks" "N"
, reqArg "" "qc-max-size" "N"
, reqArg "" "seed" "N"
, reqArg "" "skip" "PATTERN"
, reqArg "a" "qc-max-success" "N"
, reqArg "f" "format" "NAME"
, reqArg "m" "match" "PATTERN"
, [Option "p" ["print-slow-items"] (OptArg (maybe Valid intArg) "N") ""]
]
where
flag :: String -> [OptDescr Valid]
flag name = concat [
noArg "" name
, noArg "" ("no-" <> name)
]

noArg :: [Char] -> String -> [OptDescr Valid]
noArg short name = [Option short [name] (NoArg Valid) ""]

reqArg :: [Char] -> String -> String -> [OptDescr Valid]
reqArg short name arg = [Option short [name] (ReqArg (const Valid) arg) ""]

intArg :: String -> Valid
intArg = maybe Invalid (const Valid) . readMaybe @Int
14 changes: 12 additions & 2 deletions test/OptionsSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,18 @@ main = hspec spec
spec :: Spec
spec = do
describe "splitArgs" $ do
it "returns longest matching list of Hspec arguments from end of given list" $ do
it "returns longest matching list of Hspec options from end of given list" $ do
splitArgs ["foo", "--bar", "-m", "FooSpec", "-a", "1000"] `shouldBe` (["foo", "--bar"], ["-m", "FooSpec", "-a", "1000"])

it "assumes everything after the last '--' to be Hspec arguments" $ do
it "assumes everything after the last '--' to be Hspec options" $ do
splitArgs ["foo", "bar", "--", "foo", "baz"] `shouldBe` (["foo", "bar"], ["foo", "baz"])

context "with -p" $ do
it "recognizes -p as an Hspec option" $ do
splitArgs ["-p"] `shouldBe` ([], ["-p"])

it "recognizes -pN as an Hspec option" $ do
splitArgs ["-p20"] `shouldBe` ([], ["-p20"])

it "recognizes -packageNAME as a GHC option" $ do
splitArgs ["-packagebase"] `shouldBe` (["-packagebase"], [])