aboutsummaryrefslogtreecommitdiff
blob: c55e786feb6cc0e459cc647c7dec2c60312c6743 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
import argparse
import os
import tempfile
from functools import partial
from importlib import reload
from unittest import mock

import pytest
from snakeoil.cli import arghparse
from snakeoil.test import argparse_helpers


class TestArgparseDocs:
    def test_add_argument_docs(self):
        # force using an unpatched version of argparse
        reload(argparse)

        parser = argparse.ArgumentParser()
        parser.add_argument("--foo", action="store_true")

        # vanilla argparse doesn't support docs kwargs
        with pytest.raises(TypeError):
            parser.add_argument(
                "-b", "--blah", action="store_true", docs="Blah blah blah"
            )
        with pytest.raises(TypeError):
            parser.add_argument_group("fa", description="fa la la", docs="fa la la la")
        with pytest.raises(TypeError):
            parser.add_mutually_exclusive_group("fee", description="fi", docs="fo fum")

        # forcibly monkey-patch argparse to allow docs kwargs
        reload(arghparse)

        default = "baz baz"
        docs = "blah blah"
        for enable_docs, expected_txt in ((False, default), (True, docs)):
            arghparse._generate_docs = enable_docs
            parser = argparse.ArgumentParser()
            subparsers = parser.add_subparsers(description=default, docs=docs)
            subparser = subparsers.add_parser("foo", description=default, docs=docs)
            action = parser.add_argument(
                "-b", "--blah", action="store_true", help=default, docs=docs
            )
            arg_group = parser.add_argument_group("fa", description=default, docs=docs)
            mut_arg_group = parser.add_mutually_exclusive_group()
            mut_action = mut_arg_group.add_argument(
                "-f", "--fee", action="store_true", help=default, docs=docs
            )

            assert getattr(parser._subparsers, "description", None) == expected_txt
            assert getattr(subparser, "description", None) == expected_txt
            assert getattr(action, "help", None) == expected_txt
            assert getattr(arg_group, "description", None) == expected_txt
            assert getattr(mut_action, "help", None) == expected_txt

        # list/tuple-based docs
        arghparse._generate_docs = True
        docs = "foo bar"
        parser = argparse.ArgumentParser()
        list_action = parser.add_argument(
            "-b", "--blah", action="store_true", help=default, docs=list(docs.split())
        )
        tuple_action = parser.add_argument(
            "-c", "--cat", action="store_true", help=default, docs=tuple(docs.split())
        )
        assert getattr(list_action, "help", None) == "foo\nbar"
        assert getattr(tuple_action, "help", None) == "foo\nbar"


class TestOptionalsParser:
    # TODO: move this to a generic argparse fixture
    @pytest.fixture(autouse=True)
    def __setup_optionals_parser(self):
        self.optionals_parser = argparse_helpers.mangle_parser(
            arghparse.OptionalsParser()
        )

    def test_no_args(self):
        args, unknown = self.optionals_parser.parse_known_optionals([])
        assert vars(args) == {}
        assert unknown == []

    def test_only_positionals(self):
        self.optionals_parser.add_argument("args")
        args, unknown = self.optionals_parser.parse_known_optionals([])
        assert vars(args) == {"args": None}
        assert unknown == []

    def test_optionals(self):
        self.optionals_parser.add_argument("--opt1")
        self.optionals_parser.add_argument("args")
        parse = self.optionals_parser.parse_known_optionals

        # no args
        args, unknown = parse([])
        assert args.opt1 is None
        assert unknown == []

        # only known optional
        args, unknown = parse(["--opt1", "yes"])
        assert args.opt1 == "yes"
        assert unknown == []

        # unknown optional
        args, unknown = parse(["--foo"])
        assert args.opt1 is None
        assert unknown == ["--foo"]

        # unknown optional and positional
        args, unknown = parse(["--foo", "arg"])
        assert args.opt1 is None
        assert unknown == ["--foo", "arg"]

        # known optional with unknown optional
        args, unknown = parse(["--opt1", "yes", "--foo"])
        assert args.opt1 == "yes"
        assert unknown == ["--foo"]
        # different order
        args, unknown = parse(["--foo", "--opt1", "yes"])
        assert args.opt1 == "yes"
        assert unknown == ["--foo"]

        # known optional with unknown positional
        args, unknown = parse(["--opt1", "yes", "arg"])
        assert args.opt1 == "yes"
        assert unknown == ["arg"]
        # known optionals parsing stops at the first positional arg
        args, unknown = parse(["arg", "--opt1", "yes"])
        assert args.opt1 is None
        assert unknown == ["arg", "--opt1", "yes"]


