File: test_tree.py

package info (click to toggle)
datalad-next 1.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,584 kB
  • sloc: python: 23,970; makefile: 205; sh: 61
file content (980 lines) | stat: -rw-r--r-- 31,765 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
from contextlib import contextmanager
from pathlib import Path
from os import sep

import pytest
from datalad_next.tests import (
    BasicGitTestRepo,
    assert_raises,
    create_tree,
    get_deeply_nested_structure,
    skip_wo_symlink_capability,
    skip_if_on_windows,
    skip_if_root,
    ok_good_symlink,
    ok_broken_symlink,
    run_main,
)
from datalad_next.utils import chpwd
from datalad_next.uis import ui_switcher as ui

from datalad_next.datasets import Dataset

from ..tree import (
    Tree,
    TreeCommand
)

"""Tests for the ``datalad tree`` command."""


# ============================ Helper functions ===============================

@contextmanager
def ensure_no_permissions(path: Path):
    """Remove all permissions for given file/directory and restore the
    original permissions at the end"""

    # modeled after 'datalad.utils.ensure_write_permission'
    original_mode = path.stat().st_mode
    try:
        path.chmod(0o000)
        yield
    finally:
        try:
            path.chmod(original_mode)
        except FileNotFoundError:
            # ignore error if path was deleted in the context block
            pass


@pytest.fixture(scope="module")
def path_no_ds(tmp_path_factory):
    """Fixture for creating a temporary directory tree (**without** datasets)
    to be used in tests.

    Returns
    -------
    Path
        Root directory of the newly created tree
    """
    dir_tree = {
        "root": {
            ".dir3": {
                "dir3_file0": '',
                ".dir3_file1": '',
            },
            "dir0": {},
            "dir1": {
                "dir1_file0": '',
            },
            "dir2": {
                "dir2_dir0": {},
                "dir2_dir1": {
                    "dir2_dir1_file0": '',
                },
                "dir2_dir2": {
                    "dir2_dir2_file0": '',
                    "dir2_dir2_file1": '',
                },
                "dir2_file0": '',
                "dir2_file1": '',
            },
            ".file2": '',
            "file0": '',
            "file1": '',
        }
    }

    temp_dir_root = tmp_path_factory.mktemp("no-ds")
    create_tree(temp_dir_root, dir_tree)
    yield temp_dir_root


@pytest.fixture(scope="module")
def path_ds(tmp_path_factory):
    """Fixture for creating a temporary directory tree (**including** datasets)
    to be used in tests.

    Returns
    -------
    Path
        Root directory of the newly created tree
    """
    ds_tree = {
        "root": {
            "superds0": {
                "sd0_file0": "",
                "sd0_subds0": {
                    "sd0_sub0_subds0": {}
                }
            },
            "superds1": {
                "sd1_file0": "",
                "sd1_dir0": {
                    "sd1_d0_repo0": {},
                    "sd1_d0_subds0": {},
                },
                "sd1_ds0": {},  # not registered as subdataset
                "sd1_subds0": {},  # not installed (drop all)
            },
            # plain git repo (contents are defined in BasicGitTestRepo)
            "repo0": {},
            "file0": "",
        }
    }

    temp_dir_root = tmp_path_factory.mktemp('ds')
    create_tree(
        temp_dir_root,
        ds_tree,
    )

    # create datasets / repos
    root = temp_dir_root / "root"
    BasicGitTestRepo(path=root / "repo0", puke_if_exists=False)
    ckwa = dict(force=True, result_renderer="disabled")
    superds0 = Dataset(root / "superds0").create(**ckwa)
    sd0_subds0 = superds0.create("sd0_subds0", **ckwa)
    sd0_subds0.create("sd0_sub0_subds0", **ckwa)
    superds1 = Dataset(root / "superds1").create(**ckwa)
    superds1.create(Path("sd1_dir0") / "sd1_d0_subds0", **ckwa)
    Dataset(root / "superds1" / "sd1_ds0").create(**ckwa)
    BasicGitTestRepo(
        path=root / "superds1" / "sd1_dir0" / "sd1_d0_repo0",
        puke_if_exists=False)
    sd1_subds0 = superds1.create("sd1_subds0", **ckwa)
    sd1_subds0.drop(what='all', reckless='kill',
                    recursive=True, result_renderer='disabled')

    yield temp_dir_root


