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

Fix some ETSConfig and test nits #1683

Merged
merged 5 commits into from
Aug 9, 2022
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
17 changes: 8 additions & 9 deletions traits/etsconfig/etsconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@


# Standard library imports.
import sys
import contextlib
import os
from os import path
from contextlib import contextmanager
import sys


class ETSToolkitError(RuntimeError):
Expand Down Expand Up @@ -94,7 +93,7 @@ def get_application_data(self, create=False):

- The actual location differs between operating systems.

"""
"""
if self._application_data is None:
self._application_data = self._initialize_application_data(
create=create
Expand Down Expand Up @@ -155,9 +154,9 @@ def get_application_home(self, create=False):

- The actual location differs between operating systems.

"""
"""
if self._application_home is None:
self._application_home = path.join(
self._application_home = os.path.join(
self.get_application_data(create=create),
self._get_application_dirname(),
)
Expand Down Expand Up @@ -208,7 +207,7 @@ def company(self, company):
def company(self):
self._company = None

@contextmanager
@contextlib.contextmanager
def provisional_toolkit(self, toolkit):
""" Perform an operation with toolkit provisionally set

Expand Down Expand Up @@ -401,8 +400,8 @@ def _get_application_dirname(self):
main_mod = sys.modules.get("__main__", None)
if main_mod is not None:
if hasattr(main_mod, "__file__"):
main_mod_file = path.abspath(main_mod.__file__)
dirname = path.basename(path.dirname(main_mod_file))
main_mod_file = os.path.abspath(main_mod.__file__)
dirname = os.path.basename(os.path.dirname(main_mod_file))

return dirname

Expand Down
19 changes: 9 additions & 10 deletions traits/etsconfig/tests/test_etsconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# Standard library imports.
import contextlib
import os
import pathlib
import shutil
import sys
import tempfile
Expand Down Expand Up @@ -69,7 +70,7 @@ def temporary_home_directory():
with temporary_directory() as temp_home:
with restore_mapping_entry(os.environ, home_var):
os.environ[home_var] = temp_home
yield
yield temp_home


@contextlib.contextmanager
Expand Down Expand Up @@ -109,11 +110,9 @@ def setUp(self):

# Make a fresh instance each time.
self.ETSConfig = type(ETSConfig)()

def run(self, result=None):
# Extend TestCase.run to use a temporary home directory.
with temporary_home_directory():
super().run(result)
with contextlib.ExitStack() as stack:
self._temp_home = stack.enter_context(temporary_home_directory())
self.addCleanup(stack.pop_all().close)

###########################################################################
# 'ETSConfigTestCase' interface.
Expand Down Expand Up @@ -251,10 +250,10 @@ def test_default_application_home(self):
(dirname, app_name) = os.path.split(app_home)

self.assertEqual(dirname, self.ETSConfig.application_data)

# The assumption here is that the test was run using unittest and not
# a different test runner e.g. using "python -m unittest ...".
self.assertEqual(app_name, "unittest")
self.assertEqual(
app_name,
pathlib.Path(sys.modules["__main__"].__file__).parts[-2]
)

def test_delete_application_home(self):
# given
Expand Down