Ben Pastene | 3b05a63 | 2023-01-12 22:05:30 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright 2023 The Chromium Authors |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | import os |
| 7 | import pathlib |
| 8 | import shutil |
Junji Watanabe | 3145bd0 | 2023-02-22 07:57:18 | [diff] [blame] | 9 | import sys |
Ben Pastene | 3b05a63 | 2023-01-12 22:05:30 | [diff] [blame] | 10 | import tempfile |
Ben Pastene | e9d888e | 2023-01-17 23:24:10 | [diff] [blame] | 11 | import textwrap |
Ben Pastene | 3b05a63 | 2023-01-12 22:05:30 | [diff] [blame] | 12 | import unittest |
Ben Pastene | e9d888e | 2023-01-17 23:24:10 | [diff] [blame] | 13 | from unittest import mock |
Ben Pastene | 3b05a63 | 2023-01-12 22:05:30 | [diff] [blame] | 14 | |
| 15 | import clobber |
| 16 | |
| 17 | |
Ben Pastene | e9d888e | 2023-01-17 23:24:10 | [diff] [blame] | 18 | class TestExtractBuildCommand(unittest.TestCase): |
| 19 | def setUp(self): |
Junji Watanabe | 00584253 | 2023-01-18 18:21:32 | [diff] [blame] | 20 | self.build_ninja_file, self.build_ninja_path = tempfile.mkstemp(text=True) |
Ben Pastene | e9d888e | 2023-01-17 23:24:10 | [diff] [blame] | 21 | |
| 22 | def tearDown(self): |
Junji Watanabe | 00584253 | 2023-01-18 18:21:32 | [diff] [blame] | 23 | os.close(self.build_ninja_file) |
Ben Pastene | e9d888e | 2023-01-17 23:24:10 | [diff] [blame] | 24 | os.remove(self.build_ninja_path) |
| 25 | |
| 26 | def test_normal_extraction(self): |
| 27 | build_ninja_file_contents = textwrap.dedent(""" |
| 28 | ninja_required_version = 1.7.2 |
| 29 | |
| 30 | rule gn |
| 31 | command = ../../buildtools/gn --root=../.. -q --regeneration gen . |
| 32 | pool = console |
| 33 | description = Regenerating ninja files |
| 34 | |
| 35 | build build.ninja.stamp: gn |
| 36 | generator = 1 |
| 37 | depfile = build.ninja.d |
| 38 | |
| 39 | build build.ninja: phony build.ninja.stamp |
| 40 | generator = 1 |
| 41 | |
| 42 | pool build_toolchain_action_pool |
| 43 | depth = 72 |
| 44 | |
| 45 | pool build_toolchain_link_pool |
| 46 | depth = 23 |
| 47 | |
| 48 | subninja toolchain.ninja |
| 49 | subninja clang_newlib_x64/toolchain.ninja |
| 50 | subninja glibc_x64/toolchain.ninja |
| 51 | subninja irt_x64/toolchain.ninja |
| 52 | subninja nacl_bootstrap_x64/toolchain.ninja |
| 53 | subninja newlib_pnacl/toolchain.ninja |
| 54 | |
| 55 | build blink_python_tests: phony obj/blink_python_tests.stamp |
| 56 | build blink_tests: phony obj/blink_tests.stamp |
| 57 | |
| 58 | default all |
| 59 | """) # Based off of a standard linux build dir. |
| 60 | with open(self.build_ninja_path, 'w') as f: |
| 61 | f.write(build_ninja_file_contents) |
| 62 | |
| 63 | expected_build_ninja_file_contents = textwrap.dedent(""" |
| 64 | ninja_required_version = 1.7.2 |
| 65 | |
| 66 | rule gn |
| 67 | command = ../../buildtools/gn --root=../.. -q --regeneration gen . |
| 68 | pool = console |
| 69 | description = Regenerating ninja files |
| 70 | |
| 71 | build build.ninja.stamp: gn |
| 72 | generator = 1 |
| 73 | depfile = build.ninja.d |
| 74 | |
| 75 | build build.ninja: phony build.ninja.stamp |
| 76 | generator = 1 |
| 77 | |
| 78 | """) |
| 79 | |
| 80 | self.assertEqual(clobber.extract_gn_build_commands(self.build_ninja_path), |
| 81 | expected_build_ninja_file_contents) |
| 82 | |
| 83 | def test_unexpected_format(self): |
| 84 | # No "build build.ninja:" line should make it return an empty string. |
| 85 | build_ninja_file_contents = textwrap.dedent(""" |
| 86 | ninja_required_version = 1.7.2 |
| 87 | |
| 88 | rule gn |
| 89 | command = ../../buildtools/gn --root=../.. -q --regeneration gen . |
| 90 | pool = console |
| 91 | description = Regenerating ninja files |
| 92 | |
| 93 | subninja toolchain.ninja |
| 94 | |
| 95 | build blink_python_tests: phony obj/blink_python_tests.stamp |
| 96 | build blink_tests: phony obj/blink_tests.stamp |
| 97 | |
| 98 | """) |
| 99 | with open(self.build_ninja_path, 'w') as f: |
| 100 | f.write(build_ninja_file_contents) |
| 101 | |
| 102 | self.assertEqual(clobber.extract_gn_build_commands(self.build_ninja_path), |
| 103 | '') |
| 104 | |
| 105 | |
| 106 | class TestDelete(unittest.TestCase): |
Ben Pastene | 3b05a63 | 2023-01-12 22:05:30 | [diff] [blame] | 107 | def setUp(self): |
| 108 | self.build_dir = tempfile.mkdtemp() |
| 109 | |
Ben Pastene | e9d888e | 2023-01-17 23:24:10 | [diff] [blame] | 110 | pathlib.Path(os.path.join(self.build_dir, 'build.ninja')).touch() |
| 111 | pathlib.Path(os.path.join(self.build_dir, 'build.ninja.d')).touch() |
| 112 | |
Ben Pastene | 3b05a63 | 2023-01-12 22:05:30 | [diff] [blame] | 113 | def tearDown(self): |
| 114 | shutil.rmtree(self.build_dir) |
| 115 | |
Ben Pastene | e9d888e | 2023-01-17 23:24:10 | [diff] [blame] | 116 | def test_delete_build_dir_full(self): |
Ben Pastene | 3b05a63 | 2023-01-12 22:05:30 | [diff] [blame] | 117 | # Create a dummy file in the build dir and ensure it gets removed. |
| 118 | dummy_file = os.path.join(self.build_dir, 'dummy') |
| 119 | pathlib.Path(dummy_file).touch() |
| 120 | |
Ben Pastene | e9d888e | 2023-01-17 23:24:10 | [diff] [blame] | 121 | clobber.delete_build_dir(self.build_dir) |
| 122 | |
Ben Pastene | 3b05a63 | 2023-01-12 22:05:30 | [diff] [blame] | 123 | self.assertFalse(os.path.exists(dummy_file)) |
| 124 | |
Ben Pastene | e9d888e | 2023-01-17 23:24:10 | [diff] [blame] | 125 | def test_delete_build_dir_fail(self): |
| 126 | # Make delete_dir() throw to ensure it's handled gracefully. |
| 127 | |
Junji Watanabe | 741c3f6 | 2023-01-19 22:00:05 | [diff] [blame] | 128 | with mock.patch('clobber._clean_dir', side_effect=OSError): |
Ben Pastene | e9d888e | 2023-01-17 23:24:10 | [diff] [blame] | 129 | with self.assertRaises(OSError): |
| 130 | clobber.delete_build_dir(self.build_dir) |
Ben Pastene | 3b05a63 | 2023-01-12 22:05:30 | [diff] [blame] | 131 | |
Junji Watanabe | 3145bd0 | 2023-02-22 07:57:18 | [diff] [blame] | 132 | @unittest.skipIf(sys.platform == 'win32', 'Symlinks are not allowed on Windows by default') |
Junji Watanabe | 741c3f6 | 2023-01-19 22:00:05 | [diff] [blame] | 133 | def test_delete_build_dir_link(self): |
| 134 | with tempfile.TemporaryDirectory() as tmpdir: |
| 135 | # create a symlink. |
| 136 | build_dir = os.path.join(tmpdir, 'link') |
| 137 | os.symlink(self.build_dir, build_dir) |
| 138 | |
| 139 | # create a dummy file. |
| 140 | dummy_file = os.path.join(build_dir, 'dummy') |
| 141 | pathlib.Path(dummy_file).touch() |
| 142 | clobber.delete_build_dir(build_dir) |
| 143 | |
| 144 | self.assertFalse(os.path.exists(dummy_file)) |
| 145 | |
Ben Pastene | 3b05a63 | 2023-01-12 22:05:30 | [diff] [blame] | 146 | |
| 147 | if __name__ == '__main__': |
| 148 | unittest.main() |