leveldb_patch_test.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. # Copyright 2022 Google LLC
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import leveldb_patch
  15. import pathlib
  16. import unittest
  17. class CMakeListsPatcherTest(unittest.TestCase):
  18. def setUp(self):
  19. super().setUp()
  20. self.sample_snappy_source_dir = pathlib.Path("a/b/snappy_source_dir")
  21. self.sample_snappy_binary_dir = pathlib.Path("a/b/snappy_binary_dir")
  22. def test_snappy_detect_line_is_commented_and_replaced(self):
  23. lines = (
  24. """check_library_exists(snappy snappy_compress "" HAVE_SNAPPY)""",
  25. )
  26. patcher = leveldb_patch.CMakeListsPatcher(
  27. lines,
  28. "MyCoolScript",
  29. self.sample_snappy_source_dir,
  30. self.sample_snappy_binary_dir,
  31. )
  32. patched_lines = tuple(patcher.patch())
  33. self.assertSequenceEqual(patched_lines, [
  34. "# BEGIN: snappy_detect_line modification by MyCoolScript",
  35. """# check_library_exists(snappy snappy_compress "" HAVE_SNAPPY)""",
  36. """set(HAVE_SNAPPY ON CACHE BOOL "")""",
  37. "# END: snappy_detect_line modification by MyCoolScript",
  38. ])
  39. def test_snappy_detect_line_has_indent_and_eol_preserved(self):
  40. lines = (
  41. """ check_library_exists(snappy snappy_compress "" HAVE_SNAPPY) \n""",
  42. )
  43. patcher = leveldb_patch.CMakeListsPatcher(
  44. lines,
  45. "MyCoolScript",
  46. self.sample_snappy_source_dir,
  47. self.sample_snappy_binary_dir,
  48. )
  49. patched_lines = tuple(patcher.patch())
  50. self.assertSequenceEqual(patched_lines, [
  51. "# BEGIN: snappy_detect_line modification by MyCoolScript \n",
  52. """ # check_library_exists(snappy snappy_compress "" HAVE_SNAPPY) \n""",
  53. """ set(HAVE_SNAPPY ON CACHE BOOL "") \n""",
  54. "# END: snappy_detect_line modification by MyCoolScript \n",
  55. ])
  56. def test_snappy_detect_line_does_not_affect_surrounding_lines(self):
  57. lines = (
  58. "aaa",
  59. "bbb",
  60. """check_library_exists(snappy snappy_compress "" HAVE_SNAPPY)""",
  61. "ccc",
  62. "ddd",
  63. )
  64. patcher = leveldb_patch.CMakeListsPatcher(
  65. lines,
  66. "MyCoolScript",
  67. self.sample_snappy_source_dir,
  68. self.sample_snappy_binary_dir,
  69. )
  70. patched_lines = tuple(patcher.patch())
  71. self.assertSequenceEqual(patched_lines, [
  72. "aaa",
  73. "bbb",
  74. "# BEGIN: snappy_detect_line modification by MyCoolScript",
  75. """# check_library_exists(snappy snappy_compress "" HAVE_SNAPPY)""",
  76. """set(HAVE_SNAPPY ON CACHE BOOL "")""",
  77. "# END: snappy_detect_line modification by MyCoolScript",
  78. "ccc",
  79. "ddd",
  80. ])
  81. def test_snappy_include_is_amended(self):
  82. lines = (
  83. "target_include_directories(leveldb",
  84. "PUBLIC",
  85. "path1",
  86. "path2",
  87. ")",
  88. )
  89. patcher = leveldb_patch.CMakeListsPatcher(
  90. lines,
  91. script_name="MyCoolScript",
  92. snappy_source_dir=pathlib.Path("a/b"),
  93. snappy_binary_dir=pathlib.Path("c/d"),
  94. )
  95. patched_lines = tuple(patcher.patch())
  96. self.assertSequenceEqual(patched_lines, [
  97. "target_include_directories(leveldb",
  98. "# BEGIN: leveldb_include_start modification by MyCoolScript",
  99. "PRIVATE",
  100. "a/b",
  101. "c/d",
  102. "# END: leveldb_include_start modification by MyCoolScript",
  103. "PUBLIC",
  104. "path1",
  105. "path2",
  106. ")",
  107. ])
  108. def test_snappy_include_lines_adopt_indenting_and_eol_convention(self):
  109. lines = (
  110. "target_include_directories(leveldb\n",
  111. " PUBLIC \n",
  112. " path1 \n",
  113. " path2 \n",
  114. ")\n",
  115. )
  116. patcher = leveldb_patch.CMakeListsPatcher(
  117. lines,
  118. script_name="MyCoolScript",
  119. snappy_source_dir=pathlib.Path("a/b"),
  120. snappy_binary_dir=pathlib.Path("c/d"),
  121. )
  122. patched_lines = tuple(patcher.patch())
  123. self.assertSequenceEqual(patched_lines, [
  124. "target_include_directories(leveldb\n",
  125. "# BEGIN: leveldb_include_start modification by MyCoolScript \n",
  126. " PRIVATE \n",
  127. " a/b \n",
  128. " c/d \n",
  129. "# END: leveldb_include_start modification by MyCoolScript \n",
  130. " PUBLIC \n",
  131. " path1 \n",
  132. " path2 \n",
  133. ")\n",
  134. ])
  135. def test_snappy_include_line_does_not_affect_surrounding_lines(self):
  136. lines = (
  137. "aaa",
  138. "bbb",
  139. "target_include_directories(leveldb",
  140. "PUBLIC",
  141. "path1",
  142. "path2",
  143. ")",
  144. "ccc",
  145. "ddd",
  146. )
  147. patcher = leveldb_patch.CMakeListsPatcher(
  148. lines,
  149. script_name="MyCoolScript",
  150. snappy_source_dir=pathlib.Path("a/b"),
  151. snappy_binary_dir=pathlib.Path("c/d"),
  152. )
  153. patched_lines = tuple(patcher.patch())
  154. self.assertSequenceEqual(patched_lines, [
  155. "aaa",
  156. "bbb",
  157. "target_include_directories(leveldb",
  158. "# BEGIN: leveldb_include_start modification by MyCoolScript",
  159. "PRIVATE",
  160. "a/b",
  161. "c/d",
  162. "# END: leveldb_include_start modification by MyCoolScript",
  163. "PUBLIC",
  164. "path1",
  165. "path2",
  166. ")",
  167. "ccc",
  168. "ddd",
  169. ])
  170. def test_leveldb_snappy_link_line_is_commented_and_replaced(self):
  171. lines = (
  172. "target_link_libraries(leveldb snappy)",
  173. )
  174. patcher = leveldb_patch.CMakeListsPatcher(
  175. lines,
  176. script_name="MyCoolScript",
  177. snappy_source_dir=pathlib.Path("a/b"),
  178. snappy_binary_dir=pathlib.Path("c/d"),
  179. )
  180. patched_lines = tuple(patcher.patch())
  181. self.assertSequenceEqual(patched_lines, [
  182. "# BEGIN: leveldb_snappy_link_line modification by MyCoolScript",
  183. "# target_link_libraries(leveldb snappy)",
  184. "target_link_libraries(leveldb Snappy::Snappy)",
  185. "# END: leveldb_snappy_link_line modification by MyCoolScript",
  186. ])
  187. def test_leveldb_snappy_link_line_has_indent_and_eol_preserved(self):
  188. lines = (
  189. " target_link_libraries(leveldb snappy) \n",
  190. )
  191. patcher = leveldb_patch.CMakeListsPatcher(
  192. lines,
  193. script_name="MyCoolScript",
  194. snappy_source_dir=pathlib.Path("a/b"),
  195. snappy_binary_dir=pathlib.Path("c/d"),
  196. )
  197. patched_lines = tuple(patcher.patch())
  198. self.assertSequenceEqual(patched_lines, [
  199. "# BEGIN: leveldb_snappy_link_line modification by MyCoolScript \n",
  200. " # target_link_libraries(leveldb snappy) \n",
  201. " target_link_libraries(leveldb Snappy::Snappy) \n",
  202. "# END: leveldb_snappy_link_line modification by MyCoolScript \n",
  203. ])
  204. def test_leveldb_snappy_link_line_does_not_affect_surrounding_lines(self):
  205. lines = (
  206. "aaa",
  207. "bbb",
  208. "target_link_libraries(leveldb snappy)",
  209. "ccc",
  210. "ddd",
  211. )
  212. patcher = leveldb_patch.CMakeListsPatcher(
  213. lines,
  214. script_name="MyCoolScript",
  215. snappy_source_dir=pathlib.Path("a/b"),
  216. snappy_binary_dir=pathlib.Path("c/d"),
  217. )
  218. patched_lines = tuple(patcher.patch())
  219. self.assertSequenceEqual(patched_lines, [
  220. "aaa",
  221. "bbb",
  222. "# BEGIN: leveldb_snappy_link_line modification by MyCoolScript",
  223. "# target_link_libraries(leveldb snappy)",
  224. "target_link_libraries(leveldb Snappy::Snappy)",
  225. "# END: leveldb_snappy_link_line modification by MyCoolScript",
  226. "ccc",
  227. "ddd",
  228. ])
  229. def test_all_patches_combined(self):
  230. lines = (
  231. """check_library_exists(snappy snappy_compress "" HAVE_SNAPPY)""",
  232. "target_include_directories(leveldb",
  233. "PUBLIC",
  234. "path1",
  235. ")",
  236. "target_link_libraries(leveldb snappy)",
  237. )
  238. patcher = leveldb_patch.CMakeListsPatcher(
  239. lines,
  240. script_name="MyCoolScript",
  241. snappy_source_dir=pathlib.Path("a/b"),
  242. snappy_binary_dir=pathlib.Path("c/d"),
  243. )
  244. patched_lines = tuple(patcher.patch())
  245. self.assertSequenceEqual(patched_lines, [
  246. "# BEGIN: snappy_detect_line modification by MyCoolScript",
  247. """# check_library_exists(snappy snappy_compress "" HAVE_SNAPPY)""",
  248. """set(HAVE_SNAPPY ON CACHE BOOL "")""",
  249. "# END: snappy_detect_line modification by MyCoolScript",
  250. "target_include_directories(leveldb",
  251. "# BEGIN: leveldb_include_start modification by MyCoolScript",
  252. "PRIVATE",
  253. "a/b",
  254. "c/d",
  255. "# END: leveldb_include_start modification by MyCoolScript",
  256. "PUBLIC",
  257. "path1",
  258. ")",
  259. "# BEGIN: leveldb_snappy_link_line modification by MyCoolScript",
  260. "# target_link_libraries(leveldb snappy)",
  261. "target_link_libraries(leveldb Snappy::Snappy)",
  262. "# END: leveldb_snappy_link_line modification by MyCoolScript",
  263. ])
  264. def test_idempotence(self):
  265. lines = (
  266. """check_library_exists(snappy snappy_compress "" HAVE_SNAPPY)\n""",
  267. "target_include_directories(leveldb",
  268. "PUBLIC",
  269. "path1",
  270. ")",
  271. "target_link_libraries(leveldb snappy)",
  272. )
  273. patcher1 = leveldb_patch.CMakeListsPatcher(
  274. lines,
  275. script_name="MyCoolScript",
  276. snappy_source_dir=pathlib.Path("a/b"),
  277. snappy_binary_dir=pathlib.Path("c/d"),
  278. )
  279. patched_lines1 = tuple(patcher1.patch())
  280. patcher2 = leveldb_patch.CMakeListsPatcher(
  281. patched_lines1,
  282. script_name="MyCoolScript",
  283. snappy_source_dir=pathlib.Path("a/b"),
  284. snappy_binary_dir=pathlib.Path("c/d"),
  285. )
  286. patched_lines2 = tuple(patcher2.patch())
  287. self.assertSequenceEqual(patched_lines1, patched_lines2)
  288. if __name__ == "__main__":
  289. unittest.main()