1# Copyright 2021 Google LLC 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# 15################################################################################ 16"""Unit tests for build_and_run_coverage.""" 17import json 18import os 19import sys 20import unittest 21from unittest import mock 22 23from pyfakefs import fake_filesystem_unittest 24 25FUNCTIONS_DIR = os.path.dirname(__file__) 26sys.path.append(FUNCTIONS_DIR) 27# pylint: disable=wrong-import-position 28 29import build_and_run_coverage 30import build_project 31import test_utils 32 33# pylint: disable=no-member 34 35 36class TestRequestCoverageBuilds(fake_filesystem_unittest.TestCase): 37 """Unit tests for sync.""" 38 39 def setUp(self): 40 self.maxDiff = None # pylint: disable=invalid-name 41 self.setUpPyfakefs() 42 43 @mock.patch('build_lib.get_signed_url', return_value='test_url') 44 @mock.patch('build_lib.download_corpora_steps', 45 return_value=[{ 46 'url': 'test_download' 47 }]) 48 @mock.patch('build_project.get_datetime_now', 49 return_value=test_utils.FAKE_DATETIME) 50 def test_get_coverage_build_steps(self, mock_url, mock_corpora_steps, 51 mock_get_datetime_now): 52 """Test for get_build_steps.""" 53 del mock_url, mock_corpora_steps, mock_get_datetime_now 54 project_yaml_contents = ('language: c++\n' 55 'sanitizers:\n' 56 ' - address\n' 57 'architectures:\n' 58 ' - x86_64\n') 59 self.fs.create_dir(test_utils.PROJECT_DIR) 60 test_utils.create_project_data(test_utils.PROJECT, project_yaml_contents) 61 62 expected_build_steps_file_path = test_utils.get_test_data_file_path( 63 'expected_coverage_build_steps.json') 64 self.fs.add_real_file(expected_build_steps_file_path) 65 with open(expected_build_steps_file_path) as expected_build_steps_file: 66 expected_coverage_build_steps = json.load(expected_build_steps_file) 67 68 config = build_project.Config(False, False, None, False) 69 project_yaml, dockerfile = build_project.get_project_data( 70 test_utils.PROJECT) 71 build_steps = build_and_run_coverage.get_build_steps( 72 test_utils.PROJECT, project_yaml, dockerfile, test_utils.IMAGE_PROJECT, 73 test_utils.BASE_IMAGES_PROJECT, config) 74 self.assertEqual(build_steps, expected_coverage_build_steps) 75 76 77if __name__ == '__main__': 78 unittest.main(exit=False) 79