Skip to content

Commit

Permalink
Merge pull request #1 from pathunstrom/fix-sprite-repl
Browse files Browse the repository at this point in the history
Uses patch to simulate the failure state.
  • Loading branch information
AstraLuma authored May 15, 2019
2 parents 9deef05 + 2a642c7 commit 09741c9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
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

0 comments on commit 09741c9

Please sign in to comment.