File: test_input_author.py

package info (click to toggle)
python-briefcase 0.3.25-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,596 kB
  • sloc: python: 62,519; makefile: 60
file content (226 lines) | stat: -rw-r--r-- 8,483 bytes parent folder | download
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
from unittest.mock import MagicMock

import pytest

from ...utils import NoMatchString, PartialMatchString


def test_multiple_pep621_authors(convert_command, monkeypatch):
    """All authors are added to the options in addition to an option for Other."""
    mock_selection_question = MagicMock()
    mock_selection_question.return_value = "Firstname Firstauthor"
    monkeypatch.setattr(
        convert_command.console, "selection_question", mock_selection_question
    )

    (convert_command.base_path / "pyproject.toml").write_text(
        "[project]\n"
        "authors = [\n"
        '    {name="Firstname Firstauthor", email="mail1@tld.com"},\n'
        '    {email="mail2@tld.com"},\n'
        '    {name="Name Thirdauthor", email="mail3@tld.com"},\n'
        '    {name="Firstname Fourthauthor", email="mail4@tld.com"},\n'
        "]",
        encoding="utf-8",
    )
    convert_command.input_author(None)

    mock_selection_question.assert_called_once_with(
        intro=PartialMatchString(
            "We found these author names in the PEP621 formatted 'pyproject.toml'."
        ),
        description="Author",
        options=[
            "Firstname Firstauthor",
            "Name Thirdauthor",
            "Firstname Fourthauthor",
            "Firstname Firstauthor, Name Thirdauthor & Firstname Fourthauthor",
            "Other",
        ],
        default="Firstname Firstauthor",
        override_value=None,
    )


def test_single_pep621_author(convert_command, monkeypatch):
    """If there is only one author, then you don't get the joined authors-option."""
    mock_selection_question = MagicMock()
    mock_selection_question.return_value = "Firstname Firstauthor"
    monkeypatch.setattr(
        convert_command.console, "selection_question", mock_selection_question
    )

    (convert_command.base_path / "pyproject.toml").write_text(
        "[project]\n"
        "authors = [\n"
        '    {name="Firstname Firstauthor", email="mail1@tld.com"},\n'
        "]",
        encoding="utf-8",
    )
    convert_command.input_author(None)

    mock_selection_question.assert_called_once_with(
        intro=PartialMatchString(
            "We found these author names in the PEP621 formatted 'pyproject.toml'."
        ),
        description="Author",
        options=[
            "Firstname Firstauthor",
            "Other",
        ],
        default="Firstname Firstauthor",
        override_value=None,
    )


def test_multiple_pep621_authors_select_other(convert_command, monkeypatch):
    """If you select "Other", then you can type in a name."""
    mock_selection_question = MagicMock()
    mock_selection_question.return_value = "Other"
    monkeypatch.setattr(
        convert_command.console, "selection_question", mock_selection_question
    )
    mock_text_question = MagicMock()
    mock_text_question.return_value = "Some Name"
    monkeypatch.setattr(convert_command.console, "text_question", mock_text_question)

    (convert_command.base_path / "pyproject.toml").write_text(
        "[project]\n"
        "authors = [\n"
        '    {name="Firstname Firstauthor", email="mail1@tld.com"},\n'
        '    {email="mail2@tld.com"},\n'
        '    {name="Name Thirdauthor", email="mail3@tld.com"},\n'
        '    {name="Firstname Fourthauthor", email="mail4@tld.com"},\n'
        "]",
        encoding="utf-8",
    )
    assert convert_command.input_author(None) == "Some Name"
    mock_selection_question.assert_called_once_with(
        intro=PartialMatchString(
            "We found these author names in the PEP621 formatted 'pyproject.toml'."
        ),
        description="Author",
        options=[
            "Firstname Firstauthor",
            "Name Thirdauthor",
            "Firstname Fourthauthor",
            "Firstname Firstauthor, Name Thirdauthor & Firstname Fourthauthor",
            "Other",
        ],
        default="Firstname Firstauthor",
        override_value=None,
    )
    mock_text_question.assert_called_once_with(
        intro="Who do you want to be credited as the author of this application?",
        description="Author",
        default="Jane Developer",
        override_value=None,
    )


