|
| 1 | +import subprocess |
| 2 | +import unittest |
| 3 | + |
| 4 | +from cmpcodesize import otool |
| 5 | + |
| 6 | + |
| 7 | +# Store parameters passed to subprocess.check_output into |
| 8 | +# this global variable. |
| 9 | +_subprocess_check_output_arguments = [] |
| 10 | + |
| 11 | + |
| 12 | +# We'll monkey-patch subprocess.check_output with this stub |
| 13 | +# function, which simply records whatever's passed to |
| 14 | +# check_output into the global variables above. |
| 15 | +def _stub_subprocess_check_output(arguments, *args, **kwargs): |
| 16 | + global _subprocess_check_output_arguments |
| 17 | + _subprocess_check_output_arguments = arguments |
| 18 | + |
| 19 | + |
| 20 | +class OtoolTestCase(unittest.TestCase): |
| 21 | + def setUp(self): |
| 22 | + # Monkey-patch subprocess.check_output with our stub function. |
| 23 | + self._original_check_output = subprocess.check_output |
| 24 | + subprocess.check_output = _stub_subprocess_check_output |
| 25 | + |
| 26 | + def tearDown(self): |
| 27 | + # Undo the monkey-patching. |
| 28 | + subprocess.check_output = self._original_check_output |
| 29 | + |
| 30 | + def test_fat_headers(self): |
| 31 | + otool.fat_headers('/path/to/foo') |
| 32 | + self.assertEqual(_subprocess_check_output_arguments, |
| 33 | + ['otool', '-V', '-f', '/path/to/foo']) |
| 34 | + |
| 35 | + def test_load_commands_with_no_architecture(self): |
| 36 | + otool.load_commands('/path/to/bar') |
| 37 | + self.assertEqual(_subprocess_check_output_arguments, |
| 38 | + ['otool', '-l', '/path/to/bar']) |
| 39 | + |
| 40 | + def test_load_commands_with_architecture(self): |
| 41 | + otool.load_commands('/path/to/baz', architecture='arch-foo') |
| 42 | + self.assertEqual( |
| 43 | + _subprocess_check_output_arguments, |
| 44 | + ['otool', '-arch', 'arch-foo', '-l', '/path/to/baz']) |
| 45 | + |
| 46 | + def test_load_commands_no_architecture_but_including_text_sections(self): |
| 47 | + otool.load_commands( |
| 48 | + '/path/to/flim', include_text_sections=True) |
| 49 | + self.assertEqual( |
| 50 | + _subprocess_check_output_arguments, |
| 51 | + ['otool', '-l', '-v', '-t', '/path/to/flim']) |
| 52 | + |
| 53 | + def test_load_commands_with_architecture_and_including_text_sections(self): |
| 54 | + otool.load_commands( |
| 55 | + '/path/to/flam', |
| 56 | + architecture='arch-bar', |
| 57 | + include_text_sections=True) |
| 58 | + self.assertEqual( |
| 59 | + _subprocess_check_output_arguments, |
| 60 | + ['otool', '-arch', 'arch-bar', '-l', '-v', '-t', '/path/to/flam']) |
| 61 | + |
| 62 | + def test_text_sections_no_architecture(self): |
| 63 | + otool.text_sections('/path/to/fish') |
| 64 | + self.assertEqual( |
| 65 | + _subprocess_check_output_arguments, |
| 66 | + ['otool', '-v', '-s', '__TEXT', '__textcoal_nt', '/path/to/fish']) |
| 67 | + |
| 68 | + def test_text_sections_with_architecture(self): |
| 69 | + otool.text_sections('/path/to/frosh', architecture='arch-baz') |
| 70 | + self.assertEqual( |
| 71 | + _subprocess_check_output_arguments, |
| 72 | + ['otool', '-arch', 'arch-baz', '-v', '-s', |
| 73 | + '__TEXT', '__textcoal_nt', '/path/to/frosh']) |
0 commit comments