xref: /aosp_15_r20/tools/external_updater/tests/endtoend/treebuilder/treebuilder.py (revision 3c875a214f382db1236d28570d1304ce57138f32)
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"""Builder for creating repo trees for use in testing."""
17from pathlib import Path
18
19from .fakerepo import FakeRepo
20
21
22class TreeBuilder:  # pylint: disable=too-few-public-methods
23    """Creates test repo trees in a temporary directory."""
24
25    def __init__(self, temp_dir: Path) -> None:
26        self.temp_dir = temp_dir
27        self._trees: set[str] = set()
28
29    def repo_tree(self, name: str) -> FakeRepo:
30        """Creates a new repo tree with the given name."""
31        if name in self._trees:
32            raise KeyError(f"A repo tree named {name} already exists")
33        self._trees.add(name)
34        return FakeRepo(self.temp_dir / name)
35