test_utils.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: Apache-2.0
  3. import os
  4. import sys
  5. from typing import Any, Dict, Union
  6. sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
  7. import fatfsgen # noqa E402 # pylint: disable=C0413
  8. CFG = dict(
  9. sector_size=4096,
  10. entry_size=32,
  11. fat_start=0x1000,
  12. data_start=0x7000,
  13. root_start=0x2000,
  14. output_file=os.path.join('output_data', 'tmp_file.img'),
  15. test_dir=os.path.join('output_data', 'test'),
  16. test_dir2=os.path.join('output_data', 'tst_str'),
  17. ) # type: Union[Dict[str, Any]]
  18. def generate_test_dir_1() -> None:
  19. os.makedirs(os.path.join(CFG['test_dir'], 'test', 'test'))
  20. with open(os.path.join(CFG['test_dir'], 'test', 'test', 'lastfile'), 'w') as file:
  21. file.write('deeptest\n')
  22. with open(os.path.join(CFG['test_dir'], 'test', 'testfil2'), 'w') as file:
  23. file.write('thisistest\n')
  24. with open(os.path.join(CFG['test_dir'], 'testfile'), 'w') as file:
  25. file.write('ahoj\n')
  26. def generate_test_dir_2() -> None:
  27. os.makedirs(os.path.join(CFG['test_dir2'], 'test', 'test'))
  28. with open(os.path.join(CFG['test_dir2'], 'test', 'test', 'lastfile.txt'), 'w') as file:
  29. file.write('deeptest\n')
  30. with open(os.path.join(CFG['test_dir2'], 'test', 'testfil2'), 'w') as file:
  31. file.write('thisistest\n')
  32. with open(os.path.join(CFG['test_dir2'], 'testfile'), 'w') as file:
  33. file.write('ahoj\n')
  34. def fill_sector(fatfs: fatfsgen.FATFS, file_prefix: str = 'A') -> None:
  35. for i in range(CFG['sector_size'] // CFG['entry_size']):
  36. fatfs.create_file(f'{file_prefix}{str(i).upper()}', path_from_root=['TESTFOLD'])
  37. def generate_local_folder_structure(structure_: dict, path_: str) -> None:
  38. if structure_['type'] == 'folder':
  39. new_path_ = os.path.join(path_, structure_['name'])
  40. os.makedirs(new_path_)
  41. for item_ in structure_['content']:
  42. generate_local_folder_structure(item_, new_path_)
  43. else:
  44. new_path_ = os.path.join(path_, structure_['name'])
  45. with open(new_path_, 'w') as f_:
  46. f_.write(structure_['content'])
  47. def compare_folders(fp1: str, fp2: str) -> bool:
  48. if os.path.isdir(fp1) != os.path.isdir(fp2):
  49. return False
  50. if os.path.isdir(fp1):
  51. if set(os.listdir(fp1)) != set(os.listdir(fp2)):
  52. return False
  53. return all([compare_folders(os.path.join(fp1, path_), os.path.join(fp2, path_)) for path_ in os.listdir(fp1)])
  54. with open(fp1, 'rb') as f1_, open(fp2, 'rb') as f2_:
  55. return f1_.read() == f2_.read()