|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from somesy.core.models import LicenseEnum, Person, ProjectMetadata |
| 6 | +from somesy.pyproject.writer import Poetry, SetupTools |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +def pyproject_poetry(load_files, file_types): |
| 11 | + files = load_files([file_types.POETRY]) |
| 12 | + return files[file_types.POETRY] |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture |
| 16 | +def pyproject_poetry_file(create_files, file_types): |
| 17 | + folder = create_files([(file_types.POETRY, "pyproject.toml")]) |
| 18 | + return folder / Path("pyproject.toml") |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def pyproject_setuptools(load_files, file_types): |
| 23 | + files = load_files([file_types.SETUPTOOLS]) |
| 24 | + return files[file_types.SETUPTOOLS] |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture |
| 28 | +def pyproject_setuptools_file(create_files, file_types): |
| 29 | + folder = create_files([(file_types.SETUPTOOLS, "pyproject.toml")]) |
| 30 | + return folder / Path("pyproject.toml") |
| 31 | + |
| 32 | + |
| 33 | +def test_content_match(pyproject_poetry, pyproject_setuptools): |
| 34 | + # create a function to check both file formats |
| 35 | + def assert_content_match(pyproject_file): |
| 36 | + assert pyproject_file.name == "test-package" |
| 37 | + assert ( |
| 38 | + pyproject_file.description |
| 39 | + == "This is a test package for demonstration purposes." |
| 40 | + ) |
| 41 | + assert pyproject_file.license == "MIT" |
| 42 | + assert len(pyproject_file.authors) == 1 |
| 43 | + |
| 44 | + # assert for both formats |
| 45 | + assert_content_match(pyproject_poetry) |
| 46 | + assert_content_match(pyproject_setuptools) |
| 47 | + |
| 48 | + |
| 49 | +def test_sync(pyproject_poetry, pyproject_setuptools, somesy_input): |
| 50 | + def assert_sync(pyproject): |
| 51 | + pyproject.sync(somesy_input.project) |
| 52 | + assert pyproject.name == "testproject" |
| 53 | + assert pyproject.version == "1.0.0" |
| 54 | + |
| 55 | + assert_sync(pyproject_poetry) |
| 56 | + assert_sync(pyproject_setuptools) |
| 57 | + |
| 58 | + |
| 59 | +def test_save(tmp_path, pyproject_poetry, pyproject_setuptools): |
| 60 | + def assert_save(pyproject): |
| 61 | + custom_path = tmp_path / Path("pyproject.toml") |
| 62 | + pyproject.save(custom_path) |
| 63 | + assert custom_path.is_file() |
| 64 | + custom_path.unlink() |
| 65 | + |
| 66 | + assert_save(pyproject_poetry) |
| 67 | + assert_save(pyproject_setuptools) |
| 68 | + |
| 69 | + |
| 70 | +def test_from_to_person(person): |
| 71 | + # test for poetry |
| 72 | + assert Poetry._from_person(person) == f"{person.full_name} <{person.email}>" |
| 73 | + |
| 74 | + p = Poetry._to_person(Poetry._from_person(person)) |
| 75 | + assert p.full_name == person.full_name |
| 76 | + assert p.email == person.email |
| 77 | + |
| 78 | + # test for setuptools |
| 79 | + assert SetupTools._from_person(person) == { |
| 80 | + "name": person.full_name, |
| 81 | + "email": person.email, |
| 82 | + } |
| 83 | + |
| 84 | + p = SetupTools._to_person(SetupTools._from_person(person)) |
| 85 | + assert p.full_name == person.full_name |
| 86 | + assert p.email == person.email |
| 87 | + |
| 88 | + |
| 89 | +@pytest.mark.parametrize( |
| 90 | + "writer_class, writer_file_fixture", |
| 91 | + [(Poetry, "pyproject_poetry_file"), (SetupTools, "pyproject_setuptools_file")], |
| 92 | +) |
| 93 | +def test_person_merge_pyproject(request, writer_class, writer_file_fixture, person): |
| 94 | + # get suitable project file |
| 95 | + writer_file = request.getfixturevalue(writer_file_fixture) |
| 96 | + pj = writer_class(writer_file) |
| 97 | + # update project file with known data |
| 98 | + pm = ProjectMetadata( |
| 99 | + name="My awesome project", |
| 100 | + description="Project description", |
| 101 | + license=LicenseEnum.MIT, |
| 102 | + version="0.1.0", |
| 103 | + people=[person.model_copy(update=dict(author=True, publication_author=True))], |
| 104 | + ) |
| 105 | + pj.sync(pm) |
| 106 | + pj.save() |
| 107 | + # ---- |
| 108 | + |
| 109 | + # jane becomes john -> modified person |
| 110 | + person1b = person.model_copy( |
| 111 | + update={"given_names": "John", "author": True, "publication_author": True} |
| 112 | + ) |
| 113 | + |
| 114 | + # different Jane Doe with different orcid -> new person |
| 115 | + person2 = person.model_copy( |
| 116 | + update={ |
| 117 | + "orcid": "https://orcid.org/4321-0987-3231", |
| 118 | + "email": "i.am.jane@doe.com", |
| 119 | + "author": True, |
| 120 | + "publication_author": True, |
| 121 | + } |
| 122 | + ) |
| 123 | + # use different order, just for some difference |
| 124 | + person2.set_key_order(["given_names", "orcid", "family_names", "email"]) |
| 125 | + |
| 126 | + # listed in "arbitrary" order in somesy metadata (new person comes first) |
| 127 | + pm.people = [person2, person1b] # need to assign like that to keep _key_order |
| 128 | + pj.sync(pm) |
| 129 | + pj.save() |
| 130 | + |
| 131 | + # existing author info preserved, order not preserved because no orcid |
| 132 | + person1b_rep = writer_class._from_person(person1b) |
| 133 | + person2_rep = writer_class._from_person(person2) |
| 134 | + assert (pj.authors[0] == person1b_rep) or (pj.authors[1] == person1b_rep) |
| 135 | + assert (pj.authors[0] == person2_rep) or (pj.authors[1] == person2_rep) |
| 136 | + |
| 137 | + # new person |
| 138 | + person3 = Person( |
| 139 | + **{ |
| 140 | + "given_names": "Janice", |
| 141 | + "family_names": "Doethan", |
| 142 | + "email": "jane93@gmail.com", |
| 143 | + "author": True, |
| 144 | + "publication_author": True, |
| 145 | + } |
| 146 | + ) |
| 147 | + person3_rep = writer_class._from_person(person3) |
| 148 | + |
| 149 | + # john has a new email address |
| 150 | + person1c = person1b.model_copy(update={"email": "john.of.us@qualityland.com"}) |
| 151 | + person1c_rep = writer_class._from_person(person1c) |
| 152 | + |
| 153 | + # jane 2 is removed from authors, but added to maintainers |
| 154 | + person2.author = False |
| 155 | + person2.publication_author = False |
| 156 | + person2.maintainer = True |
| 157 | + # reflect in project metadata |
| 158 | + pm.people = [person3, person2, person1c] |
| 159 | + # sync to CFF file |
| 160 | + pj.sync(pm) |
| 161 | + pj.save() |
| 162 | + |
| 163 | + assert len(pj.maintainers) == 1 |
| 164 | + assert pj.authors[0] == person1c_rep |
| 165 | + assert pj.authors[1] == person3_rep |
0 commit comments