2018-07-11 12:50:42 +00:00
|
|
|
"""Testing Utilities
|
|
|
|
|
|
|
|
This module holds values/functions common to multiple tests.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
ALT_FILE1 = 'test_alt'
|
|
|
|
ALT_FILE2 = 'test alt/test alt'
|
2019-04-04 23:01:27 +00:00
|
|
|
ALT_DIR = 'test alt/test alt dir'
|
|
|
|
|
|
|
|
# Directory based alternates must have a tracked contained file.
|
|
|
|
# This will be the test contained file name
|
|
|
|
CONTAINED = 'contained_file'
|
2018-07-11 12:50:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
def set_local(paths, variable, value):
|
|
|
|
"""Set local override"""
|
|
|
|
os.system(
|
|
|
|
f'GIT_DIR={str(paths.repo)} '
|
|
|
|
f'git config --local "local.{variable}" "{value}"'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def create_alt_files(paths, suffix,
|
|
|
|
preserve=False, tracked=True,
|
|
|
|
encrypt=False, exclude=False,
|
|
|
|
content=None):
|
|
|
|
"""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.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if not preserve:
|
2019-04-04 23:01:27 +00:00
|
|
|
for remove_path in (ALT_FILE1, ALT_FILE2, ALT_DIR):
|
|
|
|
if paths.work.join(remove_path).exists():
|
|
|
|
paths.work.join(remove_path).remove(rec=1, ignore_errors=True)
|
|
|
|
assert not paths.work.join(remove_path).exists()
|
2018-07-11 12:50:42 +00:00
|
|
|
|
|
|
|
new_file1 = paths.work.join(ALT_FILE1 + suffix)
|
|
|
|
new_file1.write(ALT_FILE1 + suffix, ensure=True)
|
|
|
|
new_file2 = paths.work.join(ALT_FILE2 + suffix)
|
|
|
|
new_file2.write(ALT_FILE2 + suffix, ensure=True)
|
2019-04-04 23:01:27 +00:00
|
|
|
new_dir = paths.work.join(ALT_DIR + suffix).join(CONTAINED)
|
|
|
|
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]
|
|
|
|
if suffix != '##yadm.j2':
|
|
|
|
test_paths += [new_dir]
|
|
|
|
test_names += [ALT_DIR]
|
|
|
|
|
|
|
|
for test_path in test_paths:
|
|
|
|
if content:
|
|
|
|
test_path.write('\n' + content, mode='a', ensure=True)
|
|
|
|
assert test_path.exists()
|
2018-07-11 12:50:42 +00:00
|
|
|
|
|
|
|
if tracked:
|
2019-04-04 23:01:27 +00:00
|
|
|
for track_path in test_paths:
|
|
|
|
os.system(f'GIT_DIR={str(paths.repo)} git add "{track_path}"')
|
2018-07-11 12:50:42 +00:00
|
|
|
os.system(f'GIT_DIR={str(paths.repo)} git commit -m "Add test files"')
|
|
|
|
|
|
|
|
if encrypt:
|
2019-04-04 23:01:27 +00:00
|
|
|
for encrypt_name in test_names:
|
|
|
|
paths.encrypt.write(f'{encrypt_name + suffix}\n', mode='a')
|
|
|
|
if exclude:
|
|
|
|
paths.encrypt.write(f'!{encrypt_name + suffix}\n', mode='a')
|