Skip to content

Commit

Permalink
Changed logger names, updated tests, and settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Vikram Jayanthi committed Jul 8, 2020
1 parent dacab70 commit 27f029b
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 27 deletions.
14 changes: 7 additions & 7 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ def test_settings_transforms_repository_config(tmpdir):
assert s.disable_progress_bar is False


@pytest.mark.parametrize("verbose", [True, False])
def test_setup_logging(verbose: bool):
@pytest.mark.parametrize(
"verbose, log_level", [(True, logging.INFO), (False, logging.WARNING)]
)
def test_setup_logging(verbose, log_level):
"""Set log level based on verbose field."""
settings._setup_logging(verbose)
settings.Settings(verbose=verbose)

logger = logging.getLogger("twine")

if verbose:
assert logger.level == logging.INFO
else:
assert logger.level == logging.WARNING
assert logger.level == log_level


def test_identity_requires_sign():
Expand Down
15 changes: 8 additions & 7 deletions tests/test_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ def upload_settings(make_settings, stub_repository):
return upload_settings


@pytest.fixture
def caplog(caplog):
caplog.set_level(logging.INFO, logger="twine")
return caplog


def test_make_package_pre_signed_dist(upload_settings, caplog):
"""Create a PackageFile and print path, size, and user-provided signature."""
filename = helpers.WHEEL_FIXTURE
Expand All @@ -64,7 +70,6 @@ def test_make_package_pre_signed_dist(upload_settings, caplog):

upload_settings.sign = True
upload_settings.verbose = True
caplog.set_level(logging.INFO, logger="twine")

package = upload._make_package(filename, signatures, upload_settings)

Expand All @@ -84,7 +89,6 @@ def test_make_package_unsigned_dist(upload_settings, monkeypatch, caplog):

upload_settings.sign = True
upload_settings.verbose = True
caplog.set_level(logging.INFO, logger="twine")

def stub_sign(package, *_):
package.gpg_signature = (package.signed_basefilename, b"signature")
Expand Down Expand Up @@ -131,7 +135,6 @@ def test_print_packages_if_verbose(upload_settings, caplog):
}

upload_settings.verbose = True
caplog.set_level(logging.INFO, logger="twine")

result = upload.upload(upload_settings, dists_to_upload)

Expand Down Expand Up @@ -188,10 +191,8 @@ def test_success_when_gpg_is_run(upload_settings, stub_repository, monkeypatch):
@pytest.mark.parametrize("verbose", [False, True])
def test_exception_for_http_status(verbose, upload_settings, stub_response, caplog):
upload_settings.verbose = verbose
if verbose:
caplog.set_level(logging.INFO, logger="twine")
else:
caplog.set_level(logging.WARNING, logger="twine")
log_level = logging.INFO if verbose else logging.WARNING
caplog.set_level(log_level, logger="twine")

stub_response.is_redirect = False
stub_response.status_code = 403
Expand Down
2 changes: 1 addition & 1 deletion twine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import sys

if sys.version_info[:2] >= (3, 8):
from importlib import metadata as importlib_metadata
import importlib.metadata as importlib_metadata
else:
import importlib_metadata

Expand Down
2 changes: 1 addition & 1 deletion twine/commands/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from twine import settings
from twine import utils

logger = logging.getLogger("twine")
logger = logging.getLogger(__name__)


def skip_upload(
Expand Down
11 changes: 4 additions & 7 deletions twine/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,10 @@


def _setup_logging(verbose: bool) -> None:
"""Initialize a logger based on verbosity available throughout twine."""
logger = logging.getLogger("twine")
log_level = logging.INFO if verbose else logging.WARNING
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(log_level)
logger.addHandler(handler)
logger.setLevel(log_level)
"""Initialize a logger based on the --verbose option."""
root_logger = logging.getLogger("twine")
root_logger.addHandler(logging.StreamHandler(sys.stdout))
root_logger.setLevel(logging.INFO if verbose else logging.WARNING)


class Settings:
Expand Down
7 changes: 3 additions & 4 deletions twine/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
# get_userpass_value.
RepositoryConfig = Dict[str, Optional[str]]

logger = logging.getLogger("twine")
logger = logging.getLogger(__name__)


def get_config(path: str = "~/.pypirc") -> Dict[str, RepositoryConfig]:
Expand Down Expand Up @@ -203,9 +203,8 @@ def check_status_code(response: requests.Response, verbose: bool) -> None:
response.raise_for_status()
except requests.HTTPError as err:
if response.text:
if verbose:
logger.info("Content received from server:\n{}".format(response.text))
else:
logger.info("Content received from server:\n{}".format(response.text))
if not verbose:
logger.warning("NOTE: Try --verbose to see response content.")
raise err

Expand Down

0 comments on commit 27f029b

Please sign in to comment.