File: test_graph_parser.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 (997 lines) | stat: -rw-r--r-- 28,612 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
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
# 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/>.
"""Unit tests for the GraphParser."""

import logging
from typing import Dict, List
import pytest
from itertools import product
from pytest import param
from types import SimpleNamespace

from cylc.flow import CYLC_LOG
from cylc.flow.exceptions import GraphParseError, ParamExpandError
from cylc.flow.graph_parser import GraphParser
from cylc.flow.task_outputs import (
    TASK_OUTPUT_SUBMITTED,
    TASK_OUTPUT_STARTED,
    TASK_OUTPUT_SUCCEEDED,
    TASK_OUTPUT_FAILED
)


@pytest.mark.parametrize(
    'graph',
    [
        't1 => & t2',
        't1 => t2 &',
        '& t1 => t2',
        't1 & => t2',
        't1 => => t2'
    ]
)
def test_parse_graph_fails_null_task_name(graph):
    """Test fail null task names."""
    with pytest.raises(GraphParseError) as cm:
        GraphParser().parse_graph(graph)
        assert "Null task name in graph:" in str(cm.value)


@pytest.mark.parametrize('seq', ('&', '|', '=>'))
@pytest.mark.parametrize(
    'graph, expected_err',
    [
        [
            "{0} b",
            "Leading {0}"
        ],
        [
            "a {0}",
            "Dangling {0}"
        ],
        [
            "{0} b {0} c",
            "Leading {0}"
        ],
        [
            "a {0} b {0}",
            "Dangling {0}"
        ]
    ]
)
def test_graph_syntax_errors_2(seq, graph, expected_err):
    """Test various graph syntax errors."""
    graph = graph.format(seq)
    expected_err = expected_err.format(seq)
    with pytest.raises(GraphParseError) as cm:
        GraphParser().parse_graph(graph)
    assert (
        expected_err in str(cm.value)
    )


@pytest.mark.parametrize(
    'graph, expected_err',
    [
        (
            "a b => c",
            "Bad graph node format"
        ),
        (
            "a => b c",
            "Bad graph node format"
        ),
        (
            "!foo => bar",
            "Suicide markers must be on the right of a trigger:"
        ),
        (
            "( foo & bar => baz",
            'Mismatched parentheses in: "(foo&bar"'
        ),
        (
            "a => b & c)",
            'Mismatched parentheses in: "b&c)"'
        ),
        (
            "(a => b & c)",
            'Mismatched parentheses in: "(a"'
        ),
        (
            "(a => b[+P1]",
            'Mismatched parentheses in: "(a"'
        ),
        (
            """(a | b & c) => d
               foo => bar
               (a | b & c) => !d""",
            "can't trigger both d and !d"
        ),
        (
            "a => b | c",
            "Illegal OR on right side"
        ),
        (
            "foo && bar => baz",
            "The graph AND operator is '&'"
        ),
        (
            "foo || bar => baz",
            "The graph OR operator is '|'"
        ),
        param(
            # See https://github.com/cylc/cylc-flow/issues/5844
            "foo => bar[1649]",
            'Invalid cycle point offsets only on right',
            id='no-cycle-point-RHS'
        ),
        # See https://github.com/cylc/cylc-flow/issues/6523
        # For the next 4 tests:
        # NB "foo:succeeded" now explicit on the right (this used to test
        # inferred "foo:succeeded" from "=> foo", which we no longer infer).
        param(
            # Yes I know it's circular, but it's here to
            # demonstrate that the test below is broken:
            "foo:finished => foo:succeeded",
            'Output foo:succeeded can\'t be both required and optional',
            id='finish-implies-success-optional'
        ),
        param(
            "foo[-P1]:finish => foo:succeeded",
            'Output foo:succeeded can\'t be both required and optional',
            id='finish-implies-success-optional-offset'
        ),
        param(
            "foo[-P1]:succeeded | foo[-P1]:failed => bar",
            # order of outputs varies in the error message
            'must both be optional if both are used',
            id='succeed-or-failed-mustbe-optional'
        ),
        param(
            "foo[-P1]:succeeded? | foo[-P1]:failed? => foo:succeeded",
            'Output foo:succeeded can\'t be both required and optional',
            id='succeed-or-failed-implies-success-optional'
        ),
    ]
)
def test_graph_syntax_errors(graph, expected_err):
    """Test various graph syntax errors."""
    with pytest.raises(GraphParseError) as cm:
        GraphParser().parse_graph(graph)
    assert expected_err in str(cm.value)


