Skip to content

Commit

Permalink
Add tests for annotating symlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
matrss committed Jun 22, 2023
1 parent fcee822 commit 0552ef1
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions tests/test_main_annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,27 @@
# SPDX-FileCopyrightText: © 2020 Liferay, Inc. <https://liferay.com>
# SPDX-FileCopyrightText: 2022 Florian Snow <florian@familysnow.net>
# SPDX-FileCopyrightText: 2022 Carmen Bianca Bakker <carmenbianca@fsfe.org>
# SPDX-FileCopyrightText: 2023 Matthias Riße
#
# SPDX-License-Identifier: GPL-3.0-or-later

"""Tests for reuse._main: annotate"""
import logging
import stat
from importlib import import_module
from inspect import cleandoc

import pytest

from reuse._main import main

try:
IS_POSIX = bool(import_module("posix"))
except ImportError:
IS_POSIX = False

posix = pytest.mark.skipif(not IS_POSIX, reason="Windows not supported")

# pylint: disable=too-many-lines,unused-argument


Expand Down Expand Up @@ -971,6 +980,55 @@ def test_annotate_force_dot_license_doesnt_write_to_file(
assert simple_file.read_text() == "Preserve this"


@posix
@pytest.mark.parametrize(
"create_target",
[True, False],
ids=map(lambda x: f"create_target={x}", [True, False]),
)
def test_annotate_force_dot_license_for_symlinks(
fake_repository, stringio, mock_date_today, create_target
):
"""Annotating a symlink, broken or not, with --force-dot-license should
result in a .license file next to the symlink.
"""
target_file = fake_repository / "target-file"
if create_target:
target_file.write_text("Preserve this")
symlink = fake_repository / "symlink"
symlink.symlink_to(target_file.relative_to(fake_repository))
expected = cleandoc(
"""
SPDX-FileCopyrightText: 2018 Jane Doe
SPDX-License-Identifier: GPL-3.0-or-later
"""
)

result = main(
[
"annotate",
"--license",
"GPL-3.0-or-later",
"--copyright",
"Jane Doe",
"--force-dot-license",
"symlink",
],
out=stringio,
)

assert result == 0
assert (
symlink.with_name(f"{symlink.name}.license").read_text().strip()
== expected
)
if create_target:
assert target_file.read_text() == "Preserve this"
else:
assert not symlink.exists()


def test_annotate_to_read_only_file_does_not_traceback(
fake_repository, stringio, mock_date_today
):
Expand Down

0 comments on commit 0552ef1

Please sign in to comment.