|
| 1 | +import io |
| 2 | +import itertools |
1 | 3 | import os |
2 | 4 | import copy |
| 5 | +import contextlib |
3 | 6 | import pickle |
4 | 7 | import platform |
5 | 8 | import subprocess |
6 | 9 | import sys |
7 | 10 | import unittest |
8 | 11 | from unittest import mock |
| 12 | +from textwrap import dedent |
9 | 13 |
|
10 | 14 | from test import support |
11 | 15 | from test.support import os_helper |
@@ -741,5 +745,52 @@ def test_parse_os_release(self): |
741 | 745 | self.assertEqual(len(info["SPECIALS"]), 5) |
742 | 746 |
|
743 | 747 |
|
| 748 | +class CommandLineTest(unittest.TestCase): |
| 749 | + def setUp(self): |
| 750 | + self.clear_caches() |
| 751 | + self.addCleanup(self.clear_caches) |
| 752 | + |
| 753 | + def clear_caches(self): |
| 754 | + platform._platform_cache.clear() |
| 755 | + platform._sys_version_cache.clear() |
| 756 | + platform._uname_cache = None |
| 757 | + platform._os_release_cache = None |
| 758 | + |
| 759 | + @staticmethod |
| 760 | + def text_normalize(string): |
| 761 | + """Dedent *string* and strip it from its surrounding whitespaces. |
| 762 | + This method is used by the other utility functions so that any |
| 763 | + string to write or to match against can be freely indented. |
| 764 | + """ |
| 765 | + return dedent(string).strip() |
| 766 | + |
| 767 | + def invoke_platform(self, *flags): |
| 768 | + output = io.StringIO() |
| 769 | + with contextlib.redirect_stdout(output): |
| 770 | + platform._main(args=flags) |
| 771 | + return self.text_normalize(output.getvalue()) |
| 772 | + |
| 773 | + def test_unknown_flag(self): |
| 774 | + with self.assertRaises(SystemExit): |
| 775 | + # suppress argparse error message |
| 776 | + with contextlib.redirect_stderr(io.StringIO()): |
| 777 | + _ = self.invoke_platform('--unknown') |
| 778 | + |
| 779 | + def test_invocation(self): |
| 780 | + self.invoke_platform("--terse", "--nonaliased") |
| 781 | + self.invoke_platform("--nonaliased") |
| 782 | + self.invoke_platform("--terse") |
| 783 | + self.invoke_platform() |
| 784 | + |
| 785 | + def test_help(self): |
| 786 | + output = io.StringIO() |
| 787 | + |
| 788 | + with self.assertRaises(SystemExit): |
| 789 | + with contextlib.redirect_stdout(output): |
| 790 | + platform._main(args=["--help"]) |
| 791 | + |
| 792 | + self.assertIn("usage:", output.getvalue()) |
| 793 | + |
| 794 | + |
744 | 795 | if __name__ == '__main__': |
745 | 796 | unittest.main() |
0 commit comments