Skip to content

Commit

Permalink
Update rcfile and banner normalization.
Browse files Browse the repository at this point in the history
Also remove tests for bogus assignment and add type hints instead.

This change moves the normalization from the property setters into
the getters. Doing this makes sure we are checking the data as it
comes in to the application rather than when it leaves the app.

This also means that we are trusting data generated from within
the app. And the type hints have been added to help check that
the implementation is correct.
  • Loading branch information
shawnbrown committed Sep 11, 2021
1 parent d283c22 commit 087f1aa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
21 changes: 9 additions & 12 deletions envlauncher/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,6 @@ def __init__(self, file_or_path):
self._parser.read_string(string)

self._terminal_emulator_choices = get_terminal_emulators()
self._rcfile = self._parser.get('X-EnvLauncher Options', 'Rcfile', fallback='')
self._banner = self._parser.get('X-EnvLauncher Options', 'Banner', fallback='color')
self._venv_number = itertools.count(1)
self._app_data_subdir = 'envlauncher'

Expand Down Expand Up @@ -209,13 +207,11 @@ def rcfile(self) -> str:
"""An "rc" file to execute after activating the environment
(e.g., ~/.bashrc).
"""
return self._rcfile
return self._parser.get('X-EnvLauncher Options', 'Rcfile', fallback='')

@rcfile.setter
def rcfile(self, value):
if not isinstance(value, str):
value = ''
self._rcfile = value
def rcfile(self, value: str):
self._parser['X-EnvLauncher Options']['Rcfile'] = value

@property
def terminal_emulator_choices(self) -> List[str]:
Expand All @@ -239,13 +235,14 @@ def terminal_emulator(self, value):
@property
def banner(self) -> str:
"""Python logo banner option."""
return self._banner
value = self._parser.get('X-EnvLauncher Options', 'Banner', fallback='')
if value not in {'color', 'plain', 'none'}:
return 'color'
return value

@banner.setter
def banner(self, value):
if value not in {'color', 'plain', 'none'}:
value = 'color'
self._banner = value
def banner(self, value: str):
self._parser['X-EnvLauncher Options']['Banner'] = value

@property
def banner_resource(self) -> Optional[Tuple[str, str]]:
Expand Down
12 changes: 4 additions & 8 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,6 @@ def test_set_value(self):
self.settings.rcfile = '.venvrc'
self.assertEqual(self.settings.rcfile, '.venvrc')

def test_invalid_value(self):
self.settings.rcfile = 1234 # <- Bogus value.
self.assertEqual(self.settings.rcfile, '') # <- Empty string.


class TestSettingsTerminalEmulator(unittest.TestCase):
def setUp(self):
Expand Down Expand Up @@ -309,17 +305,17 @@ def setUp(self):
def test_read(self):
self.assertEqual(self.settings.banner, 'color')

def test_read_bad_value(self):
self.settings.banner = 'some bad value'
self.assertEqual(self.settings.banner, 'color')

def test_set_value(self):
self.settings.banner = 'plain'
self.assertEqual(self.settings.banner, 'plain')

self.settings.banner = 'none'
self.assertEqual(self.settings.banner, 'none')

def test_invalid_value(self):
self.settings.banner = 1234 # <- Bogus value.
self.assertEqual(self.settings.banner, 'color') # <- Defaults to color.

def test_banner_resource(self):
self.assertEqual(self.settings.banner_resource,
('envlauncher', 'banner-color.ascii'))
Expand Down

0 comments on commit 087f1aa

Please sign in to comment.