From 77d724897b6349328274aa28e26e04c1fa83db48 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Mon, 1 Mar 2021 17:25:13 +0100 Subject: [PATCH] Test v1 completion protocol (cherry picked from commit 9934680993727f41dc2e6867d92f0d6074d53725) --- src/Options/Applicative.hs | 1 + src/Options/Applicative/BashCompletion.hs | 6 +++++ tests/test.hs | 30 +++++++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/src/Options/Applicative.hs b/src/Options/Applicative.hs index 4d84e033..ea4f4b6e 100644 --- a/src/Options/Applicative.hs +++ b/src/Options/Applicative.hs @@ -215,6 +215,7 @@ module Options.Applicative ( Completer, mkCompleter, CompletionItem(..), + CompletionItemOptions(..), mkCompleterWithOptions, listIOCompleter, diff --git a/src/Options/Applicative/BashCompletion.hs b/src/Options/Applicative/BashCompletion.hs index cff22498..20874994 100644 --- a/src/Options/Applicative/BashCompletion.hs +++ b/src/Options/Applicative/BashCompletion.hs @@ -31,6 +31,12 @@ data Features = Features , protocolVersion :: Int } +-- | Version of the output format that the shell integration script +-- expects optparse-applicative to write to stdout. +-- +-- Version increases should be rare, because most changes +-- can be handled by adding a new % keyword. Unknown keywords +-- are ignored by the shell integration scripts. currentProtocolVerson :: Int currentProtocolVerson = 1 diff --git a/tests/test.hs b/tests/test.hs index 57f4ead4..2507276a 100644 --- a/tests/test.hs +++ b/tests/test.hs @@ -468,6 +468,36 @@ prop_completion_rich_lengths = once . ioProperty $ Failure _ -> return $ counterexample "unexpected failure" failed Success val -> return $ counterexample ("unexpected result " ++ show val) failed +prop_completion_v1_default :: Property +prop_completion_v1_default = once . ioProperty $ + let p :: Parser String + p = strArgument (completer (mkCompleterWithOptions (pure (pure [CompletionItem mempty "reachable"])))) + i = info p idm + result = run i [ "--optparse-completion-version", "1" + , "--bash-completion-index=0" + ] + in case result of + CompletionInvoked (CompletionResult err) -> do + completions <- lines <$> err "test" + return $ ["%addspace", "%files", "%value", "reachable"] === completions + Failure _ -> return $ counterexample "unexpected failure" failed + Success val -> return $ counterexample ("unexpected result " ++ show val) failed + +prop_completion_v1_minimal :: Property +prop_completion_v1_minimal = once . ioProperty $ + let p :: Parser String + p = strArgument (completer (mkCompleterWithOptions (pure (pure [CompletionItem (mempty { cioAddSpace = False, cioFiles = False }) "reachable"])))) + i = info p idm + result = run i [ "--optparse-completion-version", "1" + , "--bash-completion-index=0" + ] + in case result of + CompletionInvoked (CompletionResult err) -> do + completions <- lines <$> err "test" + return $ ["%value", "reachable"] === completions + Failure _ -> return $ counterexample "unexpected failure" failed + Success val -> return $ counterexample ("unexpected result " ++ show val) failed + prop_bind_usage :: Property prop_bind_usage = once $ let p :: Parser [String]