1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
# SPDX-FileCopyrightText: 2019 Free Software Foundation Europe e.V. <https://fsfe.org>
#
# SPDX-License-Identifier: GPL-3.0-or-later
"""Tests for download."""
# pylint: disable=redefined-outer-name,unused-argument,unspecified-encoding
import errno
import os
import shutil
from pathlib import Path
from unittest.mock import create_autospec
from urllib.error import URLError
import pytest
from click.testing import CliRunner
from reuse.cli import download
from reuse.cli.main import main
# REUSE-IgnoreStart
@pytest.fixture()
def mock_put_license_in_file(monkeypatch):
"""Create a mocked version of put_license_in_file."""
result = create_autospec(download.put_license_in_file)
monkeypatch.setattr(download, "put_license_in_file", result)
return result
class TestDownload:
"""Tests for download."""
def test_simple(self, empty_directory, mock_put_license_in_file):
"""Straightforward test."""
result = CliRunner().invoke(main, ["download", "0BSD"])
assert result.exit_code == 0
mock_put_license_in_file.assert_called_with(
"0BSD", Path(os.path.realpath("LICENSES/0BSD.txt")), source=None
)
def test_strip_plus(self, empty_directory, mock_put_license_in_file):
"""If downloading LIC+, download LIC instead."""
result = CliRunner().invoke(main, ["download", "EUPL-1.2+"])
assert result.exit_code == 0
mock_put_license_in_file.assert_called_with(
"EUPL-1.2",
Path(os.path.realpath("LICENSES/EUPL-1.2.txt")),
source=None,
)
def test_all(self, fake_repository, mock_put_license_in_file):
"""--all downloads all detected licenses."""
shutil.rmtree("LICENSES")
result = CliRunner().invoke(main, ["download", "--all"])
assert result.exit_code == 0
for lic in [
"GPL-3.0-or-later",
"LicenseRef-custom",
"Autoconf-exception-3.0",
"Apache-2.0",
"CC0-1.0",
]:
mock_put_license_in_file.assert_any_call(
lic, Path(os.path.realpath(f"LICENSES/{lic}.txt")), source=None
)
def test_all_with_plus(self, fake_repository, mock_put_license_in_file):
"""--all downloads EUPL-1.2 if EUPL-1.2+ is detected."""
Path("foo.py").write_text("# SPDX-License-Identifier: EUPL-1.2+")
result = CliRunner().invoke(main, ["download", "--all"])
assert result.exit_code == 0
mock_put_license_in_file.assert_called_once_with(
"EUPL-1.2",
Path(os.path.realpath("LICENSES/EUPL-1.2.txt")),
source=None,
)
def test_all_with_plus_and_non_plus(
self, fake_repository, mock_put_license_in_file
):
"""If both EUPL-1.2 and EUPL-1.2+ is detected, download EUPL-1.2 only
once.
"""
Path("foo.py").write_text(
"""
# SPDX-License-Identifier: EUPL-1.2+
# SPDX-License-Identifier: EUPL-1.2
"""
)
result = CliRunner().invoke(main, ["download", "--all"])
assert result.exit_code == 0
mock_put_license_in_file.assert_called_once_with(
"EUPL-1.2",
Path(os.path.realpath("LICENSES/EUPL-1.2.txt")),
source=None,
)
def test_all_and_license_mutually_exclusive(self, empty_directory):
"""--all and license args are mutually exclusive."""
result = CliRunner().invoke(main, ["download", "--all", "0BSD"])
assert result.exit_code != 0
assert "are mutually exclusive" in result.output
def test_all_and_output_mutually_exclusive(self, empty_directory):
"""--all and --output are mutually exclusive."""
result = CliRunner().invoke(
main, ["download", "--all", "--output", "foo"]
)
assert result.exit_code != 0
assert "is mutually exclusive with" in result.output
def test_file_exists(self, fake_repository, mock_put_license_in_file):
"""The to-be-downloaded file already exists."""
mock_put_license_in_file.side_effect = FileExistsError(
errno.EEXIST, "", "GPL-3.0-or-later.txt"
)
result = CliRunner().invoke(main, ["download", "GPL-3.0-or-later"])
assert result.exit_code == 1
assert "GPL-3.0-or-later.txt already exists" in result.output
def test_exception(self, empty_directory, mock_put_license_in_file):
"""There was an error while downloading the license file."""
mock_put_license_in_file.side_effect = URLError("test")
result = CliRunner().invoke(main, ["download", "0BSD"])
assert result.exit_code == 1
assert "internet" in result.output
def test_invalid_spdx(self, empty_directory, mock_put_license_in_file):
"""An invalid SPDX identifier was provided."""
mock_put_license_in_file.side_effect = URLError("test")
result = CliRunner().invoke(main, ["download", "does-not-exist"])
assert result.exit_code == 1
assert "not a valid SPDX License Identifier" in result.output
def test_custom_output(self, empty_directory, mock_put_license_in_file):
"""Download the license into a custom file."""
result = CliRunner().invoke(main, ["download", "-o", "foo", "0BSD"])
assert result.exit_code == 0
mock_put_license_in_file.assert_called_with(
"0BSD", destination=Path("foo"), source=None
)
def test_custom_output_too_many(
self, empty_directory, mock_put_license_in_file
):
"""Providing more than one license with a custom output results in an
error.
"""
result = CliRunner().invoke(
main,
["download", "-o", "foo", "0BSD", "GPL-3.0-or-later"],
)
assert result.exit_code != 0
assert (
"Cannot use '--output' with more than one license" in result.output
)
def test_inside_licenses_dir(
self, fake_repository, mock_put_license_in_file
):
"""While inside the LICENSES/ directory, don't create another LICENSES/
directory.
"""
os.chdir(fake_repository / "LICENSES")
result = CliRunner().invoke(main, ["download", "0BSD"])
assert result.exit_code == 0
mock_put_license_in_file.assert_called_with(
"0BSD", destination=Path("0BSD.txt").absolute(), source=None
)
def test_inside_licenses_dir_in_git(
self, git_repository, mock_put_license_in_file
):
"""While inside a random LICENSES/ directory in a Git repository, use
the root LICENSES/ directory.
"""
(git_repository / "doc/LICENSES").mkdir()
os.chdir(git_repository / "doc/LICENSES")
result = CliRunner().invoke(main, ["download", "0BSD"])
assert result.exit_code == 0
mock_put_license_in_file.assert_called_with(
"0BSD", destination=Path("../../LICENSES/0BSD.txt"), source=None
)
def test_different_root(self, fake_repository, mock_put_license_in_file):
"""Download using a different root."""
(fake_repository / "new_root").mkdir()
result = CliRunner().invoke(
main,
[
"--root",
str((fake_repository / "new_root").resolve()),
"download",
"MIT",
],
)
assert result.exit_code == 0
mock_put_license_in_file.assert_called_with(
"MIT", Path("new_root/LICENSES/MIT.txt").resolve(), source=None
)
def test_licenseref_no_source(self, empty_directory):
"""Downloading a LicenseRef license creates an empty file."""
CliRunner().invoke(main, ["download", "LicenseRef-hello"])
assert (
empty_directory / "LICENSES/LicenseRef-hello.txt"
).read_text() == ""
def test_licenseref_source_file(
self,
empty_directory,
):
"""Downloading a LicenseRef license with a source file copies that
file's contents.
"""
(empty_directory / "foo.txt").write_text("foo")
CliRunner().invoke(
main,
["download", "--source", "foo.txt", "LicenseRef-hello"],
)
assert (
empty_directory / "LICENSES/LicenseRef-hello.txt"
).read_text() == "foo"
def test_licenseref_source_dir(self, empty_directory):
"""Downloading a LicenseRef license with a source dir copies the text
from the corresponding file in the directory.
"""
(empty_directory / "lics").mkdir()
(empty_directory / "lics/LicenseRef-hello.txt").write_text("foo")
CliRunner().invoke(
main, ["download", "--source", "lics", "LicenseRef-hello"]
)
assert (
empty_directory / "LICENSES/LicenseRef-hello.txt"
).read_text() == "foo"
def test_licenseref_false_source_dir(self, empty_directory):
"""Downloading a LicenseRef license with a source that does not contain
the license results in an error.
"""
(empty_directory / "lics").mkdir()
result = CliRunner().invoke(
main, ["download", "--source", "lics", "LicenseRef-hello"]
)
assert result.exit_code == 1
assert (
f"{Path('lics') / 'LicenseRef-hello.txt'} does not exist"
in result.output
)
class TestSimilarIdentifiers:
"""Test a private function _similar_spdx_identifiers."""
# pylint: disable=protected-access
def test_typo(self):
"""Given a misspelt SPDX License Identifier, suggest a better one."""
result = download._similar_spdx_identifiers("GPL-3.0-or-lter")
assert "GPL-3.0-or-later" in result
assert "AGPL-3.0-or-later" in result
assert "LGPL-3.0-or-later" in result
def test_prefix(self):
"""Given an incomplete SPDX License Identifier, suggest a better one."""
result = download._similar_spdx_identifiers("CC0")
assert "CC0-1.0" in result
# REUSE-IgnoreEnd
|