utils.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. ALT_DIR = 'test alt/test alt dir'
  8. # Directory based alternates must have a tracked contained file.
  9. # This will be the test contained file name
  10. CONTAINED = 'contained_file'
  11. # These variables are used for making include files which will be processed
  12. # within jinja templates
  13. INCLUDE_FILE = 'inc_file'
  14. INCLUDE_DIRS = ['', 'test alt']
  15. INCLUDE_CONTENT = '8780846c02e34c930d0afd127906668f'
  16. def set_local(paths, variable, value):
  17. """Set local override"""
  18. os.system(
  19. f'GIT_DIR={str(paths.repo)} '
  20. f'git config --local "local.{variable}" "{value}"'
  21. )
  22. def create_alt_files(paths, suffix,
  23. preserve=False, tracked=True,
  24. encrypt=False, exclude=False,
  25. content=None, includefile=False):
  26. """Create new files, and add to the repo
  27. This is used for testing alternate files. In each case, a suffix is
  28. appended to two standard file paths. Particulars of the file creation and
  29. repo handling are dependent upon the function arguments.
  30. """
  31. if not preserve:
  32. for remove_path in (ALT_FILE1, ALT_FILE2, ALT_DIR):
  33. if paths.work.join(remove_path).exists():
  34. paths.work.join(remove_path).remove(rec=1, ignore_errors=True)
  35. assert not paths.work.join(remove_path).exists()
  36. new_file1 = paths.work.join(ALT_FILE1 + suffix)
  37. new_file1.write(ALT_FILE1 + suffix, ensure=True)
  38. new_file2 = paths.work.join(ALT_FILE2 + suffix)
  39. new_file2.write(ALT_FILE2 + suffix, ensure=True)
  40. new_dir = paths.work.join(ALT_DIR + suffix).join(CONTAINED)
  41. new_dir.write(ALT_DIR + suffix, ensure=True)
  42. # Do not test directory support for jinja alternates
  43. test_paths = [new_file1, new_file2]
  44. test_names = [ALT_FILE1, ALT_FILE2]
  45. if suffix != '##yadm.j2':
  46. test_paths += [new_dir]
  47. test_names += [ALT_DIR]
  48. for test_path in test_paths:
  49. if content:
  50. test_path.write('\n' + content, mode='a', ensure=True)
  51. assert test_path.exists()
  52. _create_includefiles(includefile, paths, test_paths)
  53. _create_tracked(tracked, test_paths, paths)
  54. _create_encrypt(encrypt, test_names, suffix, paths, exclude)
  55. def _create_includefiles(includefile, paths, test_paths):
  56. if includefile:
  57. for dpath in INCLUDE_DIRS:
  58. incfile = paths.work.join(dpath + '/' + INCLUDE_FILE)
  59. incfile.write(INCLUDE_CONTENT, ensure=True)
  60. test_paths += [incfile]
  61. def _create_tracked(tracked, test_paths, paths):
  62. if tracked:
  63. for track_path in test_paths:
  64. os.system(f'GIT_DIR={str(paths.repo)} git add "{track_path}"')
  65. os.system(f'GIT_DIR={str(paths.repo)} git commit -m "Add test files"')
  66. def _create_encrypt(encrypt, test_names, suffix, paths, exclude):
  67. if encrypt:
  68. for encrypt_name in test_names:
  69. paths.encrypt.write(f'{encrypt_name + suffix}\n', mode='a')
  70. if exclude:
  71. paths.encrypt.write(f'!{encrypt_name + suffix}\n', mode='a')