From a292b4e07b39f5d6f58c261164fb76d7ef334ad1 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Sun, 15 Jan 2012 01:26:36 +0100
Subject: [PATCH] Add unit-tests for --help/--version as common options
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

After reorganizing the way we process options, we can finally add a
test that all program personalities accept --help and --version and
that they exit early for these two.

This trivial patch has a higher than expected impact on the coverage,
as the use of short options finally enforces evaluation of the short
option names, thus it affects more than just the help/version options.

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: RenΓ© Nussbaumer <rn@google.com>
---
 htools/Ganeti/HTools/QC.hs | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/htools/Ganeti/HTools/QC.hs b/htools/Ganeti/HTools/QC.hs
index e87aabbad..d7f8f5b3e 100644
--- a/htools/Ganeti/HTools/QC.hs
+++ b/htools/Ganeti/HTools/QC.hs
@@ -75,6 +75,7 @@ import qualified Ganeti.HTools.Utils as Utils
 import qualified Ganeti.HTools.Version
 import qualified Ganeti.Constants as C
 
+import qualified Ganeti.HTools.Program as Program
 import qualified Ganeti.HTools.Program.Hail
 import qualified Ganeti.HTools.Program.Hbal
 import qualified Ganeti.HTools.Program.Hscan
@@ -1511,9 +1512,29 @@ prop_CLI_StringArg argument =
              ]
   in conjoin $ map (checkStringArg argument) args
 
+-- | Helper to test that a given option is accepted OK with quick exit.
+checkEarlyExit name options param =
+  case CLI.parseOptsInner [param] name options of
+    Left (code, _) -> if code == 0
+                          then property True
+                          else failTest $ "Program " ++ name ++
+                                 " returns invalid code " ++ show code ++
+                                 " for option " ++ param
+    _ -> failTest $ "Program " ++ name ++ " doesn't consider option " ++
+         param ++ " as early exit one"
+
+-- | Test that all binaries support some common options. There is
+-- nothing actually random about this test...
+prop_CLI_stdopts =
+  let params = ["-h", "--help", "-V", "--version"]
+      opts = map (\(name, (_, o)) -> (name, o)) Program.personalities
+      -- apply checkEarlyExit across the cartesian product of params and opts
+  in conjoin [checkEarlyExit n o p | p <- params, (n, o) <- opts]
+
 testSuite "CLI"
           [ 'prop_CLI_parseISpec
           , 'prop_CLI_parseISpecFail
           , 'prop_CLI_parseYesNo
           , 'prop_CLI_StringArg
+          , 'prop_CLI_stdopts
           ]
-- 
GitLab