|
1 | 1 | from _compat_pickle import (IMPORT_MAPPING, REVERSE_IMPORT_MAPPING,
|
2 | 2 | NAME_MAPPING, REVERSE_NAME_MAPPING)
|
3 | 3 | import builtins
|
4 |
| -import pickle |
5 |
| -import io |
6 | 4 | import collections
|
| 5 | +import contextlib |
| 6 | +import io |
| 7 | +import pickle |
7 | 8 | import struct
|
8 | 9 | import sys
|
| 10 | +import tempfile |
9 | 11 | import warnings
|
10 | 12 | import weakref
|
| 13 | +from textwrap import dedent |
11 | 14 |
|
12 | 15 | import doctest
|
13 | 16 | import unittest
|
14 | 17 | from test import support
|
15 |
| -from test.support import import_helper |
| 18 | +from test.support import import_helper, os_helper |
16 | 19 |
|
17 | 20 | from test.pickletester import AbstractHookTests
|
18 | 21 | from test.pickletester import AbstractUnpickleTests
|
@@ -699,6 +702,58 @@ def test_multiprocessing_exceptions(self):
|
699 | 702 | ('multiprocessing.context', name))
|
700 | 703 |
|
701 | 704 |
|
| 705 | +class CommandLineTest(unittest.TestCase): |
| 706 | + def setUp(self): |
| 707 | + self.filename = tempfile.mktemp() |
| 708 | + self.addCleanup(os_helper.unlink, self.filename) |
| 709 | + |
| 710 | + @staticmethod |
| 711 | + def text_normalize(string): |
| 712 | + """Dedent *string* and strip it from its surrounding whitespaces. |
| 713 | +
|
| 714 | + This method is used by the other utility functions so that any |
| 715 | + string to write or to match against can be freely indented. |
| 716 | + """ |
| 717 | + return dedent(string).strip() |
| 718 | + |
| 719 | + def set_pickle_data(self, data): |
| 720 | + with open(self.filename, 'wb') as f: |
| 721 | + pickle.dump(data, f) |
| 722 | + |
| 723 | + def invoke_pickle(self, *flags): |
| 724 | + output = io.StringIO() |
| 725 | + with contextlib.redirect_stdout(output): |
| 726 | + pickle._main(args=[*flags, self.filename]) |
| 727 | + return self.text_normalize(output.getvalue()) |
| 728 | + |
| 729 | + def test_invocation(self): |
| 730 | + # test 'python -m pickle pickle_file' |
| 731 | + data = { |
| 732 | + 'a': [1, 2.0, 3+4j], |
| 733 | + 'b': ('character string', b'byte string'), |
| 734 | + 'c': 'string' |
| 735 | + } |
| 736 | + expect = ''' |
| 737 | + {'a': [1, 2.0, (3+4j)], |
| 738 | + 'b': ('character string', b'byte string'), |
| 739 | + 'c': 'string'} |
| 740 | + ''' |
| 741 | + self.set_pickle_data(data) |
| 742 | + |
| 743 | + with self.subTest(data=data): |
| 744 | + res = self.invoke_pickle() |
| 745 | + expect = self.text_normalize(expect) |
| 746 | + self.assertListEqual(res.splitlines(), expect.splitlines()) |
| 747 | + |
| 748 | + def test_unknown_flag(self): |
| 749 | + stderr = io.StringIO() |
| 750 | + with self.assertRaises(SystemExit): |
| 751 | + # check that the parser help is shown |
| 752 | + with contextlib.redirect_stderr(stderr): |
| 753 | + _ = self.invoke_pickle('--unknown') |
| 754 | + self.assertStartsWith(stderr.getvalue(), 'usage: ') |
| 755 | + |
| 756 | + |
702 | 757 | def load_tests(loader, tests, pattern):
|
703 | 758 | tests.addTest(doctest.DocTestSuite(pickle))
|
704 | 759 | return tests
|
|
0 commit comments