Skip to content

Commit 46067bc

Browse files
committed
Fix tests
1 parent 2fe2aaa commit 46067bc

File tree

1 file changed

+96
-47
lines changed

1 file changed

+96
-47
lines changed

tests/test_source_cli.py

Lines changed: 96 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
)
5454

5555
ARGPARSE_OPTIONS_TEXT = 'options' if sys.version_info >= (3, 10) else 'optional arguments'
56+
PYTHON_3_14 = sys.version_info >= (3, 14)
5657

5758

5859
@pytest.fixture(autouse=True)
@@ -247,7 +248,7 @@ class Cfg(BaseSettings):
247248
with pytest.raises(SystemExit):
248249
CliApp.run(Cfg)
249250
assert (
250-
capsys.readouterr().out
251+
capsys.readouterr().out.replace('python3 ', '')
251252
== f"""usage: example.py [-h] {{sub-cmd}} ...
252253
253254
{ARGPARSE_OPTIONS_TEXT}:
@@ -263,7 +264,7 @@ class Cfg(BaseSettings):
263264
with pytest.raises(SystemExit):
264265
CliApp.run(Cfg)
265266
assert (
266-
capsys.readouterr().out
267+
capsys.readouterr().out.replace('python3 ', '')
267268
== f"""usage: example.py sub-cmd [-h] POS-ARG
268269
269270
positional arguments:
@@ -465,7 +466,7 @@ class Cfg(BaseSettings):
465466
CliApp.run(Cfg)
466467

467468
assert (
468-
re.sub(r'0x\w+', '0xffffffff', capsys.readouterr().out, flags=re.MULTILINE)
469+
re.sub(r'0x\w+', '0xffffffff', capsys.readouterr().out.replace('python3 ', ''), flags=re.MULTILINE)
469470
== f"""usage: example.py [-h] [--foo str] [--bar int] [--boo int]
470471
471472
{ARGPARSE_OPTIONS_TEXT}:
@@ -495,7 +496,7 @@ class MultilineDoc(BaseSettings, cli_parse_args=True):
495496
Cfg()
496497

497498
assert (
498-
re.sub(r'0x\w+', '0xffffffff', capsys.readouterr().out, flags=re.MULTILINE)
499+
re.sub(r'0x\w+', '0xffffffff', capsys.readouterr().out.replace('python3 ', ''), flags=re.MULTILINE)
499500
== f"""usage: example.py [-h] [--date_str str]
500501
501502
{ARGPARSE_OPTIONS_TEXT}:
@@ -507,7 +508,7 @@ class MultilineDoc(BaseSettings, cli_parse_args=True):
507508
with pytest.raises(SystemExit):
508509
MultilineDoc()
509510
assert (
510-
capsys.readouterr().out
511+
capsys.readouterr().out.replace('python3 ', '')
511512
== f"""usage: example.py [-h]
512513
513514
My
@@ -523,7 +524,7 @@ class MultilineDoc(BaseSettings, cli_parse_args=True):
523524
cli_settings_source = CliSettingsSource(MultilineDoc, formatter_class=argparse.HelpFormatter)
524525
MultilineDoc(_cli_settings_source=cli_settings_source(args=True))
525526
assert (
526-
capsys.readouterr().out
527+
capsys.readouterr().out.replace('python3 ', '')
527528
== f"""usage: example.py [-h]
528529
529530
My Multiline Doc
@@ -554,12 +555,19 @@ class Car(BaseSettings, cli_parse_args=True):
554555
with monkeypatch.context() as m:
555556
m.setattr(sys, 'argv', ['example.py', '--help'])
556557

558+
if PYTHON_3_14:
559+
text1 = ' [--driver.bark str] [--driver.caww str]\n [--driver.tweet str]'
560+
text2 = '\n'
561+
else:
562+
text1 = ' [--driver.bark str] [--driver.caww str] [--driver.tweet str]'
563+
text2 = ''
557564
with pytest.raises(SystemExit):
558565
Car()
566+
print(capsys.readouterr().out.replace('python3 ', ''))
559567
assert (
560-
capsys.readouterr().out
568+
capsys.readouterr().out.replace('python3 ', '')
561569
== f"""usage: example.py [-h] [--driver [JSON]] [--driver.meow str]
562-
[--driver.bark str] [--driver.caww str] [--driver.tweet str]
570+
{text1}
563571
564572
{ARGPARSE_OPTIONS_TEXT}:
565573
-h, --help show this help message and exit
@@ -569,7 +577,7 @@ class Car(BaseSettings, cli_parse_args=True):
569577
--driver.meow str (default: purr)
570578
--driver.bark str (default: bark)
571579
--driver.caww str (default: caww)
572-
--driver.tweet str (ifdef: required)
580+
--driver.tweet str (ifdef: required){text2}
573581
"""
574582
)
575583

@@ -595,12 +603,21 @@ class Settings(BaseSettings, cli_parse_args=True):
595603
with monkeypatch.context() as m:
596604
m.setattr(sys, 'argv', ['example.py', '--help'])
597605

598-
with pytest.raises(SystemExit):
599-
Settings()
600-
assert (
601-
capsys.readouterr().out
602-
== f"""usage: example.py [-h] [--flag bool] [--sub_model [JSON]]
603-
[--sub_model.flag bool] [--sub_model.deep [JSON]]
606+
if PYTHON_3_14:
607+
text = """ [--sub_model.flag bool] [--sub_model.deep [JSON]]
608+
[--sub_model.deep.flag bool]
609+
[--sub_model.deep.deeper [{JSON,null}]]
610+
[--sub_model.deep.deeper.flag bool]
611+
[--opt_model [{JSON,null}]] [--opt_model.flag bool]
612+
[--opt_model.deeper [{JSON,null}]]
613+
[--opt_model.deeper.flag bool] [--fact_model [JSON]]
614+
[--fact_model.flag bool] [--fact_model.deep [JSON]]
615+
[--fact_model.deep.flag bool]
616+
[--fact_model.deep.deeper [{JSON,null}]]
617+
[--fact_model.deep.deeper.flag bool]
618+
"""
619+
else:
620+
text = """ [--sub_model.flag bool] [--sub_model.deep [JSON]]
604621
[--sub_model.deep.flag bool]
605622
[--sub_model.deep.deeper [{{JSON,null}}]]
606623
[--sub_model.deep.deeper.flag bool]
@@ -610,8 +627,13 @@ class Settings(BaseSettings, cli_parse_args=True):
610627
[--fact_model.flag bool] [--fact_model.deep [JSON]]
611628
[--fact_model.deep.flag bool]
612629
[--fact_model.deep.deeper [{{JSON,null}}]]
613-
[--fact_model.deep.deeper.flag bool]
614-
630+
[--fact_model.deep.deeper.flag bool]"""
631+
with pytest.raises(SystemExit):
632+
Settings()
633+
assert (
634+
capsys.readouterr().out.replace('python3 ', '')
635+
== f"""usage: example.py [-h] [--flag bool] [--sub_model [JSON]]
636+
{text}
615637
{ARGPARSE_OPTIONS_TEXT}:
616638
-h, --help show this help message and exit
617639
--flag bool (default: True)
@@ -1084,7 +1106,7 @@ class Root1(BaseSettings):
10841106
with pytest.raises(SystemExit):
10851107
CliApp.run(Root1)
10861108
assert (
1087-
capsys.readouterr().out
1109+
capsys.readouterr().out.replace('python3 ', '')
10881110
== f"""usage: example.py [-h] {{AlphaCmd,BetaCmd,GammaCmd}} ...
10891111
10901112
Root Help
@@ -1105,7 +1127,7 @@ class Root1(BaseSettings):
11051127
with pytest.raises(SystemExit):
11061128
Root1(_cli_parse_args=True, _cli_use_class_docs_for_groups=True)
11071129
assert (
1108-
capsys.readouterr().out
1130+
capsys.readouterr().out.replace('python3 ', '')
11091131
== f"""usage: example.py [-h] {{AlphaCmd,BetaCmd,GammaCmd}} ...
11101132
11111133
Root Help
@@ -1145,7 +1167,7 @@ class Root2(BaseSettings):
11451167
with pytest.raises(SystemExit):
11461168
CliApp.run(Root2, cli_args=True)
11471169
assert (
1148-
capsys.readouterr().out
1170+
capsys.readouterr().out.replace('python3 ', '')
11491171
== f"""usage: example.py [-h] {{AlphaCmd,GammaCmd,beta}} ...
11501172
11511173
Root Help
@@ -1166,7 +1188,7 @@ class Root2(BaseSettings):
11661188
with pytest.raises(SystemExit):
11671189
Root2(_cli_parse_args=True, _cli_use_class_docs_for_groups=True)
11681190
assert (
1169-
capsys.readouterr().out
1191+
capsys.readouterr().out.replace('python3 ', '')
11701192
== f"""usage: example.py [-h] {{AlphaCmd,GammaCmd,beta}} ...
11711193
11721194
Root Help
@@ -1206,7 +1228,7 @@ class Root3(BaseSettings):
12061228
with pytest.raises(SystemExit):
12071229
CliApp.run(Root3)
12081230
assert (
1209-
capsys.readouterr().out
1231+
capsys.readouterr().out.replace('python3 ', '')
12101232
== f"""usage: example.py [-h] {{beta,AlphaCmd,GammaCmd}} ...
12111233
12121234
Root Help
@@ -1225,7 +1247,7 @@ class Root3(BaseSettings):
12251247
with pytest.raises(SystemExit):
12261248
Root3(_cli_parse_args=True, _cli_use_class_docs_for_groups=True)
12271249
assert (
1228-
capsys.readouterr().out
1250+
capsys.readouterr().out.replace('python3 ', '')
12291251
== f"""usage: example.py [-h] {{beta,AlphaCmd,GammaCmd}} ...
12301252
12311253
Root Help
@@ -1419,12 +1441,16 @@ class Cfg(BaseSettings):
14191441
with monkeypatch.context() as m:
14201442
m.setattr(sys, 'argv', ['example.py', '--help'])
14211443

1444+
if PYTHON_3_14:
1445+
text = ' [--union_pet {{dog,cat,bird},int}]'
1446+
else:
1447+
text = ' [--union_pet {{dog,cat,bird},int}]'
14221448
with pytest.raises(SystemExit):
14231449
CliApp.run(Cfg)
14241450
assert (
1425-
capsys.readouterr().out
1451+
capsys.readouterr().out.replace('python3 ', '')
14261452
== f"""usage: example.py [-h] [--pet {{dog,cat,bird}}]
1427-
[--union_pet {{{{dog,cat,bird}},int}}]
1453+
{text}
14281454
14291455
{ARGPARSE_OPTIONS_TEXT}:
14301456
-h, --help show this help message and exit
@@ -1617,7 +1643,7 @@ class Settings(BaseSettings):
16171643
Settings(_cli_avoid_json=False)
16181644

16191645
assert (
1620-
capsys.readouterr().out
1646+
capsys.readouterr().out.replace('python3 ', '')
16211647
== f"""usage: example.py [-h] [--sub_model [JSON]] [--sub_model.v1 int]
16221648
16231649
{ARGPARSE_OPTIONS_TEXT}:
@@ -1633,7 +1659,7 @@ class Settings(BaseSettings):
16331659
Settings(_cli_avoid_json=True)
16341660

16351661
assert (
1636-
capsys.readouterr().out
1662+
capsys.readouterr().out.replace('python3 ', '')
16371663
== f"""usage: example.py [-h] [--sub_model.v1 int]
16381664
16391665
{ARGPARSE_OPTIONS_TEXT}:
@@ -1661,7 +1687,7 @@ class Settings(BaseSettings):
16611687
Settings(_cli_avoid_json=False)
16621688

16631689
assert (
1664-
capsys.readouterr().out
1690+
capsys.readouterr().out.replace('python3 ', '')
16651691
== f"""usage: example.py [-h] [--sub_model [JSON]]
16661692
16671693
{ARGPARSE_OPTIONS_TEXT}:
@@ -1676,7 +1702,7 @@ class Settings(BaseSettings):
16761702
Settings(_cli_avoid_json=True)
16771703

16781704
assert (
1679-
capsys.readouterr().out
1705+
capsys.readouterr().out.replace('python3 ', '')
16801706
== f"""usage: example.py [-h]
16811707
16821708
{ARGPARSE_OPTIONS_TEXT}:
@@ -1698,7 +1724,7 @@ class Settings(BaseSettings):
16981724
Settings(_cli_hide_none_type=False)
16991725

17001726
assert (
1701-
capsys.readouterr().out
1727+
capsys.readouterr().out.replace('python3 ', '')
17021728
== f"""usage: example.py [-h] [--v0 {{str,null}}]
17031729
17041730
{ARGPARSE_OPTIONS_TEXT}:
@@ -1711,7 +1737,7 @@ class Settings(BaseSettings):
17111737
Settings(_cli_hide_none_type=True)
17121738

17131739
assert (
1714-
capsys.readouterr().out
1740+
capsys.readouterr().out.replace('python3 ', '')
17151741
== f"""usage: example.py [-h] [--v0 str]
17161742
17171743
{ARGPARSE_OPTIONS_TEXT}:
@@ -1741,7 +1767,7 @@ class Settings(BaseSettings):
17411767
Settings(_cli_use_class_docs_for_groups=False)
17421768

17431769
assert (
1744-
capsys.readouterr().out
1770+
capsys.readouterr().out.replace('python3 ', '')
17451771
== f"""usage: example.py [-h] [--sub_model [JSON]] [--sub_model.v1 int]
17461772
17471773
My application help text.
@@ -1761,7 +1787,7 @@ class Settings(BaseSettings):
17611787
Settings(_cli_use_class_docs_for_groups=True)
17621788

17631789
assert (
1764-
capsys.readouterr().out
1790+
capsys.readouterr().out.replace('python3 ', '')
17651791
== f"""usage: example.py [-h] [--sub_model [JSON]] [--sub_model.v1 int]
17661792
17671793
My application help text.
@@ -1814,7 +1840,7 @@ class Settings(BaseSettings, cli_parse_args=True): ...
18141840
with pytest.raises(SystemExit):
18151841
Settings()
18161842
assert (
1817-
capsys.readouterr().err
1843+
capsys.readouterr().err.replace('python3 ', '')
18181844
== """usage: example.py [-h]
18191845
example.py: error: unrecognized arguments: --bad-arg
18201846
"""
@@ -2259,10 +2285,17 @@ class Settings(BaseSettings, cli_parse_args=True):
22592285
with pytest.raises(SystemExit):
22602286
CliApp.run(Settings)
22612287

2288+
if PYTHON_3_14:
2289+
text = """usage: python3 example.py [-h] [--visible_obj [JSON]]
2290+
[--visible_obj.visible_a int]
2291+
[--visible_obj.visible_b int]"""
2292+
2293+
else:
2294+
text = """usage: example.py [-h] [--visible_obj [JSON]] [--visible_obj.visible_a int]
2295+
[--visible_obj.visible_b int]"""
22622296
assert (
22632297
capsys.readouterr().out
2264-
== f"""usage: example.py [-h] [--visible_obj [JSON]] [--visible_obj.visible_a int]
2265-
[--visible_obj.visible_b int]
2298+
== f"""{text}
22662299
22672300
{ARGPARSE_OPTIONS_TEXT}:
22682301
-h, --help show this help message and exit
@@ -2313,18 +2346,24 @@ class Settings(BaseModel):
23132346
m.setattr(sys, 'argv', ['example.py', '--help'])
23142347
with pytest.raises(SystemExit):
23152348
CliApp.run(Settings)
2316-
usage = (
2317-
"""usage: example.py [-h] [--circle-optional.radius float |
2349+
if PYTHON_3_14:
2350+
usage = """usage: python3 example.py [-h] [--circle-optional.radius float |
2351+
--circle-optional.diameter float |
2352+
--circle-optional.perimeter float]
2353+
(--circle-required.radius float |
2354+
--circle-required.diameter float |
2355+
--circle-required.perimeter float)"""
2356+
elif sys.version_info >= (3, 13):
2357+
usage = """usage: example.py [-h]
2358+
[--circle-optional.radius float | --circle-optional.diameter float | --circle-optional.perimeter float]
2359+
(--circle-required.radius float | --circle-required.diameter float | --circle-required.perimeter float)"""
2360+
else:
2361+
usage = """usage: example.py [-h] [--circle-optional.radius float |
23182362
--circle-optional.diameter float |
23192363
--circle-optional.perimeter float]
23202364
(--circle-required.radius float |
23212365
--circle-required.diameter float |
23222366
--circle-required.perimeter float)"""
2323-
if sys.version_info >= (3, 13)
2324-
else """usage: example.py [-h]
2325-
[--circle-optional.radius float | --circle-optional.diameter float | --circle-optional.perimeter float]
2326-
(--circle-required.radius float | --circle-required.diameter float | --circle-required.perimeter float)"""
2327-
)
23282367
assert (
23292368
capsys.readouterr().out
23302369
== f"""{usage}
@@ -2523,7 +2562,7 @@ class Root(BaseModel):
25232562
with pytest.raises(SystemExit):
25242563
CliApp.run(Root)
25252564
assert (
2526-
capsys.readouterr().out
2565+
capsys.readouterr().out.replace('python3 ', '')
25272566
== f"""usage: example.py [-h] --root-arg str {{root-subcmd,other-subcmd}} ...
25282567
25292568
{ARGPARSE_OPTIONS_TEXT}:
@@ -2537,13 +2576,18 @@ class Root(BaseModel):
25372576
"""
25382577
)
25392578

2579+
if PYTHON_3_14:
2580+
usage = """usage: python3 example.py root-subcmd [-h] --sub-arg str
2581+
{sub-subcmd,sub-other-subcmd} ..."""
2582+
else:
2583+
usage = """usage: example.py root-subcmd [-h] --sub-arg str
2584+
{{sub-subcmd,sub-other-subcmd}} ..."""
25402585
m.setattr(sys, 'argv', ['example.py', 'root-subcmd', '--help'])
25412586
with pytest.raises(SystemExit):
25422587
CliApp.run(Root)
25432588
assert (
25442589
capsys.readouterr().out
2545-
== f"""usage: example.py root-subcmd [-h] --sub-arg str
2546-
{{sub-subcmd,sub-other-subcmd}} ...
2590+
== f"""{usage}
25472591
25482592
{ARGPARSE_OPTIONS_TEXT}:
25492593
-h, --help show this help message and exit
@@ -2556,12 +2600,17 @@ class Root(BaseModel):
25562600
"""
25572601
)
25582602

2603+
if PYTHON_3_14:
2604+
usage = """usage: python3 example.py root-subcmd sub-subcmd [-h] --deep-arg str
2605+
DEEP-POS-ARG"""
2606+
else:
2607+
usage = """usage: example.py root-subcmd sub-subcmd [-h] --deep-arg str DEEP-POS-ARG"""
25592608
m.setattr(sys, 'argv', ['example.py', 'root-subcmd', 'sub-subcmd', '--help'])
25602609
with pytest.raises(SystemExit):
25612610
CliApp.run(Root)
25622611
assert (
25632612
capsys.readouterr().out
2564-
== f"""usage: example.py root-subcmd sub-subcmd [-h] --deep-arg str DEEP-POS-ARG
2613+
== f"""{usage}
25652614
25662615
positional arguments:
25672616
DEEP-POS-ARG

0 commit comments

Comments
 (0)