File: test_create_output.py

package info (click to toggle)
streamlink 8.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,564 kB
  • sloc: python: 51,188; sh: 184; makefile: 152
file content (282 lines) | stat: -rw-r--r-- 8,924 bytes parent folder | download | duplicates (2)
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
import re
from pathlib import Path
from unittest.mock import Mock, call

import pytest

from streamlink.exceptions import StreamlinkDeprecationWarning
from streamlink_cli.compat import stdout
from streamlink_cli.exceptions import StreamlinkCLIError
from streamlink_cli.main import (
    Formatter,
    build_parser,
    create_output,
    setup_args,
)
from streamlink_cli.output import FileOutput, PlayerOutput


ARGS_PLAYER_ENV = "--player-env=VAR1=abc", "--player-env=VAR2=def"


@pytest.fixture(autouse=True)
def argv(argv: list):
    parser = build_parser()
    setup_args(parser)

    return argv


@pytest.fixture(autouse=True)
def _default_stream_metadata(monkeypatch: pytest.MonkeyPatch):
    monkeypatch.setattr("streamlink_cli.main.DEFAULT_STREAM_METADATA", {"title": "bar"})


@pytest.fixture()
def formatter():
    return Formatter(
        {
            "author": lambda: "foo",
        },
    )


@pytest.fixture()
def check_file_output(monkeypatch: pytest.MonkeyPatch):
    mock_check_file_output = Mock(side_effect=lambda path, skip, force: path)
    monkeypatch.setattr("streamlink_cli.main.check_file_output", mock_check_file_output)

    return mock_check_file_output


@pytest.mark.parametrize(
    ("argv", "title"),
    [
        pytest.param(
            ["--player=mpv", "--player-args=--no-border", *ARGS_PLAYER_ENV, "URL"],
            "URL",
            id="title-default",
        ),
        pytest.param(
            ["--player=mpv", "--player-args=--no-border", *ARGS_PLAYER_ENV, "--title={author} - {title}", "URL"],
            "foo - bar",
            id="title-custom",
        ),
    ],
    indirect=["argv"],
)
def test_player(argv: list, formatter: Formatter, title: str):
    output = create_output(formatter)
    assert isinstance(output, PlayerOutput)
    assert output.playerargs.path == Path("mpv")
    assert output.playerargs.args == "--no-border"
    assert output.playerargs.title == title
    assert output.env == {"VAR1": "abc", "VAR2": "def"}
    assert output.record is None


@pytest.mark.parametrize(
    ("argv", "skip", "force", "title"),
    [
        pytest.param(
            ["--record=foo", "--player=mpv", *ARGS_PLAYER_ENV, "URL"],
            False,
            False,
            "URL",
            id="title-default",
        ),
        pytest.param(
            ["--record=foo", "--player=mpv", *ARGS_PLAYER_ENV, "--title={author} - {title}", "URL"],
            False,
            False,
            "foo - bar",
            id="title-custom",
        ),
        pytest.param(
            ["--record=foo", "--force", "--player=mpv", *ARGS_PLAYER_ENV, "URL"],
            False,
            True,
            "URL",
            id="force",
        ),
        pytest.param(
            ["--record=foo", "--skip", "--player=mpv", *ARGS_PLAYER_ENV, "URL"],
            True,
            False,
            "URL",
            id="skip",
        ),
    ],
    indirect=["argv"],
)
def test_player_record(check_file_output: Mock, formatter: Formatter, argv: list, skip: bool, force: bool, title: str):
    output = create_output(formatter)
    assert check_file_output.call_args_list == [call(Path("foo"), skip, force)]
    assert isinstance(output, PlayerOutput)
    assert output.playerargs.title == title
    assert output.env == {"VAR1": "abc", "VAR2": "def"}
    assert isinstance(output.record, FileOutput)
    assert output.record.filename == Path("foo")
    assert output.record.fd is None
    assert output.record.record is None


@pytest.mark.parametrize(
    "argv",
    [pytest.param(["--record=-", "--player=mpv", *ARGS_PLAYER_ENV, "--title={author} - {title}", "URL"])],
    indirect=["argv"],
)
def test_player_record_stdout(formatter: Formatter, argv: list):
    output = create_output(formatter)
    assert type(output) is PlayerOutput
    assert output.playerargs.title == "foo - bar"
    assert output.env == {"VAR1": "abc", "VAR2": "def"}
    assert type(output.record) is FileOutput
    assert output.record.filename is None
    assert output.record.fd is stdout
    assert output.record.record is None


