|
1 |
| -import os |
| 1 | +import contextlib |
2 | 2 | import copy
|
| 3 | +import io |
| 4 | +import itertools |
| 5 | +import os |
3 | 6 | import pickle
|
4 | 7 | import platform
|
5 | 8 | import subprocess
|
@@ -741,5 +744,65 @@ def test_parse_os_release(self):
|
741 | 744 | self.assertEqual(len(info["SPECIALS"]), 5)
|
742 | 745 |
|
743 | 746 |
|
| 747 | +class CommandLineTest(unittest.TestCase): |
| 748 | + def setUp(self): |
| 749 | + platform.invalidate_caches() |
| 750 | + self.addCleanup(platform.invalidate_caches) |
| 751 | + |
| 752 | + def invoke_platform(self, *flags): |
| 753 | + output = io.StringIO() |
| 754 | + with contextlib.redirect_stdout(output): |
| 755 | + platform._main(args=flags) |
| 756 | + return output.getvalue() |
| 757 | + |
| 758 | + def test_unknown_flag(self): |
| 759 | + with self.assertRaises(SystemExit): |
| 760 | + output = io.StringIO() |
| 761 | + # suppress argparse error message |
| 762 | + with contextlib.redirect_stderr(output): |
| 763 | + _ = self.invoke_platform('--unknown') |
| 764 | + self.assertStartsWith(output, "usage: ") |
| 765 | + |
| 766 | + def test_invocation(self): |
| 767 | + flags = ( |
| 768 | + "--terse", "--nonaliased", "terse", "nonaliased" |
| 769 | + ) |
| 770 | + |
| 771 | + for r in range(len(flags) + 1): |
| 772 | + for combination in itertools.combinations(flags, r): |
| 773 | + self.invoke_platform(*combination) |
| 774 | + |
| 775 | + def test_arg_parsing(self): |
| 776 | + # For backwards compatibility, the `aliased` and `terse` parameters are |
| 777 | + # computed based on a combination of positional arguments and flags. |
| 778 | + # |
| 779 | + # Test that the arguments are correctly passed to the underlying |
| 780 | + # `platform.platform()` call. |
| 781 | + options = ( |
| 782 | + (["--nonaliased"], False, False), |
| 783 | + (["nonaliased"], False, False), |
| 784 | + (["--terse"], True, True), |
| 785 | + (["terse"], True, True), |
| 786 | + (["nonaliased", "terse"], False, True), |
| 787 | + (["--nonaliased", "terse"], False, True), |
| 788 | + (["--terse", "nonaliased"], False, True), |
| 789 | + ) |
| 790 | + |
| 791 | + for flags, aliased, terse in options: |
| 792 | + with self.subTest(flags=flags, aliased=aliased, terse=terse): |
| 793 | + with mock.patch.object(platform, 'platform') as obj: |
| 794 | + self.invoke_platform(*flags) |
| 795 | + obj.assert_called_once_with(aliased, terse) |
| 796 | + |
| 797 | + def test_help(self): |
| 798 | + output = io.StringIO() |
| 799 | + |
| 800 | + with self.assertRaises(SystemExit): |
| 801 | + with contextlib.redirect_stdout(output): |
| 802 | + platform._main(args=["--help"]) |
| 803 | + |
| 804 | + self.assertStartsWith(output.getvalue(), "usage:") |
| 805 | + |
| 806 | + |
744 | 807 | if __name__ == '__main__':
|
745 | 808 | unittest.main()
|
0 commit comments