def get_tree_rendered_output(tree_cmd: list, exit_code: int = 0):
    """
    Run 'tree' CLI command with the given list of arguments and
    return the output of the custom results renderer, broken down into
    3 components (tree root, tree body, report line).

    Assumes command exit code 0 and no additional logging to stdout.

    Parameters
    ----------
    tree_cmd: list(str)
        'tree' command given as list of strings
    exit_code: int
        Expected exit code of command (default: 0)

    Returns
    -------
    Tuple[str, str, str]
        3-value tuple consisting of: tree root, tree body, report line
    """
    # remove any empty strings from command
    out, _ = run_main([c for c in tree_cmd if c != ''], exit_code=exit_code)

    # remove trailing newline
    lines = out.rstrip("\n").split("\n")

    root = lines[0]  # first line of tree output
    body = "\n".join(lines[1:-1])
    report = lines[-1]

    return root, body, report


@pytest.fixture(scope="class")
def inject_path(request, path_ds, path_no_ds):
    """
    Set a path fixture (root path of temp directory tree) as class attribute,
    to make it available to all tests in the class. The fixture is chosen based
    on the class' ``tree_with_ds`` attribute.
    """
    if request.cls.tree_with_ds:
        request.cls.path = path_ds
    else:
        request.cls.path = path_no_ds


def format_param_ids(val) -> str:
    """
    Helper to format pytest parameter IDs.

    If the parameter is a multiline string, we assume it is the
    parameter 'expected' (expected output of tree), and just
    give it a fixed ID (otherwise, it would be displayed in the
    parameter list as a long unreadable string).

    Parameters
    ----------
    val
        Parameter value
    """
    if isinstance(val, str) and "\n" in val:
        return "expected"


def build_param_matrix(matrix, params):
    """Turn inner dicts into lists (required by pytest parametrize)"""
    matrix_out = []
    for combination in matrix:
        matrix_out.append(
            # order of combinations does not matter
            [val for key, val in combination.items() if key in params]
        )
    return matrix_out


def pytest_generate_tests(metafunc):
    """Pytest helper to automatically configure parametrization.

    Avoids having to duplicate definition of parameter names and values
    across tests that use the same data.

    See: https://docs.pytest.org/en/7.1.x/example/parametrize.html#parametrizing-test-methods-through-per-class-configuration
    """
    if metafunc.cls and \
            hasattr(metafunc.cls, 'params') and \
            hasattr(metafunc.cls, 'MATRIX'):
        test_id = metafunc.function.__name__
        test_params_dict = metafunc.cls.params
        matrix = metafunc.cls.MATRIX
        if test_id in metafunc.cls.params:
            param_names = test_params_dict[test_id]
            metafunc.parametrize(
                param_names,
                build_param_matrix(matrix, param_names),
                ids=format_param_ids
            )

# ================================= Tests =====================================


@pytest.mark.usefixtures("inject_path")
class TestTree:
    """Base class with tests that should run for multiple Tree
    configurations.

    Configurations are defined by:

    - ``MATRIX``: dicts of pytest parameters and their values, where each dict
      corresponds to a separate parametrized test instance.
    - ``params``: a dict defining for each test method, which parameters
      will be used in that test (from the parameter names contained in
      ``MATRIX``).
    """
    __test__ = False  # tells pytest to not collect tests in this class
    tree_with_ds = False
    path = None  # will be set by the inject_* fixture to temp dir tree root

    # matrix of combinations of parameters to be tested and their
    # expected results
    MATRIX = []

    # dict specifying parameter sets for each test method
    params = {
        "test_print_tree": [
            "depth", "include_files", "include_hidden", "expected_str"
        ],
        "test_print_stats": [
            "depth", "include_files", "include_hidden", "expected_stats_str"
        ],
        "test_exhausted_levels_are_below_current_depth": [
            "depth", "include_files", "include_hidden"
        ]
    }


