converted to config.py
This commit is contained in:
@@ -55,27 +55,37 @@ Ban Template;
|
||||
|
||||
|
||||
def parse_cmd_line_args(args: List[str], logger: Logger, config_file: Path, posts: Posts) -> bool:
|
||||
"""Parse a very small set of operations from ``sys.argv``.
|
||||
|
||||
``config_file`` now refers to the Python configuration module path
|
||||
(typically ``.../config/config.py``). ``reset_config`` will overwrite the
|
||||
file with the default template, which is imported from the configuration
|
||||
module itself so that it does not need to be duplicated here.
|
||||
"""
|
||||
help_msg = """Command line help prompt
|
||||
Command: help
|
||||
Args: []
|
||||
Decription: Prints the help prompt
|
||||
Description: Prints the help prompt
|
||||
|
||||
Command: reset_config
|
||||
Args: []
|
||||
Decription: Reset the bot credentials
|
||||
Description: Overwrite the Python configuration file with default values
|
||||
|
||||
Command: reset_db
|
||||
Args: []
|
||||
Decription: Reset the database
|
||||
Description: Reset the database
|
||||
"""
|
||||
if len(args) > 1:
|
||||
if args[1] == 'help':
|
||||
logger.info(help_msg)
|
||||
elif args[1] == 'reset_config':
|
||||
# write the template text back to the configuration file. import
|
||||
# TEMPLATE lazily in case the module has not yet been created.
|
||||
try:
|
||||
os.remove(config_file)
|
||||
except FileNotFoundError:
|
||||
logger.error("No configuration file found")
|
||||
from config import TEMPLATE
|
||||
config_file.write_text(TEMPLATE)
|
||||
except Exception:
|
||||
logger.error("Unable to reset configuration file")
|
||||
elif args[1] == 'reset_db':
|
||||
try:
|
||||
os.remove(posts.path)
|
||||
|
||||
@@ -1,11 +1,19 @@
|
||||
import unittest
|
||||
import datetime as dt
|
||||
from pathlib import Path
|
||||
from .actions import (
|
||||
Flair,
|
||||
get_flair,
|
||||
string_to_dt,
|
||||
submission_is_older,
|
||||
parse_cmd_line_args,
|
||||
)
|
||||
from logger import Logger
|
||||
|
||||
|
||||
class DummyPosts:
|
||||
def __init__(self, path):
|
||||
self.path = path
|
||||
|
||||
|
||||
class TestActions(unittest.TestCase):
|
||||
@@ -46,3 +54,27 @@ class TestActions(unittest.TestCase):
|
||||
post_made = today - dt.timedelta(days=(max_days + 1))
|
||||
result = submission_is_older(post_made.date(), max_days)
|
||||
self.assertTrue(result)
|
||||
|
||||
def test_parse_cmd_line_args_reset_config_and_db(self) -> None:
|
||||
tmp = Path(__file__).parent / "tmp_test"
|
||||
tmp.mkdir(exist_ok=True)
|
||||
cfg_file = tmp / "config.py"
|
||||
# ensure file exists with junk content
|
||||
cfg_file.write_text("not important")
|
||||
db_file = tmp / "db.sqlite"
|
||||
db_file.write_text("x")
|
||||
posts = DummyPosts(db_file)
|
||||
logger = Logger(1)
|
||||
|
||||
# reset_config should rewrite the config file
|
||||
result = parse_cmd_line_args(["prog", "reset_config"], logger, cfg_file, posts)
|
||||
self.assertTrue(result)
|
||||
self.assertTrue(cfg_file.exists())
|
||||
content = cfg_file.read_text()
|
||||
self.assertIn("client_id", content)
|
||||
|
||||
# reset_db should remove the database file
|
||||
db_file.write_text("x")
|
||||
result = parse_cmd_line_args(["prog", "reset_db"], logger, cfg_file, posts)
|
||||
self.assertTrue(result)
|
||||
self.assertFalse(db_file.exists())
|
||||
|
||||
Reference in New Issue
Block a user