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"""Common tools for end-to-end tests.""" 17*3c875a21SAndroid Build Coastguard Workerimport subprocess 18*3c875a21SAndroid Build Coastguard Workerfrom pathlib import Path 19*3c875a21SAndroid Build Coastguard Worker 20*3c875a21SAndroid Build Coastguard Workerimport pytest 21*3c875a21SAndroid Build Coastguard Worker 22*3c875a21SAndroid Build Coastguard Workerfrom .treebuilder import TreeBuilder 23*3c875a21SAndroid Build Coastguard Worker 24*3c875a21SAndroid Build Coastguard WorkerTHIS_DIR = Path(__file__).parent.resolve() 25*3c875a21SAndroid Build Coastguard WorkerEXTERNAL_UPDATER_DIR = THIS_DIR.parent.parent 26*3c875a21SAndroid Build Coastguard WorkerANDROID_DIR = EXTERNAL_UPDATER_DIR.parent.parent 27*3c875a21SAndroid Build Coastguard Worker 28*3c875a21SAndroid Build Coastguard Worker 29*3c875a21SAndroid Build Coastguard Workerdef pytest_addoption(parser: pytest.Parser) -> None: 30*3c875a21SAndroid Build Coastguard Worker """Add custom options to pytest.""" 31*3c875a21SAndroid Build Coastguard Worker parser.addoption( 32*3c875a21SAndroid Build Coastguard Worker "--build-updater", 33*3c875a21SAndroid Build Coastguard Worker action="store_true", 34*3c875a21SAndroid Build Coastguard Worker default=True, 35*3c875a21SAndroid Build Coastguard Worker help=( 36*3c875a21SAndroid Build Coastguard Worker "Build external_updater before running tests. This is the default behavior." 37*3c875a21SAndroid Build Coastguard Worker ), 38*3c875a21SAndroid Build Coastguard Worker ) 39*3c875a21SAndroid Build Coastguard Worker parser.addoption( 40*3c875a21SAndroid Build Coastguard Worker "--no-build-updater", 41*3c875a21SAndroid Build Coastguard Worker action="store_false", 42*3c875a21SAndroid Build Coastguard Worker dest="build_updater", 43*3c875a21SAndroid Build Coastguard Worker help=( 44*3c875a21SAndroid Build Coastguard Worker "Do not build external_updater before running tests. Only use this option " 45*3c875a21SAndroid Build Coastguard Worker "if you've manually built external_updater. It will make test startup " 46*3c875a21SAndroid Build Coastguard Worker "faster." 47*3c875a21SAndroid Build Coastguard Worker ), 48*3c875a21SAndroid Build Coastguard Worker ) 49*3c875a21SAndroid Build Coastguard Worker 50*3c875a21SAndroid Build Coastguard Worker 51*3c875a21SAndroid Build Coastguard Worker@pytest.fixture(name="should_build_updater", scope="session") 52*3c875a21SAndroid Build Coastguard Workerdef should_build_updater_fixture(request: pytest.FixtureRequest) -> bool: 53*3c875a21SAndroid Build Coastguard Worker """True if external_updater should be built before running tests.""" 54*3c875a21SAndroid Build Coastguard Worker return request.config.getoption("--build-updater") 55*3c875a21SAndroid Build Coastguard Worker 56*3c875a21SAndroid Build Coastguard Worker 57*3c875a21SAndroid Build Coastguard Worker# Session scope means that this fixture will only run the first time it's used. We don't 58*3c875a21SAndroid Build Coastguard Worker# want to re-run soong for every test because it's horrendously slow to do so. 59*3c875a21SAndroid Build Coastguard Worker@pytest.fixture(scope="session") 60*3c875a21SAndroid Build Coastguard Workerdef updater_cmd(should_build_updater: bool) -> list[str]: 61*3c875a21SAndroid Build Coastguard Worker """The command to run for external_updater. 62*3c875a21SAndroid Build Coastguard Worker 63*3c875a21SAndroid Build Coastguard Worker The result is the prefix of the command that should be used with subprocess.run or 64*3c875a21SAndroid Build Coastguard Worker similar. 65*3c875a21SAndroid Build Coastguard Worker """ 66*3c875a21SAndroid Build Coastguard Worker # Running updater.sh should be a more accurate test, but doing so isn't really 67*3c875a21SAndroid Build Coastguard Worker # feasible with a no-op `m external_updater` taking ~10 seconds. Doing that would 68*3c875a21SAndroid Build Coastguard Worker # add 10 seconds to every test. Build the updater once for the first thing that 69*3c875a21SAndroid Build Coastguard Worker # requires it (that's the "session" scope above) and run the PAR directly. 70*3c875a21SAndroid Build Coastguard Worker if should_build_updater: 71*3c875a21SAndroid Build Coastguard Worker subprocess.run( 72*3c875a21SAndroid Build Coastguard Worker [ 73*3c875a21SAndroid Build Coastguard Worker ANDROID_DIR / "build/soong/soong_ui.bash", 74*3c875a21SAndroid Build Coastguard Worker "--make-mode", 75*3c875a21SAndroid Build Coastguard Worker "external_updater", 76*3c875a21SAndroid Build Coastguard Worker ], 77*3c875a21SAndroid Build Coastguard Worker check=True, 78*3c875a21SAndroid Build Coastguard Worker ) 79*3c875a21SAndroid Build Coastguard Worker return [str("external_updater")] 80*3c875a21SAndroid Build Coastguard Worker 81*3c875a21SAndroid Build Coastguard Worker 82*3c875a21SAndroid Build Coastguard Worker@pytest.fixture(name="tree_builder") 83*3c875a21SAndroid Build Coastguard Workerdef tree_builder_fixture(tmp_path: Path) -> TreeBuilder: 84*3c875a21SAndroid Build Coastguard Worker """Creates a TreeBuilder for making test repo trees.""" 85*3c875a21SAndroid Build Coastguard Worker return TreeBuilder(tmp_path) 86