Skip to content

Commit

Permalink
Merge pull request #999 from carmenbianca/fix-deprecation-convert-dep5
Browse files Browse the repository at this point in the history
Do not print DEP5 deprecation warning during convert-dep5
  • Loading branch information
carmenbianca authored Jun 11, 2024
2 parents 3831234 + 1bcfb9b commit b9f07f1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
6 changes: 6 additions & 0 deletions src/reuse/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import argparse
import logging
import os
import sys
import warnings
from gettext import gettext as _
Expand Down Expand Up @@ -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()
Expand Down
18 changes: 10 additions & 8 deletions src/reuse/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 8 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

# pylint: disable=redefined-outer-name,invalid-name

import contextlib
import datetime
import logging
import multiprocessing as mp
Expand Down Expand Up @@ -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:
Expand Down
9 changes: 9 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit b9f07f1

Please sign in to comment.