@pytest.mark.parametrize(
    ("argv", "skip", "force"),
    [
        pytest.param(["--output=foo"], False, False, id="default"),
        pytest.param(["--output=foo", "--force"], False, True, id="force"),
        pytest.param(["--output=foo", "--skip"], True, False, id="force"),
    ],
    indirect=["argv"],
)
def test_output(check_file_output: Mock, formatter: Formatter, argv: list, skip: bool, force: bool):
    output = create_output(formatter)
    assert check_file_output.call_args_list == [call(Path("foo"), skip, force)]
    assert isinstance(output, FileOutput)
    assert output.filename == Path("foo")
    assert output.fd is None
    assert output.record is None


@pytest.mark.parametrize(
    "argv",
    [
        pytest.param(["--stdout"], id="stdout"),
        pytest.param(["--stdout", "--record=-"], id="stdout-and-record-dash"),
        pytest.param(["--output=-"], id="output-dash"),
    ],
    indirect=["argv"],
)
def test_stdout(formatter: Formatter, argv: list):
    output = create_output(formatter)
    assert isinstance(output, FileOutput)
    assert output.filename is None
    assert output.fd is stdout
    assert output.record is None


@pytest.mark.parametrize(
    ("argv", "skip", "force", "warnings"),
    [
        pytest.param(
            ["--stdout", "--record=foo"],
            False,
            False,
            [],
            id="default",
        ),
        pytest.param(
            ["--stdout", "--record=foo", "--force"],
            False,
            True,
            [],
            id="force",
        ),
        pytest.param(
            ["--stdout", "--record=foo", "--skip"],
            True,
            False,
            [],
            id="skip",
        ),
        pytest.param(
            ["--record-and-pipe=foo"],
            False,
            False,
            [(StreamlinkDeprecationWarning, "-R/--record-and-pipe=... has been deprecated in favor of --stdout --record=...")],
            id="record-and-pipe-no-force",
        ),
        pytest.param(
            ["--record-and-pipe=foo", "--force"],
            False,
            True,
            [(StreamlinkDeprecationWarning, "-R/--record-and-pipe=... has been deprecated in favor of --stdout --record=...")],
            id="record-and-pipe-force",
        ),
        pytest.param(
            ["--record-and-pipe=foo", "--skip"],
            True,
            False,
            [(StreamlinkDeprecationWarning, "-R/--record-and-pipe=... has been deprecated in favor of --stdout --record=...")],
            id="record-and-pipe-skip",
        ),
    ],
    indirect=["argv"],
)
def test_stdout_record(
    recwarn: pytest.WarningsRecorder,
    check_file_output: Mock,
    formatter: Formatter,
    argv: list,
    skip: bool,
    force: bool,
    warnings: list,
):
    output = create_output(formatter)
    assert check_file_output.call_args_list == [call(Path("foo"), skip, force)]
    assert isinstance(output, FileOutput)
    assert output.filename is None
    assert output.fd is stdout
    assert isinstance(output.record, FileOutput)
    assert output.record.filename == Path("foo")
    assert output.record.fd is None
    assert output.record.record is None
    assert [(record.category, str(record.message)) for record in recwarn.list] == warnings


@pytest.mark.parametrize(
    ("argv", "errormsg"),
    [
        pytest.param(
            ["--output=foo", "--stdout"],
            "The -o/--output argument is incompatible with -O/--stdout",
            id="output-stdout",
        ),
        pytest.param(
            ["--output=foo", "--record=bar"],
            "The -o/--output argument is incompatible with -r/--record and -R/--record-and-pipe",
            id="output-record",
        ),
        pytest.param(
            ["--output=foo", "--record-and-pipe=bar"],
            "The -o/--output argument is incompatible with -r/--record and -R/--record-and-pipe",
            id="output-record-and-pipe",
        ),
        pytest.param(
            ["--stdout", "--record-and-pipe=bar"],
            "The -O/--stdout argument is incompatible with -R/--record-and-pipe",
            id="stdout-record-and-pipe",
        ),
    ],
    indirect=["argv"],
)
def test_incompatible_options(formatter: Formatter, argv: list, errormsg: str):
    with pytest.raises(StreamlinkCLIError) as excinfo:
        create_output(formatter)
    assert str(excinfo.value) == errormsg
    assert excinfo.value.code == 1


@pytest.mark.parametrize("argv", [pytest.param([], id="default-player")], indirect=["argv"])
def test_no_default_player(formatter: Formatter, argv: list):
    with pytest.raises(StreamlinkCLIError) as excinfo:
        create_output(formatter)
    assert re.search(r"^The default player \(\w+\) does not seem to be installed\.", str(excinfo.value))
    assert excinfo.value.code == 1