def test_parse_graph_simple():
    """Test parsing graphs."""
    # added white spaces and comments to show that these change nothing

    gp = GraphParser()
    gp.parse_graph('a => b\n  \n# this is a comment\n')

    original = gp.original
    triggers = gp.triggers
    families = gp.family_map

    assert (
        original == {'a': {'': ''}, 'b': {'a:succeeded': 'a:succeeded'}}
    )

    assert (
        triggers == {
            'a': {'': ([], False)},
            'b': {'a:succeeded': (['a:succeeded'], False)}
        }
    )
    assert not families


@pytest.mark.parametrize(
    'graph, expect',
    [
        param(
            'a => b\n=> c',
            SimpleNamespace(
                original={
                    'c': {'b:succeeded': 'b:succeeded'},
                    'b': {'a:succeeded': 'a:succeeded'},
                    'a': {'': ''}
                },
                triggers={
                    'c': {'b:succeeded': (['b:succeeded'], False)},
                    'b': {'a:succeeded': (['a:succeeded'], False)},
                    'a': {'': ([], False)}
                },
                families={}
            ),
            id='line break on =>'
        ),
        param(
            'a & b\n& c',
            SimpleNamespace(
                original={'b': {'': ''}, 'a': {'': ''}, 'c': {'': ''}},
                triggers={
                    'b': {'': ([], False)},
                    'a': {'': ([], False)},
                    'c': {'': ([], False)}
                },
                families={}
            ),
            id='line break on &'
        ),
        param(
            'a | b\n| c',
            SimpleNamespace(
                original={'b': {'': ''}, 'c': {'': ''}, 'a': {'': ''}},
                triggers={
                    'b': {'': ([], False)},
                    'c': {'': ([], False)},
                    'a': {'': ([], False)}
                },
                families={}
            ),
            id='line break on |'
        )
    ]
)
def test_parse_graph_simple_with_break_line_01(graph, expect):
    """Test parsing graphs."""
    parser = GraphParser()
    parser.parse_graph(graph)
    assert parser.original == expect.original
    assert parser.triggers == expect.triggers
    assert not parser.family_map


def test_parse_graph_simple_with_break_line_02():
    """Test parsing graphs."""
    gp = GraphParser()
    gp.parse_graph(
        'a => b\n'
        '=> c =>\n'
        'd'
    )
    original = gp.original
    triggers = gp.triggers
    families = gp.family_map

    assert original['a'] == {'': ''}
    assert original['b'] == {'a:succeeded': 'a:succeeded'}
    assert original['c'] == {'b:succeeded': 'b:succeeded'}
    assert original['d'] == {'c:succeeded': 'c:succeeded'}

    assert triggers['a'] == {'': ([], False)}
    assert triggers['b'] == {'a:succeeded': (['a:succeeded'], False)}
    assert triggers['c'] == {'b:succeeded': (['b:succeeded'], False)}
    assert triggers['d'] == {'c:succeeded': (['c:succeeded'], False)}

    assert not families


def test_parse_graph_with_parameters():
    """Test parsing graphs with parameters."""
    parameterized_parser = GraphParser(
        None, ({'city': ['la_paz']}, {'city': '_%(city)s'}))
    parameterized_parser.parse_graph('a => b<city>')
    original = parameterized_parser.original
    triggers = parameterized_parser.triggers
    families = parameterized_parser.family_map
    assert (
        original == {'a': {'': ''}, 'b_la_paz': {'a:succeeded': 'a:succeeded'}}
    )
    assert (
        triggers == {
            'a': {'': ([], False)},
            'b_la_paz': {'a:succeeded': (['a:succeeded'], False)}
        }
    )
    assert not families


def test_parse_graph_with_invalid_parameters():
    """Test parsing graphs with invalid parameters."""
    parameterized_parser = GraphParser(
        None, ({'city': ['la_paz']}, {'city': '_%(city)s'}))
    with pytest.raises(ParamExpandError):
        # no state in the parameters list
        parameterized_parser.parse_graph('a => b<state>')


