test_unit_parse_encrypt.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. """Unit tests: parse_encrypt"""
  2. import pytest
  3. def test_not_called(runner, paths):
  4. """Test parse_encrypt (not called)"""
  5. run = run_parse_encrypt(runner, paths, skip_parse=True)
  6. assert run.success
  7. assert run.err == ''
  8. assert 'EIF:unparsed' in run.out, 'EIF should be unparsed'
  9. assert 'EIF_COUNT:1' in run.out, 'Only value of EIF should be unparsed'
  10. def test_short_circuit(runner, paths):
  11. """Test parse_encrypt (short-circuit)"""
  12. run = run_parse_encrypt(runner, paths, twice=True)
  13. assert run.success
  14. assert run.err == ''
  15. assert 'PARSE_ENCRYPT_SHORT=parse_encrypt() not reprocessed' in run.out, (
  16. 'parse_encrypt() should short-circuit')
  17. @pytest.mark.parametrize(
  18. 'encrypt', [
  19. ('missing'),
  20. ('empty'),
  21. ])
  22. def test_empty(runner, paths, encrypt):
  23. """Test parse_encrypt (file missing/empty)"""
  24. # write encrypt file
  25. if encrypt == 'missing':
  26. assert not paths.encrypt.exists(), 'Encrypt should be missing'
  27. else:
  28. paths.encrypt.write('')
  29. assert paths.encrypt.exists(), 'Encrypt should exist'
  30. assert paths.encrypt.size() == 0, 'Encrypt should be empty'
  31. # run parse_encrypt
  32. run = run_parse_encrypt(runner, paths)
  33. assert run.success
  34. assert run.err == ''
  35. # validate parsing result
  36. assert 'EIF_COUNT:0' in run.out, 'EIF should be empty'
  37. @pytest.mark.usefixtures('ds1_repo_copy')
  38. def test_file_parse_encrypt(runner, paths):
  39. """Test parse_encrypt
  40. Test an array of supported features of the encrypt configuration.
  41. """
  42. edata = ''
  43. expected = set()
  44. # empty line
  45. edata += '\n'
  46. # simple comments
  47. edata += '# a simple comment\n'
  48. edata += ' # a comment with leading space\n'
  49. # unreferenced directory
  50. paths.work.join('unreferenced').mkdir()
  51. # simple files
  52. edata += 'simple_file\n'
  53. edata += 'simple.file\n'
  54. paths.work.join('simple_file').write('')
  55. paths.work.join('simple.file').write('')
  56. paths.work.join('simple_file2').write('')
  57. paths.work.join('simple.file2').write('')
  58. expected.add('simple_file')
  59. expected.add('simple.file')
  60. # simple files in directories
  61. edata += 'simple_dir/simple_file\n'
  62. paths.work.join('simple_dir/simple_file').write('', ensure=True)
  63. paths.work.join('simple_dir/simple_file2').write('', ensure=True)
  64. expected.add('simple_dir/simple_file')
  65. # paths with spaces
  66. edata += 'with space/with space\n'
  67. paths.work.join('with space/with space').write('', ensure=True)
  68. paths.work.join('with space/with space2').write('', ensure=True)
  69. expected.add('with space/with space')
  70. # hidden files
  71. edata += '.hidden\n'
  72. paths.work.join('.hidden').write('')
  73. expected.add('.hidden')
  74. # hidden files in directories
  75. edata += '.hidden_dir/.hidden_file\n'
  76. paths.work.join('.hidden_dir/.hidden_file').write('', ensure=True)
  77. expected.add('.hidden_dir/.hidden_file')
  78. # wildcards
  79. edata += 'wild*\n'
  80. paths.work.join('wildcard1').write('', ensure=True)
  81. paths.work.join('wildcard2').write('', ensure=True)
  82. expected.add('wildcard1')
  83. expected.add('wildcard2')
  84. edata += 'dirwild*\n'
  85. paths.work.join('dirwildcard/file1').write('', ensure=True)
  86. paths.work.join('dirwildcard/file2').write('', ensure=True)
  87. expected.add('dirwildcard')
  88. # excludes
  89. edata += 'exclude*\n'
  90. edata += 'ex ex/*\n'
  91. paths.work.join('exclude_file1').write('')
  92. paths.work.join('exclude_file2.ex').write('')
  93. paths.work.join('exclude_file3.ex3').write('')
  94. expected.add('exclude_file1')
  95. expected.add('exclude_file3.ex3')
  96. edata += '!*.ex\n'
  97. edata += '!ex ex/*.txt\n'
  98. paths.work.join('ex ex/file4').write('', ensure=True)
  99. paths.work.join('ex ex/file5.txt').write('', ensure=True)
  100. paths.work.join('ex ex/file6.text').write('', ensure=True)
  101. expected.add('ex ex/file4')
  102. expected.add('ex ex/file6.text')
  103. # write encrypt file
  104. print(f'ENCRYPT:\n---\n{edata}---\n')
  105. paths.encrypt.write(edata)
  106. assert paths.encrypt.isfile()
  107. # run parse_encrypt
  108. run = run_parse_encrypt(runner, paths)
  109. assert run.success
  110. assert run.err == ''
  111. assert f'EIF_COUNT:{len(expected)}' in run.out, 'EIF count wrong'
  112. for expected_file in expected:
  113. assert f'EIF:{expected_file}\n' in run.out
  114. def run_parse_encrypt(
  115. runner, paths,
  116. skip_parse=False,
  117. twice=False):
  118. """Run parse_encrypt
  119. A count of ENCRYPT_INCLUDE_FILES will be reported as EIF_COUNT:X. All
  120. values of ENCRYPT_INCLUDE_FILES will be reported as individual EIF:value
  121. lines.
  122. """
  123. parse_cmd = 'parse_encrypt'
  124. if skip_parse:
  125. parse_cmd = ''
  126. if twice:
  127. parse_cmd = 'parse_encrypt; parse_encrypt'
  128. script = f"""
  129. YADM_TEST=1 source {paths.pgm}
  130. YADM_ENCRYPT={paths.encrypt}
  131. export YADM_ENCRYPT
  132. GIT_DIR={paths.repo}
  133. export GIT_DIR
  134. {parse_cmd}
  135. export ENCRYPT_INCLUDE_FILES
  136. export PARSE_ENCRYPT_SHORT
  137. env
  138. echo EIF_COUNT:${{#ENCRYPT_INCLUDE_FILES[@]}}
  139. for value in "${{ENCRYPT_INCLUDE_FILES[@]}}"; do
  140. echo "EIF:$value"
  141. done
  142. """
  143. run = runner(command=['bash'], inp=script)
  144. return run