1*800a58d9SAndroid Build Coastguard Worker#!/usr/bin/env python 2*800a58d9SAndroid Build Coastguard Worker# 3*800a58d9SAndroid Build Coastguard Worker# Copyright 2017 - The Android Open Source Project 4*800a58d9SAndroid Build Coastguard Worker# 5*800a58d9SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 6*800a58d9SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 7*800a58d9SAndroid Build Coastguard Worker# You may obtain a copy of the License at 8*800a58d9SAndroid Build Coastguard Worker# 9*800a58d9SAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 10*800a58d9SAndroid Build Coastguard Worker# 11*800a58d9SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 12*800a58d9SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 13*800a58d9SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14*800a58d9SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 15*800a58d9SAndroid Build Coastguard Worker# limitations under the License. 16*800a58d9SAndroid Build Coastguard Worker"""Driver test library.""" 17*800a58d9SAndroid Build Coastguard Worker 18*800a58d9SAndroid Build Coastguard Workerimport os 19*800a58d9SAndroid Build Coastguard Workerimport unittest 20*800a58d9SAndroid Build Coastguard Worker 21*800a58d9SAndroid Build Coastguard Workerfrom unittest import mock 22*800a58d9SAndroid Build Coastguard Worker 23*800a58d9SAndroid Build Coastguard Worker 24*800a58d9SAndroid Build Coastguard Workerclass BaseDriverTest(unittest.TestCase): 25*800a58d9SAndroid Build Coastguard Worker """Base class for driver tests.""" 26*800a58d9SAndroid Build Coastguard Worker 27*800a58d9SAndroid Build Coastguard Worker def setUp(self): 28*800a58d9SAndroid Build Coastguard Worker """Set up test.""" 29*800a58d9SAndroid Build Coastguard Worker self._patchers = [] 30*800a58d9SAndroid Build Coastguard Worker 31*800a58d9SAndroid Build Coastguard Worker def tearDown(self): 32*800a58d9SAndroid Build Coastguard Worker """Tear down test.""" 33*800a58d9SAndroid Build Coastguard Worker for patcher in reversed(self._patchers): 34*800a58d9SAndroid Build Coastguard Worker patcher.stop() 35*800a58d9SAndroid Build Coastguard Worker 36*800a58d9SAndroid Build Coastguard Worker def Patch(self, *args, **kwargs): 37*800a58d9SAndroid Build Coastguard Worker """A wrapper for mock.patch.object. 38*800a58d9SAndroid Build Coastguard Worker 39*800a58d9SAndroid Build Coastguard Worker This wrapper starts a patcher and store it in self._patchers, 40*800a58d9SAndroid Build Coastguard Worker so that we can later stop them in tearDown. 41*800a58d9SAndroid Build Coastguard Worker 42*800a58d9SAndroid Build Coastguard Worker Args: 43*800a58d9SAndroid Build Coastguard Worker *args: Arguments to pass to mock.patch. 44*800a58d9SAndroid Build Coastguard Worker **kwargs: Keyword arguments to pass to mock.patch. 45*800a58d9SAndroid Build Coastguard Worker 46*800a58d9SAndroid Build Coastguard Worker Returns: 47*800a58d9SAndroid Build Coastguard Worker Mock object 48*800a58d9SAndroid Build Coastguard Worker """ 49*800a58d9SAndroid Build Coastguard Worker patcher = mock.patch.object(*args, **kwargs) 50*800a58d9SAndroid Build Coastguard Worker self._patchers.append(patcher) 51*800a58d9SAndroid Build Coastguard Worker return patcher.start() 52*800a58d9SAndroid Build Coastguard Worker 53*800a58d9SAndroid Build Coastguard Worker @staticmethod 54*800a58d9SAndroid Build Coastguard Worker def CreateFile(path, data=b""): 55*800a58d9SAndroid Build Coastguard Worker """Create and write binary data to a file.""" 56*800a58d9SAndroid Build Coastguard Worker os.makedirs(os.path.dirname(path), exist_ok=True) 57*800a58d9SAndroid Build Coastguard Worker with open(path, "wb") as file_obj: 58*800a58d9SAndroid Build Coastguard Worker file_obj.write(data) 59