| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- # Copyright 2022 Google LLC
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- import leveldb_patch
- import pathlib
- import unittest
- class CMakeListsPatcherTest(unittest.TestCase):
- def setUp(self):
- super().setUp()
- self.sample_snappy_source_dir = pathlib.Path("a/b/snappy_source_dir")
- self.sample_snappy_binary_dir = pathlib.Path("a/b/snappy_binary_dir")
- def test_snappy_detect_line_is_commented_and_replaced(self):
- lines = (
- """check_library_exists(snappy snappy_compress "" HAVE_SNAPPY)""",
- )
- patcher = leveldb_patch.CMakeListsPatcher(
- lines,
- "MyCoolScript",
- self.sample_snappy_source_dir,
- self.sample_snappy_binary_dir,
- )
- patched_lines = tuple(patcher.patch())
- self.assertSequenceEqual(patched_lines, [
- "# BEGIN: snappy_detect_line modification by MyCoolScript",
- """# check_library_exists(snappy snappy_compress "" HAVE_SNAPPY)""",
- """set(HAVE_SNAPPY ON CACHE BOOL "")""",
- "# END: snappy_detect_line modification by MyCoolScript",
- ])
- def test_snappy_detect_line_has_indent_and_eol_preserved(self):
- lines = (
- """ check_library_exists(snappy snappy_compress "" HAVE_SNAPPY) \n""",
- )
- patcher = leveldb_patch.CMakeListsPatcher(
- lines,
- "MyCoolScript",
- self.sample_snappy_source_dir,
- self.sample_snappy_binary_dir,
- )
- patched_lines = tuple(patcher.patch())
- self.assertSequenceEqual(patched_lines, [
- "# BEGIN: snappy_detect_line modification by MyCoolScript \n",
- """ # check_library_exists(snappy snappy_compress "" HAVE_SNAPPY) \n""",
- """ set(HAVE_SNAPPY ON CACHE BOOL "") \n""",
- "# END: snappy_detect_line modification by MyCoolScript \n",
- ])
- def test_snappy_detect_line_does_not_affect_surrounding_lines(self):
- lines = (
- "aaa",
- "bbb",
- """check_library_exists(snappy snappy_compress "" HAVE_SNAPPY)""",
- "ccc",
- "ddd",
- )
- patcher = leveldb_patch.CMakeListsPatcher(
- lines,
- "MyCoolScript",
- self.sample_snappy_source_dir,
- self.sample_snappy_binary_dir,
- )
- patched_lines = tuple(patcher.patch())
- self.assertSequenceEqual(patched_lines, [
- "aaa",
- "bbb",
- "# BEGIN: snappy_detect_line modification by MyCoolScript",
- """# check_library_exists(snappy snappy_compress "" HAVE_SNAPPY)""",
- """set(HAVE_SNAPPY ON CACHE BOOL "")""",
- "# END: snappy_detect_line modification by MyCoolScript",
- "ccc",
- "ddd",
- ])
- def test_snappy_include_is_amended(self):
- lines = (
- "target_include_directories(leveldb",
- "PUBLIC",
- "path1",
- "path2",
- ")",
- )
- patcher = leveldb_patch.CMakeListsPatcher(
- lines,
- script_name="MyCoolSript",
- snappy_source_dir=pathlib.Path("a/b"),
- snappy_binary_dir=pathlib.Path("c/d"),
- )
- patched_lines = tuple(patcher.patch())
- self.assertSequenceEqual(patched_lines, [
- "target_include_directories(leveldb",
- "# BEGIN: leveldb_include_start modification by MyCoolSript",
- "PRIVATE",
- "a/b",
- "c/d",
- "# END: leveldb_include_start modification by MyCoolSript",
- "PUBLIC",
- "path1",
- "path2",
- ")",
- ])
- def test_snappy_include_lines_adopt_indenting_and_eol_convention(self):
- lines = (
- "target_include_directories(leveldb\n",
- " PUBLIC \n",
- " path1 \n",
- " path2 \n",
- ")\n",
- )
- patcher = leveldb_patch.CMakeListsPatcher(
- lines,
- script_name="MyCoolSript",
- snappy_source_dir=pathlib.Path("a/b"),
- snappy_binary_dir=pathlib.Path("c/d"),
- )
- patched_lines = tuple(patcher.patch())
- self.assertSequenceEqual(patched_lines, [
- "target_include_directories(leveldb\n",
- "# BEGIN: leveldb_include_start modification by MyCoolSript \n",
- " PRIVATE \n",
- " a/b \n",
- " c/d \n",
- "# END: leveldb_include_start modification by MyCoolSript \n",
- " PUBLIC \n",
- " path1 \n",
- " path2 \n",
- ")\n",
- ])
- def test_snappy_include_line_does_not_affect_surrounding_lines(self):
- lines = (
- "aaa",
- "bbb",
- "target_include_directories(leveldb",
- "PUBLIC",
- "path1",
- "path2",
- ")",
- "ccc",
- "ddd",
- )
- patcher = leveldb_patch.CMakeListsPatcher(
- lines,
- script_name="MyCoolSript",
- snappy_source_dir=pathlib.Path("a/b"),
- snappy_binary_dir=pathlib.Path("c/d"),
- )
- patched_lines = tuple(patcher.patch())
- self.assertSequenceEqual(patched_lines, [
- "aaa",
- "bbb",
- "target_include_directories(leveldb",
- "# BEGIN: leveldb_include_start modification by MyCoolSript",
- "PRIVATE",
- "a/b",
- "c/d",
- "# END: leveldb_include_start modification by MyCoolSript",
- "PUBLIC",
- "path1",
- "path2",
- ")",
- "ccc",
- "ddd",
- ])
- def test_leveldb_snappy_link_line_is_commented_and_replaced(self):
- lines = (
- "target_link_libraries(leveldb snappy)",
- )
- patcher = leveldb_patch.CMakeListsPatcher(
- lines,
- script_name="MyCoolSript",
- snappy_source_dir=pathlib.Path("a/b"),
- snappy_binary_dir=pathlib.Path("c/d"),
- )
- patched_lines = tuple(patcher.patch())
- self.assertSequenceEqual(patched_lines, [
- "# BEGIN: leveldb_snappy_link_line modification by MyCoolSript",
- "# target_link_libraries(leveldb snappy)",
- "target_link_libraries(leveldb Snappy::Snappy)",
- "# END: leveldb_snappy_link_line modification by MyCoolSript",
- ])
- def test_leveldb_snappy_link_line_has_indent_and_eol_preserved(self):
- lines = (
- " target_link_libraries(leveldb snappy) \n",
- )
- patcher = leveldb_patch.CMakeListsPatcher(
- lines,
- script_name="MyCoolSript",
- snappy_source_dir=pathlib.Path("a/b"),
- snappy_binary_dir=pathlib.Path("c/d"),
- )
- patched_lines = tuple(patcher.patch())
- self.assertSequenceEqual(patched_lines, [
- "# BEGIN: leveldb_snappy_link_line modification by MyCoolSript \n",
- " # target_link_libraries(leveldb snappy) \n",
- " target_link_libraries(leveldb Snappy::Snappy) \n",
- "# END: leveldb_snappy_link_line modification by MyCoolSript \n",
- ])
- def test_leveldb_snappy_link_line_does_not_affect_surrounding_lines(self):
- lines = (
- "aaa",
- "bbb",
- "target_link_libraries(leveldb snappy)",
- "ccc",
- "ddd",
- )
- patcher = leveldb_patch.CMakeListsPatcher(
- lines,
- script_name="MyCoolSript",
- snappy_source_dir=pathlib.Path("a/b"),
- snappy_binary_dir=pathlib.Path("c/d"),
- )
- patched_lines = tuple(patcher.patch())
- self.assertSequenceEqual(patched_lines, [
- "aaa",
- "bbb",
- "# BEGIN: leveldb_snappy_link_line modification by MyCoolSript",
- "# target_link_libraries(leveldb snappy)",
- "target_link_libraries(leveldb Snappy::Snappy)",
- "# END: leveldb_snappy_link_line modification by MyCoolSript",
- "ccc",
- "ddd",
- ])
- def test_all_patches_combined(self):
- lines = (
- """check_library_exists(snappy snappy_compress "" HAVE_SNAPPY)""",
- "target_include_directories(leveldb",
- "PUBLIC",
- "path1",
- ")",
- "target_link_libraries(leveldb snappy)",
- )
- patcher = leveldb_patch.CMakeListsPatcher(
- lines,
- script_name="MyCoolSript",
- snappy_source_dir=pathlib.Path("a/b"),
- snappy_binary_dir=pathlib.Path("c/d"),
- )
- patched_lines = tuple(patcher.patch())
- self.assertSequenceEqual(patched_lines, [
- "# BEGIN: snappy_detect_line modification by MyCoolSript",
- """# check_library_exists(snappy snappy_compress "" HAVE_SNAPPY)""",
- """set(HAVE_SNAPPY ON CACHE BOOL "")""",
- "# END: snappy_detect_line modification by MyCoolSript",
- "target_include_directories(leveldb",
- "# BEGIN: leveldb_include_start modification by MyCoolSript",
- "PRIVATE",
- "a/b",
- "c/d",
- "# END: leveldb_include_start modification by MyCoolSript",
- "PUBLIC",
- "path1",
- ")",
- "# BEGIN: leveldb_snappy_link_line modification by MyCoolSript",
- "# target_link_libraries(leveldb snappy)",
- "target_link_libraries(leveldb Snappy::Snappy)",
- "# END: leveldb_snappy_link_line modification by MyCoolSript",
- ])
- def test_idempotence(self):
- lines = (
- """check_library_exists(snappy snappy_compress "" HAVE_SNAPPY)\n""",
- "target_include_directories(leveldb",
- "PUBLIC",
- "path1",
- ")",
- "target_link_libraries(leveldb snappy)",
- )
- patcher1 = leveldb_patch.CMakeListsPatcher(
- lines,
- script_name="MyCoolSript",
- snappy_source_dir=pathlib.Path("a/b"),
- snappy_binary_dir=pathlib.Path("c/d"),
- )
- patched_lines1 = tuple(patcher1.patch())
- patcher2 = leveldb_patch.CMakeListsPatcher(
- patched_lines1,
- script_name="MyCoolSript",
- snappy_source_dir=pathlib.Path("a/b"),
- snappy_binary_dir=pathlib.Path("c/d"),
- )
- patched_lines2 = tuple(patcher2.patch())
- self.assertSequenceEqual(patched_lines1, patched_lines2)
- if __name__ == "__main__":
- unittest.main()
|