class TestTreeWithoutDatasets(TestTree):
    """Test directory tree without any datasets"""

    __test__ = True
    tree_with_ds = False

    MATRIX = [
    {
        "depth": 1,
        "include_files": False,
        "include_hidden": False,
        "expected_stats_str": "0 datasets, 3 directories",
        "expected_str": """
├── dir0/
├── dir1/
└── dir2/
"""
    },
    {
        "depth": 3,
        "include_files": False,
        "include_hidden": False,
        "expected_stats_str": "0 datasets, 6 directories",
        "expected_str": """
├── dir0/
├── dir1/
└── dir2/
    ├── dir2_dir0/
    ├── dir2_dir1/
    └── dir2_dir2/
"""
    },
    {
        "depth": 1,
        "include_files": True,
        "include_hidden": False,
        "expected_stats_str": "0 datasets, 3 directories, 2 files",
        "expected_str": """
├── dir0/
├── dir1/
├── dir2/
├── file0
└── file1
"""
    },
    {
        "depth": 3,
        "include_files": True,
        "include_hidden": False,
        "expected_stats_str": "0 datasets, 6 directories, 8 files",
        "expected_str": """
├── dir0/
├── dir1/
│   └── dir1_file0
├── dir2/
│   ├── dir2_dir0/
│   ├── dir2_dir1/
│   │   └── dir2_dir1_file0
│   ├── dir2_dir2/
│   │   ├── dir2_dir2_file0
│   │   └── dir2_dir2_file1
│   ├── dir2_file0
│   └── dir2_file1
├── file0
└── file1
"""
    },
    {
        "depth": 1,
        "include_files": True,
        "include_hidden": True,
        "expected_stats_str": "0 datasets, 4 directories, 3 files",
        "expected_str": """
├── .dir3/
├── .file2
├── dir0/
├── dir1/
├── dir2/
├── file0
└── file1
"""
    },
    {
        "depth": 3,
        "include_files": True,
        "include_hidden": True,
        "expected_stats_str": "0 datasets, 7 directories, 11 files",
        "expected_str": """
├── .dir3/
│   ├── .dir3_file1
│   └── dir3_file0
├── .file2
├── dir0/
├── dir1/
│   └── dir1_file0
├── dir2/
│   ├── dir2_dir0/
│   ├── dir2_dir1/
│   │   └── dir2_dir1_file0
│   ├── dir2_dir2/
│   │   ├── dir2_dir2_file0
│   │   └── dir2_dir2_file1
│   ├── dir2_file0
│   └── dir2_file1
├── file0
└── file1
"""
    },
    {
        "depth": 1,
        "include_files": False,
        "include_hidden": True,
        "expected_stats_str": "0 datasets, 4 directories",
        "expected_str": """
├── .dir3/
├── dir0/
├── dir1/
└── dir2/
"""
    },
    {
        "depth": 3,
        "include_files": False,
        "include_hidden": True,
        "expected_stats_str": "0 datasets, 7 directories",
        "expected_str": """
├── .dir3/
├── dir0/
├── dir1/
└── dir2/
    ├── dir2_dir0/
    ├── dir2_dir1/
    └── dir2_dir2/
"""
    },
    ]

    def test_print_tree(
            self, depth, include_files, include_hidden, expected_str
    ):
        root = str(self.path / "root")
        command = [
            'tree',
            root,
            '--depth', str(depth),
            '--include-hidden' if include_hidden else '',
            '--include-files' if include_files else ''
        ]
        _, actual_res, _ = get_tree_rendered_output(command)
        expected_res = expected_str.lstrip("\n")  # strip first newline
        ui.message("expected:")
        ui.message(expected_res)
        ui.message("actual:")
        ui.message(actual_res)
        assert expected_res == actual_res

    def test_print_stats(
            self, depth, include_files, include_hidden, expected_stats_str
    ):
        root = str(self.path / 'root')
        command = [
            'tree',
            root,
            '--depth', str(depth),
            '--include-hidden' if include_hidden else '',
            '--include-files' if include_files else ''
        ]
        _, _, actual_res = get_tree_rendered_output(command)
        expected_res = expected_stats_str
        assert expected_res == actual_res

    @pytest.mark.parametrize(
        "root_dir_name", ["root/", "root/.", "root/./", "root/../root"]
    )
    def test_root_path_is_normalized(self, root_dir_name):
        """
        Test that root path in the first line of string output
        is normalized path
        """
        root = str(self.path / root_dir_name)
        command = ['tree', root, '--depth', '0']
        actual, _, _ = get_tree_rendered_output(command)
        expected = str(self.path / "root")
        assert expected == actual

    def test_no_difference_if_root_path_absolute_or_relative(self):
        """Tree output should be identical whether the root directory
        is given as absolute or relative path"""
        root = str(self.path / "root")
        output_abs_path = get_tree_rendered_output(['tree', root])
        with chpwd(root):
            output_rel_path = get_tree_rendered_output(['tree', '.'])

        assert output_abs_path == output_rel_path

    def test_print_tree_depth_zero(self):
        root = str(self.path / "root")
        # including files should have no effect
        command = ['tree', root, '--depth', '0', '--include-files']
        actual = get_tree_rendered_output(command)
        expected = (root, '', '0 datasets, 0 directories, 0 files')
        assert expected == actual

    def test_exhausted_levels_are_below_current_depth(
            self, depth, include_files, include_hidden):
        """For each node, the exhausted levels reported for that node
        should be smaller or equal to the node's depth"""

        results = TreeCommand.__call__(
            self.path,
            depth=depth,
            include_files=include_files,
            include_hidden=include_hidden,
            result_renderer="disabled",
            # return only 'depth' and 'exhausted_levels' from result dicts
            result_xfm=lambda res: {k: res[k]
                                    for k in ("depth", "exhausted_levels")}
        )
        # sanity checks
        assert len(results) > 1
        assert any(res["exhausted_levels"] for res in results)

        # actual test
        assert all(level <= res["depth"]
                   for res in results
                   for level in res["exhausted_levels"])


