예제 #1
0
 def test_capture_none(self):
     """Test capture no sys.exit."""
     with outputAndExitCaptured() as (out, err, status):
         print("hohoho")
         print("hihihi", file=sys.stderr)
     self.assertEqual(out.getvalue(), "hohoho\n")
     self.assertEqual(err.getvalue(), "hihihi\n")
     self.assertEqual(status.value, None)
예제 #2
0
 def test_capture_exit(self):
     with outputAndExitCaptured() as (out, err, status):
         print("hohoho")
         print("hihihi", file=sys.stderr)
         sys.exit("hahaha")
     self.assertEqual(out.getvalue(), "hohoho\n")
     self.assertEqual(err.getvalue(), "hihihi\n")
     self.assertEqual(status.value, "hahaha")
예제 #3
0
    def test_usage5(self):
        """test usage output 3 args, to stdout, no message"""
        ovc, args = parse(
            {
                "v": ("verbose", bool, 1, "increase verbosity"),
                "z": ("zounds", int, 1, "number of zounds"),
                "_arguments": ["1", "2", "drei"],
            }, ["-v", "hahaha", "hihihi", "hohoho"])
        with outputAndExitCaptured() as (out, err, status):
            ovc.ovc_usage(exit_status=0)
        self.assertEqual(err.getvalue(), "")
        self.assertEqual(
            out.getvalue().rstrip(), """usage: {} [-vz] 1 2 drei
use '-?' option to get more help""".format(ovc._program))
        self.assertEqual(status.value, 0)
예제 #4
0
    def test_usage1(self):
        """test usage output 0 args, to err, other exit status"""
        ovc, args = parse(
            {
                "v": ("verbose", bool, 1, "increase verbosity"),
                "z": ("zounds", int, 1, "number of zounds"),
                "_arguments": [],
            }, ["-v"])
        with outputAndExitCaptured() as (out, err, status):
            ovc.ovc_usage(exit_status=63)
        self.assertEqual(out.getvalue(), "")
        self.assertEqual(
            err.getvalue().rstrip(), """usage: {} [-vz]
use '-?' option to get more help""".format(ovc._program))
        self.assertEqual(status.value, 63)
예제 #5
0
    def test_err_exit_noargs(self):
        """exit due to error"""
        with outputAndExitCaptured() as (out, err, status):
            parse(
                {
                    "i": ("iterations", int, 1, "number of iterations"),
                    "_program": "bungabunga",
                }, ["-x", "3", "bunga"])
        self.assertEqual(status.value, 64)
        self.assertEqual(out.getvalue(), "")
        self.assertEqual(
            err.getvalue(), """bungabunga: {}: 'x'

usage: bungabunga [-i] <arguments>
use '-?' option to get more help
""".format(ErrorNotopt))
예제 #6
0
    def test_called_help(self):
        """Test called ovc_help()."""
        with outputAndExitCaptured() as (out, err, status):
            ovc, args = parse(
                {
                    # opt: (name,        type, default value, helptext[, arg name])
                    "s": ("schmooze", bool, 0, "more schmooziness"),
                    "o": ("output_file", str, None, "output file (or stdout)",
                          "NAME"),
                    "n": ("repetitions", int, 3, "number of repetitions"),
                    "d": ("debug", str, [], "debug topics", "DEBUG_TOPIC"),
                    # keyword:        value
                    "_arguments": ["string_to_print", "..."],
                    "_help_header":
                    "print a string a number of times",
                    "_help_footer":
                    "This is just an example program.",
                    "_program":
                    "schmooze",
                },
                ["narg"],
                exit_on_error=False)
            ovc.ovc_help()
        self.assertEqual(err.getvalue(), "")
        self.assertEqual(status.value, 0)
        self.assertEqual(
            out.getvalue(), """\
usage: schmooze [-ns] [-o NAME] [-d DEBUG_TOPIC] string_to_print ...
print a string a number of times

 -?, --help:
    show help on options and things
 -d, --debug DEBUG_TOPIC:
    debug topics (str arg, default [])
 -h, --help:
    show help on options and things
 -n, --repetitions ARG:
    number of repetitions (int arg, default 3)
 -o, --output-file NAME:
    output file (or stdout) (str arg, default None)
 -s, --schmooze:
    more schmooziness

This is just an example program.
""")