From 1bcfb9bd56f2a350c618892a5d10f52ab6d12a8b Mon Sep 17 00:00:00 2001 From: Carmen Bianca BAKKER Date: Tue, 11 Jun 2024 16:57:11 +0200 Subject: [PATCH] Do not print DEP5 deprecation warning during convert-dep5 Signed-off-by: Carmen Bianca BAKKER --- src/reuse/_main.py | 6 ++++++ src/reuse/project.py | 18 ++++++++++-------- tests/conftest.py | 8 ++++++++ tests/test_main.py | 9 +++++++++ 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/reuse/_main.py b/src/reuse/_main.py index 4d76db26..fec1f731 100644 --- a/src/reuse/_main.py +++ b/src/reuse/_main.py @@ -9,6 +9,7 @@ import argparse import logging +import os import sys import warnings from gettext import gettext as _ @@ -242,6 +243,11 @@ def main(args: Optional[List[str]] = None, out: IO[str] = sys.stdout) -> int: out.write(f"reuse {__version__}\n") return 0 + # Very stupid workaround to not print a DEP5 deprecation warning in the + # middle of conversion to REUSE.toml. + if args and args[0] == "convert-dep5": + os.environ["_SUPPRESS_DEP5_WARNING"] = "1" + root = parsed_args.root if root is None: root = find_root() diff --git a/src/reuse/project.py b/src/reuse/project.py index 1dc6acdf..2c00da0f 100644 --- a/src/reuse/project.py +++ b/src/reuse/project.py @@ -354,14 +354,16 @@ def find_global_licensing( candidate: Optional[GlobalLicensingFound] = None dep5_path = root / ".reuse/dep5" if (dep5_path).exists(): - warnings.warn( - _( - "'.reuse/dep5' is deprecated. You are recommended to" - " instead use REUSE.toml. Use `reuse convert-dep5` to" - " convert." - ), - PendingDeprecationWarning, - ) + # Sneaky workaround to not print this warning. + if not os.environ.get("_SUPPRESS_DEP5_WARNING"): + warnings.warn( + _( + "'.reuse/dep5' is deprecated. You are recommended to" + " instead use REUSE.toml. Use `reuse convert-dep5` to" + " convert." + ), + PendingDeprecationWarning, + ) candidate = GlobalLicensingFound(dep5_path, ReuseDep5) toml_path = None with contextlib.suppress(StopIteration): diff --git a/tests/conftest.py b/tests/conftest.py index 91316d78..94b3b095 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -9,6 +9,7 @@ # pylint: disable=redefined-outer-name,invalid-name +import contextlib import datetime import logging import multiprocessing as mp @@ -83,6 +84,13 @@ def pytest_runtest_setup(item): # Make sure to restore CWD os.chdir(CWD) + # TODO: Awful workaround. In `main`, this environment variable is set under + # certain conditions. This means that all tests that run _after_ that + # condition is met also have the environment variable set, because the + # environment had been changed. There should be a better way to scope this. + with contextlib.suppress(KeyError): + del os.environ["_SUPPRESS_DEP5_WARNING"] + @pytest.fixture() def git_exe() -> str: diff --git a/tests/test_main.py b/tests/test_main.py index 0b38cb19..3068b330 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -16,6 +16,7 @@ import os import re import shutil +import warnings from inspect import cleandoc from pathlib import Path from typing import Generator, Optional @@ -639,4 +640,12 @@ def test_convert_dep5_no_dep5_file(fake_repository, stringio): main(["convert-dep5"], out=stringio) +def test_convert_dep5_no_warning(fake_repository_dep5, stringio): + """No PendingDeprecationWarning when running convert-dep5.""" + with warnings.catch_warnings(record=True) as caught_warnings: + result = main(["convert-dep5"], out=stringio) + assert result == 0 + assert not caught_warnings + + # REUSE-IgnoreEnd