def test_inter_workflow_dependence_simple():
    """Test invalid inter-workflow dependence"""
    gp = GraphParser()
    gp.parse_graph(
        """
        a<WORKFLOW::TASK:fail> => b
        c<WORKFLOW::TASK> => d
        """
    )
    assert (
        gp.original ==
        {
            'a': {'': ''},
            'b': {'a:succeeded': 'a:succeeded'},
            'c': {'': ''},
            'd': {'c:succeeded': 'c:succeeded'}
        }
    )
    assert (
        gp.triggers == {
            'a': {'': ([], False)},
            'c': {'': ([], False)},
            'b': {'a:succeeded': (['a:succeeded'], False)},
            'd': {'c:succeeded': (['c:succeeded'], False)}
        }
    )
    assert (
        gp.workflow_state_polling_tasks == {
            'a': (
                'WORKFLOW', 'TASK', 'failed', '<WORKFLOW::TASK:fail>'
            ),
            # Default to "succeeded" is done in config module.
            'c': (
                'WORKFLOW', 'TASK', None, '<WORKFLOW::TASK>'
            )
        }
    )
    assert not gp.family_map


def test_line_continuation():
    """Test syntax-driven line continuation."""
    graph1 = "a => b => c"
    graph2 = """a =>
b => c"""
    graph3 = """a => b
=> c"""
    gp1 = GraphParser()
    gp1.parse_graph(graph1)
    gp2 = GraphParser()
    gp2.parse_graph(graph2)
    gp3 = GraphParser()
    gp3.parse_graph(graph3)
    res = {
        'a': {'': ([], False)},
        'c': {'b:succeeded': (['b:succeeded'], False)},
        'b': {'a:succeeded': (['a:succeeded'], False)}
    }
    assert res == gp1.triggers
    assert gp1.triggers == gp2.triggers
    assert gp1.triggers == gp3.triggers
    graph = """foo => bar
        a => b =>"""
    gp = GraphParser()
    pytest.raises(GraphParseError, gp.parse_graph, graph)
    graph = """ => a => b
        foo => bar"""
    gp = GraphParser()
    pytest.raises(GraphParseError, gp.parse_graph, graph)


@pytest.mark.parametrize(
    'graph1, graph2',
    [
        [
            "foo => bar",  # default trigger
            "foo:succeed => bar"
        ],
        [
            "foo => bar",  # default trigger
            "foo:succeeded => bar"
        ],

        [
            "foo => bar",  # repeat trigger
            """foo => bar
            foo => bar"""
        ],
        [
            "foo:finished => bar",  # finish trigger
            "(foo:succeed? | foo:fail?) => bar"
        ],
        [
            """
            bar
            foo => bar:succeed => baz  # ignore qualifier on RHS
            """,
            """
            foo => bar
            bar:succeed => baz
            """
        ],
        [
            """
            foo => bar[1649] => baz
            """,
            """
            foo => bar[1649]
            bar[1649] => baz
            """
        ],
    ]
)
def test_trigger_equivalence(graph1, graph2):
    gp1 = GraphParser()
    gp1.parse_graph(graph1)
    gp2 = GraphParser()
    gp2.parse_graph(graph2)
    assert gp1.triggers == gp2.triggers


@pytest.mark.parametrize(
    'fam_map, fam_graph, member_graph',
    [
        [
            {'FAM': ['m1', 'm2'], 'BAM': ['b1', 'b2']},
            "FAM:succeed-all => BAM",
            """(m1 & m2) => b1
            (m1 & m2) => b2"""
        ],
        [
            {'FAM': ['m1', 'm2']},
            "pre => FAM",
            """pre => m1
            pre => m2"""
        ],
        [
            {'FAM': ['m1', 'm2']},
            "FAM:succeed-all => post",
            "(m1 & m2) => post"
        ],
        [
            {'FAM': ['m1', 'm2']},
            "FAM:succeed-any => post",
            "(m1 | m2) => post",
        ],
        [
            {'FAM': ['m1', 'm2'], 'BAM': ['b1', 'b2']},
            "FAM:fail-any => BAM",
            """(m1:fail | m2:fail) => b1
            (m1:fail | m2:fail) => b2"""
        ],
        [
            {'FAM': ['m1', 'm2']},
            "FAM:finish-all => post",
            "((m1? | m1:fail?) & (m2? | m2:fail?)) => post"
        ]
    ]
)
def test_family_trigger_equivalence(fam_map, fam_graph, member_graph):
    """Test family trigger semantics."""
    gp1 = GraphParser(fam_map)
    gp1.parse_graph(fam_graph)
    gp2 = GraphParser()
    gp2.parse_graph(member_graph)
    assert gp1.triggers == gp2.triggers


