|
16 | 16 | #
|
17 | 17 | """System tests for ndk-stack.py"""
|
18 | 18 |
|
19 |
| -from __future__ import print_function |
20 |
| - |
21 | 19 | import os.path
|
| 20 | +import subprocess |
22 | 21 | import unittest
|
23 |
| -from io import StringIO |
24 |
| -from unittest.mock import patch |
25 | 22 |
|
26 |
| -import ndk.hosts |
| 23 | +import ndk.paths |
27 | 24 | import ndk.toolchains
|
28 |
| -import ndkstack |
| 25 | +from ndk.hosts import Host |
29 | 26 |
|
30 | 27 |
|
31 | 28 | class SystemTests(unittest.TestCase):
|
32 | 29 | """Complete system test of ndk-stack.py script."""
|
33 | 30 |
|
34 |
| - def setUp(self): |
35 |
| - default_host = ndk.hosts.get_default_host() |
36 |
| - clang_toolchain = ndk.toolchains.ClangToolchain(default_host) |
37 |
| - |
38 |
| - # First try and use the normal functions, and if they fail, then |
39 |
| - # use hard-coded paths from the development locations. |
40 |
| - ndk_paths = ndkstack.get_ndk_paths() |
41 |
| - self.readelf = ndkstack.find_readelf(*ndk_paths) |
42 |
| - if not self.readelf: |
43 |
| - self.readelf = clang_toolchain.clang_tool("llvm-readelf") |
44 |
| - self.assertTrue(self.readelf) |
45 |
| - self.assertTrue(os.path.exists(self.readelf)) |
46 |
| - |
47 |
| - try: |
48 |
| - self.llvm_symbolizer = ndkstack.find_llvm_symbolizer(*ndk_paths) |
49 |
| - except OSError: |
50 |
| - self.llvm_symbolizer = str(clang_toolchain.clang_tool("llvm-symbolizer")) |
51 |
| - self.assertTrue(self.llvm_symbolizer) |
52 |
| - self.assertTrue(os.path.exists(self.llvm_symbolizer)) |
| 31 | + def system_test(self, backtrace_file: str, expected_file: str) -> None: |
| 32 | + ndk_path = ndk.paths.get_install_path() |
| 33 | + self.assertTrue( |
| 34 | + ndk_path.exists(), |
| 35 | + f"{ndk_path} does not exist. Build the NDK before running this test.", |
| 36 | + ) |
53 | 37 |
|
54 |
| - @patch.object(ndkstack, "find_llvm_symbolizer") |
55 |
| - @patch.object(ndkstack, "find_readelf") |
56 |
| - def system_test( |
57 |
| - self, backtrace_file, expected_file, mock_readelf, mock_llvm_symbolizer |
58 |
| - ): |
59 |
| - mock_readelf.return_value = self.readelf |
60 |
| - mock_llvm_symbolizer.return_value = self.llvm_symbolizer |
| 38 | + ndk_stack = ndk_path / "ndk-stack" |
| 39 | + if Host.current() is Host.Windows64: |
| 40 | + ndk_stack = ndk_stack.with_suffix(".bat") |
61 | 41 |
|
62 | 42 | symbol_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "files")
|
63 |
| - with patch("sys.stdout", new_callable=StringIO) as mock_stdout: |
64 |
| - ndkstack.main( |
65 |
| - ["-s", symbol_dir, "-i", os.path.join(symbol_dir, backtrace_file)] |
66 |
| - ) |
| 43 | + proc = subprocess.run( |
| 44 | + [ |
| 45 | + ndk_stack, |
| 46 | + "-s", |
| 47 | + symbol_dir, |
| 48 | + "-i", |
| 49 | + os.path.join(symbol_dir, backtrace_file), |
| 50 | + ], |
| 51 | + check=True, |
| 52 | + capture_output=True, |
| 53 | + text=True, |
| 54 | + ) |
67 | 55 |
|
68 | 56 | # Read the expected output.
|
69 | 57 | file_name = os.path.join(symbol_dir, expected_file)
|
70 | 58 | with open(file_name, "r", encoding="utf-8") as exp_file:
|
71 | 59 | expected = exp_file.read()
|
72 | 60 | expected = expected.replace("SYMBOL_DIR", symbol_dir)
|
73 | 61 | self.maxDiff = None
|
74 |
| - self.assertEqual(expected, mock_stdout.getvalue()) |
| 62 | + self.assertEqual(expected, proc.stdout) |
75 | 63 |
|
76 |
| - def test_all_stacks(self): |
77 |
| - self.system_test( # pylint:disable=no-value-for-parameter |
78 |
| - "backtrace.txt", "expected.txt" |
79 |
| - ) |
| 64 | + def test_all_stacks(self) -> None: |
| 65 | + self.system_test("backtrace.txt", "expected.txt") |
80 | 66 |
|
81 |
| - def test_multiple_crashes(self): |
82 |
| - self.system_test( # pylint:disable=no-value-for-parameter |
83 |
| - "multiple.txt", "expected_multiple.txt" |
84 |
| - ) |
| 67 | + def test_multiple_crashes(self) -> None: |
| 68 | + self.system_test("multiple.txt", "expected_multiple.txt") |
85 | 69 |
|
86 |
| - def test_hwasan(self): |
87 |
| - self.system_test( # pylint:disable=no-value-for-parameter |
88 |
| - "hwasan.txt", "expected_hwasan.txt" |
89 |
| - ) |
| 70 | + def test_hwasan(self) -> None: |
| 71 | + self.system_test("hwasan.txt", "expected_hwasan.txt") |
90 | 72 |
|
91 | 73 |
|
92 | 74 | if __name__ == "__main__":
|
|
0 commit comments