utils.py 3.9 KB

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