def test_parameter_expand():
    """Test graph parameter expansion."""
    fam_map = {
        'FAM_m0': ['fa_m0', 'fb_m0'],
        'FAM_m1': ['fa_m1', 'fb_m1'],
    }
    params = {'m': ['0', '1'], 'n': ['0', '1']}
    templates = {'m': '_m%(m)s', 'n': '_n%(n)s'}
    gp1 = GraphParser(fam_map, (params, templates))
    gp1.parse_graph("""
        pre => foo<m,n> => bar<n>
        bar<n=0> => baz  # specific case
        bar<n-1> => bar<n>  # inter-chunk
        """)
    gp2 = GraphParser()
    gp2.parse_graph("""
        pre => foo_m0_n0 => bar_n0
        pre => foo_m0_n1 => bar_n1
        pre => foo_m1_n0 => bar_n0
        pre => foo_m1_n1 => bar_n1
        bar_n0 => baz
        bar_n0 => bar_n1
        """)
    assert gp1.triggers == gp2.triggers


def test_parameter_specific():
    """Test graph parameter expansion with a specific value."""
    params = {'i': ['0', '1'], 'j': ['0', '1', '2']}
    templates = {'i': '_i%(i)s', 'j': '_j%(j)s'}
    gp1 = GraphParser(family_map=None, parameters=(params, templates))
    gp1.parse_graph("bar<i-1,j> => baz<i,j>\nfoo<i=1,j> => qux")
    gp2 = GraphParser()
    gp2.parse_graph("""
       foo_i1_j0 => qux
       foo_i1_j1 => qux
       foo_i1_j2 => qux
       bar_i0_j0 => baz_i1_j0
       bar_i0_j1 => baz_i1_j1
       bar_i0_j2 => baz_i1_j2""")
    assert gp1.triggers == gp2.triggers


def test_parameter_offset():
    """Test graph parameter expansion with an offset."""
    params = {'i': ['0', '1'], 'j': ['0', '1', '2']}
    templates = {'i': '_i%(i)s', 'j': '_j%(j)s'}
    gp1 = GraphParser(family_map=None, parameters=(params, templates))
    gp1.parse_graph("bar<i-1,j> => baz<i,j>")
    gp2 = GraphParser()
    gp2.parse_graph("""
       bar_i0_j0 => baz_i1_j0
       bar_i0_j1 => baz_i1_j1
       bar_i0_j2 => baz_i1_j2""")
    assert gp1.triggers == gp2.triggers


def test_conditional():
    """Test generation of conditional triggers."""
    gp1 = GraphParser()
    gp1.parse_graph("(foo:start | bar) => baz")
    res = {
        'baz': {
            '(foo:started|bar:succeeded)': (
                ['foo:started', 'bar:succeeded'], False)
        },
        'foo': {
            '': ([], False)
        },
        'bar': {
            '': ([], False)
        }
    }
    assert res == gp1.triggers == res


@pytest.mark.parametrize(
    'graph',
    [
        "foo[-P1Y]<m,n> => bar",
        "foo:fail<m,n> => bar",
        "foo:fail[-P1Y] => bar",
        "foo[-P1Y]:fail<m,n> => bar",
        "foo[-P1Y]<m,n>:fail => bar",
        "foo<m,n>:fail[-P1Y] => bar",
        "foo:fail<m,n>[-P1Y] => bar",
        "<m,n>:fail[-P1Y] => bar",
        "[-P1Y]<m,n> => bar",
        "[-P1Y]<m,n>:fail => bar",
        "bar => foo:fail<m,n>[-P1Y]",
        "foo[-P1Y]baz => bar"
    ]
)
def test_bad_node_syntax(graph):
    """Test that badly formatted graph nodes are detected.

    The correct format is:
      NAME(<PARAMS>)([CYCLE-POINT-OFFSET])(:TRIGGER-TYPE)")
    """
    params = {'m': ['0', '1'], 'n': ['0', '1']}
    templates = {'m': '_m%(m)s', 'n': '_n%(n)s'}
    gp = GraphParser(parameters=(params, templates))
    with pytest.raises(GraphParseError) as cm:
        gp.parse_graph(graph)
    assert "Bad graph node format" in str(cm.value)


