test_validate_patches.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # -*- coding: UTF-8 -*-
  2. # Copyright (c) 2020 The ungoogled-chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Test validate_patches.py"""
  6. import logging
  7. import tempfile
  8. import sys
  9. from pathlib import Path
  10. sys.path.insert(0, str(Path(__file__).resolve().parent.parent.parent / 'utils'))
  11. from _common import get_logger, set_logging_level
  12. sys.path.pop(0)
  13. sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
  14. import validate_patches
  15. sys.path.pop(0)
  16. def test_test_patches():
  17. """Test _dry_check_patched_file"""
  18. #pylint: disable=protected-access
  19. set_logging_level(logging.DEBUG)
  20. orig_file_content = """bye world"""
  21. series_iter = ['test.patch']
  22. def _run_test_patches(patch_content):
  23. with tempfile.TemporaryDirectory() as tmpdirname:
  24. Path(tmpdirname, 'foobar.txt').write_text(orig_file_content)
  25. Path(tmpdirname, 'test.patch').write_text(patch_content)
  26. _, patch_cache = validate_patches._load_all_patches(series_iter, Path(tmpdirname))
  27. required_files = validate_patches._get_required_files(patch_cache)
  28. files_under_test = validate_patches._retrieve_local_files(required_files,
  29. Path(tmpdirname))
  30. return validate_patches._test_patches(series_iter, patch_cache, files_under_test)
  31. get_logger().info('Check valid modification')
  32. patch_content = """--- a/foobar.txt
  33. +++ b/foobar.txt
  34. @@ -1 +1 @@
  35. -bye world
  36. +hello world
  37. """
  38. assert not _run_test_patches(patch_content)
  39. get_logger().info('Check invalid modification')
  40. patch_content = """--- a/foobar.txt
  41. +++ b/foobar.txt
  42. @@ -1 +1 @@
  43. -hello world
  44. +olleh world
  45. """
  46. assert _run_test_patches(patch_content)
  47. get_logger().info('Check correct removal')
  48. patch_content = """--- a/foobar.txt
  49. +++ /dev/null
  50. @@ -1 +0,0 @@
  51. -bye world
  52. """
  53. assert not _run_test_patches(patch_content)
  54. get_logger().info('Check incorrect removal')
  55. patch_content = """--- a/foobar.txt
  56. +++ /dev/null
  57. @@ -1 +0,0 @@
  58. -this line does not exist in foobar
  59. """
  60. assert _run_test_patches(patch_content)
  61. if __name__ == '__main__':
  62. test_test_patches()