1*3c875a21SAndroid Build Coastguard Worker# 2*3c875a21SAndroid Build Coastguard Worker# Copyright (C) 2023 The Android Open Source Project 3*3c875a21SAndroid Build Coastguard Worker# 4*3c875a21SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 5*3c875a21SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 6*3c875a21SAndroid Build Coastguard Worker# You may obtain a copy of the License at 7*3c875a21SAndroid Build Coastguard Worker# 8*3c875a21SAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 9*3c875a21SAndroid Build Coastguard Worker# 10*3c875a21SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 11*3c875a21SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 12*3c875a21SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*3c875a21SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 14*3c875a21SAndroid Build Coastguard Worker# limitations under the License. 15*3c875a21SAndroid Build Coastguard Worker# 16*3c875a21SAndroid Build Coastguard Worker"""Builder for creating repo trees for use in testing.""" 17*3c875a21SAndroid Build Coastguard Workerfrom pathlib import Path 18*3c875a21SAndroid Build Coastguard Worker 19*3c875a21SAndroid Build Coastguard Workerfrom .fakerepo import FakeRepo 20*3c875a21SAndroid Build Coastguard Worker 21*3c875a21SAndroid Build Coastguard Worker 22*3c875a21SAndroid Build Coastguard Workerclass TreeBuilder: # pylint: disable=too-few-public-methods 23*3c875a21SAndroid Build Coastguard Worker """Creates test repo trees in a temporary directory.""" 24*3c875a21SAndroid Build Coastguard Worker 25*3c875a21SAndroid Build Coastguard Worker def __init__(self, temp_dir: Path) -> None: 26*3c875a21SAndroid Build Coastguard Worker self.temp_dir = temp_dir 27*3c875a21SAndroid Build Coastguard Worker self._trees: set[str] = set() 28*3c875a21SAndroid Build Coastguard Worker 29*3c875a21SAndroid Build Coastguard Worker def repo_tree(self, name: str) -> FakeRepo: 30*3c875a21SAndroid Build Coastguard Worker """Creates a new repo tree with the given name.""" 31*3c875a21SAndroid Build Coastguard Worker if name in self._trees: 32*3c875a21SAndroid Build Coastguard Worker raise KeyError(f"A repo tree named {name} already exists") 33*3c875a21SAndroid Build Coastguard Worker self._trees.add(name) 34*3c875a21SAndroid Build Coastguard Worker return FakeRepo(self.temp_dir / name) 35