def test_from_file(path):
    datafile = path.with_suffix('.json')
    try:
        data = json.loads(datafile.read_text())
    except FileNotFoundError:
        cfg = None
    else:
        cfg = Configuration.parse_obj(data)
    assert Configuration.from_file(path) == cfg
def test_from_file_in_project(cfgname, cfgsrc, tmp_path):
    project_dir = tmp_path / "project"
    copytree(PROJECT_TREE, project_dir)
    cfgpath = project_dir / cfgname
    cfgpath.write_text(cfgsrc)
    assert Configuration.from_file(cfgpath) == Configuration(
        select={Check.W001, Check.W002},
        ignore={Check.W003, Check.W004},
        toplevel=["foo.py", "quux"],
        package_paths=[project_dir / 'bar'],
        src_dirs=[project_dir / 'src'],
        package_omit=["__pycache__", "test/data"],
    )
def test_from_file_bad_tool_section():
    path = DATA_DIR / 'bad-tool-sect.toml'
    with pytest.raises(UserInputError) as excinfo:
        Configuration.from_file(path)
    assert str(excinfo.value).startswith(f'{path}: ')