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

Code before:
    config_file = tmp_conf_dir / (file or "dask.yaml")

    if not empty_config:
        expected_conf = {"dataframe": {"foo": "bar"}}
        config_file.parent.mkdir(parents=True, exist_ok=True)
        config_file.write_text(yaml.dump(expected_conf))
    else:
        expected_conf = dict()
        assert not config_file.exists()

    cmd = ["fizz.buzz"]

    # will default to setting key to None/null when not supplied
    if value:
        cmd.append(str(value))

    # default dask config location when file not specified or first file containing key
    if file:
        cmd.extend(["--file", str(config_file)])

    runner = CliRunner()

    if existing_key_config_file and not file:
        # Now we want to ensure the config file being used is this one b/c it already
        # has this key and hasn't explicitly provided a different file to use.
        config_file = pathlib.Path(tmp_conf_dir).joinpath("existing_conf.yaml")
        cmd_ = ["fizz.buzz", "foobar", "--file", str(config_file)]
        runner.invoke(dask.cli.config_set, cmd_, catch_exceptions=False)

        # Expected config is now just what's in this file, not the default file
        expected_conf = {"fizz": {"buzz": "foobar"}}

    result = runner.invoke(dask.cli.config_set, cmd, catch_exceptions=False)

    expected = f"Updated [fizz.buzz] to [{value}], config saved to {config_file}\n"
    assert expected == result.output

    actual_conf = yaml.safe_load(config_file.read_text())

    expected_conf.update({"fizz": {"buzz": value}})
    assert expected_conf == actual_conf


def test_config_find(tmp_conf_dir):
    runner = CliRunner()

    # no config files
    result = runner.invoke(dask.cli.config_find, ["fizz.buzz"], catch_exceptions=False)
    expected = (
        f"Unable to find [fizz.buzz] in any of the following paths:\n{tmp_conf_dir}\n"
    )

Review comment (suggestion):
```suggestion
        config_file = pathlib.Path(tmp_conf_dir) / "existing_conf.yaml"
```

Code after:
@pytest.mark.parametrize("value", ("333MiB", 2, [1, 2], {"foo": "bar"}, None))
@pytest.mark.parametrize("empty_config", (True, False))
@pytest.mark.parametrize("file", (None, "bar.yaml", "foo/bar.yaml"))
@pytest.mark.parametrize("existing_key_config_file", (True, False))
def test_config_set_value(
    tmp_conf_dir,
    value,
    empty_config,
    file,
    existing_key_config_file,
):
    # Potentially custom file location, CLI defaulting to the default dir / 'dask.yaml'
    config_file = tmp_conf_dir / (file or "dask.yaml")

    if not empty_config:
        expected_conf = {"dataframe": {"foo": "bar"}}
        config_file.parent.mkdir(parents=True, exist_ok=True)
        config_file.write_text(yaml.dump(expected_conf))
    else:
        expected_conf = dict()
        assert not config_file.exists()

    cmd = ["fizz.buzz", str(value)]

    # default dask config location when file not specified or first file containing key
    if file:
        cmd.extend(["--file", str(config_file)])

    runner = CliRunner()

    if existing_key_config_file and not file:
        # Now we want to ensure the config file being used is this one b/c it already
        # has this key and hasn't explicitly provided a different file to use.
        config_file = pathlib.Path(tmp_conf_dir) / "existing_conf.yaml"
        cmd_ = ["fizz.buzz", "foobar", "--file", str(config_file)]
        runner.invoke(dask.cli.config_set, cmd_, catch_exceptions=False)

        # Expected config is now just what's in this file, not the default file
        expected_conf = {"fizz": {"buzz": "foobar"}}

    result = runner.invoke(dask.cli.config_set, cmd, catch_exceptions=False)

    expected = f"Updated [fizz.buzz] to [{value}], config saved to {config_file}\n"
    assert expected == result.output

    actual_conf = yaml.safe_load(config_file.read_text())

    expected_conf.update({"fizz": {"buzz": value}})
    assert expected_conf == actual_conf