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
|
# 3rd party
import pytest
from domdf_python_tools.paths import PathPlus
# this package
from pyproject_parser import License, Readme
class TestReadme:
def test_no_test_or_file(self):
with pytest.raises(TypeError, match="At least one of 'text' and 'file' must be supplied to .*"):
Readme()
with pytest.raises(TypeError, match="At least one of 'text' and 'file' must be supplied to .*"):
Readme(charset="UTF-8")
def test_content_type_sole_arg(self):
match = "'content_type' cannot be provided on its own; please provide either 'text' or 'file' or use the 'from_file' method."
with pytest.raises(ValueError, match=match):
Readme(content_type="text/x-rst")
def test_invalid_content_types(self):
with pytest.raises(ValueError, match="Unsupported readme content-type 'application/json'"):
Readme(text="This is the readme", content_type="application/json") # type: ignore[arg-type]
with pytest.raises(ValueError, match="Unsupported readme content-type 'image/jpeg'"):
Readme(file="photo.jpg", content_type="image/jpeg") # type: ignore[arg-type]
def test_from_file(self, tmp_pathplus: PathPlus):
(tmp_pathplus / "README.rst").write_clean("This is the README")
readme = Readme.from_file(tmp_pathplus / "README.rst")
assert readme.file == tmp_pathplus / "README.rst"
assert readme.content_type == "text/x-rst"
assert readme.text is None
readme.resolve(inplace=True)
assert readme.file == tmp_pathplus / "README.rst"
assert readme.content_type == "text/x-rst"
assert readme.text == "This is the README\n"
(tmp_pathplus / "README.txt").write_clean("This is the README")
readme = Readme.from_file(tmp_pathplus / "README.txt")
assert readme.file == tmp_pathplus / "README.txt"
assert readme.content_type == "text/plain"
assert readme.text is None
readme.resolve(inplace=True)
assert readme.file == tmp_pathplus / "README.txt"
assert readme.content_type == "text/plain"
assert readme.text == "This is the README\n"
def test_from_file_bad_filetype(self, tmp_pathplus: PathPlus):
with pytest.raises(ValueError, match="Unrecognised filetype for '.*README'"):
Readme.from_file(tmp_pathplus / "README")
with pytest.raises(ValueError, match="Unrecognised filetype for '.*README.rtf'"):
Readme.from_file(tmp_pathplus / "README.rtf")
with pytest.raises(ValueError, match="Unrecognised filetype for '.*README.doc'"):
Readme.from_file(tmp_pathplus / "README.doc")
def test_from_file_charset(self, tmp_pathplus: PathPlus):
(tmp_pathplus / "README.md").write_clean("This is the README", encoding="cp1252")
readme = Readme.from_file(tmp_pathplus / "README.md", charset="cp1252")
assert readme.file == tmp_pathplus / "README.md"
assert readme.content_type == "text/markdown"
assert readme.text is None
readme.resolve(inplace=True)
assert readme.file == tmp_pathplus / "README.md"
assert readme.content_type == "text/markdown"
assert readme.text == "This is the README\n"
def test_resolve(self, tmp_pathplus: PathPlus):
(tmp_pathplus / "README.txt").write_clean("This is the README")
readme = Readme(file=tmp_pathplus / "README.txt", content_type="text/plain")
assert readme.file == tmp_pathplus / "README.txt"
assert readme.content_type == "text/plain"
assert readme.text is None
resolved_readme = readme.resolve()
assert readme is not resolved_readme
assert resolved_readme.file == tmp_pathplus / "README.txt"
assert resolved_readme.content_type == "text/plain"
assert resolved_readme.text == "This is the README\n"
def test_resolve_inplace(self, tmp_pathplus: PathPlus):
(tmp_pathplus / "README.txt").write_clean("This is the README")
readme = Readme(file=tmp_pathplus / "README.txt", content_type="text/plain")
assert readme.file == tmp_pathplus / "README.txt"
assert readme.content_type == "text/plain"
assert readme.text is None
resolved_readme = readme.resolve(inplace=True)
assert readme is resolved_readme
assert readme.file == tmp_pathplus / "README.txt"
assert readme.content_type == "text/plain"
assert readme.text == "This is the README\n"
assert resolved_readme.file == tmp_pathplus / "README.txt"
assert resolved_readme.content_type == "text/plain"
assert resolved_readme.text == "This is the README\n"
def test_to_dict(self, tmp_pathplus: PathPlus):
(tmp_pathplus / "README.rst").write_clean("This is the README")
readme = Readme(file=tmp_pathplus / "README.rst", content_type="text/x-rst")
assert readme.to_dict() == {
"file": f"{tmp_pathplus.as_posix()}/README.rst",
"content_type": "text/x-rst",
}
assert Readme.from_dict(readme.to_dict()) == readme
assert Readme(**readme.to_dict()) == readme
(tmp_pathplus / "README.rst").write_clean("This is the README", encoding="cp1252")
readme = Readme(file=tmp_pathplus / "README.rst", content_type="text/x-rst", charset="cp1252")
assert readme.to_dict() == {
"file": f"{tmp_pathplus.as_posix()}/README.rst",
"content_type": "text/x-rst",
"charset": "cp1252",
}
assert Readme.from_dict(readme.to_dict()) == readme
assert Readme(**readme.to_dict()) == readme
readme = Readme(text="This is the README", content_type="text/x-rst")
assert readme.to_dict() == {
"text": "This is the README",
"content_type": "text/x-rst",
}
assert Readme.from_dict(readme.to_dict()) == readme
assert Readme(**readme.to_dict()) == readme
def test_to_pep621_dict(self, tmp_pathplus: PathPlus):
(tmp_pathplus / "README.rst").write_clean("This is the README")
readme = Readme(file=tmp_pathplus / "README.rst", content_type="text/x-rst")
assert readme.to_pep621_dict() == {
"file": f"{tmp_pathplus.as_posix()}/README.rst",
}
# TODO: the type ignore needs the TypedDict function fix in typing-extensions
assert Readme.from_dict(readme.to_pep621_dict()) == readme # type: ignore[arg-type]
(tmp_pathplus / "README.md").write_clean("This is the README")
readme = Readme(file=tmp_pathplus / "README.md", content_type="text/x-rst")
assert readme.to_pep621_dict() == {
"file": f"{tmp_pathplus.as_posix()}/README.md",
"content-type": "text/x-rst",
}
# TODO: the type ignore needs the TypedDict function fix in typing-extensions
assert Readme.from_dict(readme.to_pep621_dict()) == readme # type: ignore[arg-type]
(tmp_pathplus / "README.rst").write_clean("This is the README", encoding="cp1252")
readme = Readme(file=tmp_pathplus / "README.rst", content_type="text/x-rst", charset="cp1252")
assert readme.to_pep621_dict() == {
"file": f"{tmp_pathplus.as_posix()}/README.rst",
"charset": "cp1252",
}
# TODO: the type ignore needs the TypedDict function fix in typing-extensions
assert Readme.from_dict(readme.to_pep621_dict()) == readme # type: ignore[arg-type]
readme = Readme(text="This is the README", content_type="text/x-rst")
assert readme.to_pep621_dict() == {
"text": "This is the README",
"content-type": "text/x-rst",
}
# TODO: the type ignore needs the TypedDict function fix in typing-extensions
assert Readme.from_dict(readme.to_pep621_dict()) == readme # type: ignore[arg-type]
class TestLicense:
def test_no_test_or_file(self):
with pytest.raises(TypeError, match="At least one of 'text' and 'file' must be supplied to .*"):
License()
def test_resolve(self, tmp_pathplus: PathPlus):
(tmp_pathplus / "LICENSE.txt").write_clean("This is the LICENSE")
lic = License(file=tmp_pathplus / "LICENSE.txt")
assert lic.file == tmp_pathplus / "LICENSE.txt"
assert lic.text is None
resolved_lic = lic.resolve()
assert lic is not resolved_lic
assert resolved_lic.file == tmp_pathplus / "LICENSE.txt"
assert resolved_lic.text == "This is the LICENSE\n"
def test_resolve_inplace(self, tmp_pathplus: PathPlus):
(tmp_pathplus / "LICENSE.txt").write_clean("This is the LICENSE")
lic = License(file=tmp_pathplus / "LICENSE.txt")
assert lic.file == tmp_pathplus / "LICENSE.txt"
assert lic.text is None
resolved_lic = lic.resolve(inplace=True)
assert lic is resolved_lic
assert lic.file == tmp_pathplus / "LICENSE.txt"
assert lic.text == "This is the LICENSE\n"
assert resolved_lic.file == tmp_pathplus / "LICENSE.txt"
assert resolved_lic.text == "This is the LICENSE\n"
def test_to_dict(self, tmp_pathplus: PathPlus):
(tmp_pathplus / "LICENSE.rst").write_clean("This is the LICENSE")
lic = License(file=tmp_pathplus / "LICENSE.rst")
assert lic.to_dict() == {"file": f"{tmp_pathplus.as_posix()}/LICENSE.rst"}
assert License.from_dict(lic.to_dict()) == lic
assert License(**lic.to_dict()) == lic
(tmp_pathplus / "LICENSE.rst").write_clean("This is the LICENSE", encoding="cp1252")
lic = License(file=tmp_pathplus / "LICENSE.rst")
assert lic.to_dict() == {"file": f"{tmp_pathplus.as_posix()}/LICENSE.rst"}
assert License.from_dict(lic.to_dict()) == lic
assert License(**lic.to_dict()) == lic
lic = License(text="This is the LICENSE")
assert lic.to_dict() == {"text": "This is the LICENSE"}
assert License.from_dict(lic.to_dict()) == lic
assert License(**lic.to_dict()) == lic
def test_to_pep621_dict(self, tmp_pathplus: PathPlus):
(tmp_pathplus / "LICENSE.rst").write_clean("This is the LICENSE")
lic = License(file=tmp_pathplus / "LICENSE.rst")
assert lic.to_pep621_dict() == {"file": f"{tmp_pathplus.as_posix()}/LICENSE.rst"}
assert License.from_dict(lic.to_pep621_dict()) == lic
(tmp_pathplus / "LICENSE.md").write_clean("This is the LICENSE")
lic = License(file=tmp_pathplus / "LICENSE.md")
assert lic.to_pep621_dict() == {"file": f"{tmp_pathplus.as_posix()}/LICENSE.md"}
assert License.from_dict(lic.to_pep621_dict()) == lic
(tmp_pathplus / "LICENSE.rst").write_clean("This is the LICENSE", encoding="cp1252")
lic = License(file=tmp_pathplus / "LICENSE.rst")
assert lic.to_pep621_dict() == {"file": f"{tmp_pathplus.as_posix()}/LICENSE.rst"}
assert License.from_dict(lic.to_pep621_dict()) == lic
lic = License(text="This is the LICENSE")
assert lic.to_pep621_dict() == {"text": "This is the LICENSE"}
assert License.from_dict(lic.to_pep621_dict()) == lic
(tmp_pathplus / "LICENSE").write_clean("This is the LICENSE")
lic = License(text="This is the LICENSE\n", file=tmp_pathplus / "LICENSE")
assert lic.to_pep621_dict() == {"file": (tmp_pathplus / "LICENSE").as_posix()}
assert License.from_dict(lic.to_pep621_dict()).resolve() == lic
|