class TestTreeWithDatasets(TestTreeWithoutDatasets):
    """Test directory tree with datasets"""

    __test__ = True
    tree_with_ds = True
    # set `include_files` and `include_hidden` to False,
    # they should be already covered in `TestTreeWithoutDatasets`
    MATRIX = [
    {
        "depth": 1,
        "include_files": False,
        "include_hidden": False,
        "expected_stats_str": "2 datasets, 1 directory",
        "expected_str": """
├── repo0/
├── [DS~0] superds0/
└── [DS~0] superds1/
""",
    },
    {
        "depth": 4,
        "include_files": False,
        "include_hidden": False,
        "expected_stats_str": "7 datasets, 3 directories",
        "expected_str": """
├── repo0/
├── [DS~0] superds0/
│   └── [DS~1] sd0_subds0/
│       └── [DS~2] sd0_sub0_subds0/
└── [DS~0] superds1/
    ├── sd1_dir0/
    │   ├── sd1_d0_repo0/
    │   └── [DS~1] sd1_d0_subds0/
    ├── [DS~0] sd1_ds0/
    └── [DS~1] (not installed) sd1_subds0/
""",
    },
    ]


class TestDatasetTree(TestTree):
    """Test dataset tree with max_dataset_depth parameter"""

    __test__ = True
    tree_with_ds = True
    MATRIX = [
    {
        "dataset_depth": 0,
        "depth": 0,
        "expected_stats_str": "3 datasets, 0 directories",
        "expected_str": """
├── [DS~0] superds0/
└── [DS~0] superds1/
    └── [DS~0] sd1_ds0/
"""
    },
    {
        "dataset_depth": 0,
        "depth": 1,
        "expected_stats_str": "3 datasets, 1 directory",
        "expected_str": """
├── [DS~0] superds0/
└── [DS~0] superds1/
    ├── sd1_dir0/
    └── [DS~0] sd1_ds0/
"""
    },
    {
        "dataset_depth": 0,
        "depth": 2,
        "expected_stats_str": "3 datasets, 2 directories",
        "expected_str": """
├── [DS~0] superds0/
└── [DS~0] superds1/
    ├── sd1_dir0/
    │   └── sd1_d0_repo0/
    └── [DS~0] sd1_ds0/
"""
    },
    {
        "dataset_depth": 1,
        "depth": 0,
        "expected_stats_str": "6 datasets, 1 directory",
        "expected_str": """
├── [DS~0] superds0/
│   └── [DS~1] sd0_subds0/
└── [DS~0] superds1/
    ├── sd1_dir0/
    │   └── [DS~1] sd1_d0_subds0/
    ├── [DS~0] sd1_ds0/
    └── [DS~1] (not installed) sd1_subds0/
"""
    },
    {
        "dataset_depth": 1,
        "depth": 2,
        "expected_stats_str": "6 datasets, 2 directories",
        "expected_str": """
├── [DS~0] superds0/
│   └── [DS~1] sd0_subds0/
└── [DS~0] superds1/
    ├── sd1_dir0/
    │   ├── sd1_d0_repo0/
    │   └── [DS~1] sd1_d0_subds0/
    ├── [DS~0] sd1_ds0/
    └── [DS~1] (not installed) sd1_subds0/
"""
    },
    {
        "dataset_depth": None,
        "depth": 0,
        "expected_stats_str": "7 datasets, 1 directory",
        "expected_str": """
├── [DS~0] superds0/
│   └── [DS~1] sd0_subds0/
│       └── [DS~2] sd0_sub0_subds0/
└── [DS~0] superds1/
    ├── sd1_dir0/
    │   └── [DS~1] sd1_d0_subds0/
    ├── [DS~0] sd1_ds0/
    └── [DS~1] (not installed) sd1_subds0/
"""
    },
    {
        "dataset_depth": None,
        "depth": 2,
        "expected_stats_str": "7 datasets, 2 directories",
        "expected_str": """
├── [DS~0] superds0/
│   └── [DS~1] sd0_subds0/
│       └── [DS~2] sd0_sub0_subds0/
└── [DS~0] superds1/
    ├── sd1_dir0/
    │   ├── sd1_d0_repo0/
    │   └── [DS~1] sd1_d0_subds0/
    ├── [DS~0] sd1_ds0/
    └── [DS~1] (not installed) sd1_subds0/
"""
    },
    ]

    params = {
        "test_print_tree": [
            "dataset_depth", "depth", "expected_str"
        ],
        "test_print_stats": [
            "dataset_depth", "depth", "expected_stats_str"
        ]
    }

    def test_print_tree(
            self, dataset_depth, depth, expected_str
    ):
        root = str(self.path / "root")

        recursive_opts = ["--recursive"]
        if dataset_depth is not None:
            recursive_opts = ['--recursion-limit', str(dataset_depth)]

        command = [
            'tree',
            root,
            '--depth', str(depth),
            *recursive_opts
        ]
        _, actual_res, _ = get_tree_rendered_output(command)
        expected_res = expected_str.lstrip("\n")  # strip first newline
        ui.message("expected:")
        ui.message(expected_res)
        ui.message("actual:")
        ui.message(actual_res)
        assert expected_res == actual_res

    def test_print_tree_without_datasets(self):
        """If there are no datasets, should only print the root"""
        root = str(self.path / "root" / "repo0")
        command = [
            'tree',
            root,
            '--depth', '10',
            '--recursive',
            '--include-files'
        ]
        _, actual_res, _ = get_tree_rendered_output(command)
        expected_res = ""
        ui.message("expected:")
        ui.message(expected_res)
        ui.message("actual:")
        ui.message(actual_res)
        assert expected_res == actual_res

    def test_print_stats(
            self, dataset_depth, depth, expected_stats_str
    ):
        root = str(self.path / "root")

        recursive_opts = ["--recursive"]
        if dataset_depth is not None:
            recursive_opts = ['--recursion-limit', str(dataset_depth)]

        command = [
            'tree',
            root,
            '--depth', str(depth),
            *recursive_opts
        ]
        _, _, actual_res = get_tree_rendered_output(command)
        expected_res = expected_stats_str
        assert expected_res == actual_res


