Skip to content
This repository has been archived by the owner on Jan 14, 2024. It is now read-only.

Commit

Permalink
#54: Cover with unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
blackandred committed Nov 27, 2020
1 parent 55fbcd8 commit 05e4d79
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
2 changes: 1 addition & 1 deletion rkd/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,9 @@ def create_unified_context(self, chdir: str = '', additional_imports: List[str]

paths = [
CURRENT_SCRIPT_PATH + '/misc/internal',
get_user_site_packages() + '/usr/share/rkd/internal',
'/usr/lib/rkd',
'/usr/share/rkd/internal',
get_user_site_packages() + '/usr/share/rkd/internal',
os.path.expanduser('~/.rkd'),
os.getcwd() + '/.rkd'
]
Expand Down
13 changes: 8 additions & 5 deletions rkd/packaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ def get_possible_paths(path: str) -> List[str]:
# eg. ~/.local/share/rkd/banner.txt
os.path.expanduser('~/.local/share/rkd/' + path),

# eg. /home/andrew/.local/lib/python3.8/site-packages/usr/share/rkd/banner.txt
get_user_site_packages() + '/usr/share/rkd/' + path,
# eg. /home/andrew/.local/lib/python3.8/site-packages/rkd/misc/banner.txt
get_user_site_packages() + '/rkd/misc/' + path,

# eg. /usr/lib/python3.8/site-packages/usr/share/rkd/banner.txt
_get_global_site_packages() + '/usr/share/rkd/' + path,
# eg. /usr/lib/python3.8/site-packages/rkd/misc/banner.txt
_get_global_site_packages() + '/rkd/misc/' + path,

# eg. /usr/share/rkd/banner.txt
'/usr/share/rkd/' + path
]

# eg. ./rkd/misc/banner.txt
global_module_path = os.path.dirname(os.path.realpath(__file__)) + '/misc/' + path
global_module_path = _get_current_script_path() + '/misc/' + path

# installed module directory should be less important to allow customizations
if "site-packages" in global_module_path:
Expand Down Expand Up @@ -73,3 +73,6 @@ def _find(path: str, method: Callable) -> Optional[str]:
def _get_global_site_packages() -> str:
return get_python_lib()


def _get_current_script_path() -> str:
return os.path.dirname(os.path.realpath(__file__))
47 changes: 47 additions & 0 deletions test/test_packaging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python3

from unittest import mock
from rkd.api.testing import BasicTestingCase
import rkd.packaging


class TestPackaging(BasicTestingCase):
def test_get_possible_paths_puts_local_development_path_in_first_priority(self):
"""
When RKD is in development mode, and code is taken from local directory in current working directory
then current working directory has FIRST priority.
When RKD is installed, then the installation path in site-packages has lower priorite.
:return:
"""

with self.subTest('Local path - not installed will be first'):
with mock.patch.object(rkd.packaging, '_get_current_script_path', return_value='/home/iwa/rkd'):
paths = rkd.packaging.get_possible_paths('banner.txt')
self.assertEqual('/home/iwa/rkd/misc/banner.txt', paths[0])

with self.subTest('RKD installed as package will be last'):
with mock.patch.object(rkd.packaging, '_get_current_script_path', return_value='/usr/lib/py/site-packages/rkd'):
paths = rkd.packaging.get_possible_paths('banner.txt')
self.assertEqual('/usr/lib/py/site-packages/rkd/misc/banner.txt', paths[-1])

def test_find_resource_file(self):
"""Checks if file is found"""

with self.subTest('Finds a one of defaults files'):
self.assertIsNotNone(rkd.packaging.find_resource_file('banner.txt'))

with self.subTest('Does not find a file'):
self.assertIsNone(rkd.packaging.find_resource_file('this-name-does-not-exist'))

def test_find_resource_directory(self):
"""Checks if directory is found"""

with self.subTest('Looks and finds an "internal" directory'):
self.assertIsNotNone(rkd.packaging.find_resource_directory('internal'))

with self.subTest('Does not find a file'):
self.assertIsNone(rkd.packaging.find_resource_directory('external-ths-does-not-exist'))

def test_get_user_site_packages(self):
self.assertIsNotNone(rkd.packaging.get_user_site_packages())

0 comments on commit 05e4d79

Please sign in to comment.