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.

66 lines
1.8 KiB

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