class TestCsvActionsParser:
    # TODO: move this to a generic argparse fixture
    @pytest.fixture(autouse=True)
    def __setup_csv_actions_parser(self):
        self.csv_parser = argparse_helpers.mangle_parser(arghparse.CsvActionsParser())

    def test_bad_action(self):
        with pytest.raises(ValueError) as excinfo:
            self.csv_parser.add_argument("--arg1", action="unknown")
        assert 'unknown action "unknown"' == str(excinfo.value)

    def test_csv_actions(self):
        self.csv_parser.add_argument("--arg1", action="csv")
        self.csv_parser.add_argument("--arg2", action="csv_append")
        self.csv_parser.add_argument("--arg3", action="csv_negations")
        self.csv_parser.add_argument("--arg4", action="csv_negations_append")
        self.csv_parser.add_argument("--arg5", action="csv_elements")
        self.csv_parser.add_argument("--arg6", action="csv_elements_append")


class TestArgumentParser(TestCsvActionsParser, TestOptionalsParser):
    def test_debug(self):
        # debug passed
        parser = argparse_helpers.mangle_parser(arghparse.ArgumentParser(debug=True))
        namespace = parser.parse_args(["--debug"])
        assert parser.debug is False
        assert namespace.debug is True

        # debug not passed
        namespace = parser.parse_args([])
        assert parser.debug is False
        assert namespace.debug is False

        # debug passed in sys.argv -- early debug attr on the parser instance is set
        with mock.patch("sys.argv", ["script", "--debug"]):
            parser = argparse_helpers.mangle_parser(
                arghparse.ArgumentParser(debug=True)
            )
            assert parser.debug is True

    def test_debug_disabled(self):
        parser = argparse_helpers.mangle_parser(arghparse.ArgumentParser(debug=False))

        # ensure the option isn't there if disabled
        with pytest.raises(argparse_helpers.Error):
            namespace = parser.parse_args(["--debug"])

        namespace = parser.parse_args([])
        # parser attribute still exists
        assert parser.debug is False
        # but namespace attribute doesn't
        assert not hasattr(namespace, "debug")

    def test_verbosity(self):
        values = (
            ([], 0),
            (["-q"], -1),
            (["--quiet"], -1),
            (["-v"], 1),
            (["--verbose"], 1),
            (["-q", "-v"], 0),
            (["--quiet", "--verbose"], 0),
            (["-q", "-q"], -2),
            (["-v", "-v"], 2),
        )
        for args, val in values:
            with mock.patch("sys.argv", ["script"] + args):
                parser = argparse_helpers.mangle_parser(
                    arghparse.ArgumentParser(quiet=True, verbose=True)
                )
                namespace = parser.parse_args(args)
                assert parser.verbosity == val, "{} failed".format(args)
                assert namespace.verbosity == val, "{} failed".format(args)

    def test_verbosity_disabled(self):
        parser = argparse_helpers.mangle_parser(
            arghparse.ArgumentParser(quiet=False, verbose=False)
        )

        # ensure the options aren't there if disabled
        for args in ("-q", "--quiet", "-v", "--verbose"):
            with pytest.raises(argparse_helpers.Error):
                namespace = parser.parse_args([args])

        namespace = parser.parse_args([])
        # parser attribute still exists
        assert parser.verbosity == 0
        # but namespace attribute doesn't
        assert not hasattr(namespace, "verbosity")


class BaseArgparseOptions:
    def setup_method(self, method):
        self.parser = argparse_helpers.mangle_parser(arghparse.ArgumentParser())


class TestStoreBoolAction(BaseArgparseOptions):
    def setup_method(self, method):
        super().setup_method(method)
        self.parser.add_argument("--testing", action=arghparse.StoreBool, default=None)

    def test_bool_disabled(self):
        for raw_val in ("n", "no", "false"):
            for allowed in (raw_val.upper(), raw_val.lower()):
                namespace = self.parser.parse_args(["--testing=" + allowed])
                assert namespace.testing is False

    def test_bool_enabled(self):
        for raw_val in ("y", "yes", "true"):
            for allowed in (raw_val.upper(), raw_val.lower()):
                namespace = self.parser.parse_args(["--testing=" + allowed])
                assert namespace.testing is True

    def test_bool_invalid(self):
        with pytest.raises(argparse_helpers.Error):
            self.parser.parse_args(["--testing=invalid"])


