File: test_list.py

package info (click to toggle)
cylc-flow 8.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,368 kB
  • sloc: python: 87,751; sh: 17,109; sql: 233; xml: 171; javascript: 78; lisp: 55; makefile: 11
file content (338 lines) | stat: -rw-r--r-- 9,078 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
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
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
# Copyright (C) NIWA & British Crown (Met Office) & Contributors.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""Test the "cylc list" command."""

import pytest

from cylc.flow.exceptions import InputError
from cylc.flow.option_parsers import Options
from cylc.flow.scripts.list import (
    get_option_parser,
    _main,
)


ListOptions = Options(get_option_parser())


@pytest.fixture(scope='module')
async def cylc_list(mod_flow, mod_scheduler, mod_start):
    id_ = mod_flow(
        {
            'scheduling': {
                # NOTE: all "a*" tasks are in the graph, but not "b*" tasks
                'initial cycle point': 1,
                'cycling mode': 'integer',
                'graph': {'P1': 'a12 & a111 & a112 & a121 & a2'},
            },
            'runtime': {
                'A': {'meta': {'title': 'Title For A'}},
                'A1': {'inherit': 'A'},
                'A11': {'inherit': 'A1', 'meta': {'title': 'Title For A11'}},
                'A12': {'inherit': 'A1'},
                'a2': {'inherit': 'A'},
                'a12': {'inherit': 'A1'},
                'a111': {'inherit': 'A11'},
                'a112': {'inherit': 'A11'},
                'a121': {'inherit': 'A12'},
                'B': {},
                'b1': {'inherit': 'B'},
            },
        }
    )
    schd = mod_scheduler(id_)

    async def _list(capsys, **kwargs):
        capsys.readouterr()
        await _main(ListOptions(**kwargs), schd.workflow)
        out, err = capsys.readouterr()
        return out.splitlines()

    async with mod_start(schd):
        yield _list


@pytest.fixture
def supports_utf8(monkeypatch):
    monkeypatch.setenv('LANG', 'C.utf-8')


@pytest.fixture
def does_not_support_utf8(monkeypatch):
    monkeypatch.setenv('LANG', 'C')


async def test_plain(cylc_list, supports_utf8, capsys):
    """Test the default output format."""
    assert await cylc_list(capsys) == [
        'a111',
        'a112',
        'a12',
        'a121',
        'a2',
    ]

    assert await cylc_list(capsys, all_tasks=True) == [
        'a111',
        'a112',
        'a12',
        'a121',
        'a2',
        'b1',  # <= in the runtime but not in the graph
    ]

    assert await cylc_list(capsys, all_namespaces=True) == [
        'A',
        'A1',
        'A11',
        'A12',
        'B',
        'a111',
        'a112',
        'a12',
        'a121',
        'a2',
        'b1',
        'root',
    ]

    with pytest.raises(InputError):
        await cylc_list(capsys, all_tasks=True, all_namespaces=True)


async def test_mro(cylc_list, supports_utf8, capsys):
    """Test the --mro (method resolution order) option."""
    assert await cylc_list(capsys, mro=True) == [
        'a111  a111 A11 A1 A root',
        'a112  a112 A11 A1 A root',
        'a12   a12 A1 A root',
        'a121  a121 A12 A1 A root',
        'a2    a2 A root',
    ]

    assert await cylc_list(capsys, mro=True, all_tasks=True) == [
        'a111  a111 A11 A1 A root',
        'a112  a112 A11 A1 A root',
        'a12   a12 A1 A root',
        'a121  a121 A12 A1 A root',
        'a2    a2 A root',
        'b1    b1 B root',
    ]

    assert await cylc_list(capsys, mro=True, all_namespaces=True) == [
        'A     A root',
        'A1    A1 A root',
        'A11   A11 A1 A root',
        'A12   A12 A1 A root',
        'B     B root',
        'a111  a111 A11 A1 A root',
        'a112  a112 A11 A1 A root',
        'a12   a12 A1 A root',
        'a121  a121 A12 A1 A root',
        'a2    a2 A root',
        'b1    b1 B root',
        'root  root',
    ]

    with pytest.raises(InputError):
        await cylc_list(capsys, titles=True, mro=True)


