Repository: dask/dask
File: dask/tests/test_config.py (Python)

Code before:

            configs = collect_yaml(paths=[fn1, fn2], return_paths=return_paths)
            if return_paths:
                assert configs[0] == (pathlib.Path(fn1), a)
                assert configs[1] == (pathlib.Path(fn2), b)
            else:
                assert configs[0] == a
                assert configs[1] == b

            config = merge(*collect_yaml(paths=[fn1, fn2]))
            assert config == expected


def test_paths_containing_key():
    a = {"x": 1, "y": {"a": 1}}
    b = {"x": 2, "z": 3, "y": {"b": 2}}

    with tmpfile(extension="yaml") as fn1:
        with tmpfile(extension="yaml") as fn2:
            with open(fn1, "w") as f:
                yaml.dump(a, f)
            with open(fn2, "w") as f:
                yaml.dump(b, f)

        paths = list(paths_containing_key("y.a", paths=[fn1, fn2]))
        assert len(paths) == 1
        assert paths[0] == pathlib.Path(fn1)


def test_collect_yaml_dir():
    a = {"x": 1, "y": {"a": 1}}
    b = {"x": 2, "z": 3, "y": {"b": 2}}

    expected = {"x": 2, "y": {"a": 1, "b": 2}, "z": 3}

    with tmpfile() as dirname:
        os.mkdir(dirname)
        with open(os.path.join(dirname, "a.yaml"), mode="w") as f:
            yaml.dump(a, f)
        with open(os.path.join(dirname, "b.yaml"), mode="w") as f:
            yaml.dump(b, f)

        config = merge(*collect_yaml(paths=[dirname]))
        assert config == expected


@contextmanager
def no_read_permissions(path):
    perm_orig = stat.S_IMODE(os.stat(path).st_mode)
    perm_new = perm_orig ^ stat.S_IREAD
    try:

Review comment (suggestion):
```suggestion

        assert not list(paths_containing_key("w", paths=[fn1, fn2]))
        assert not list(paths_containing_key("x", paths=[]))
```

Code after:
            configs = list(collect_yaml(paths=[fn1, fn2], return_paths=True))
            assert configs[0] == (pathlib.Path(fn1), a)
            assert configs[1] == (pathlib.Path(fn2), b)

            # `return_paths` defaults to False
            config = merge(*collect_yaml(paths=[fn1, fn2]))
            assert config == expected


def test_paths_containing_key():
    a = {"x": 1, "y": {"a": 1}}
    b = {"x": 2, "z": 3, "y": {"b": 2}}

    with tmpfile(extension="yaml") as fn1:
        with tmpfile(extension="yaml") as fn2:
            with open(fn1, "w") as f:
                yaml.dump(a, f)
            with open(fn2, "w") as f:
                yaml.dump(b, f)

        paths = list(paths_containing_key("y.a", paths=[fn1, fn2]))
        assert paths == [pathlib.Path(fn1)]

        assert not list(paths_containing_key("w", paths=[fn1, fn2]))
        assert not list(paths_containing_key("x", paths=[]))


def test_collect_yaml_dir():
    a = {"x": 1, "y": {"a": 1}}
    b = {"x": 2, "z": 3, "y": {"b": 2}}

    expected = {"x": 2, "y": {"a": 1, "b": 2}, "z": 3}

    with tmpfile() as dirname:
        os.mkdir(dirname)
        with open(os.path.join(dirname, "a.yaml"), mode="w") as f:
            yaml.dump(a, f)
        with open(os.path.join(dirname, "b.yaml"), mode="w") as f:
            yaml.dump(b, f)

        config = merge(*collect_yaml(paths=[dirname]))
        assert config == expected


@contextmanager
def no_read_permissions(path):
    perm_orig = stat.S_IMODE(os.stat(path).st_mode)
    perm_new = perm_orig ^ stat.S_IREAD
    try:
        os.chmod(path, perm_new)
        yield