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