test_unit_configure_paths.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. """Unit tests: configure_paths"""
  2. import pytest
  3. ARCHIVE = 'files.gpg'
  4. BOOTSTRAP = 'bootstrap'
  5. CONFIG = 'config'
  6. ENCRYPT = 'encrypt'
  7. HOME = '/testhome'
  8. REPO = 'repo.git'
  9. YDIR = '.yadm'
  10. @pytest.mark.parametrize(
  11. 'override, expect', [
  12. (None, {}),
  13. ('-Y', {}),
  14. ('--yadm-repo', {'repo': 'YADM_REPO', 'git': 'GIT_DIR'}),
  15. ('--yadm-config', {'config': 'YADM_CONFIG'}),
  16. ('--yadm-encrypt', {'encrypt': 'YADM_ENCRYPT'}),
  17. ('--yadm-archive', {'archive': 'YADM_ARCHIVE'}),
  18. ('--yadm-bootstrap', {'bootstrap': 'YADM_BOOTSTRAP'}),
  19. ], ids=[
  20. 'default',
  21. 'override yadm dir',
  22. 'override repo',
  23. 'override config',
  24. 'override encrypt',
  25. 'override archive',
  26. 'override bootstrap',
  27. ])
  28. def test_config(runner, paths, override, expect):
  29. """Test configure_paths"""
  30. opath = 'override'
  31. matches = match_map()
  32. args = []
  33. if override == '-Y':
  34. matches = match_map('/' + opath)
  35. if override:
  36. args = [override, '/' + opath]
  37. for ekey in expect.keys():
  38. matches[ekey] = f'{expect[ekey]}="/{opath}"'
  39. run_test(
  40. runner, paths,
  41. [override, opath],
  42. ['must specify a fully qualified'], 1)
  43. run_test(runner, paths, args, matches.values(), 0)
  44. def match_map(yadm_dir=None):
  45. """Create a dictionary of matches, relative to yadm_dir"""
  46. if not yadm_dir:
  47. yadm_dir = '/'.join([HOME, YDIR])
  48. return {
  49. 'yadm': f'YADM_DIR="{yadm_dir}"',
  50. 'repo': f'YADM_REPO="{yadm_dir}/{REPO}"',
  51. 'config': f'YADM_CONFIG="{yadm_dir}/{CONFIG}"',
  52. 'encrypt': f'YADM_ENCRYPT="{yadm_dir}/{ENCRYPT}"',
  53. 'archive': f'YADM_ARCHIVE="{yadm_dir}/{ARCHIVE}"',
  54. 'bootstrap': f'YADM_BOOTSTRAP="{yadm_dir}/{BOOTSTRAP}"',
  55. 'git': f'GIT_DIR="{yadm_dir}/{REPO}"',
  56. }
  57. def run_test(runner, paths, args, expected_matches, expected_code=0):
  58. """Run proces global args, and run configure_paths"""
  59. argstring = ' '.join(['"'+a+'"' for a in args])
  60. script = f"""
  61. YADM_TEST=1 HOME="{HOME}" source {paths.pgm}
  62. process_global_args {argstring}
  63. configure_paths
  64. declare -p | grep -E '(YADM|GIT)_'
  65. """
  66. run = runner(command=['bash'], inp=script)
  67. assert run.code == expected_code
  68. assert run.err == ''
  69. for match in expected_matches:
  70. assert match in run.out