class ParseStdinTest(BaseArgparseOptions):
    def setup_method(self, method):
        super().setup_method(method)
        self.parser.add_argument("testing", nargs="+", action=arghparse.ParseStdin)

    def test_none_invalid(self):
        with pytest.raises(argparse_helpers.Error):
            self.parser.parse_args([])

    def test_non_stdin(self):
        namespace = self.parser.parse_args(["foo"])
        assert namespace.testing == ["foo"]

    def test_non_stdin_multiple(self):
        namespace = self.parser.parse_args(["foo", "bar"])
        assert namespace.testing == ["foo", "bar"]

    def test_stdin(self):
        # stdin is an interactive tty
        with mock.patch("sys.stdin.isatty", return_value=True):
            with pytest.raises(argparse_helpers.Error) as excinfo:
                namespace = self.parser.parse_args(["-"])
            assert "only valid when piping data in" in str(excinfo.value)

        # fake piping data in
        for readlines, expected in (
            ([], []),
            ([" "], []),
            (["\n"], []),
            (["\n", "\n"], []),
            (["foo"], ["foo"]),
            (["foo "], ["foo"]),
            (["foo\n"], ["foo"]),
            (["foo", "bar", "baz"], ["foo", "bar", "baz"]),
            (["\nfoo\n", " bar ", "\nbaz"], ["\nfoo", " bar", "\nbaz"]),
        ):
            with (
                mock.patch("sys.stdin") as stdin,
                mock.patch("builtins.open", mock.mock_open()) as mock_file,
            ):
                stdin.readlines.return_value = readlines
                stdin.isatty.return_value = False
                namespace = self.parser.parse_args(["-"])
                mock_file.assert_called_once_with("/dev/tty")
            assert namespace.testing == expected


class TestCommaSeparatedValuesAction(BaseArgparseOptions):
    def setup_method(self, method):
        super().setup_method(method)
        self.test_values = (
            ("", []),
            (",", []),
            (",,", []),
            ("a", ["a"]),
            ("a,b,-c", ["a", "b", "-c"]),
        )

        self.action = "csv"
        self.single_expected = lambda x: x
        self.multi_expected = lambda x: x

    def test_parse_args(self):
        self.parser.add_argument("--testing", action=self.action)
        for raw_val, expected in self.test_values:
            namespace = self.parser.parse_args(["--testing=" + raw_val])
            assert namespace.testing == self.single_expected(expected)

    def test_parse_multi_args(self):
        self.parser.add_argument("--testing", action=self.action)
        for raw_val, expected in self.test_values:
            namespace = self.parser.parse_args(
                [
                    "--testing=" + raw_val,
                    "--testing=" + raw_val,
                ]
            )
            assert namespace.testing == self.multi_expected(expected)


class TestCommaSeparatedValuesAppendAction(TestCommaSeparatedValuesAction):
    def setup_method(self, method):
        super().setup_method(method)
        self.action = "csv_append"
        self.multi_expected = lambda x: x + x


class TestCommaSeparatedNegationsAction(TestCommaSeparatedValuesAction):
    def setup_method(self, method):
        super().setup_method(method)
        self.test_values = (
            ("", ([], [])),
            (",", ([], [])),
            (",,", ([], [])),
            ("a", ([], ["a"])),
            ("-a", (["a"], [])),
            ("a,-b,-c,d", (["b", "c"], ["a", "d"])),
        )
        self.bad_args = ("-",)
        self.action = "csv_negations"

    def test_parse_bad_args(self):
        self.parser.add_argument("--testing", action=self.action)
        for arg in self.bad_args:
            with pytest.raises(argparse.ArgumentTypeError) as excinfo:
                namespace = self.parser.parse_args(["--testing=" + arg])
            assert "without a token" in str(excinfo.value)


class TestCommaSeparatedNegationsAppendAction(TestCommaSeparatedNegationsAction):
    def setup_method(self, method):
        super().setup_method(method)
        self.action = "csv_negations_append"
        self.multi_expected = lambda x: tuple(x + y for x, y in zip(x, x))


