main reworking
This commit is contained in:
parent
f3efee5add
commit
6ac1ca476d
4 changed files with 70 additions and 25 deletions
|
|
@ -4,7 +4,7 @@ from ytdl_sub.cli.parsers.main import parser
|
||||||
from ytdl_sub.utils.logger import Logger
|
from ytdl_sub.utils.logger import Logger
|
||||||
|
|
||||||
|
|
||||||
def _main():
|
def _main() -> int:
|
||||||
# Set log level before any other ytdl-sub files are imported. That way, when loggers
|
# Set log level before any other ytdl-sub files are imported. That way, when loggers
|
||||||
# get initialized, they will see the set log level
|
# get initialized, they will see the set log level
|
||||||
args, _ = parser.parse_known_args()
|
args, _ = parser.parse_known_args()
|
||||||
|
|
@ -15,7 +15,10 @@ def _main():
|
||||||
|
|
||||||
# pylint: enable=import-outside-toplevel
|
# pylint: enable=import-outside-toplevel
|
||||||
|
|
||||||
ytdl_sub.cli.entrypoint.main()
|
subs = ytdl_sub.cli.entrypoint.main()
|
||||||
|
if any(sub.exception for sub in subs):
|
||||||
|
return 1 # Return error-code if any exceptions occurred
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
@ -23,14 +26,13 @@ def main():
|
||||||
Entrypoint for ytdl-sub
|
Entrypoint for ytdl-sub
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
_main()
|
return_code = _main()
|
||||||
Logger.cleanup(cleanup_error_log=True) # Ran successfully, so we can delete the debug file
|
Logger.cleanup(cleanup_error_log=return_code == 0)
|
||||||
|
sys.exit(return_code)
|
||||||
except Exception as exc: # pylint: disable=broad-except
|
except Exception as exc: # pylint: disable=broad-except
|
||||||
Logger.log_exception(exception=exc)
|
Logger.log_exception(exception=exc)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
sys.exit(0)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
|
||||||
|
|
@ -7,15 +7,9 @@ from unittest.mock import Mock
|
||||||
from ytdl_sub.cli.output_summary import output_summary
|
from ytdl_sub.cli.output_summary import output_summary
|
||||||
|
|
||||||
|
|
||||||
def test_output_summary_one_error():
|
def _to_mock_subscriptions(
|
||||||
subscription_values: List[Tuple[str, int, int, int, int, Optional[Exception]]] = [
|
subscription_values: List[Tuple[str, int, int, int, int, Optional[Exception]]]
|
||||||
("long_name_but_lil_values", 0, 0, 0, 6, None),
|
) -> List[MagicMock]:
|
||||||
("john_smith", 1, 0, 0, 52, None),
|
|
||||||
("david_gore", 0, 0, 0, 4, None),
|
|
||||||
("christopher_snoop", 50, 0, 3, 518, None),
|
|
||||||
("beyond funk", 0, 0, 0, 176, ValueError("lol")),
|
|
||||||
]
|
|
||||||
|
|
||||||
mock_subscriptions: List[MagicMock] = []
|
mock_subscriptions: List[MagicMock] = []
|
||||||
for values in subscription_values:
|
for values in subscription_values:
|
||||||
sub = Mock()
|
sub = Mock()
|
||||||
|
|
@ -28,5 +22,46 @@ def test_output_summary_one_error():
|
||||||
|
|
||||||
mock_subscriptions.append(sub)
|
mock_subscriptions.append(sub)
|
||||||
|
|
||||||
_ = output_summary(subscriptions=mock_subscriptions)
|
return mock_subscriptions
|
||||||
assert True # Test used for manual inspection - too hard to test ansi color codes
|
|
||||||
|
|
||||||
|
def test_output_summary_no_errors():
|
||||||
|
mock_subscriptions = _to_mock_subscriptions(
|
||||||
|
[
|
||||||
|
("long_name_but_lil_values", 0, 0, 0, 6, None),
|
||||||
|
("john_smith", 1, 0, 0, 52, None),
|
||||||
|
("david_gore", 0, 0, 0, 4, None),
|
||||||
|
("christopher_snoop", 50, 0, 3, 518, None),
|
||||||
|
("beyond funk", 352, 0, 0, 2342, None),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
output_summary(subscriptions=mock_subscriptions)
|
||||||
|
|
||||||
|
|
||||||
|
def test_output_summary_one_error():
|
||||||
|
mock_subscriptions = _to_mock_subscriptions(
|
||||||
|
[
|
||||||
|
("long_name_but_lil_values", 0, 0, 0, 6, None),
|
||||||
|
("john_smith", 1, 0, 0, 52, None),
|
||||||
|
("david_gore", 0, 0, 0, 4, None),
|
||||||
|
("christopher_snoop", 50, 0, 3, 518, None),
|
||||||
|
("beyond funk", 0, 0, 0, 176, ValueError("lol")),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
output_summary(subscriptions=mock_subscriptions)
|
||||||
|
|
||||||
|
|
||||||
|
def test_output_summary_multiple_errors():
|
||||||
|
mock_subscriptions = _to_mock_subscriptions(
|
||||||
|
[
|
||||||
|
("long_name_but_lil_values", 0, 0, 0, 6, None),
|
||||||
|
("john_smith", 1, 0, 0, 52, None),
|
||||||
|
("david_gore", 0, 0, 0, 4, PermissionError("ack")),
|
||||||
|
("christopher_snoop", 50, 0, 3, 518, None),
|
||||||
|
("beyond funk", 0, 0, 0, 176, ValueError("lol")),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
output_summary(subscriptions=mock_subscriptions)
|
||||||
|
|
|
||||||
|
|
@ -45,9 +45,9 @@ def test_suppress_transaction_log(
|
||||||
]
|
]
|
||||||
+ (["--transaction-log", file_transaction_log] if file_transaction_log else []),
|
+ (["--transaction-log", file_transaction_log] if file_transaction_log else []),
|
||||||
), patch("ytdl_sub.cli.output_transaction_log.output_transaction_log") as mock_transaction_log:
|
), patch("ytdl_sub.cli.output_transaction_log.output_transaction_log") as mock_transaction_log:
|
||||||
transaction_logs = main()
|
subscriptions = main()
|
||||||
|
|
||||||
assert transaction_logs
|
assert subscriptions
|
||||||
assert mock_transaction_log.call_count == 0
|
assert mock_transaction_log.call_count == 0
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -97,5 +97,5 @@ def test_transaction_log_to_logger(
|
||||||
expected_message="Transaction log for john_smith:\n",
|
expected_message="Transaction log for john_smith:\n",
|
||||||
log_level="info",
|
log_level="info",
|
||||||
):
|
):
|
||||||
transaction_logs = main()
|
subscriptions = main()
|
||||||
assert transaction_logs
|
assert subscriptions
|
||||||
|
|
|
||||||
|
|
@ -38,11 +38,19 @@ def mock_sys_exit():
|
||||||
return _mock_sys_exit
|
return _mock_sys_exit
|
||||||
|
|
||||||
|
|
||||||
def test_main_success(mock_sys_exit):
|
@pytest.mark.parametrize("return_code", [0, 1])
|
||||||
with mock_sys_exit(expected_exit_code=0):
|
def test_main_exit_code(mock_sys_exit, return_code: int):
|
||||||
with patch("src.ytdl_sub.main._main"):
|
with mock_sys_exit(expected_exit_code=return_code), patch(
|
||||||
|
"src.ytdl_sub.main._main"
|
||||||
|
) as mock_inner_main, patch.object(Logger, "cleanup") as mock_logger_cleanup:
|
||||||
|
mock_inner_main.return_value = return_code
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
assert mock_logger_cleanup.call_count == 1
|
||||||
|
assert mock_logger_cleanup.call_args.kwargs["cleanup_error_log"] == (
|
||||||
|
True if return_code == 0 else False
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_main_validation_error(capsys, mock_sys_exit):
|
def test_main_validation_error(capsys, mock_sys_exit):
|
||||||
validation_exception = ValidationException("test exc")
|
validation_exception = ValidationException("test exc")
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue