2018-07-11 08:50:42 -04:00
|
|
|
"""Testing Utilities
|
|
|
|
|
|
|
|
This module holds values/functions common to multiple tests.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
2023-07-10 10:14:33 -04:00
|
|
|
import re
|
2018-07-11 08:50:42 -04:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
ALT_FILE1 = "test_alt"
|
|
|
|
ALT_FILE2 = "test alt/test alt"
|
|
|
|
ALT_DIR = "test alt/test alt dir"
|
2019-04-04 19:01:27 -04:00
|
|
|
|
|
|
|
# Directory based alternates must have a tracked contained file.
|
|
|
|
# This will be the test contained file name
|
2024-11-28 17:28:32 -05:00
|
|
|
CONTAINED = "contained_dir/contained_file"
|
2018-07-11 08:50:42 -04:00
|
|
|
|
2019-04-12 08:43:18 -04:00
|
|
|
# These variables are used for making include files which will be processed
|
|
|
|
# within jinja templates
|
2023-07-10 15:43:17 -04:00
|
|
|
INCLUDE_FILE = "inc_file"
|
|
|
|
INCLUDE_DIRS = ["", "test alt"]
|
|
|
|
INCLUDE_CONTENT = "8780846c02e34c930d0afd127906668f"
|
2019-04-12 08:43:18 -04:00
|
|
|
|
2018-07-11 08:50:42 -04:00
|
|
|
|
2021-01-10 16:38:42 -05:00
|
|
|
def set_local(paths, variable, value, add=False):
|
2018-07-11 08:50:42 -04:00
|
|
|
"""Set local override"""
|
2021-01-10 16:38:42 -05:00
|
|
|
add = "--add" if add else ""
|
2023-07-10 15:43:17 -04:00
|
|
|
os.system(f"GIT_DIR={str(paths.repo)} " f'git config --local {add} "local.{variable}" "{value}"')
|
|
|
|
|
|
|
|
|
|
|
|
def create_alt_files(
|
|
|
|
paths,
|
|
|
|
suffix,
|
|
|
|
preserve=False,
|
|
|
|
tracked=True,
|
|
|
|
encrypt=False,
|
|
|
|
exclude=False,
|
|
|
|
content=None,
|
|
|
|
includefile=False,
|
|
|
|
yadm_alt=False,
|
|
|
|
yadm_dir=None,
|
|
|
|
):
|
2018-07-11 08:50:42 -04:00
|
|
|
"""Create new files, and add to the repo
|
|
|
|
|
|
|
|
This is used for testing alternate files. In each case, a suffix is
|
|
|
|
appended to two standard file paths. Particulars of the file creation and
|
|
|
|
repo handling are dependent upon the function arguments.
|
|
|
|
"""
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
basepath = yadm_dir.join("alt") if yadm_alt else paths.work
|
2019-10-10 09:23:36 -04:00
|
|
|
|
2018-07-11 08:50:42 -04:00
|
|
|
if not preserve:
|
2019-04-04 19:01:27 -04:00
|
|
|
for remove_path in (ALT_FILE1, ALT_FILE2, ALT_DIR):
|
2019-10-10 09:23:36 -04:00
|
|
|
if basepath.join(remove_path).exists():
|
|
|
|
basepath.join(remove_path).remove(rec=1, ignore_errors=True)
|
|
|
|
assert not basepath.join(remove_path).exists()
|
2018-07-11 08:50:42 -04:00
|
|
|
|
2019-10-10 09:23:36 -04:00
|
|
|
new_file1 = basepath.join(ALT_FILE1 + suffix)
|
2018-07-11 08:50:42 -04:00
|
|
|
new_file1.write(ALT_FILE1 + suffix, ensure=True)
|
2019-10-10 09:23:36 -04:00
|
|
|
new_file2 = basepath.join(ALT_FILE2 + suffix)
|
2018-07-11 08:50:42 -04:00
|
|
|
new_file2.write(ALT_FILE2 + suffix, ensure=True)
|
2019-10-10 09:23:36 -04:00
|
|
|
new_dir = basepath.join(ALT_DIR + suffix).join(CONTAINED)
|
2019-04-04 19:01:27 -04:00
|
|
|
new_dir.write(ALT_DIR + suffix, ensure=True)
|
|
|
|
|
|
|
|
# Do not test directory support for jinja alternates
|
|
|
|
test_paths = [new_file1, new_file2]
|
|
|
|
test_names = [ALT_FILE1, ALT_FILE2]
|
2023-07-10 15:43:17 -04:00
|
|
|
if not re.match(r"##(t$|t\.|template|yadm)", suffix):
|
2019-04-04 19:01:27 -04:00
|
|
|
test_paths += [new_dir]
|
|
|
|
test_names += [ALT_DIR]
|
|
|
|
|
|
|
|
for test_path in test_paths:
|
|
|
|
if content:
|
2023-07-10 15:43:17 -04:00
|
|
|
test_path.write("\n" + content, mode="a", ensure=True)
|
2019-04-04 19:01:27 -04:00
|
|
|
assert test_path.exists()
|
2018-07-11 08:50:42 -04:00
|
|
|
|
2019-10-10 09:23:36 -04:00
|
|
|
_create_includefiles(includefile, test_paths, basepath)
|
2019-04-12 08:48:10 -04:00
|
|
|
_create_tracked(tracked, test_paths, paths)
|
2019-10-10 09:23:36 -04:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
prefix = ".config/yadm/alt/" if yadm_alt else ""
|
2019-10-10 09:23:36 -04:00
|
|
|
_create_encrypt(encrypt, test_names, suffix, paths, exclude, prefix)
|
2019-04-12 08:43:18 -04:00
|
|
|
|
2019-04-12 08:48:10 -04:00
|
|
|
|
2019-10-01 09:12:18 -04:00
|
|
|
def parse_alt_output(output, linked=True):
|
|
|
|
"""Parse output of 'alt', and return list of linked files"""
|
2023-07-10 15:43:17 -04:00
|
|
|
regex = r"Creating (.+) from template (.+)$"
|
2019-10-01 09:12:18 -04:00
|
|
|
if linked:
|
2024-11-28 17:28:32 -05:00
|
|
|
regex = r"(?:Copy|Link)ing (.+) to (.+)$"
|
2023-07-09 12:13:13 -04:00
|
|
|
parsed_list = {}
|
2019-10-01 09:12:18 -04:00
|
|
|
for line in output.splitlines():
|
|
|
|
match = re.match(regex, line)
|
|
|
|
if match:
|
|
|
|
if linked:
|
|
|
|
parsed_list[match.group(2)] = match.group(1)
|
|
|
|
else:
|
|
|
|
parsed_list[match.group(1)] = match.group(2)
|
|
|
|
return parsed_list.values()
|
|
|
|
|
|
|
|
|
2019-10-10 09:23:36 -04:00
|
|
|
def _create_includefiles(includefile, test_paths, basepath):
|
2019-04-12 08:48:10 -04:00
|
|
|
if includefile:
|
|
|
|
for dpath in INCLUDE_DIRS:
|
2023-07-10 15:43:17 -04:00
|
|
|
incfile = basepath.join(dpath + "/" + INCLUDE_FILE)
|
2019-04-12 08:48:10 -04:00
|
|
|
incfile.write(INCLUDE_CONTENT, ensure=True)
|
|
|
|
test_paths += [incfile]
|
|
|
|
|
|
|
|
|
|
|
|
def _create_tracked(tracked, test_paths, paths):
|
2018-07-11 08:50:42 -04:00
|
|
|
if tracked:
|
2019-04-04 19:01:27 -04:00
|
|
|
for track_path in test_paths:
|
|
|
|
os.system(f'GIT_DIR={str(paths.repo)} git add "{track_path}"')
|
2018-07-11 08:50:42 -04:00
|
|
|
os.system(f'GIT_DIR={str(paths.repo)} git commit -m "Add test files"')
|
|
|
|
|
2019-04-12 08:48:10 -04:00
|
|
|
|
2019-10-10 09:23:36 -04:00
|
|
|
def _create_encrypt(encrypt, test_names, suffix, paths, exclude, prefix):
|
2018-07-11 08:50:42 -04:00
|
|
|
if encrypt:
|
2019-04-04 19:01:27 -04:00
|
|
|
for encrypt_name in test_names:
|
2023-07-10 15:43:17 -04:00
|
|
|
paths.encrypt.write(f"{prefix + encrypt_name + suffix}\n", mode="a")
|
2019-04-04 19:01:27 -04:00
|
|
|
if exclude:
|
2023-07-10 15:43:17 -04:00
|
|
|
paths.encrypt.write(f"!{prefix + encrypt_name + suffix}\n", mode="a")
|