Debian Codegraph @

integrations/argparse/tests/test_fig.py

raw symbols SCIP
1 import argparse
2 import pytest
3 from argparse_complete_fig import fig
4 import io
5 from contextlib import redirect_stdout
6 import sys
7
8 ### Fixtures
9 @pytest.fixture
10 def case_1_fixture():
11 return '''// Autogenerated by argparse_complete_fig
12 const completionSpec: Fig.Spec = {
13 "name": "__main__.py",
14 "options": [
15 {
16 "name": [
17 "-h",
18 "--help"
19 ],
20 "description": "show this help message and exit"
21 },
22 {
23 "name": "--output",
24 "description": "Select output directory",
25 "args": [
26 {
27 "name": "O"
28 },
29 {
30 "isOptional": true,
31 "isVariadic": true,
32 "name": "O",
33 "optionsCanBreakVariadicArg": true
34 }
35 ]
36 },
37 {
38 "name": "--sum",
39 "description": "sum the integers (default: find the max)"
40 }
41 ],
42 "args": [
43 {
44 "name": "N",
45 "description": "an integer for the accumulator"
46 },
47 {
48 "isOptional": true,
49 "isVariadic": true,
50 "name": "N",
51 "optionsCanBreakVariadicArg": true,
52 "description": "an integer for the accumulator"
53 }
54 ]
55 }
56 export default completionSpec;'''
57
58
59 @pytest.fixture
60 def case_2_fixture():
61 return '''// Autogenerated by argparse_complete_fig
62 const completionSpec: Fig.Spec = {
63 "name": "__main__.py",
64 "options": [
65 {
66 "name": [
67 "-h",
68 "--help"
69 ],
70 "description": "show this help message and exit"
71 },
72 {
73 "name": [
74 "-f",
75 "--file"
76 ],
77 "description": "yaml file to process",
78 "args": [
79 {
80 "name": "file"
81 }
82 ]
83 },
84 {
85 "name": "--generate-fig-spec",
86 "description": "Generate fig completion spec for this parser",
87 "priority": 0
88 }
89 ]
90 }
91 export default completionSpec;
92 '''
93
94 ### Test cases
95 def test_cli_generate(case_1_fixture):
96 parser = argparse.ArgumentParser(description="A root description")
97 parser.add_argument('integers', metavar='N', type=int,
98 nargs='+',help='an integer for the accumulator')
99 parser.add_argument('--output', dest='accumulate', metavar="O", type=str, nargs=argparse.ONE_OR_MORE, help="Select output directory")
100 parser.add_argument('--sum', dest='accumulate', action='store_const',
101 const=sum, default=max, help='sum the integers (default: find the max)')
102 assert fig.generate_completion_spec(parser) == case_1_fixture
103
104 def test_case_2(case_2_fixture):
105 parser = argparse.ArgumentParser(description='process yaml file.')
106 parser.add_argument('-f', '--file', type=str, help='yaml file to process')
107 fig.add_completion_spec_command(parser)
108
109
110 f = io.StringIO()
111 with redirect_stdout(f):
112 def blank_fn(*args, **kwargs):
113 pass
114 exit = sys.exit
115 sys.exit = blank_fn
116 args = parser.parse_args(['--generate-fig-spec'])
117 sys.exit = exit
118 assert f.getvalue() == case_2_fixture
119