test_spiffsgen.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/env python
  2. import os
  3. import sys
  4. import unittest
  5. try:
  6. import typing
  7. except ImportError:
  8. pass
  9. sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
  10. try:
  11. import spiffsgen
  12. except ImportError:
  13. raise
  14. class SpiffsgenTest(unittest.TestCase):
  15. def test_configs(self): # type: () -> None
  16. """Run spiffsgen with different configs, and check that
  17. an image is generated (there is no exception), and the image size
  18. is as expected.
  19. """
  20. default_config = dict(
  21. page_size=256,
  22. page_ix_len=spiffsgen.SPIFFS_PAGE_IX_LEN,
  23. block_size=4096,
  24. block_ix_len=spiffsgen.SPIFFS_BLOCK_IX_LEN,
  25. meta_len=4,
  26. obj_name_len=32,
  27. obj_id_len=spiffsgen.SPIFFS_BLOCK_IX_LEN,
  28. span_ix_len=spiffsgen.SPIFFS_SPAN_IX_LEN,
  29. packed=True,
  30. aligned=True,
  31. endianness='little',
  32. use_magic=True,
  33. use_magic_len=True,
  34. aligned_obj_ix_tables=False
  35. )
  36. def make_config(**kwargs): # type: (typing.Any) -> spiffsgen.SpiffsBuildConfig
  37. """Return SpiffsBuildConfig object with configuration set
  38. by default_config plus any options overridden in kwargs.
  39. """
  40. new_config = dict(default_config)
  41. new_config.update(**kwargs)
  42. return spiffsgen.SpiffsBuildConfig(**new_config)
  43. configs = [
  44. make_config(),
  45. make_config(use_magic_len=False, use_magic=False, aligned_obj_ix_tables=True),
  46. make_config(meta_len=4, obj_name_len=16),
  47. make_config(block_size=8192),
  48. make_config(page_size=512)
  49. ]
  50. image_size = 64 * 1024
  51. for config in configs:
  52. spiffs = spiffsgen.SpiffsFS(image_size, config)
  53. spiffs.create_file('/test', __file__)
  54. image = spiffs.to_binary()
  55. self.assertEqual(len(image), image_size)
  56. # Note: it would be nice to compile spiffs for host with the given
  57. # config, and verify that the image is parsed correctly.
  58. if __name__ == '__main__':
  59. unittest.main()