async def test_tree(cylc_list, supports_utf8, capsys):
    """Test the --tree option."""
    assert (
        await cylc_list(capsys, tree=True)
        # NOTE: the --all-tasks and --all-namespaces opts should be ignored
        == await cylc_list(capsys, tree=True, all_tasks=True)
        == await cylc_list(capsys, tree=True, all_namespaces=True)
        == [
            'root ',
            ' `-A ',
            '   |-A1 ',
            '   | |-A11 ',
            '   | | |-a111 ',
            '   | | `-a112 ',
            '   | |-A12 ',
            '   | | `-a121 ',
            '   | `-a12 ',
            '   `-a2 ',
        ]
    )

    assert (
        await cylc_list(capsys, tree=True, box=True)
        == await cylc_list(capsys, box=True)  # --tree implicit with --box
        == [
            'root ',
            ' └─A ',
            '   ├─A1 ',
            '   │ ├─A11 ',
            '   │ │ ├─a111 ',
            '   │ │ └─a112 ',
            '   │ ├─A12 ',
            '   │ │ └─a121 ',
            '   │ └─a12 ',
            '   └─a2 ',
        ]
    )

    assert await cylc_list(capsys, tree=True, titles=True) == [
        'root          ',
        ' `-A          ',
        '   |-A1       ',
        '   | |-A11    ',
        '   | | |-a111 Title For A11',
        '   | | `-a112 Title For A11',
        '   | |-A12    ',
        '   | | `-a121 Title For A',
        '   | `-a12    Title For A',
        '   `-a2       Title For A',
    ]

    assert await cylc_list(capsys, tree=True, box=True, titles=True) == [
        'root          ',
        ' └─A          ',
        '   ├─A1       ',
        '   │ ├─A11    ',
        '   │ │ ├─a111 Title For A11',
        '   │ │ └─a112 Title For A11',
        '   │ ├─A12    ',
        '   │ │ └─a121 Title For A',
        '   │ └─a12    Title For A',
        '   └─a2       Title For A',
    ]


async def test_box_with_lang_c(cylc_list, does_not_support_utf8, capsys):
    """It falls back to plain output if unicode support isn't there."""
    assert await cylc_list(capsys, tree=True, box=True) == [
        'a111',
        'a112',
        'a12',
        'a121',
        'a2',
    ]


async def test_titles(cylc_list, supports_utf8, capsys):
    """Test the --titles option."""
    assert await cylc_list(capsys, titles=True) == [
        'a111  Title For A11',
        'a112  Title For A11',
        'a12   Title For A',
        'a121  Title For A',
        'a2    Title For A',
    ]

    assert await cylc_list(capsys, titles=True, all_tasks=True) == [
        'a111  Title For A11',
        'a112  Title For A11',
        'a12   Title For A',
        'a121  Title For A',
        'a2    Title For A',
        'b1    ',
    ]

    assert await cylc_list(capsys, titles=True, all_namespaces=True) == [
        'A     Title For A',
        'A1    Title For A',
        'A11   Title For A11',
        'A12   Title For A',
        'B     ',
        'a111  Title For A11',
        'a112  Title For A11',
        'a12   Title For A',
        'a121  Title For A',
        'a2    Title For A',
        'b1    ',
        'root  ',
    ]


async def test_points(cylc_list, supports_utf8, capsys):
    """Test the --points option."""
    # specify start and stop points
    assert await cylc_list(capsys, prange='1,2') == [
        '1/a111',
        '1/a112',
        '1/a12',
        '1/a121',
        '1/a2',
        '2/a111',
        '2/a112',
        '2/a12',
        '2/a121',
        '2/a2',
    ]

    # leave start and stop points implicit
    assert await cylc_list(capsys, prange=',') == [
        '1/a111',
        '1/a112',
        '1/a12',
        '1/a121',
        '1/a2',
        '2/a111',
        '2/a112',
        '2/a12',
        '2/a121',
        '2/a2',
        '3/a111',
        '3/a112',
        '3/a12',
        '3/a121',
        '3/a2',
    ]

    # leave start point implicit
    assert await cylc_list(capsys, prange=',2') == [
        '1/a111',
        '1/a112',
        '1/a12',
        '1/a121',
        '1/a2',
        '2/a111',
        '2/a112',
        '2/a12',
        '2/a121',
        '2/a2',
    ]

    # leave stop point implicit
    assert await cylc_list(capsys, prange='4,') == [
        '4/a111',
        '4/a112',
        '4/a12',
        '4/a121',
        '4/a2',
        '5/a111',
        '5/a112',
        '5/a12',
        '5/a121',
        '5/a2',
        '6/a111',
        '6/a112',
        '6/a12',
        '6/a121',
        '6/a2',
    ]

    with pytest.raises(InputError):
        await cylc_list(capsys, prange='1,2', all_tasks=True)

    with pytest.raises(InputError):
        await cylc_list(capsys, prange='1,2', all_namespaces=True)