Skip to content

Commit

Permalink
feat/better config error messages (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
hollanbm committed May 30, 2024
1 parent b2b3cf1 commit 4976221
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/postCreateCommand.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ poetry install --no-interaction --no-ansi --quiet
source $(poetry env info --path)/bin/activate

# setup pre-commit hook
pre-commit install
pre-commit install
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
# Allow files and directories
!/pyproject.toml
!/poetry.lock
!/src
!/src
2 changes: 1 addition & 1 deletion .github/workflows/buildImage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
push:
branches:
- feat/**
- bug/*
- bug/**


# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds.
Expand Down
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ repos:
- id: ruff
args: [ --fix ]
- id: ruff-format
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: trailing-whitespace
args: [--markdown-linebreak-ext=md]
- id: end-of-file-fixer
- repo: local
hooks:
- id: pytest
Expand Down
4 changes: 2 additions & 2 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ RUN mkdir -p /config && chown -R app /config
USER app
SHELL ["/bin/bash", "-c"]

WORKDIR /home/app/series_scanner
WORKDIR /home/app/renamarr

# See .dockerignore for files whitelist
COPY --chown=app . .
COPY --chown=app . .

# install app dependencies
RUN poetry install --no-interaction --no-ansi --no-cache --quiet
Expand Down
31 changes: 25 additions & 6 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from os import path
import os
from contextlib import contextmanager
from sys import stdout
from time import sleep

Expand Down Expand Up @@ -92,12 +93,20 @@ def __schedule_sonarr_renamarr(self, sonarr_config):

def start(self) -> None:
try:
config = configparser.get_config(
CONFIG_SCHEMA,
config_dir=path.relpath("/config"),
file_name="config.yml",
# configparser uses cwd, for file opens.
# Change to root directory, to look for config in /config/config.yml
with set_directory("/"):
config = configparser.get_config(CONFIG_SCHEMA)
except ConfigFileNotFoundError as exc:
logger.error(
"Unable to locate config file, please check volume mount paths, config must be mounted at /config/config.yaml"
)
logger.error(exc)
exit(1)
except ConfigError as exc:
logger.error(
"Unable to parse config file, Please see example config for comparison -- https://github.com/hollanbm/renamarr/blob/main/docker/config.yml.example"
)
except (ConfigError, ConfigFileNotFoundError) as exc:
logger.error(exc)
exit(1)

Expand Down Expand Up @@ -144,5 +153,15 @@ def start(self) -> None:
sleep(1)


@contextmanager
def set_directory(path):
oldpwd = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(oldpwd)


if __name__ == "__main__": # pragma nocover
Main().start() # pragma: no cover
32 changes: 19 additions & 13 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from unittest.mock import call

import pytest
from config_schema import CONFIG_SCHEMA
from main import Main
Expand Down Expand Up @@ -132,9 +130,13 @@ def test_config_parser_error(self, mock_loguru_error, capsys, mocker) -> None:
with pytest.raises(SystemExit) as excinfo:
Main().start()

mock_loguru_error.assert_called_once_with(exception)
mock_loguru_error.assert_called_with(exception)
assert excinfo.value.code == 1

mock_loguru_error.assert_any_call(
"Unable to parse config file, Please see example config for comparison -- https://github.com/hollanbm/renamarr/blob/main/docker/config.yml.example"
)

def test_config_file_not_found_error(
self, mock_loguru_error, capsys, mocker
) -> None:
Expand All @@ -144,9 +146,13 @@ def test_config_file_not_found_error(
with pytest.raises(SystemExit) as excinfo:
Main().start()

mock_loguru_error.assert_called_once_with(exception)
mock_loguru_error.assert_called_with(exception)
assert excinfo.value.code == 1

mock_loguru_error.assert_any_call(
"Unable to locate config file, please check volume mount paths, config must be mounted at /config/config.yaml"
)

def test_legacy_config_existing_renamer_enabled(
self, mocker, legacy_sonarr_config, mock_loguru_warning
):
Expand All @@ -158,20 +164,20 @@ def test_legacy_config_existing_renamer_enabled(

mocker.patch.object(Job, "do")

mocker.patch.object(SonarrSeriesScanner, "scan")
sonarr_renamarr = mocker.patch.object(SonarrRenamarr, "scan")
mocker.patch.object(RadarrRenamarr, "scan")

Main().start()

sonarr_renamarr.assert_called()
mock_loguru_warning.assert_has_calls(
[
call(
"sonarr[].existing_renamer config option, has been renamed to sonarr[].renamarr. Please update config, as this will stop working in future versions"
),
call(
"Please see example config for comparison -- https://github.com/hollanbm/renamarr/blob/main/docker/config.yml.example"
),
]

mock_loguru_warning.assert_any_call(
"sonarr[].existing_renamer config option, has been renamed to sonarr[].renamarr. Please update config, as this will stop working in future versions"
)

mock_loguru_warning.assert_any_call(
"Please see example config for comparison -- https://github.com/hollanbm/renamarr/blob/main/docker/config.yml.example"
)

def test_legacy_config_existing_renamer_exception(
Expand Down

0 comments on commit 4976221

Please sign in to comment.