test_file.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import os
  2. import pytest
  3. import tempfile
  4. import checksymbolslib.file as m
  5. def test_get_list_of_files_in_the_repo():
  6. all_files = m.get_list_of_files_in_the_repo()
  7. assert 'Makefile' in all_files
  8. assert 'package/Config.in' in all_files
  9. assert len(all_files) > 1000
  10. get_list_of_files_to_process = [
  11. ('unknown file type',
  12. ['a/file/Config.in',
  13. 'another/file.mk',
  14. 'unknown/file/type'],
  15. ['a/file/Config.in',
  16. 'another/file.mk']),
  17. ('runtime test infra fixtures',
  18. ['a/file/Config.in',
  19. 'support/testing/a/broken/Config.in',
  20. 'another/file.mk'],
  21. ['a/file/Config.in',
  22. 'another/file.mk']),
  23. ]
  24. @pytest.mark.parametrize('testname,all_files,expected', get_list_of_files_to_process)
  25. def test_get_list_of_files_to_process(testname, all_files, expected):
  26. files_to_process = m.get_list_of_files_to_process(all_files)
  27. assert files_to_process == expected
  28. get_list_of_filenames_with_pattern = [
  29. ('ignored directories',
  30. ['a/file/Config.in',
  31. 'support/testing/a/broken/file/Config.in',
  32. 'not/found.mk',
  33. 'another/file.mk'],
  34. ['a/file/Config.in',
  35. 'not/found.mk',
  36. 'another/file.mk'],
  37. 'file',
  38. ['support/testing/a/broken/file/Config.in']),
  39. ('processed files',
  40. ['a/file/Config.in',
  41. 'not/found.mk',
  42. 'another/file.mk'],
  43. [],
  44. 'file',
  45. ['a/file/Config.in',
  46. 'another/file.mk']),
  47. ('case sensitive',
  48. ['a/file/Config.in',
  49. 'not/found.mk',
  50. 'another/file.mk'],
  51. [],
  52. 'FILE',
  53. []),
  54. ('or',
  55. ['a/file/Config.in',
  56. 'not/found.mk',
  57. 'another/file.mk'],
  58. [],
  59. 'file|FILE',
  60. ['a/file/Config.in',
  61. 'another/file.mk']),
  62. ('complex regexp',
  63. ['a/file/Config.in',
  64. 'not/found.mk',
  65. 'another/file.mk'],
  66. [],
  67. '^n[oO]+t.*mk$',
  68. ['not/found.mk']),
  69. ]
  70. @pytest.mark.parametrize('testname,all_files,files_to_process,pattern,expected', get_list_of_filenames_with_pattern)
  71. def test_get_list_of_filenames_with_pattern(testname, all_files, files_to_process, pattern, expected):
  72. files_to_process = m.get_list_of_filenames_with_pattern(all_files, files_to_process, pattern)
  73. assert files_to_process == expected
  74. read_file = [
  75. ('indent',
  76. 'file1',
  77. ' content1\n'
  78. '\t# comment1',
  79. [[1, ' content1\n'],
  80. [2, '\t# comment1']]),
  81. ('trailing space',
  82. 'file2',
  83. 'content2 \n'
  84. '# comment2\t\n',
  85. [[1, 'content2 \n'],
  86. [2, '# comment2\t\n']]),
  87. ('empty line',
  88. 'file3',
  89. '\n'
  90. '\n',
  91. [[1, '\n'],
  92. [2, '\n']]),
  93. ('missing newline at EOF',
  94. 'file4',
  95. '\n'
  96. ' text\t',
  97. [[1, '\n'],
  98. [2, ' text\t']]),
  99. ]
  100. @pytest.mark.parametrize('testname,filename,content,,expected', read_file)
  101. def test_read_file(testname, filename, content, expected):
  102. with tempfile.TemporaryDirectory(suffix='-checksymbolslib-test-file') as workdir:
  103. full_filename = os.path.join(workdir, filename)
  104. with open(full_filename, 'wb') as f:
  105. f.write(content.encode())
  106. read_file_content = m.read_file(full_filename)
  107. assert read_file_content == expected
  108. cleanup_file_content = [
  109. ('empty file',
  110. [],
  111. []),
  112. ('empty line',
  113. [[5, '\n']],
  114. [[5, '']]),
  115. ('trailing space',
  116. [[3, ' \n']],
  117. [[3, ' ']]),
  118. ('trailing tab',
  119. [[3, '\t\n']],
  120. [[3, '\t']]),
  121. ('1 continuation',
  122. [[1, 'foo \\\n'],
  123. [2, 'bar\n']],
  124. [[1, 'foo bar']]),
  125. ('2 continuations',
  126. [[1, 'foo \\\n'],
  127. [2, 'bar \\\n'],
  128. [3, 'baz\n']],
  129. [[1, 'foo bar baz']]),
  130. ]
  131. @pytest.mark.parametrize('testname,file_content_raw,expected', cleanup_file_content)
  132. def test_cleanup_file_content(testname, file_content_raw, expected):
  133. cleaned_up_content = m.cleanup_file_content(file_content_raw)
  134. assert cleaned_up_content == expected