-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathtest_download.py
170 lines (125 loc) · 5.18 KB
/
test_download.py
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
# SPDX-FileCopyrightText: 2019 Free Software Foundation Europe e.V. <https://fsfe.org>
#
# SPDX-License-Identifier: GPL-3.0-or-later
"""All tests for reuse.download"""
import urllib.request
from pathlib import Path
from urllib.error import URLError
import pytest
from reuse.download import download_license, put_license_in_file
class MockResponse:
"""Super simple mocked version of Response."""
def __init__(self, text=None, status_code=None):
self.text = text
self.status_code = status_code
def __enter__(self):
return self
def __exit__(self, type_, value, traceback):
return False
def read(self):
return self.text.encode("utf-8")
def getcode(self):
return self.status_code
def test_download(monkeypatch):
"""A straightforward test: Request license text, get license text."""
monkeypatch.setattr(
urllib.request, "urlopen", lambda _: MockResponse("hello", 200)
)
result = download_license("0BSD")
assert result == "hello"
def test_download_404(monkeypatch):
"""If the server returns a 404, there is no license text."""
monkeypatch.setattr(
urllib.request, "urlopen", lambda _: MockResponse(status_code=404)
)
with pytest.raises(URLError):
download_license("does-not-exist")
def test_download_exception(monkeypatch):
"""If urllib raises an exception itself, that exception is not escaped."""
def raise_exception(_):
raise URLError("test")
monkeypatch.setattr(urllib.request, "urlopen", raise_exception)
with pytest.raises(URLError):
download_license("hello world")
def test_put_simple(fake_repository, monkeypatch):
"""Straightforward test."""
monkeypatch.setattr(
urllib.request, "urlopen", lambda _: MockResponse("hello\n", 200)
)
put_license_in_file("0BSD", "LICENSES/0BSD.txt")
assert (fake_repository / "LICENSES/0BSD.txt").read_text() == "hello\n"
def test_put_file_exists(fake_repository, monkeypatch):
"""The to-be-downloaded file already exists."""
# pylint: disable=unused-argument
monkeypatch.setattr(
urllib.request, "urlopen", lambda _: MockResponse("hello\n", 200)
)
with pytest.raises(FileExistsError) as exc_info:
put_license_in_file("GPL-3.0-or-later", "LICENSES/GPL-3.0-or-later.txt")
assert Path(exc_info.value.filename).name == "GPL-3.0-or-later.txt"
def test_put_request_exception(fake_repository, monkeypatch):
"""There was an error while downloading the license file."""
# pylint: disable=unused-argument
monkeypatch.setattr(
urllib.request, "urlopen", lambda _: MockResponse(status_code=404)
)
with pytest.raises(URLError):
put_license_in_file("0BSD", "LICENSES/0BSD.txt")
def test_put_empty_dir(empty_directory, monkeypatch):
"""Create a LICENSES/ directory if one does not yet exist."""
monkeypatch.setattr(
urllib.request, "urlopen", lambda _: MockResponse("hello\n", 200)
)
put_license_in_file("0BSD", "LICENSES/0BSD.txt")
assert (empty_directory / "LICENSES").exists()
assert (empty_directory / "LICENSES/0BSD.txt").read_text() == "hello\n"
def test_put_custom_without_source(fake_repository):
"""When 'downloading' a LicenseRef license without source, create an empty
file.
"""
put_license_in_file("LicenseRef-hello", "LICENSES/LicenseRef-hello.txt")
assert (fake_repository / "LICENSES/LicenseRef-hello.txt").exists()
assert (fake_repository / "LICENSES/LicenseRef-hello.txt").read_text() == ""
def test_put_custom_with_source(fake_repository):
"""When 'downloading' a LicenseRef license with source file, copy the source
text.
"""
(fake_repository / "foo.txt").write_text("foo")
put_license_in_file(
"LicenseRef-hello",
"LICENSES/LicenseRef-hello.txt",
source=fake_repository / "foo.txt",
)
assert (fake_repository / "LICENSES/LicenseRef-hello.txt").exists()
assert (
fake_repository / "LICENSES/LicenseRef-hello.txt"
).read_text() == "foo"
def test_put_custom_with_source_dir(fake_repository):
"""When 'downloading' a LicenseRef license with source directory, copy the
source text from a matching file in the directory.
"""
(fake_repository / "lics").mkdir()
(fake_repository / "lics/LicenseRef-hello.txt").write_text("foo")
put_license_in_file(
"LicenseRef-hello",
"LICENSES/LicenseRef-hello.txt",
source=fake_repository / "lics",
)
assert (fake_repository / "LICENSES/LicenseRef-hello.txt").exists()
assert (
fake_repository / "LICENSES/LicenseRef-hello.txt"
).read_text() == "foo"
def test_put_custom_with_false_source_dir(fake_repository):
"""When 'downloading' a LicenseRef license with source directory, but the
source directory does not contain the license, expect a FileNotFoundError.
"""
(fake_repository / "lics").mkdir()
with pytest.raises(FileNotFoundError) as exc_info:
put_license_in_file(
"LicenseRef-hello",
"LICENSES/LicenseRef-hello.txt",
source=fake_repository / "lics",
)
assert exc_info.value.filename.endswith(
str(Path("lics") / "LicenseRef-hello.txt")
)