Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uses patch to simulate the failure state. #1

Merged
merged 2 commits into from
May 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions ppb/sprites.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,9 @@ def __image__(self):

def __resource_path__(self):
if self.resource_path is None:
if type(self).__module__ == '__main__':
self.resource_path = Path.cwd().resolve()
else:
self.resource_path = Path(getfile(type(self))).resolve().parent
try:
file_path = Path(getfile(type(self))).resolve().parent
except TypeError:
file_path = Path.cwd().resolve()
self.resource_path = file_path
return self.resource_path
11 changes: 8 additions & 3 deletions tests/test_sprites.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from unittest import TestCase
from unittest.mock import patch

from ppb import BaseSprite, Vector
from ppb.sprites import Rotatable
Expand Down Expand Up @@ -328,11 +329,15 @@ def test_rotatable_base_sprite():


def test_sprite_in_main():
"""
Test that Sprite.__resource_path__ returns a meaningful value inside
REPLs where __main__ doesn't have a file.
"""
class TestSprite(BaseSprite):
pass

TestSprite.__module__ = '__main__'

s = TestSprite()

assert s.__resource_path__() # We don't care what it is, as long as it's something
with patch("ppb.sprites.getfile", side_effect=TypeError):
# This patch simulates what happens when TestSprite was defined in the REPL
assert s.__resource_path__() # We don't care what it is, as long as it's something