1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# Copyright 2019 The Chromium OS Authors. All rights reserved. 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7"""Unit tests for retrieving the LLVM hash.""" 8 9from __future__ import print_function 10 11import subprocess 12import unittest 13import unittest.mock as mock 14 15import get_llvm_hash 16from get_llvm_hash import LLVMHash 17 18# We grab protected stuff from get_llvm_hash. That's OK. 19# pylint: disable=protected-access 20 21 22def MakeMockPopen(return_code): 23 24 def MockPopen(*_args, **_kwargs): 25 result = mock.MagicMock() 26 result.returncode = return_code 27 28 communicate_result = result.communicate.return_value 29 # Communicate returns stdout, stderr. 30 communicate_result.__iter__.return_value = (None, 'some stderr') 31 return result 32 33 return MockPopen 34 35 36class TestGetLLVMHash(unittest.TestCase): 37 """The LLVMHash test class.""" 38 39 @mock.patch.object(subprocess, 'Popen') 40 def testCloneRepoSucceedsWhenGitSucceeds(self, popen_mock): 41 popen_mock.side_effect = MakeMockPopen(return_code=0) 42 llvm_hash = LLVMHash() 43 44 into_tempdir = '/tmp/tmpTest' 45 llvm_hash.CloneLLVMRepo(into_tempdir) 46 popen_mock.assert_called_with( 47 ['git', 'clone', get_llvm_hash._LLVM_GIT_URL, into_tempdir], 48 stderr=subprocess.PIPE) 49 50 @mock.patch.object(subprocess, 'Popen') 51 def testCloneRepoFailsWhenGitFails(self, popen_mock): 52 popen_mock.side_effect = MakeMockPopen(return_code=1) 53 54 with self.assertRaises(ValueError) as err: 55 LLVMHash().CloneLLVMRepo('/tmp/tmp1') 56 57 self.assertIn('Failed to clone', str(err.exception.args)) 58 self.assertIn('some stderr', str(err.exception.args)) 59 60 @mock.patch.object(get_llvm_hash, 'GetGitHashFrom') 61 def testGetGitHashWorks(self, mock_get_git_hash): 62 mock_get_git_hash.return_value = 'a13testhash2' 63 64 self.assertEqual( 65 get_llvm_hash.GetGitHashFrom('/tmp/tmpTest', 100), 'a13testhash2') 66 67 mock_get_git_hash.assert_called_once() 68 69 @mock.patch.object(LLVMHash, 'GetLLVMHash') 70 @mock.patch.object(get_llvm_hash, 'GetGoogle3LLVMVersion') 71 def testReturnGoogle3LLVMHash(self, mock_google3_llvm_version, 72 mock_get_llvm_hash): 73 mock_get_llvm_hash.return_value = 'a13testhash3' 74 mock_google3_llvm_version.return_value = 1000 75 self.assertEqual(LLVMHash().GetGoogle3LLVMHash(), 'a13testhash3') 76 mock_get_llvm_hash.assert_called_once_with(1000) 77 78 @mock.patch.object(LLVMHash, 'GetLLVMHash') 79 @mock.patch.object(get_llvm_hash, 'GetGoogle3LLVMVersion') 80 def testReturnGoogle3UnstableLLVMHash(self, mock_google3_llvm_version, 81 mock_get_llvm_hash): 82 mock_get_llvm_hash.return_value = 'a13testhash3' 83 mock_google3_llvm_version.return_value = 1000 84 self.assertEqual(LLVMHash().GetGoogle3UnstableLLVMHash(), 'a13testhash3') 85 mock_get_llvm_hash.assert_called_once_with(1000) 86 87 @mock.patch.object(subprocess, 'check_output') 88 def testSuccessfullyGetGitHashFromToTOfLLVM(self, mock_check_output): 89 mock_check_output.return_value = 'a123testhash1 path/to/main\n' 90 self.assertEqual(LLVMHash().GetTopOfTrunkGitHash(), 'a123testhash1') 91 mock_check_output.assert_called_once() 92 93 94if __name__ == '__main__': 95 unittest.main() 96