class TestTreeFilesystemIssues:
    """Test tree with missing permissions, broken symlinks, etc."""

    def test_print_tree_fails_for_nonexistent_directory(self, tmp_path):
        """Obtain nonexistent directory by creating a temp dir and deleting it
        (may be safest method)"""
        with assert_raises(ValueError):
            Tree(tmp_path / 'nonexistent_dir', max_depth=1)

    @skip_if_root  # see https://github.com/datalad/datalad-next/issues/525
    @skip_if_on_windows
    @skip_wo_symlink_capability
    def test_print_tree_permission_denied(self, tmp_path):
        """
        - If the tree contains a directory for which the user has no
          permissions (so it would not be possible to traverse it), a message
          should be displayed next to the affected directory path
        - The rest of the tree following the forbidden directory should
          be printed as usual
        - The command should return error exit status but not crash
        """
        (tmp_path / 'z_dir' / 'subdir').mkdir(parents=True)
        forbidden_dir = tmp_path / 'a_forbidden_dir'
        forbidden_dir.mkdir(parents=True)
        # temporarily remove all permissions (octal 000)
        # restore permissions at the end, otherwise we can't delete temp dir
        with ensure_no_permissions(forbidden_dir):
            command = ['tree', str(tmp_path), '--depth', '2']
            # expect exit code 1
            _, actual, _ = get_tree_rendered_output(command, exit_code=1)
            expected = f"""
├── {forbidden_dir.name}/ [error opening dir]
└── z_dir/
    └── subdir/
""".lstrip("\n")
            ui.message("expected:")
            ui.message(expected)
            ui.message("actual:")
            ui.message(actual)
            assert expected == actual

    @skip_wo_symlink_capability
    @pytest.mark.parametrize("include_files", (True, False))
    def test_tree_with_broken_symlinks(self, tmp_path, include_files):
        """Test that broken symlinks are reported as such"""
        # prep
        dir1 = tmp_path / 'real' / 'dir1'
        file1 = tmp_path / 'real' / 'dir1' / 'file1'
        dir1.mkdir(parents=True)
        file1.touch()
        (tmp_path / 'links').mkdir()

        # create symlinks
        # 1. broken symlink pointing to non-existent target
        link_to_nonexistent = tmp_path / 'links' / '1_link_to_nonexistent'
        link_to_nonexistent.symlink_to(tmp_path / 'nonexistent')
        ok_broken_symlink(link_to_nonexistent)
        # 2. broken symlink pointing to itself
        link_to_self = tmp_path / 'links' / '2_link_to_self'
        link_to_self.symlink_to(link_to_self)
        with assert_raises((RuntimeError, OSError)):  # OSError on Windows
            # resolution should fail because of infinite loop
            link_to_self.resolve(strict=True)

        # 3. good symlink pointing to existing directory
        link_to_dir1 = tmp_path / 'links' / '3_link_to_dir1'
        link_to_dir1.symlink_to(dir1, target_is_directory=True)
        ok_good_symlink(link_to_dir1)
        # 4. good symlink pointing to existing file
        link_to_file1 = tmp_path / 'links' / '4_link_to_file1'
        link_to_file1.symlink_to(file1)
        ok_good_symlink(link_to_file1)

        # test results dict using python API
        # implicitly also tests that command yields tree without crashing
        actual = TreeCommand.__call__(
            tmp_path,
            depth=None,  # unlimited
            include_files=include_files,
            result_renderer="disabled",
            result_xfm=lambda res: (Path(res["path"]).name,
                                    res["is_broken_symlink"]),
            result_filter=lambda res: "is_broken_symlink" in res,
            return_type="list",
            on_failure="ignore"
        )

        if include_files:
            expected = [
                # (path, is_broken_symlink)
                (link_to_nonexistent.name, True),
                (link_to_self.name, True),
                (link_to_dir1.name, False),
                (link_to_file1.name, False)
            ]
        else:
            expected = [
                (link_to_dir1.name, False)
            ]
        assert set(expected) == set(actual)

    @skip_if_root  # see https://github.com/datalad/datalad-next/issues/525
    @skip_if_on_windows
    @skip_wo_symlink_capability
    @pytest.mark.parametrize("include_files", (True, False))
    def test_tree_with_broken_symlinks_to_inaccessible_targets(
            self, tmp_path, include_files):
        """Test that symlinks to targets underneath inaccessible directories
        are reported as broken, whereas symlinks to inaccessible
        file/directories themselves are not reported as broken."""
        # prep
        root = tmp_path / "root"  # tree root
        root.mkdir(parents=True)

        # create file and directory without permissions outside of tree
        # root (permissions will be removed later ad-hoc, because need to
        # create symlinks first)
        forbidden_file = tmp_path / "forbidden_file"
        forbidden_file.touch()  # permissions will be removed later ad-hoc
        forbidden_dir = tmp_path / "forbidden_dir"
        forbidden_dir.mkdir()
        file_in_forbidden_dir = forbidden_dir / "file_in_forbidden_dir"
        file_in_forbidden_dir.touch()
        dir_in_forbidden_dir = forbidden_dir / "dir_in_forbidden_dir"
        dir_in_forbidden_dir.mkdir()

        # create symlinks
        # 1. broken symlink pointing to file under inaccessible directory
        link_to_file_in_forbidden_dir = root / "1_link_to_file_in_forbidden_dir"
        link_to_file_in_forbidden_dir.symlink_to(file_in_forbidden_dir)
        with ensure_no_permissions(forbidden_dir):
            with assert_raises(PermissionError):
                # resolution should fail because of missing permissions
                link_to_file_in_forbidden_dir.resolve(strict=True)

        # 2. broken symlink pointing to directory under inaccessible directory
        link_to_dir_in_forbidden_dir = root / "2_link_to_dir_in_forbidden_dir"
        link_to_dir_in_forbidden_dir.symlink_to(dir_in_forbidden_dir)
        with ensure_no_permissions(forbidden_dir):
            with assert_raises(PermissionError):
                # resolution should fail because of missing permissions
                link_to_dir_in_forbidden_dir.resolve(strict=True)

        # 3. good symlink pointing to existing but inaccessible directory
        link_to_forbidden_dir = root / "3_link_to_forbidden_dir"
        link_to_forbidden_dir.symlink_to(forbidden_dir, target_is_directory=True)
        with ensure_no_permissions(forbidden_dir):
            ok_good_symlink(link_to_forbidden_dir)

        # 4. good symlink pointing to existing but inaccessible file
        link_to_forbidden_file = root / "4_link_to_forbidden_file"
        link_to_forbidden_file.symlink_to(forbidden_file)
        with ensure_no_permissions(forbidden_file):
            ok_good_symlink(link_to_forbidden_file)

        # temporarily remove all permissions (octal 000)
        # restore permissions at the end, otherwise we can't delete temp dir
        with ensure_no_permissions(forbidden_dir), \
                ensure_no_permissions(forbidden_file):

            # test results dict using python API
            # implicitly also tests that command yields tree without crashing
            actual = TreeCommand.__call__(
                root,
                depth=None,
                include_files=include_files,
                result_renderer="disabled",
                result_xfm=lambda res: (Path(res["path"]).name,
                                        res["is_broken_symlink"]),
                result_filter=lambda res: "is_broken_symlink" in res,
                return_type="list",
                on_failure="ignore"
            )

        if include_files:
            expected = [
                # (path, is_broken_symlink)
                (link_to_file_in_forbidden_dir.name, True),
                (link_to_dir_in_forbidden_dir.name, True),
                (link_to_forbidden_dir.name, False),
                (link_to_forbidden_file.name, False)
            ]
        else:
            expected = [
                (link_to_forbidden_dir.name, False)
            ]
        assert set(expected) == set(actual)

    @skip_wo_symlink_capability
    def test_print_tree_with_recursive_symlinks(self, tmp_path):
        """
        TODO: break down into separate tests

        - Symlinks targets are displayed in custom renderer output
        - We do not follow symlinks that point to directories underneath
          the tree root or its parent (to prevent duplicate subtrees)
        - Symlinks pointing to datasets are not considered dataset nodes
          themselves, but regular directories (to prevent duplicate counts
          of datasets)
        """
        ds = get_deeply_nested_structure(str(tmp_path / 'superds'))

        # change current dir to create symlinks with relative path
        with chpwd(ds.path):
            # create symlink to a sibling directory of the tree
            # (should be recursed into)
            (tmp_path / 'ext_dir' / 'ext_subdir').mkdir(parents=True)
            Path('link2extdir').symlink_to(Path('..') / 'ext_dir',
                                           target_is_directory=True)

            # create symlink to grandparent of the tree root (should NOT
            # be recursed into)
            Path('link2parent').symlink_to(Path('..') / '..',
                                           target_is_directory=True)

            # create symlink to subdir of the tree root at depth > max_depth
            # (should be recursed into)
            deepdir = Path('subds_modified') / 'subdir' / 'deepdir'
            deepdir.mkdir()
            (deepdir / 'subdeepdir').mkdir()
            Path('link2deepdir').symlink_to(deepdir, target_is_directory=True)

        root = ds.path
        command = ["tree", "--depth", "2", root]
        _, actual_res, counts = get_tree_rendered_output(command)
        s = sep
        expected_res = f"""
├── directory_untracked/
│   └── link2dir/ -> ..{s}subdir
├── link2deepdir/ -> subds_modified{s}subdir{s}deepdir
│   └── subdeepdir/
├── link2dir/ -> subdir
├── link2extdir/ -> ..{s}ext_dir
│   └── ext_subdir/
├── link2parent/ -> ..{s}..
├── link2subdsdir/ -> subds_modified{s}subdir
├── link2subdsroot/ -> subds_modified
├── subdir/
└── [DS~1] subds_modified/
    ├── link2superdsdir/ -> ..{s}subdir
    ├── subdir/
    └── [DS~2] subds_lvl1_modified/
""".lstrip("\n")

        # Compare with output of 'tree' command
        # ui.message(counts)
        # import subprocess
        # subprocess.run(["tree", "-dlL", "2", root])

        ui.message("expected:")
        ui.message(expected_res)
        ui.message("actual:")
        ui.message(actual_res)
        assert expected_res == actual_res