You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.6 KiB

  1. '''
  2. Image Repository acts as a funky dynamic singlton module.
  3. '''
  4. import os
  5. import pytest
  6. import tempfile
  7. def test_variable_names_are_pushed_to_module_scope(expected_attrs):
  8. '''
  9. The dynamically initialized Globals() should contain the expected images at runtime
  10. '''
  11. from gooey.gui import image_repository
  12. assert all((attr in image_repository.__dict__) for attr in expected_attrs)
  13. def test_patch_returns_error_on_invalid_dir():
  14. '''
  15. patch should explode with a helpful message if it
  16. cannot find the supplied directory
  17. '''
  18. from gooey.gui import image_repository
  19. with pytest.raises(IOError) as kaboom:
  20. image_repository.patch_images('foo/bar/not/a/path')
  21. # our error
  22. assert ' user supplied' in str(kaboom.value)
  23. assert 'foo/bar/not/a/path' in str(kaboom.value)
  24. def test_module_scope_is_updated_on_patch(expected_attrs):
  25. '''
  26. Patch should update the module's globals() on success
  27. '''
  28. from gooey.gui import image_repository
  29. testing_icons = ('config_icon.png', 'success_icon.png')
  30. try:
  31. # setup
  32. make_user_files(*testing_icons)
  33. old_icon = image_repository.config_icon
  34. # load up our new icon(s)
  35. image_repository.patch_images(tempfile.tempdir)
  36. new_icon = image_repository.config_icon
  37. assert old_icon != new_icon
  38. finally:
  39. cleanup_temp(*testing_icons)
  40. # helpers
  41. def make_user_files(*filenames):
  42. for filename in filenames:
  43. with open(os.path.join(tempfile.gettempdir(), filename), 'w') as f:
  44. f.write('temp')
  45. def cleanup_temp(*filenames):
  46. for filename in filenames:
  47. os.remove(os.path.join(tempfile.gettempdir(), filename))