• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 get_llvm_hash
12import subprocess
13import unittest
14import unittest.mock as mock
15import test_helpers
16
17from get_llvm_hash import LLVMHash
18
19# We grab protected stuff from get_llvm_hash. That's OK.
20# pylint: disable=protected-access
21
22
23def MakeMockPopen(return_code):
24
25  def MockPopen(*_args, **_kwargs):
26    result = mock.MagicMock()
27    result.returncode = return_code
28
29    communicate_result = result.communicate.return_value
30    # Communicate returns stdout, stderr.
31    communicate_result.__iter__.return_value = (None, 'some stderr')
32    return result
33
34  return MockPopen
35
36
37class TestGetLLVMHash(unittest.TestCase):
38  """The LLVMHash test class."""
39
40  @mock.patch.object(subprocess, 'Popen')
41  def testCloneRepoSucceedsWhenGitSucceeds(self, popen_mock):
42    popen_mock.side_effect = MakeMockPopen(return_code=0)
43    llvm_hash = LLVMHash()
44
45    into_tempdir = '/tmp/tmpTest'
46    llvm_hash.CloneLLVMRepo(into_tempdir)
47    popen_mock.assert_called_with(
48        ['git', 'clone', get_llvm_hash._LLVM_GIT_URL, into_tempdir],
49        stderr=subprocess.PIPE)
50
51  @mock.patch.object(subprocess, 'Popen')
52  def testCloneRepoFailsWhenGitFails(self, popen_mock):
53    popen_mock.side_effect = MakeMockPopen(return_code=1)
54
55    with self.assertRaises(ValueError) as err:
56      LLVMHash().CloneLLVMRepo('/tmp/tmp1')
57
58    self.assertIn('Failed to clone', str(err.exception.args))
59    self.assertIn('some stderr', str(err.exception.args))
60
61  @mock.patch.object(get_llvm_hash, 'GetGitHashFrom')
62  def testGetGitHashWorks(self, mock_get_git_hash):
63    mock_get_git_hash.return_value = 'a13testhash2'
64
65    self.assertEqual(
66        get_llvm_hash.GetGitHashFrom('/tmp/tmpTest', 100), 'a13testhash2')
67
68    mock_get_git_hash.assert_called_once()
69
70  @mock.patch.object(LLVMHash, 'GetLLVMHash')
71  @mock.patch.object(get_llvm_hash, 'GetGoogle3LLVMVersion')
72  def testReturnGoogle3LLVMHash(self, mock_google3_llvm_version,
73                                mock_get_llvm_hash):
74    mock_get_llvm_hash.return_value = 'a13testhash3'
75    mock_google3_llvm_version.return_value = 1000
76    self.assertEqual(LLVMHash().GetGoogle3LLVMHash(), 'a13testhash3')
77    mock_get_llvm_hash.assert_called_once_with(1000)
78
79  @mock.patch.object(LLVMHash, 'GetLLVMHash')
80  @mock.patch.object(get_llvm_hash, 'GetGoogle3LLVMVersion')
81  def testReturnGoogle3UnstableLLVMHash(self, mock_google3_llvm_version,
82                                        mock_get_llvm_hash):
83    mock_get_llvm_hash.return_value = 'a13testhash3'
84    mock_google3_llvm_version.return_value = 1000
85    self.assertEqual(LLVMHash().GetGoogle3UnstableLLVMHash(), 'a13testhash3')
86    mock_get_llvm_hash.assert_called_once_with(1000)
87
88  @mock.patch.object(subprocess, 'check_output')
89  def testSuccessfullyGetGitHashFromToTOfLLVM(self, mock_check_output):
90    mock_check_output.return_value = 'a123testhash1 path/to/master\n'
91    self.assertEqual(LLVMHash().GetTopOfTrunkGitHash(), 'a123testhash1')
92    mock_check_output.assert_called_once()
93
94
95if __name__ == '__main__':
96  unittest.main()
97