utils.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. """Testing Utilities
  2. This module holds values/functions common to multiple tests.
  3. """
  4. import os
  5. ALT_FILE1 = 'test_alt'
  6. ALT_FILE2 = 'test alt/test alt'
  7. def set_local(paths, variable, value):
  8. """Set local override"""
  9. os.system(
  10. f'GIT_DIR={str(paths.repo)} '
  11. f'git config --local "local.{variable}" "{value}"'
  12. )
  13. def create_alt_files(paths, suffix,
  14. preserve=False, tracked=True,
  15. encrypt=False, exclude=False,
  16. content=None):
  17. """Create new files, and add to the repo
  18. This is used for testing alternate files. In each case, a suffix is
  19. appended to two standard file paths. Particulars of the file creation and
  20. repo handling are dependent upon the function arguments.
  21. """
  22. if not preserve:
  23. if paths.work.join(ALT_FILE1).exists():
  24. paths.work.join(ALT_FILE1).remove(rec=1, ignore_errors=True)
  25. assert not paths.work.join(ALT_FILE1).exists()
  26. if paths.work.join(ALT_FILE2).exists():
  27. paths.work.join(ALT_FILE2).remove(rec=1, ignore_errors=True)
  28. assert not paths.work.join(ALT_FILE2).exists()
  29. new_file1 = paths.work.join(ALT_FILE1 + suffix)
  30. new_file1.write(ALT_FILE1 + suffix, ensure=True)
  31. new_file2 = paths.work.join(ALT_FILE2 + suffix)
  32. new_file2.write(ALT_FILE2 + suffix, ensure=True)
  33. if content:
  34. new_file1.write('\n' + content, mode='a', ensure=True)
  35. new_file2.write('\n' + content, mode='a', ensure=True)
  36. assert new_file1.exists()
  37. assert new_file2.exists()
  38. if tracked:
  39. for path in (new_file1, new_file2):
  40. os.system(f'GIT_DIR={str(paths.repo)} git add "{path}"')
  41. os.system(f'GIT_DIR={str(paths.repo)} git commit -m "Add test files"')
  42. if encrypt:
  43. paths.encrypt.write(f'{ALT_FILE1 + suffix}\n', mode='a')
  44. paths.encrypt.write(f'{ALT_FILE2 + suffix}\n', mode='a')
  45. if exclude:
  46. paths.encrypt.write(f'!{ALT_FILE1 + suffix}\n', mode='a')
  47. paths.encrypt.write(f'!{ALT_FILE2 + suffix}\n', mode='a')