patch_selenium_test.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import os
  2. import re
  3. import sys
  4. from unittest import mock
  5. import pytest
  6. from tools import patch_selenium
  7. if sys.version_info >= (3, 11):
  8. from contextlib import chdir
  9. else:
  10. import contextlib
  11. @contextlib.contextmanager
  12. def chdir(s):
  13. orig = os.getcwd()
  14. os.chdir(s)
  15. try:
  16. yield
  17. finally:
  18. os.chdir(orig)
  19. @pytest.fixture
  20. def getsentry_setup(tmp_path):
  21. # simulate a set of patches, and patching from `getsentry`
  22. tmp_path.joinpath("sentry/tools").mkdir(parents=True)
  23. patch = """\
  24. --- f1
  25. +++ f1,bak
  26. @@ -1 +1 @@
  27. -hello
  28. +hello hello
  29. """
  30. scripts_patches_dir = tmp_path.joinpath("sentry/scripts/patches")
  31. scripts_patches_dir.mkdir(parents=True)
  32. scripts_patches_dir.joinpath("patch1.patch").write_text(patch)
  33. getsentry_dir = tmp_path.joinpath("getsentry")
  34. getsentry_dir.mkdir()
  35. getsentry_dir.joinpath("tools").symlink_to("../sentry/tools")
  36. getsentry_dir.joinpath("f1").write_text("hello\n")
  37. fake_file = str(getsentry_dir.joinpath("tools/patch_selenium.py"))
  38. fake_patches = (
  39. (
  40. "scripts/patches/patch1.patch",
  41. "f1",
  42. re.compile(r"^hello$"),
  43. ),
  44. )
  45. with mock.patch.multiple(patch_selenium, __file__=fake_file, PATCH_FILE_PATTERN=fake_patches):
  46. yield tmp_path
  47. def test_patching_from_getsentry(capsys, getsentry_setup):
  48. with chdir(getsentry_setup.joinpath("getsentry")):
  49. assert patch_selenium.main() == 0
  50. assert getsentry_setup.joinpath("getsentry/f1").read_text() == "hello hello\n"
  51. out, _ = capsys.readouterr()
  52. assert out == "patching f1, you will only see this once\n"
  53. def test_patching_from_getsentry_called_in_sentry(capsys, getsentry_setup):
  54. # getsentry *also* calls this script from sentry when setting itself up
  55. with chdir(getsentry_setup.joinpath("sentry")):
  56. assert patch_selenium.main() == 0
  57. out, _ = capsys.readouterr()
  58. assert out == "patch_selenium: ignoring f1 (does not exist)\n"