Skip to content

Commit bb396ee

Browse files
authored
pythongh-101821: Test coverage for ast.main function (python#101822)
1 parent 534660f commit bb396ee

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Lib/test/test_ast.py

+20
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from textwrap import dedent
1212

1313
from test import support
14+
from test.support import os_helper, script_helper
1415
from test.support.ast_helper import ASTTestMixin
1516

1617
def to_tuple(t):
@@ -2564,6 +2565,25 @@ def test_subinterpreter(self):
25642565
self.assertEqual(res, 0)
25652566

25662567

2568+
class ASTMainTests(unittest.TestCase):
2569+
# Tests `ast.main()` function.
2570+
2571+
def test_cli_file_input(self):
2572+
code = "print(1, 2, 3)"
2573+
expected = ast.dump(ast.parse(code), indent=3)
2574+
2575+
with os_helper.temp_dir() as tmp_dir:
2576+
filename = os.path.join(tmp_dir, "test_module.py")
2577+
with open(filename, 'w', encoding='utf-8') as f:
2578+
f.write(code)
2579+
res, _ = script_helper.run_python_until_end("-m", "ast", filename)
2580+
2581+
self.assertEqual(res.err, b"")
2582+
self.assertEqual(expected.splitlines(),
2583+
res.out.decode("utf8").splitlines())
2584+
self.assertEqual(res.rc, 0)
2585+
2586+
25672587
def main():
25682588
if __name__ != '__main__':
25692589
return

0 commit comments

Comments
 (0)