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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
|
# SPDX-FileCopyrightText: 2023 Free Software Foundation Europe e.V. <https://fsfe.org>
#
# SPDX-License-Identifier: GPL-3.0-or-later
"""Tests for REUSE.toml and .reuse/dep5."""
import shutil
from inspect import cleandoc
from pathlib import Path
import pytest
from conftest import RESOURCES_DIRECTORY, posix
from debian.copyright import Copyright
from license_expression import LicenseSymbol
from reuse import _LICENSING, ReuseInfo, SourceType
from reuse.exceptions import (
GlobalLicensingParseError,
GlobalLicensingParseTypeError,
GlobalLicensingParseValueError,
)
from reuse.global_licensing import (
AnnotationsItem,
NestedReuseTOML,
PrecedenceType,
ReuseDep5,
ReuseTOML,
)
from reuse.vcs import VCSStrategyGit
# REUSE-IgnoreStart
# pylint: disable=redefined-outer-name,too-many-lines
@pytest.fixture()
def annotations_item():
return AnnotationsItem({"foo.py"}, "override", {"2023 Jane Doe"}, {"MIT"})
class TestAnnotationsItemValidators:
"""Test the validators of AnnotationsItem."""
def test_simple(self):
"""Create an AnnotationsItem, passing all validators."""
item = AnnotationsItem(
{"foo.py"},
"override",
{"2023 Jane Doe"},
{"MIT"},
)
assert item.paths == {"foo.py"}
assert item.precedence == PrecedenceType.OVERRIDE
assert item.copyright_lines == {"2023 Jane Doe"}
assert item.spdx_expressions == {_LICENSING.parse("MIT")}
def test_precedence_defaults_to_closest(self):
"""If precedence is NOTHING, default to closest."""
item = AnnotationsItem(
{"foo.py"},
copyright_lines={"2023 Jane Doe"},
spdx_expressions={"MIT"},
)
assert item.precedence == PrecedenceType.CLOSEST
def test_from_list(self):
"""Convert lists to sets."""
item = AnnotationsItem(
["foo.py"],
"override",
["2023 Jane Doe"],
["MIT"],
)
assert item.paths == {"foo.py"}
assert item.precedence == PrecedenceType.OVERRIDE
assert item.copyright_lines == {"2023 Jane Doe"}
assert item.spdx_expressions == {_LICENSING.parse("MIT")}
def test_str_to_set(self):
"""Convert strings to sets."""
item = AnnotationsItem(
"foo.py",
"override",
"2023 Jane Doe",
"MIT",
)
assert item.paths == {"foo.py"}
assert item.precedence == PrecedenceType.OVERRIDE
assert item.copyright_lines == {"2023 Jane Doe"}
assert item.spdx_expressions == {_LICENSING.parse("MIT")}
def test_bad_expr(self):
"""Raise an error on malformed SPDX expressions."""
with pytest.raises(GlobalLicensingParseError):
AnnotationsItem(
{"foo.py"},
{"MIT OR"},
)
def test_bad_literal(self):
"""Only a limited set of literal are accepted for precedence."""
with pytest.raises(GlobalLicensingParseValueError):
AnnotationsItem(
{"foo.py"},
"foobar",
)
def test_not_str(self):
"""Copyright must be a string."""
with pytest.raises(GlobalLicensingParseTypeError):
AnnotationsItem(
{"foo.py"},
copyright_lines=123,
)
def test_not_set_of_str(self):
"""Copyright must be a set of strings."""
with pytest.raises(GlobalLicensingParseTypeError):
AnnotationsItem(
{"foo.py"},
copyright_lines={"2023 Jane Doe", 2024},
)
def test_paths_must_not_be_empty(self):
"""'paths' may not be an empty list."""
with pytest.raises(GlobalLicensingParseValueError):
AnnotationsItem(
set(),
)
def test_everything_except_path_optional(self):
"""All fields except path are optional."""
AnnotationsItem({"foo.py"})
class TestAnnotationsItemFromDict:
"""Test AnnotationsItem's from_dict method."""
def test_simple(self):
"""A simple case."""
item = AnnotationsItem.from_dict(
{
"path": {"foo.py"},
"precedence": "override",
"SPDX-FileCopyrightText": {"2023 Jane Doe"},
"SPDX-License-Identifier": {"MIT"},
}
)
assert item.paths == {"foo.py"}
assert item.precedence == PrecedenceType.OVERRIDE
assert item.copyright_lines == {"2023 Jane Doe"}
assert item.spdx_expressions == {_LICENSING.parse("MIT")}
def test_implicit_precedence(self):
"""When precedence is not defined, default to closest."""
item = AnnotationsItem.from_dict(
{
"path": {"foo.py"},
}
)
assert item.precedence == PrecedenceType.CLOSEST
def test_trigger_validators(self):
"""It's possible to trigger the validators by providing a bad value."""
with pytest.raises(GlobalLicensingParseTypeError):
AnnotationsItem.from_dict(
{
"path": {123},
}
)
def test_path_missing(self):
"""If the path key is missing, raise an error."""
with pytest.raises(GlobalLicensingParseValueError):
AnnotationsItem.from_dict(
{
"precedence": "override",
"SPDX-FileCopyrightText": {"2023 Jane Doe"},
"SPDX-License-Identifier": {"MIT"},
}
)
def test_path_none(self):
"""If the path key is None, raise an error."""
with pytest.raises(GlobalLicensingParseValueError):
AnnotationsItem.from_dict(
{
"path": None,
}
)
def test_one_key_missing(self):
"""If one REUSE info key is missing, raise no error."""
item = AnnotationsItem.from_dict(
{
"path": {"foo.py"},
"SPDX-License-Identifier": {"MIT"},
}
)
assert not item.copyright_lines
assert isinstance(item.copyright_lines, set)
def test_both_keys_missing(self):
"""If both REUSE info keys are missing, raise no error."""
item = AnnotationsItem.from_dict(
{
"path": {"foo.py"},
}
)
assert not item.copyright_lines
assert not item.spdx_expressions
class TestAnnotationsItemMatches:
"""Test AnnotationsItem's matches method."""
def test_simple(self):
"""Simple case."""
item = AnnotationsItem(paths=["foo.py"])
assert item.matches("foo.py")
assert not item.matches("src/foo.py")
assert not item.matches("bar.py")
def test_in_directory(self):
"""Correctly handle pathname separators. Looking at you, Windows."""
item = AnnotationsItem(paths=["src/foo.py"])
assert item.matches("src/foo.py")
assert not item.matches("foo.py")
def test_all_py(self):
"""Correctly find all Python files."""
item = AnnotationsItem(paths=["**/*.py"])
assert item.matches("foo.py")
assert item.matches(".foo.py")
assert item.matches("src/foo.py")
assert not item.matches("src/foo.js")
def test_only_in_dir(self):
"""Only find files in a certain directory."""
item = AnnotationsItem(paths=["src/*.py"])
assert not item.matches("foo.py")
assert item.matches("src/foo.py")
assert not item.matches("src/other/foo.py")
def test_asterisk(self):
"""Match everything in local directory."""
item = AnnotationsItem(paths=["*"])
assert item.matches("foo.py")
assert item.matches(".gitignore")
assert not item.matches("src/foo.py")
assert not item.matches(".foo/bar")
def test_asterisk_asterisk(self):
"""Match everything."""
item = AnnotationsItem(paths=["**"])
assert item.matches("foo.py")
assert item.matches(".gitignore")
assert item.matches("src/foo.py")
assert item.matches(".foo/bar")
def test_asterisk_slash(self):
"""Handle asterisk slash."""
item = AnnotationsItem(paths=[r"*/file"])
assert item.matches("foo/file")
def test_escape_asterisk(self):
"""Handle escape asterisk."""
item = AnnotationsItem(paths=[r"\*.py"])
assert item.matches("*.py")
assert not item.matches("foo.py")
def test_escape_asterisk_asterisk(self):
"""Handle escape asterisk asterisk."""
item = AnnotationsItem(paths=[r"\**.py"])
assert item.matches("*foo.py")
assert not item.matches("foo.py")
def test_escape_asterisk_escape_asterisk(self):
"""Handle escape asterisk escape asterisk."""
item = AnnotationsItem(paths=[r"\*\*.py"])
assert item.matches("**.py")
assert not item.matches("foo.py")
assert not item.matches("*foo.py")
def test_escape_asterisk_asterisk_slash_asterisk(self):
"""Handle escape asterisk asterisk slash asterisk."""
item = AnnotationsItem(paths=[r"\**/*.py"])
assert item.matches("*foo/foo.py")
assert not item.matches("bar/foo.py")
def test_escape_asterisk_escape_asterisk_slash_asterisk(self):
"""Handle escape asterisk escape asterisk slash asterisk."""
item = AnnotationsItem(paths=[r"\*\*/*.py"])
assert item.matches("**/foo.py")
assert not item.matches("bar/foo.py")
assert not item.matches("*foo/foo.py")
def test_escape_escape_asterisk(self):
"""Handle escape escape asterisk."""
item = AnnotationsItem(paths=[r"\\*.py"])
assert item.matches(r"\foo.py")
assert not item.matches(r"foo.py")
def test_asterisk_asterisk_asterisk(self):
"""Handle asterisk asterisk asterisk."""
item = AnnotationsItem(paths=[r"***.py"])
assert item.matches("foo/bar/quz.py")
def test_escape_a(self):
"""Handle escape a."""
item = AnnotationsItem(paths=[r"\a"])
assert item.matches(r"a")
assert not item.matches(r"\a")
def test_middle_asterisk(self):
"""See what happens if the asterisk is in the middle of the path."""
item = AnnotationsItem(paths=["foo*bar"])
assert item.matches("foobar")
assert item.matches("foo2bar")
assert not item.matches("foo")
assert not item.matches("bar")
assert not item.matches("foo/bar")
def test_multiple_paths(self):
"""Match one of multiple files."""
item = AnnotationsItem(paths=["*.py", "*.js", "README"])
assert item.matches("foo.py")
assert item.matches(".foo.py")
assert item.matches("foo.js")
assert item.matches("README")
assert item.matches("README.py")
assert not item.matches("README.md")
def test_match_all(self):
"""Match everything."""
item = AnnotationsItem(paths=["**"])
assert item.matches("foo.py")
assert item.matches("src/foo.py")
assert item.matches(".gitignore")
assert item.matches(".foo/bar")
class TestReuseTOMLValidators:
"""Test the validators of ReuseTOML."""
def test_simple(self, annotations_item):
"""Pass the validators"""
result = ReuseTOML(
version=1, source="REUSE.toml", annotations=[annotations_item]
)
assert result.version == 1
assert result.source == "REUSE.toml"
assert result.annotations[0] == annotations_item
def test_version_not_int(self, annotations_item):
"""Version must be an int"""
with pytest.raises(GlobalLicensingParseTypeError) as exc_info:
ReuseTOML(
version=1.2, source="REUSE.toml", annotations=[annotations_item]
)
assert exc_info.value.source == "REUSE.toml"
def test_source_not_str(self, annotations_item):
"""Source must be a str."""
with pytest.raises(GlobalLicensingParseTypeError) as exc_info:
ReuseTOML(version=1, source=123, annotations=[annotations_item])
assert exc_info.value.source == 123
def test_annotations_must_be_list(self, annotations_item):
"""Annotations must be in a list, not any other collection."""
# TODO: Technically we could change this to 'any collection that is
# ordered', but let's not split hairs.
with pytest.raises(GlobalLicensingParseTypeError) as exc_info:
ReuseTOML(
version=1,
source="REUSE.toml",
annotations=iter([annotations_item]),
)
assert exc_info.value.source == "REUSE.toml"
def test_annotations_must_be_object(self):
"""Annotations must be AnnotationsItem objects."""
with pytest.raises(GlobalLicensingParseTypeError) as exc_info:
ReuseTOML(
version=1, source="REUSE.toml", annotations=[{"foo": "bar"}]
)
assert exc_info.value.source == "REUSE.toml"
class TestReuseTOMLFromDict:
"""Test the from_dict method of ReuseTOML."""
def test_simple(self, annotations_item):
"""Simple case."""
result = ReuseTOML.from_dict(
{
"version": 1,
"annotations": [
{
"path": {"foo.py"},
"precedence": "override",
"SPDX-FileCopyrightText": {"2023 Jane Doe"},
"SPDX-License-Identifier": {"MIT"},
}
],
},
"REUSE.toml",
)
assert result.version == 1
assert result.source == "REUSE.toml"
assert result.annotations[0] == annotations_item
def test_no_annotations(self):
"""It's OK to not provide annotations."""
result = ReuseTOML.from_dict({"version": 1}, source="REUSE.toml")
assert result.annotations == []
def test_annotations_empty_list(self):
"""It's OK if annotations is an empty list."""
result = ReuseTOML.from_dict(
{"version": 1, "annotations": []}, source="REUSE.toml"
)
assert result.annotations == []
def test_no_version(self):
"""If the version is missing, raise an error."""
with pytest.raises(GlobalLicensingParseTypeError):
ReuseTOML.from_dict(
{
"annotations": [
{
"path": {"foo.py"},
"precedence": "override",
"SPDX-FileCopyrightText": {"2023 Jane Doe"},
"SPDX-License-Identifier": {"MIT"},
}
],
},
"REUSE.toml",
)
def test_annotations_error(self):
"""If there is an error in the annotations, the error get ReuseTOML's
source.
"""
with pytest.raises(GlobalLicensingParseTypeError) as exc_info:
ReuseTOML.from_dict(
{
"version": 1,
"annotations": [
{
"path": {1},
}
],
},
"REUSE.toml",
)
assert exc_info.value.source == "REUSE.toml"
class TestReuseTOMLFromToml:
"""Test the from_toml method of ReuseTOML."""
def test_simple(self, annotations_item):
"""Simple case"""
text = cleandoc(
"""
version = 1
[[annotations]]
path = "foo.py"
precedence = "override"
SPDX-FileCopyrightText = "2023 Jane Doe"
SPDX-License-Identifier = "MIT"
"""
)
result = ReuseTOML.from_toml(text, "REUSE.toml")
assert result.version == 1
assert result.source == "REUSE.toml"
assert result.annotations[0] == annotations_item
def test_syntax_error(self):
"""If there is a TOML syntax error, raise a GlobalLicensingParseError"""
with pytest.raises(GlobalLicensingParseError):
ReuseTOML.from_toml("version = 1,", "REUSE.toml")
class TestReuseTOMLEscaping:
"""Test the escaping functionality in paths in conjunction with reading from
TOML.
"""
def test_escape_asterisk(self):
"""Handle escape asterisk."""
text = cleandoc(
r"""
version = 1
[[annotations]]
path = "\\*.py"
SPDX-FileCopyrightText = "2023 Jane Doe"
SPDX-License-Identifier = "MIT"
"""
)
toml = ReuseTOML.from_toml(text, "REUSE.toml")
assert toml.reuse_info_of(r"*.py")
assert not toml.reuse_info_of(r"\*.py")
assert not toml.reuse_info_of(r"foo.py")
assert not toml.reuse_info_of(r"\foo.py")
@posix
def test_escape_escape(self):
"""Handle escape escape."""
text = cleandoc(
r"""
version = 1
[[annotations]]
path = "\\\\.py"
SPDX-FileCopyrightText = "2023 Jane Doe"
SPDX-License-Identifier = "MIT"
"""
)
toml = ReuseTOML.from_toml(text, "REUSE.toml")
assert toml.reuse_info_of(r"\.py")
class TestReuseTOMLReuseInfoOf:
"""Test the reuse_info_of method of ReuseTOML."""
def test_simple(self, annotations_item):
"""Simple test."""
reuse_toml = ReuseTOML("REUSE.toml", 1, [annotations_item])
assert reuse_toml.reuse_info_of("foo.py") == {
PrecedenceType.OVERRIDE: [
ReuseInfo(
spdx_expressions={_LICENSING.parse("MIT")},
copyright_lines={"2023 Jane Doe"},
path="foo.py",
source_path="REUSE.toml",
source_type=SourceType.REUSE_TOML,
)
]
}
def test_latest_annotations_item(self, annotations_item):
"""If two items match, use exclusively the latest."""
reuse_toml = ReuseTOML(
"REUSE.toml",
1,
[
annotations_item,
AnnotationsItem(
paths={"foo.py"},
precedence="override",
copyright_lines={"2023 John Doe"},
spdx_expressions={"0BSD"},
),
],
)
assert reuse_toml.reuse_info_of("foo.py") == {
PrecedenceType.OVERRIDE: [
ReuseInfo(
spdx_expressions={_LICENSING.parse("0BSD")},
copyright_lines={"2023 John Doe"},
path="foo.py",
source_path="REUSE.toml",
source_type=SourceType.REUSE_TOML,
)
]
}
def test_glob_all(self):
"""When globbing all, match everything."""
reuse_toml = ReuseTOML(
"REUSE.toml",
1,
[
AnnotationsItem(
paths={"**"},
precedence="override",
copyright_lines={"2023 Jane Doe"},
spdx_expressions={"MIT"},
),
],
)
# Expected sans path
expected = ReuseInfo(
spdx_expressions={_LICENSING.parse("MIT")},
copyright_lines={"2023 Jane Doe"},
source_path="REUSE.toml",
source_type=SourceType.REUSE_TOML,
)
assert reuse_toml.reuse_info_of("foo.py") == {
PrecedenceType.OVERRIDE: [expected.copy(path="foo.py")]
}
assert reuse_toml.reuse_info_of("bar.py") == {
PrecedenceType.OVERRIDE: [expected.copy(path="bar.py")]
}
assert reuse_toml.reuse_info_of("dir/subdir/foo.py") == {
PrecedenceType.OVERRIDE: [expected.copy(path="dir/subdir/foo.py")]
}
def test_glob_py(self):
"""When globbing Python paths, match only .py files."""
reuse_toml = ReuseTOML(
"REUSE.toml",
1,
[
AnnotationsItem(
paths={"**/*.py"},
precedence="override",
copyright_lines={"2023 Jane Doe"},
spdx_expressions={"MIT"},
),
],
)
assert reuse_toml.reuse_info_of("dir/foo.py") == {
PrecedenceType.OVERRIDE: [
ReuseInfo(
spdx_expressions={_LICENSING.parse("MIT")},
copyright_lines={"2023 Jane Doe"},
path="dir/foo.py",
source_path="REUSE.toml",
source_type=SourceType.REUSE_TOML,
)
]
}
assert not reuse_toml.reuse_info_of("foo.c")
class TestReuseTOMLFromFile:
"""Test the from-file method of ReuseTOML."""
def test_simple(self, annotations_item, empty_directory):
"""Simple case."""
(empty_directory / "REUSE.toml").write_text(
cleandoc(
"""
version = 1
[[annotations]]
path = "foo.py"
precedence = "override"
SPDX-FileCopyrightText = "2023 Jane Doe"
SPDX-License-Identifier = "MIT"
"""
)
)
result = ReuseTOML.from_file("REUSE.toml")
assert result.version == 1
assert result.source == "REUSE.toml"
assert result.annotations[0] == annotations_item
def test_precedence_implicit(self, empty_directory):
"""When precedence is not set, default to closest."""
(empty_directory / "REUSE.toml").write_text(
cleandoc(
"""
version = 1
[[annotations]]
path = "foo.py"
SPDX-FileCopyrightText = "2023 Jane Doe"
SPDX-License-Identifier = "MIT"
"""
)
)
result = ReuseTOML.from_file("REUSE.toml")
assert result.annotations[0].precedence == PrecedenceType.CLOSEST
class TestReuseTOMLDirectory:
"""Test the directory property of ReuseTOML."""
def test_no_parent(self):
"""Test what happens if the source has no obvious parent."""
toml = ReuseTOML(source="REUSE.toml", version=1, annotations=[])
assert toml.directory == Path(".")
def test_nested(self):
"""Correctly identify the directory of a nested file."""
toml = ReuseTOML(source="src/REUSE.toml", version=1, annotations=[])
assert toml.directory == Path("src")
class TestNestedReuseTOMLFromFile:
"""Tests for NestedReuseTOML.from_file."""
def test_simple(self, fake_repository_reuse_toml):
"""Find a single REUSE.toml."""
result = NestedReuseTOML.from_file(fake_repository_reuse_toml)
path = fake_repository_reuse_toml / "REUSE.toml"
assert result.reuse_tomls == [ReuseTOML.from_file(path)]
def test_one_deep(self, empty_directory):
"""Find a single REUSE.toml deeper in the directory tree."""
(empty_directory / "src").mkdir()
path = empty_directory / "src/REUSE.toml"
path.write_text("version = 1")
result = NestedReuseTOML.from_file(empty_directory)
assert result.reuse_tomls == [ReuseTOML.from_file(path)]
def test_multiple(self, fake_repository_reuse_toml):
"""Find multiple REUSE.tomls."""
(fake_repository_reuse_toml / "src/REUSE.toml").write_text(
"version = 1"
)
result = NestedReuseTOML.from_file(fake_repository_reuse_toml)
assert len(result.reuse_tomls) == 2
assert (
ReuseTOML.from_file(fake_repository_reuse_toml / "src/REUSE.toml")
) in result.reuse_tomls
assert (
ReuseTOML.from_file(fake_repository_reuse_toml / "REUSE.toml")
in result.reuse_tomls
)
class TestNestedReuseTOMLFindReuseTomls:
"""Tests for NestedReuseTOML.find_reuse_tomls."""
def test_simple(self, fake_repository_reuse_toml):
"""Find a single REUSE.toml."""
result = NestedReuseTOML.find_reuse_tomls(fake_repository_reuse_toml)
assert list(result) == [fake_repository_reuse_toml / "REUSE.toml"]
def test_one_deep(self, empty_directory):
"""Find a single REUSE.toml deeper in the directory tree."""
(empty_directory / "src").mkdir()
path = empty_directory / "src/REUSE.toml"
path.write_text("version = 1")
result = NestedReuseTOML.find_reuse_tomls(empty_directory)
assert list(result) == [path]
def test_multiple(self, fake_repository_reuse_toml):
"""Find multiple REUSE.tomls."""
(fake_repository_reuse_toml / "src/REUSE.toml").write_text(
"version = 1"
)
result = NestedReuseTOML.find_reuse_tomls(fake_repository_reuse_toml)
assert set(result) == {
fake_repository_reuse_toml / "REUSE.toml",
fake_repository_reuse_toml / "src/REUSE.toml",
}
def test_with_vcs_strategy(self, git_repository):
"""Ignore the correct files ignored by the repository."""
(git_repository / "REUSE.toml").write_text("version = 1")
(git_repository / "build/REUSE.toml").write_text("version =1")
(git_repository / "src/REUSE.toml").write_text("version = 1")
result = NestedReuseTOML.find_reuse_tomls(
git_repository, vcs_strategy=VCSStrategyGit(git_repository)
)
assert set(result) == {
git_repository / "REUSE.toml",
git_repository / "src/REUSE.toml",
}
def test_includes_submodule(self, submodule_repository):
"""include_submodules is correctly implemented."""
(submodule_repository / "REUSE.toml").write_text("version = 1")
(submodule_repository / "submodule/REUSE.toml").write_text(
"version = 1"
)
result_without = NestedReuseTOML.find_reuse_tomls(
submodule_repository,
vcs_strategy=VCSStrategyGit(submodule_repository),
)
assert set(result_without) == {submodule_repository / "REUSE.toml"}
result_with = NestedReuseTOML.find_reuse_tomls(
submodule_repository,
include_submodules=True,
vcs_strategy=VCSStrategyGit(submodule_repository),
)
assert set(result_with) == {
submodule_repository / "REUSE.toml",
submodule_repository / "submodule/REUSE.toml",
}
def test_includes_meson_subprojects(self, subproject_repository):
"""include_meson_subprojects is correctly implemented."""
(subproject_repository / "REUSE.toml").write_text("version = 1")
(subproject_repository / "subprojects/REUSE.toml").write_text(
"version = 1"
)
(subproject_repository / "subprojects/libfoo/REUSE.toml").write_text(
"version = 1"
)
result_without = NestedReuseTOML.find_reuse_tomls(subproject_repository)
assert set(result_without) == {
subproject_repository / "REUSE.toml",
subproject_repository / "subprojects/REUSE.toml",
}
result_with = NestedReuseTOML.find_reuse_tomls(
subproject_repository, include_meson_subprojects=True
)
assert set(result_with) == {
subproject_repository / "REUSE.toml",
subproject_repository / "subprojects/REUSE.toml",
subproject_repository / "subprojects/libfoo/REUSE.toml",
}
class TestNestedReuseTOMLReuseInfoOf:
"""Tests for NestedReuseTOML.reuse_info_of."""
def test_simple(self, annotations_item):
"""Simple case."""
reuse_toml = ReuseTOML("REUSE.toml", 1, [annotations_item])
nested_reuse_toml = NestedReuseTOML(".", [reuse_toml])
assert nested_reuse_toml.reuse_info_of("foo.py") == {
PrecedenceType.OVERRIDE: [
ReuseInfo(
spdx_expressions={_LICENSING.parse("MIT")},
copyright_lines={"2023 Jane Doe"},
path="foo.py",
source_path="REUSE.toml",
source_type=SourceType.REUSE_TOML,
)
]
}
assert not nested_reuse_toml.reuse_info_of("bar.py")
def test_no_tomls(self):
"""Don't break when there are no nested ReuseTOMLs."""
nested_reuse_toml = NestedReuseTOML(".", [])
assert not nested_reuse_toml.reuse_info_of("foo.py")
def test_skip_outer_closest(self):
"""If a precedence is set to 'closest', it is ignored unless it is the
deepest element.
"""
outer = ReuseTOML(
"REUSE.toml",
1,
[
AnnotationsItem(
"src/**",
precedence=PrecedenceType.CLOSEST,
copyright_lines={"Copyright Jane Doe"},
spdx_expressions={"MIT"},
)
],
)
inner = ReuseTOML(
"src/REUSE.toml",
1,
[
AnnotationsItem(
"foo.py",
precedence=PrecedenceType.CLOSEST,
copyright_lines={"Copyright Alice"},
spdx_expressions={"0BSD"},
)
],
)
toml = NestedReuseTOML(".", [outer, inner])
assert toml.reuse_info_of("src/foo.py") == {
PrecedenceType.CLOSEST: [
ReuseInfo(
spdx_expressions={_LICENSING.parse("0BSD")},
copyright_lines={"Copyright Alice"},
path="src/foo.py",
source_path="src/REUSE.toml",
source_type=SourceType.REUSE_TOML,
)
]
}
assert toml.reuse_info_of("src/bar.py") == {
PrecedenceType.CLOSEST: [
ReuseInfo(
spdx_expressions={_LICENSING.parse("MIT")},
copyright_lines={"Copyright Jane Doe"},
path="src/bar.py",
source_path="REUSE.toml",
source_type=SourceType.REUSE_TOML,
)
]
}
def test_aggregate(self):
"""If a precedence is set to aggregate, aggregate."""
outer = ReuseTOML(
"REUSE.toml",
1,
[
AnnotationsItem(
"src/**",
precedence=PrecedenceType.AGGREGATE,
copyright_lines={"Copyright Jane Doe"},
spdx_expressions={"MIT"},
)
],
)
inner = ReuseTOML(
"src/REUSE.toml",
1,
[
AnnotationsItem(
"foo.py",
precedence=PrecedenceType.CLOSEST,
copyright_lines={"Copyright Alice"},
spdx_expressions={"0BSD"},
)
],
)
toml = NestedReuseTOML(".", [outer, inner])
assert toml.reuse_info_of("src/foo.py") == {
PrecedenceType.AGGREGATE: [
ReuseInfo(
spdx_expressions={_LICENSING.parse("MIT")},
copyright_lines={"Copyright Jane Doe"},
path="src/foo.py",
source_path="REUSE.toml",
source_type=SourceType.REUSE_TOML,
)
],
PrecedenceType.CLOSEST: [
ReuseInfo(
spdx_expressions={_LICENSING.parse("0BSD")},
copyright_lines={"Copyright Alice"},
path="src/foo.py",
source_path="src/REUSE.toml",
source_type=SourceType.REUSE_TOML,
),
],
}
def test_toml_precedence(self):
"""If a precedence is set to toml, ignore deeper TOMLs."""
outer = ReuseTOML(
"REUSE.toml",
1,
[
AnnotationsItem(
"src/**",
precedence=PrecedenceType.OVERRIDE,
copyright_lines={"Copyright Jane Doe"},
spdx_expressions={"MIT"},
)
],
)
inner = ReuseTOML(
"src/REUSE.toml",
1,
[
AnnotationsItem(
"foo.py",
precedence=PrecedenceType.CLOSEST,
copyright_lines={"Copyright Alice"},
spdx_expressions={"0BSD"},
)
],
)
toml = NestedReuseTOML(".", [outer, inner])
assert toml.reuse_info_of("src/foo.py") == {
PrecedenceType.OVERRIDE: [
ReuseInfo(
spdx_expressions={_LICENSING.parse("MIT")},
copyright_lines={"Copyright Jane Doe"},
path="src/foo.py",
source_path="REUSE.toml",
source_type=SourceType.REUSE_TOML,
),
]
}
def test_toml_and_aggregate(self):
"""If the top TOML says aggregate and a deeper TOML has precedence toml,
aggregate accordingly.
"""
outer = ReuseTOML(
"REUSE.toml",
1,
[
AnnotationsItem(
"foo/bar/**",
precedence=PrecedenceType.AGGREGATE,
copyright_lines={"Copyright Jane Doe"},
spdx_expressions={"MIT"},
)
],
)
mid = ReuseTOML(
"foo/REUSE.toml",
1,
[
AnnotationsItem(
"bar/**",
precedence=PrecedenceType.OVERRIDE,
copyright_lines={"Copyright Alice"},
spdx_expressions={"0BSD"},
)
],
)
inner = ReuseTOML(
"foo/bar/REUSE.toml",
1,
[
AnnotationsItem(
"foo.py",
precedence=PrecedenceType.OVERRIDE,
copyright_lines={"Copyright Bob"},
spdx_expressions={"CC0-1.0"},
)
],
)
toml = NestedReuseTOML(".", [outer, mid, inner])
assert toml.reuse_info_of("foo/bar/foo.py") == {
PrecedenceType.AGGREGATE: [
ReuseInfo(
spdx_expressions={_LICENSING.parse("MIT")},
copyright_lines={"Copyright Jane Doe"},
path="foo/bar/foo.py",
source_path="REUSE.toml",
source_type=SourceType.REUSE_TOML,
),
],
PrecedenceType.OVERRIDE: [
ReuseInfo(
spdx_expressions={_LICENSING.parse("0BSD")},
copyright_lines={"Copyright Alice"},
path="foo/bar/foo.py",
source_path="foo/REUSE.toml",
source_type=SourceType.REUSE_TOML,
),
],
}
def test_dont_go_up_hierarchy(self):
"""If a deep REUSE.toml contains instructions for a dir-globbed file,
don't match against files named as such in parent directories.
"""
deep = ReuseTOML(
"src/REUSE.toml",
1,
[
AnnotationsItem(
"**/foo.py",
precedence=PrecedenceType.CLOSEST,
copyright_lines={"Copyright Alice"},
spdx_expressions={"0BSD"},
)
],
)
toml = NestedReuseTOML(".", [deep])
assert toml.reuse_info_of("src/foo.py")
assert toml.reuse_info_of("src/bar/foo.py")
assert not toml.reuse_info_of("foo.py")
assert not toml.reuse_info_of("doc/foo.py")
def test_dont_go_up_directory(self):
"""If a deep REUSE.toml contains an instruction for '../foo.py', don't
match it against anything.
"""
deep = ReuseTOML(
"src/REUSE.toml",
1,
[
AnnotationsItem(
"../foo.py",
precedence=PrecedenceType.CLOSEST,
copyright_lines={"Copyright Alice"},
spdx_expressions={"0BSD"},
)
],
)
toml = NestedReuseTOML(".", [deep])
assert not toml.reuse_info_of("src/foo.py")
assert not toml.reuse_info_of("foo.py")
def test_aggregate_incomplete_info(self):
"""If one REUSE.toml defines the copyright, and a different one contains
the licence, then both bits of information should be used.
"""
outer = ReuseTOML(
"REUSE.toml",
1,
[
AnnotationsItem(
"src/foo.txt",
precedence=PrecedenceType.CLOSEST,
spdx_expressions={"MIT"},
)
],
)
inner = ReuseTOML(
"src/REUSE.toml",
1,
[
AnnotationsItem(
"foo.txt",
precedence=PrecedenceType.CLOSEST,
copyright_lines={"Copyright Jane Doe"},
)
],
)
toml = NestedReuseTOML(".", [outer, inner])
infos = toml.reuse_info_of("src/foo.txt")[PrecedenceType.CLOSEST]
assert len(infos) == 2
class TestReuseDep5FromFile:
"""Tests for ReuseDep5.from_file."""
def test_simple(self, fake_repository_dep5):
"""No error if everything is good."""
result = ReuseDep5.from_file(fake_repository_dep5 / ".reuse/dep5")
assert result.__class__ == ReuseDep5
assert result.dep5_copyright.__class__ == Copyright
assert result.source == str(fake_repository_dep5 / ".reuse/dep5")
def test_not_exists(self, empty_directory):
"""Raise FileNotFoundError if .reuse/dep5 doesn't exist."""
with pytest.raises(FileNotFoundError):
ReuseDep5.from_file(empty_directory / "foo")
def test_unicode_decode_error(self, fake_repository_dep5):
"""Raise UnicodeDecodeError if file can't be decoded as utf-8."""
shutil.copy(
RESOURCES_DIRECTORY / "fsfe.png", fake_repository_dep5 / "fsfe.png"
)
with pytest.raises(GlobalLicensingParseError) as exc_info:
ReuseDep5.from_file(fake_repository_dep5 / "fsfe.png")
error = exc_info.value
assert error.source == str(fake_repository_dep5 / "fsfe.png")
assert "'utf-8' codec can't decode byte" in str(error)
def test_parse_error(self, empty_directory):
"""Raise GlobalLicensingParseError on parse error."""
(empty_directory / "foo").write_text("foo")
with pytest.raises(GlobalLicensingParseError) as exc_info:
ReuseDep5.from_file(empty_directory / "foo")
error = exc_info.value
assert error.source == str(empty_directory / "foo")
def test_double_copyright_parse_error(self, empty_directory):
"""Raise GlobalLicensingParseError on double Copyright lines."""
(empty_directory / "foo").write_text(
cleandoc(
"""
Format: something
Upstream-Name: example
Upstream-Contact: Jane Doe
Source: https://example.com
Files: *
Copyright: Jane Doe
Copyright: John Doe
License: MIT
"""
)
)
with pytest.raises(GlobalLicensingParseError) as exc_info:
ReuseDep5.from_file(empty_directory / "foo")
error = exc_info.value
assert error.source == str(empty_directory / "foo")
def test_reuse_dep5_reuse_info_of(reuse_dep5):
"""Verify that the glob in the dep5 file is matched."""
infos = reuse_dep5.reuse_info_of("doc/foo.rst")
assert len(infos) == 1
assert len(infos[PrecedenceType.AGGREGATE]) == 1
result = infos[PrecedenceType.AGGREGATE][0]
assert LicenseSymbol("CC0-1.0") in result.spdx_expressions
assert "2017 Jane Doe" in result.copyright_lines
# REUSE-IgnoreEnd
|