From 0ec1af0fabe38b447e5429d9e970a65cfa7ead9e Mon Sep 17 00:00:00 2001 From: Lars Mikkelsen Date: Tue, 27 Oct 2020 20:18:44 -0400 Subject: [PATCH] Fix log-level option --- CHANGELOG.md | 1 + icloudpd/logger.py | 5 ++-- tests/test_cli.py | 74 +++++++++++++++++++++++----------------------- 3 files changed, 40 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 360aa9274..d1d42ff7c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- fix: --log-level option [#194](https://github.com/icloud-photos-downloader/icloud_photos_downloader/pull/194) - feature: Folder structure can be set to 'none' instead of a date pattern, so all photos will be placed directly into the download directory. - fix: Empty directory structure being created #185 diff --git a/icloudpd/logger.py b/icloudpd/logger.py index 2b812a487..927a0cbae 100644 --- a/icloudpd/logger.py +++ b/icloudpd/logger.py @@ -2,7 +2,7 @@ import sys import logging -from logging import DEBUG, INFO +from logging import INFO class IPDLogger(logging.Logger): @@ -32,11 +32,10 @@ def tqdm_write(self, message, loglevel=INFO): self.tqdm.write(message) -def setup_logger(loglevel=DEBUG): +def setup_logger(): """Set up logger and add stdout handler""" logging.setLoggerClass(IPDLogger) logger = logging.getLogger("icloudpd") - logger.setLevel(loglevel) has_stdout_handler = False for handler in logger.handlers: if handler.name == "stdoutLogger": diff --git a/tests/test_cli.py b/tests/test_cli.py index b42358006..82a749acb 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -3,6 +3,7 @@ import os import shutil from vcr import VCR +import pytest from click.testing import CliRunner from icloudpd.base import main @@ -10,6 +11,10 @@ class CliTestCase(TestCase): + @pytest.fixture(autouse=True) + def inject_fixtures(self, caplog): + self._caplog = caplog + def test_cli(self): runner = CliRunner() result = runner.invoke(main, ["--help"]) @@ -18,43 +23,38 @@ def test_cli(self): def test_log_levels(self): if not os.path.exists("tests/fixtures/Photos"): os.makedirs("tests/fixtures/Photos") - with vcr.use_cassette("tests/vcr_cassettes/listing_photos.yml"): - # Pass fixed client ID via environment variable - os.environ["CLIENT_ID"] = "DE309E26-942E-11E8-92F5-14109FE0B321" - runner = CliRunner() - result = runner.invoke( - main, - [ - "--username", - "jdoe@gmail.com", - "--password", - "password1", - "--recent", - "0", - "--log-level", - "info", - "-d" - "tests/fixtures/Photos", - ], - ) - assert result.exit_code == 0 - with vcr.use_cassette("tests/vcr_cassettes/listing_photos.yml"): - result = runner.invoke( - main, - [ - "--username", - "jdoe@gmail.com", - "--password", - "password1", - "--recent", - "0", - "--log-level", - "error", - "-d", - "tests/fixtures/Photos", - ], - ) - assert result.exit_code == 0 + + parameters = [ + ("debug", ["DEBUG", "INFO"], []), + ("info", ["INFO"], ["DEBUG"]), + ("error", [], ["DEBUG", "INFO"]), + ] + for log_level, expected, not_expected in parameters: + self._caplog.clear() + with vcr.use_cassette("tests/vcr_cassettes/listing_photos.yml"): + # Pass fixed client ID via environment variable + os.environ["CLIENT_ID"] = "DE309E26-942E-11E8-92F5-14109FE0B321" + runner = CliRunner() + result = runner.invoke( + main, + [ + "--username", + "jdoe@gmail.com", + "--password", + "password1", + "--recent", + "0", + "--log-level", + log_level, + "-d" + "tests/fixtures/Photos", + ], + ) + assert result.exit_code == 0 + for text in expected: + self.assertIn(text, self._caplog.text) + for text in not_expected: + self.assertNotIn(text, self._caplog.text) def test_tqdm(self): if not os.path.exists("tests/fixtures/Photos"):