def test_spaces_between_tasks_fails():
    """Test that <task> <task> is rejected (i.e. no & or | in between)"""
    gp = GraphParser()
    pytest.raises(
        GraphParseError, gp.parse_graph, "foo bar=> baz")
    pytest.raises(
        GraphParseError, gp.parse_graph, "foo&bar=> ba z")
    pytest.raises(
        GraphParseError, gp.parse_graph, "foo 123=> bar")
    pytest.raises(
        GraphParseError, gp.parse_graph, "foo - 123 baz=> bar")


def test_spaces_between_parameters_fails():
    """Test that <param param> are rejected (i.e. no comma)"""
    gp = GraphParser()
    pytest.raises(
        GraphParseError, gp.parse_graph, "<foo bar> => baz")
    pytest.raises(
        GraphParseError, gp.parse_graph, "<foo=a _bar> => baz")
    pytest.raises(
        GraphParseError, gp.parse_graph, "<foo=a_ bar> => baz")


def test_spaces_between_parameters_passes():
    """Test that <param-1> works, with spaces around the -+ signs"""
    params = {'m': ['0', '1', '2']}
    templates = {'m': '_m%(m)s'}
    gp = GraphParser(parameters=(params, templates))
    gp.parse_graph("<m- 1> => <m>")
    gp.parse_graph("<m -1> => <m>")
    gp.parse_graph("<m - 1> => <m>")
    gp.parse_graph("<m+ 1> => <m>")
    gp.parse_graph("<m +1> => <m>")
    gp.parse_graph("<m + 1> => <m>")


def test_spaces_in_trigger_fails():
    """Test that 'task:a- b' are rejected"""
    gp = GraphParser()
    pytest.raises(
        GraphParseError, gp.parse_graph, "FOO:custom -trigger => baz")
    pytest.raises(
        GraphParseError, gp.parse_graph, "FOO:custom- trigger => baz")
    pytest.raises(
        GraphParseError, gp.parse_graph, "FOO:custom - trigger => baz")


def test_parameter_graph_mixing_offset_and_conditional():
    """Test for bug reported in issue #2608 on GitHub:
    https://github.com/cylc/cylc-flow/issues/2608"""
    params = {'m': ["cat", "dog"]}
    templates = {'m': '_%(m)s'}
    gp = GraphParser(parameters=(params, templates))
    gp.parse_graph("foo<m-1> & baz => foo<m>")
    triggers = {
        'foo_cat': {
            '': (
                [], False
            ),
            'baz:succeeded': (
                ['baz:succeeded'], False
            )
        },
        'foo_dog': {
            'foo_cat:succeeded': (
                ['foo_cat:succeeded'], False
            ),
            'baz:succeeded': (
                ['baz:succeeded'], False
            )
        },
        'baz': {
            '': ([], False)
        }
    }
    assert gp.triggers == triggers


def test_param_expand_graph_parser():
    """Test to validate that the graph parser removes out-of-edge nodes:
    https://github.com/cylc/cylc-flow/pull/3452#issuecomment-677165000"""
    params = {'m': ["cat"]}
    templates = {'m': '_%(m)s'}
    gp = GraphParser(parameters=(params, templates))
    gp.parse_graph("foo => bar<m-1> => baz")
    triggers = {
        'foo': {
            '': ([], False)
        }
    }
    assert gp.triggers == triggers


@pytest.mark.parametrize(
    'expect', ('&', '|', '=>')
)
def test_parse_graph_fails_with_continuation_at_last_line(expect):
    """Fails if last line contains a continuation char.
    """
    parser = GraphParser()
    with pytest.raises(GraphParseError) as raised:
        parser.parse_graph(f't1 => t2 {expect}')
    assert isinstance(raised.value, GraphParseError)
    assert f'Dangling {expect}' in raised.value.args[0]


