1#!/usr/bin/env python3 2# Copyright 2023 The Android Open Source Project 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16"""Unittests for the results module.""" 17 18import os 19import sys 20import unittest 21 22_path = os.path.realpath(__file__ + '/../..') 23if sys.path[0] != _path: 24 sys.path.insert(0, _path) 25del _path 26 27# We have to import our local modules after the sys.path tweak. We can't use 28# relative imports because this is an executable program, not a module. 29# pylint: disable=wrong-import-position 30import rh 31import rh.results 32import rh.utils 33 34 35COMPLETED_PROCESS_PASS = rh.utils.CompletedProcess(returncode=0) 36COMPLETED_PROCESS_FAIL = rh.utils.CompletedProcess(returncode=1) 37COMPLETED_PROCESS_WARN = rh.utils.CompletedProcess(returncode=77) 38 39 40class HookResultTests(unittest.TestCase): 41 """Verify behavior of HookResult object.""" 42 43 def test_error_warning(self): 44 """Check error & warning handling.""" 45 # No errors. 46 result = rh.results.HookResult('hook', 'project', 'HEAD', False) 47 self.assertFalse(result) 48 self.assertFalse(result.is_warning()) 49 50 # An error. 51 result = rh.results.HookResult('hook', 'project', 'HEAD', True) 52 self.assertTrue(result) 53 self.assertFalse(result.is_warning()) 54 55 56class HookCommandResultTests(unittest.TestCase): 57 """Verify behavior of HookCommandResult object.""" 58 59 def test_error_warning(self): 60 """Check error & warning handling.""" 61 # No errors. 62 result = rh.results.HookCommandResult( 63 'hook', 'project', 'HEAD', COMPLETED_PROCESS_PASS) 64 self.assertFalse(result) 65 self.assertFalse(result.is_warning()) 66 67 # An error. 68 result = rh.results.HookCommandResult( 69 'hook', 'project', 'HEAD', COMPLETED_PROCESS_FAIL) 70 self.assertTrue(result) 71 self.assertFalse(result.is_warning()) 72 73 # A warning. 74 result = rh.results.HookCommandResult( 75 'hook', 'project', 'HEAD', COMPLETED_PROCESS_WARN) 76 self.assertFalse(result) 77 self.assertTrue(result.is_warning()) 78 79 80class ProjectResultsTests(unittest.TestCase): 81 """Verify behavior of ProjectResults object.""" 82 83 def test_error_warning(self): 84 """Check error & warning handling.""" 85 # No errors. 86 result = rh.results.ProjectResults('project', 'workdir') 87 self.assertFalse(result) 88 89 # Warnings are not errors. 90 result.add_results([ 91 rh.results.HookResult('hook', 'project', 'HEAD', False), 92 rh.results.HookCommandResult( 93 'hook', 'project', 'HEAD', COMPLETED_PROCESS_WARN), 94 ]) 95 self.assertFalse(result) 96 97 # Errors are errors. 98 result.add_results([ 99 rh.results.HookResult('hook', 'project', 'HEAD', True), 100 ]) 101 self.assertTrue(result) 102 103 104if __name__ == '__main__': 105 unittest.main() 106