Handle starting linkulator when there is no user config file

This commit is contained in:
asdf 2020-01-19 12:04:34 +11:00
parent 044c8e8e21
commit b1fca93754
2 changed files with 22 additions and 12 deletions

View File

@ -26,7 +26,7 @@ class DefaultUser:
datafile: Path = datadir / PATHS.datafile
ignorefile: Path = datadir / PATHS.ignorefile
settingsfile: Path = datadir / PATHS.settingsfile
lastlogin: float
lastlogin: str = "0.0"
browser: str = "lynx"
def save(self):
@ -68,21 +68,15 @@ def is_readable(st_mode: int) -> bool:
def init():
"""Performs startup checks to ensure environment is set up for use
Creates necessary data directory and data file. If they exist, no error
occurs.
Checks that the data directory and data file are group and other readable.
Sets some correct permissions if they are not.
Other errors may raise an exception.
If required, creates necessary data directory and data file.
If required, sets permissions to ensure the data directory and data file
are group and other readable.
Finally, tries to load the linkulator user configuration file, overwriting
with defaults if any issues.
"""
USER.datadir.mkdir(mode=0o755, exist_ok=True)
if not USER.settingsfile.is_file():
with open(USER.settingsfile, "w+") as the_file:
the_file.write(
"[User Status]\nlastlogin = 0.0\n\n[User Settings]\nbrowser = lynx"
)
if not is_readable(USER.datadir.stat().st_mode):
print(
"Warning: %s is not group or other readable - changing permissions"

16
tests/config_test.py Normal file
View File

@ -0,0 +1,16 @@
#!/usr/bin/env python3
"""Tests for Linkulator configuration"""
import unittest
import config
class TestConfigDefaults(unittest.TestCase):
"""Tests covering configuration default settings"""
def test_default_values_set(self):
"""Test for issue reported in #74"""
# ensure config.USER.lastlogin exists and is a string
self.assertIsInstance(config.USER.lastlogin, str)
# ensure config.USER.lastlogin can be compared to string value
self.assertGreaterEqual(config.USER.lastlogin, "0.0")