@pytest.mark.parametrize("write_empty_pyproject", [True, False])
def test_no_pep621_author(convert_command, monkeypatch, write_empty_pyproject):
    """If there is no author names in the pyproject.toml, then you're asked to write the
    name."""
    mock_text_question = MagicMock()
    monkeypatch.setattr(convert_command.console, "text_question", mock_text_question)

    if write_empty_pyproject:
        (convert_command.base_path / "pyproject.toml").write_text(
            '[project]\nauthors = [\n    {email="mail1@tld.com"},\n]',
            encoding="utf-8",
        )
    convert_command.input_author(None)
    mock_text_question.assert_called_once_with(
        intro=NoMatchString(
            "We found these author names in the PEP621 formatted pyproject.toml."
        ),
        description="Author",
        default="Jane Developer",
        override_value=None,
    )


def test_override(convert_command):
    (convert_command.base_path / "pyproject.toml").write_text(
        "[project]\n"
        "authors = [\n"
        '    {name="Firstname Firstauthor", email="mail1@tld.com"},\n'
        '    {email="mail2@tld.com"},\n'
        '    {name="Name Thirdauthor", email="mail3@tld.com"},\n'
        '    {name="Firstname Fourthauthor", email="mail4@tld.com"},\n'
        "]",
        encoding="utf-8",
    )
    assert convert_command.input_author("Some Developer") == "Some Developer"


def test_prompted_author_without_pyproject(convert_command):
    """The user is prompted for an author."""
    convert_command.console.values = ["Some author"]
    assert convert_command.input_author(None) == "Some author"


def test_prompted_author_with_pyproject(convert_command):
    (convert_command.base_path / "pyproject.toml").write_text(
        "[project]\n"
        "authors = [\n"
        '    {name="Firstname Firstauthor", email="mail1@tld.com"},\n'
        '    {email="mail2@tld.com"},\n'
        '    {name="Name Thirdauthor", email="mail3@tld.com"},\n'
        '    {name="Firstname Fourthauthor", email="mail4@tld.com"},\n'
        "]",
        encoding="utf-8",
    )
    convert_command.console.values = ["2"]
    assert convert_command.input_author(None) == "Name Thirdauthor"


def test_prompted_author_with_pyproject_joined_author(convert_command):
    """You can select all authors joined together."""
    (convert_command.base_path / "pyproject.toml").write_text(
        "[project]\n"
        "authors = [\n"
        '    {name="Firstname Firstauthor", email="mail1@tld.com"},\n'
        '    {email="mail2@tld.com"},\n'
        '    {name="Name Thirdauthor", email="mail3@tld.com"},\n'
        '    {name="Firstname Fourthauthor", email="mail4@tld.com"},\n'
        "]",
        encoding="utf-8",
    )
    convert_command.console.values = ["4"]
    assert (
        convert_command.input_author(None)
        == "Firstname Firstauthor, Name Thirdauthor & Firstname Fourthauthor"
    )


def test_prompted_author_with_pyproject_other(convert_command):
    """If you select "Other", then you can type in a name."""
    (convert_command.base_path / "pyproject.toml").write_text(
        "[project]\n"
        "authors = [\n"
        '    {name="Firstname Firstauthor", email="mail1@tld.com"},\n'
        '    {email="mail2@tld.com"},\n'
        '    {name="Name Thirdauthor", email="mail3@tld.com"},\n'
        '    {name="Firstname Fourthauthor", email="mail4@tld.com"},\n'
        "]",
        encoding="utf-8",
    )
    convert_command.console.values = ["5", "Some Author"]
    assert convert_command.input_author(None) == "Some Author"


def test_default_author_from_git_config(convert_command, monkeypatch, capsys):
    """If git integration is configured, and a config value 'user.name' is available,
    use that value as default."""
    convert_command.tools.git = object()
    convert_command.get_git_config_value = MagicMock(return_value="Some Author")
    convert_command.console.values = [""]

    assert convert_command.input_author(None) == "Some Author"
    convert_command.get_git_config_value.assert_called_once_with("user", "name")

    # RichConsole wraps long lines, so we have to unwrap before we check
    stdout = capsys.readouterr().out.replace("\n", " ")
    assert stdout == PartialMatchString("Based on your git configuration")