utils.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. """Testing Utilities
  2. This module holds values/functions common to multiple tests.
  3. """
  4. import re
  5. import os
  6. ALT_FILE1 = 'test_alt'
  7. ALT_FILE2 = 'test alt/test alt'
  8. ALT_DIR = 'test alt/test alt dir'
  9. # Directory based alternates must have a tracked contained file.
  10. # This will be the test contained file name
  11. CONTAINED = 'contained_file'
  12. # These variables are used for making include files which will be processed
  13. # within jinja templates
  14. INCLUDE_FILE = 'inc_file'
  15. INCLUDE_DIRS = ['', 'test alt']
  16. INCLUDE_CONTENT = '8780846c02e34c930d0afd127906668f'
  17. def set_local(paths, variable, value):
  18. """Set local override"""
  19. os.system(
  20. f'GIT_DIR={str(paths.repo)} '
  21. f'git config --local "local.{variable}" "{value}"'
  22. )
  23. def create_alt_files(paths, suffix,
  24. preserve=False, tracked=True,
  25. encrypt=False, exclude=False,
  26. content=None, includefile=False,
  27. yadm_alt=False, yadm_dir=None):
  28. """Create new files, and add to the repo
  29. This is used for testing alternate files. In each case, a suffix is
  30. appended to two standard file paths. Particulars of the file creation and
  31. repo handling are dependent upon the function arguments.
  32. """
  33. basepath = yadm_dir.join('alt') if yadm_alt else paths.work
  34. if not preserve:
  35. for remove_path in (ALT_FILE1, ALT_FILE2, ALT_DIR):
  36. if basepath.join(remove_path).exists():
  37. basepath.join(remove_path).remove(rec=1, ignore_errors=True)
  38. assert not basepath.join(remove_path).exists()
  39. new_file1 = basepath.join(ALT_FILE1 + suffix)
  40. new_file1.write(ALT_FILE1 + suffix, ensure=True)
  41. new_file2 = basepath.join(ALT_FILE2 + suffix)
  42. new_file2.write(ALT_FILE2 + suffix, ensure=True)
  43. new_dir = basepath.join(ALT_DIR + suffix).join(CONTAINED)
  44. new_dir.write(ALT_DIR + suffix, ensure=True)
  45. # Do not test directory support for jinja alternates
  46. test_paths = [new_file1, new_file2]
  47. test_names = [ALT_FILE1, ALT_FILE2]
  48. if not re.match(r'##(t|template|yadm)\.', suffix):
  49. test_paths += [new_dir]
  50. test_names += [ALT_DIR]
  51. for test_path in test_paths:
  52. if content:
  53. test_path.write('\n' + content, mode='a', ensure=True)
  54. assert test_path.exists()
  55. _create_includefiles(includefile, test_paths, basepath)
  56. _create_tracked(tracked, test_paths, paths)
  57. prefix = '.config/yadm/alt/' if yadm_alt else ''
  58. _create_encrypt(encrypt, test_names, suffix, paths, exclude, prefix)
  59. def parse_alt_output(output, linked=True):
  60. """Parse output of 'alt', and return list of linked files"""
  61. regex = r'Creating (.+) from template (.+)$'
  62. if linked:
  63. regex = r'Linking (.+) to (.+)$'
  64. parsed_list = dict()
  65. for line in output.splitlines():
  66. match = re.match(regex, line)
  67. if match:
  68. if linked:
  69. parsed_list[match.group(2)] = match.group(1)
  70. else:
  71. parsed_list[match.group(1)] = match.group(2)
  72. return parsed_list.values()
  73. def _create_includefiles(includefile, test_paths, basepath):
  74. if includefile:
  75. for dpath in INCLUDE_DIRS:
  76. incfile = basepath.join(dpath + '/' + INCLUDE_FILE)
  77. incfile.write(INCLUDE_CONTENT, ensure=True)
  78. test_paths += [incfile]
  79. def _create_tracked(tracked, test_paths, paths):
  80. if tracked:
  81. for track_path in test_paths:
  82. os.system(f'GIT_DIR={str(paths.repo)} git add "{track_path}"')
  83. os.system(f'GIT_DIR={str(paths.repo)} git commit -m "Add test files"')
  84. def _create_encrypt(encrypt, test_names, suffix, paths, exclude, prefix):
  85. if encrypt:
  86. for encrypt_name in test_names:
  87. paths.encrypt.write(
  88. f'{prefix + encrypt_name + suffix}\n', mode='a')
  89. if exclude:
  90. paths.encrypt.write(
  91. f'!{prefix + encrypt_name + suffix}\n', mode='a')