class TestCommaSeparatedElementsAction(TestCommaSeparatedNegationsAction):
    def setup_method(self, method):
        super().setup_method(method)
        self.test_values = (
            ("", ([], [], [])),
            (",", ([], [], [])),
            (",,", ([], [], [])),
            ("-a", (["a"], [], [])),
            ("a", ([], ["a"], [])),
            ("+a", ([], [], ["a"])),
            ("a,-b,-c,d", (["b", "c"], ["a", "d"], [])),
            ("a,-b,+c,-d,+e,f", (["b", "d"], ["a", "f"], ["c", "e"])),
        )
        self.bad_values = ("-", "+")
        self.action = "csv_elements"


class TestCommaSeparatedElementsAppendAction(TestCommaSeparatedElementsAction):
    def setup_method(self, method):
        super().setup_method(method)
        self.action = "csv_elements_append"
        self.multi_expected = lambda x: tuple(x + y for x, y in zip(x, x))


class TestExistentPathType(BaseArgparseOptions):
    def setup_method(self, method):
        super().setup_method(method)
        self.parser.add_argument("--path", type=arghparse.existent_path)

    def test_nonexistent(self):
        # nonexistent path arg raises an error
        with pytest.raises(argparse_helpers.Error):
            self.parser.parse_args(["--path=/path/to/nowhere"])

    def test_os_errors(self, tmpdir):
        # random OS/FS issues raise errors
        with mock.patch("os.path.realpath") as realpath:
            realpath.side_effect = OSError(19, "Random OS error")
            with pytest.raises(argparse_helpers.Error):
                self.parser.parse_args(["--path=%s" % tmpdir])

    def test_regular_usage(self, tmpdir):
        namespace = self.parser.parse_args(["--path=%s" % tmpdir])
        assert namespace.path == str(tmpdir)


class TestExistentDirType(BaseArgparseOptions):
    def setup_method(self, method):
        super().setup_method(method)
        self.parser.add_argument("--path", type=arghparse.existent_dir)

    def test_nonexistent(self):
        # nonexistent path arg raises an error
        with pytest.raises(argparse_helpers.Error):
            self.parser.parse_args(["--path=/path/to/nowhere"])

    def test_os_errors(self, tmp_path):
        # random OS/FS issues raise errors
        with mock.patch("os.path.realpath") as realpath:
            realpath.side_effect = OSError(19, "Random OS error")
            with pytest.raises(argparse_helpers.Error):
                self.parser.parse_args([f"--path={tmp_path}"])

    def test_file_path(self, tmp_path):
        f = tmp_path / "file"
        f.touch()
        with pytest.raises(argparse_helpers.Error):
            self.parser.parse_args([f"--path={f}"])

    def test_regular_usage(self, tmp_path):
        namespace = self.parser.parse_args([f"--path={tmp_path}"])
        assert namespace.path == str(tmp_path)


class TestNamespace:
    def setup_method(self, method):
        self.parser = argparse_helpers.mangle_parser(arghparse.ArgumentParser())

    def test_pop(self):
        self.parser.set_defaults(test="test")
        namespace = self.parser.parse_args([])
        assert namespace.pop("test") == "test"

        # re-popping raises an exception since the attr has been removed
        with pytest.raises(AttributeError):
            namespace.pop("test")

        # popping a nonexistent attr with a fallback returns the fallback
        assert namespace.pop("nonexistent", "foo") == "foo"

    def test_collapse_delayed(self):
        def _delayed_val(namespace, attr, val):
            setattr(namespace, attr, val)

        self.parser.set_defaults(
            delayed=arghparse.DelayedValue(partial(_delayed_val, val=42))
        )
        namespace = self.parser.parse_args([])
        assert namespace.delayed == 42

    def test_bool(self):
        namespace = arghparse.Namespace()
        assert not namespace
        namespace.arg = "foo"
        assert namespace


class TestManHelpAction:
    def test_help(self, capsys):
        parser = argparse_helpers.mangle_parser(arghparse.ArgumentParser())
        with mock.patch("subprocess.Popen") as popen:
            # --help long option tries man page first before falling back to help output
            with pytest.raises(argparse_helpers.Exit):
                namespace = parser.parse_args(["--help"])
            popen.assert_called_once()
            assert popen.call_args[0][0][0] == "man"
            captured = capsys.readouterr()
            assert captured.out.strip().startswith("usage: ")
            popen.reset_mock()

            # -h short option just displays the regular help output
            with pytest.raises(argparse_helpers.Exit):
                namespace = parser.parse_args(["-h"])
            popen.assert_not_called()
            captured = capsys.readouterr()
            assert captured.out.strip().startswith("usage: ")
            popen.reset_mock()