1#
2# Copyright (C) 2023 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16"""Tests for fakerepo."""
17from pathlib import Path
18from xml.etree import ElementTree
19
20import pytest
21
22from .fakerepo import FakeRepo
23
24
25class TestFakeRepo:
26    """Tests for fakerepo.FakeRepo."""
27
28    def test_project_created(self, tmp_path: Path) -> None:
29        """Tests that FakeRepo.project creates a new FakeProject."""
30        repo = FakeRepo(tmp_path)
31        project = repo.project("platform/foo", "foo")
32        assert project.local.path == repo.root / "foo"
33        assert project.android_mirror.path == repo.mirror_dir / "platform/foo"
34        assert project.upstream.path == repo.upstream_dir / "foo"
35
36    def test_project_error_if_path_reused(self, tmp_path: Path) -> None:
37        """Tests that KeyError is raised if a project path is reused."""
38        repo = FakeRepo(tmp_path)
39        repo.project("platform/foo", "foo")
40        repo.project("platform/bar", "bar")
41        with pytest.raises(KeyError):
42            repo.project("platform/baz", "foo")
43        with pytest.raises(KeyError):
44            repo.project("platform/foo", "baz")
45
46    def test_create_manifest_repo_xml_structure(self, tmp_path: Path) -> None:
47        """Tests that the correct manifest XML is created."""
48        repo = FakeRepo(tmp_path)
49        repo.project("platform/foo", "foo")
50        repo.project("platform/external/bar", "external/bar")
51        repo.create_manifest_repo()
52
53        manifest_path = repo.manifest_repo / "default.xml"
54        assert manifest_path.exists()
55        root = ElementTree.parse(manifest_path)
56        remotes = root.findall("./remote")
57        assert len(remotes) == 1
58        remote = remotes[0]
59        assert remote.attrib["name"] == "aosp"
60        assert remote.attrib["fetch"] == repo.mirror_dir.as_uri()
61
62        defaults = root.findall("./default")
63        assert len(defaults) == 1
64        default = defaults[0]
65        assert default.attrib["remote"] == "aosp"
66        assert default.attrib["revision"] == "main"
67
68        projects = root.findall("./project")
69        assert len(projects) == 2
70
71        assert projects[0].attrib["name"] == "platform/foo"
72        assert projects[0].attrib["path"] == "foo"
73
74        assert projects[1].attrib["name"] == "platform/external/bar"
75        assert projects[1].attrib["path"] == "external/bar"
76
77    def test_init_and_sync(self, tmp_path: Path) -> None:
78        """Tests that init_and_sync initializes and syncs the tree."""
79        repo = FakeRepo(tmp_path)
80        project = repo.project("platform/foo", "foo")
81        repo.create_manifest_repo()
82        project.initial_import()
83        repo.init_and_sync()
84
85        assert project.local.head() == project.android_mirror.head()
86