def test_invalid_schema(tmp_path: Path) -> None: foo_yml = tmp_path / "foo.yml" foo_yml.write_text( textwrap.dedent(""" foo: bar: 42 """)) foo_schema = schema.Schema({"foo": {"bar": str}}) with pytest.raises(tsrc.InvalidConfig) as e: tsrc.parse_config(foo_yml, foo_schema) assert isinstance(e.value.cause, schema.SchemaError)
def test_invalid_syntax(tmp_path: Path) -> None: foo_yml = tmp_path / "foo.yml" foo_yml.write_text( textwrap.dedent(""" foo: bar: baz: [ baz: 42 """)) with pytest.raises(tsrc.InvalidConfig) as e: dummy_schema = mock.Mock() tsrc.parse_config(foo_yml, dummy_schema) raised_error = e.value assert raised_error.config_path == foo_yml assert isinstance(raised_error.cause, ruamel.yaml.error.YAMLError)
def test_use_pure_python_types_when_not_roundtripping(tmp_path: Path) -> None: foo_yml = tmp_path / "foo.yml" foo_yml.write_text("foo: 42\n") foo_schema = schema.Schema({"foo": int}) parsed = tsrc.parse_config(foo_yml, foo_schema, roundtrip=False) # Usually it's bad to compare types directly, and isinstance() # should be used instead. But here we want to assert we have # a proper dict, and not an OrderedDict or a yaml's CommentedMap assert type(parsed) == type(dict()) # noqa
def test_use_pure_python_types(tmp_path: Path) -> None: """ Check that parse_config() returns pure Python dicts, not an OrderedDict or yaml's CommentedMap """ foo_yml = tmp_path / "foo.yml" foo_yml.write_text("foo: 42\n") foo_schema = schema.Schema({"foo": int}) parsed = tsrc.parse_config(foo_yml, schema=foo_schema) assert type(parsed) == type({}) # noqa
def test_roundtrip(tmp_path: Path) -> None: foo_yml = tmp_path / "foo.yml" contents = textwrap.dedent("""\ # important comment foo: 0 """) foo_yml.write_text(contents) foo_schema = schema.Schema({"foo": int}) parsed = tsrc.parse_config(foo_yml, foo_schema, roundtrip=True) parsed["foo"] = 42 tsrc.dump_config(parsed, foo_yml) actual = foo_yml.text() expected = contents.replace("0", "42") assert actual == expected
def load(manifest_path: Path) -> Manifest: gitlab_schema = {"url": str} repo_schema = schema.Use(validate_repo) group_schema = {"repos": [str], schema.Optional("includes"): [str]} manifest_schema = schema.Schema( { "repos": [repo_schema], schema.Optional("gitlab"): gitlab_schema, schema.Optional("groups"): {str: group_schema}, } ) parsed = tsrc.parse_config(manifest_path, manifest_schema) parsed = ManifestConfig(parsed) # type: ignore as_manifest_config = cast(ManifestConfig, parsed) res = Manifest() res.load(as_manifest_config) return res
def load(manifest_path: Path) -> Manifest: remote_git_server_schema = {"url": str} repo_schema = schema.Use(validate_repo) group_schema = {"repos": [str], schema.Optional("includes"): [str]} # Note: gitlab and github_enterprise_url keys are ignored, # and kept here only for backward compatibility reasons manifest_schema = schema.Schema( { "repos": [repo_schema], schema.Optional("gitlab"): remote_git_server_schema, schema.Optional("github_enterprise"): remote_git_server_schema, schema.Optional("groups"): {str: group_schema}, } ) parsed = tsrc.parse_config(manifest_path, manifest_schema) res = Manifest() res.apply_config(parsed) return res