@pytest.mark.parametrize(
    'before, after',
    product(['&', '|', '=>'], repeat=2)
)
def test_parse_graph_fails_with_too_many_continuations(before, after):
    """Fails if one line ends with continuation char and the next line
    _also_ starts with one.
    """
    parser = GraphParser()
    with pytest.raises(GraphParseError) as raised:
        parser.parse_graph(f'foo & bar {before}\n{after}baz')
    assert isinstance(raised.value, GraphParseError)
    assert 'Consecutive lines end and start' in raised.value.args[0]


def test_task_optional_outputs():
    """Test optional outputs are correctly parsed from graph.

    This checks "task_output_opt" dict which holds output optionality inferred
    from the graph. Note since https://github.com/cylc/cylc-flow/pull/6999
    we no longer infer optionality from *implicit* outputs on RHS of triggers,
    i.e. "a => b" does not imply b:succeeded is a required output.

    """
    OPTIONAL = True
    REQUIRED = False
    gp = GraphParser()
    gp.parse_graph(
        """
        a1 => b1  # does not imply b1:succeeded ...
        a2:succeed => b2:succeeded
        a3:succeed => b3:succeed

        c1? => d1?
        c2:succeed? => d2?
        c3:succeed? => d3:succeed?

        x:fail? => y

        foo:finish => bar
        """
    )
    for i in range(1, 4):
        for task in (f'a{i}', f'b{i}'):
            if task != "b1":
                assert (
                    gp.task_output_opt[(task, TASK_OUTPUT_SUCCEEDED)]
                    == (REQUIRED, False, True)
                )

        for task in (f'c{i}', f'd{i}'):
            assert (
                gp.task_output_opt[(task, TASK_OUTPUT_SUCCEEDED)]
                == (OPTIONAL, True, True)
            )

    assert (
        gp.task_output_opt[('x', TASK_OUTPUT_FAILED)]
        == (OPTIONAL, True, True)
    )

    assert (
        gp.task_output_opt[('foo', TASK_OUTPUT_SUCCEEDED)]
        == (OPTIONAL, True, True)
    )

    assert (
        gp.task_output_opt[('foo', TASK_OUTPUT_FAILED)]
        == (OPTIONAL, True, True)
    )


@pytest.mark.parametrize(
    'qual, task_output',
    [
        ('start', TASK_OUTPUT_STARTED),
        ('succeed', TASK_OUTPUT_SUCCEEDED),
        ('fail', TASK_OUTPUT_FAILED),
        ('submit', TASK_OUTPUT_SUBMITTED),
    ]
)
def test_family_optional_outputs(qual, task_output):
    """Test member output optionality inferred from family triggers."""
    fam_map = {
        'FAM': ['f1', 'f2'],
        'BAM': ['b1', 'b2'],
    }
    gp = GraphParser(fam_map)
    gp.parse_graph(
        f"""
        # required
        FAM:{qual}-all => foo
        # optional member
        f2:{task_output}?

        # required
        BAM:{qual}-any => bar
        """
    )
    # -all
    for member in ['f1', 'f2']:
        optional = (member == 'f2')
        assert gp.task_output_opt[(member, task_output)][0] == optional
    # -any
    optional = False
    for member in ['b1', 'b2']:
        assert gp.task_output_opt[(member, task_output)][0] == optional


def test_cannot_be_required():
    """Is should not allow :expired or :submit-failed to be required.

    See proposal point 4:
    https://cylc.github.io/cylc-admin/proposal-optional-output-extension.html#proposal
    """
    gp = GraphParser({})

    # outputs can be optional
    gp.parse_graph('a:expired? => b')
    gp.parse_graph('a:submit-failed? => b')

    # but cannot be required
    with pytest.raises(GraphParseError, match='must be optional'):
        gp.parse_graph('a:expired => b')
    with pytest.raises(GraphParseError, match='must be optional'):
        gp.parse_graph('a:submit-failed => b')


@pytest.mark.parametrize(
    'graph, error',
    [
        [
            """FAM:succeed-all => foo
            FAM:fail-all => foo""",
            ("must both be optional if both are used (via family trigger"
             " defaults")
        ],
        [
            """FAM:succeed-all => foo
            FAM:succeed-any? => bar""",
            ("can't default to both optional and required (via family trigger"
             " defaults)")
        ],
        [
            "FAM:blargh-all => foo",  # LHS
            "Illegal family trigger"
        ],
        [
            "foo => FAM:blargh-all",  # RHS
            "Illegal family trigger"
        ],
        [
            "FAM => foo",  # bare family on LHS
            "Family trigger required: FAM => foo"
        ],
        [
            "FAM:expire-all => foo",
            "must be optional"
        ],
    ]
)
def test_family_trigger_errors(graph, error):
    """Test errors via bad family triggers and member output optionality."""
    fam_map = {
        'FAM': ['f1', 'f2']
    }
    gp = GraphParser(fam_map)

    with pytest.raises(GraphParseError) as cm:
        gp.parse_graph(graph)
    assert error in str(cm.value)


@pytest.mark.parametrize(
    'graph, c8error',
    [
        [
            """a:x => b
            a:x? => c""",
            "Output a:x can't be both required and optional",
        ],
        [
            """a? => c
            a => b""",
            "Output a:succeeded can't be both required and optional",
        ],
        [
            """a => c
            a:fail => b""",
            ("must both be optional if both are used"),
        ],
        [
            """a:fail? => b
            a => c""",
            ("must both be optional if both are used"),
        ],
        [
            "a:finish? => b",
            "Pseudo-output a:finished can't be optional",
        ],
        [
            "a:expire => b",
            "must be optional",
        ],
    ]
)
def test_task_optional_output_errors_order(
    graph, c8error,
    caplog: pytest.LogCaptureFixture,
    monkeypatch: pytest.MonkeyPatch
):
    """Test optional output errors are raised as expected."""
    gp = GraphParser()
    with pytest.raises(GraphParseError) as cm:
        gp.parse_graph(graph)
    assert c8error in str(cm.value)

    # In Cylc 7 back compat mode these graphs should all pass with no warnings.
    monkeypatch.setattr('cylc.flow.flags.cylc7_back_compat', True)
    caplog.set_level(logging.WARNING, CYLC_LOG)
    gp = GraphParser()
    gp.parse_graph(graph)

    # No warnings logged:
    assert not caplog.messages

    # After graph parsing all Cylc 7 back compat outputs should be optional.
    # (Success outputs are set to required later, in taskdef processing.)
    for (optional, _, _) in gp.task_output_opt.values():
        assert optional


@pytest.mark.parametrize(
    'ftrig',
    GraphParser.fam_to_mem_trigger_map.keys()
)
def test_fail_family_triggers_on_tasks(ftrig):
    gp = GraphParser()
    with pytest.raises(GraphParseError) as cm:
        gp.parse_graph(f"foo:{ftrig} => bar")
        assert (
            str(cm.value).startswith(
                "family trigger on non-family namespace"
            )
        )


@pytest.mark.parametrize(
    'graph, expected_triggers',
    [
        param(
            'a => b & c',
            {'a': [''], 'b': ['a:succeeded'], 'c': ['a:succeeded']},
            id="simple"
        ),
        param(
            'a => (b & c)',
            {'a': [''], 'b': ['a:succeeded'], 'c': ['a:succeeded']},
            id="simple w/ parentheses"
        ),
        param(
            'a => (b & (c & d))',
            {
                'a': [''],
                'b': ['a:succeeded'],
                'c': ['a:succeeded'],
                'd': ['a:succeeded'],
            },
            id="more parentheses"
        ),
    ]
)
def test_RHS_AND(graph: str, expected_triggers: Dict[str, List[str]]):
    """Test '&' operator on right hand side of trigger expression."""
    gp = GraphParser()
    gp.parse_graph(graph)
    triggers = {
        task: list(trigs.keys())
        for task, trigs in gp.triggers.items()
    }
    assert triggers == expected_triggers


@pytest.mark.parametrize(
    'args, err',
    (
        # No error if offset in NON-terminal RHS:
        param((('a', 'b[-P42M]'), {}, set(), set()), None),
        # Check the left hand side if this has a non-terminal RHS:
        param(
            (('a &', 'b[-P42M]'), {}, set(), set()), 'Null task name in graph'
        ),
    ),
)
def test_proc_dep_pair(args, err):
    """
    Unit tests for _proc_dep_pair.
    """
    gp = GraphParser()
    if err:
        with pytest.raises(GraphParseError, match=err):
            gp._proc_dep_pair(*args)
    else:
        assert gp._proc_dep_pair(*args) is None