| 1 |
#!/usr/bin/env python3 |
| 2 |
# -*- coding: utf-8; mode: python; py-indent-offset: 4; py-continuation-offset: 4 -*- |
| 3 |
""" |
| 4 |
""" |
| 5 |
from __future__ import print_function |
| 6 |
|
| 7 |
try: # pragma: no cover |
| 8 |
import builtins # pragma: no cover |
| 9 |
except ImportError: # pragma: no cover |
| 10 |
import __builtin__ as builtins # pragma: no cover |
| 11 |
|
| 12 |
import sys |
| 13 |
|
| 14 |
if sys.version_info >= (3,0): # pragma: no cover |
| 15 |
from io import StringIO # pragma: no cover |
| 16 |
else: # pragma: no cover |
| 17 |
from io import BytesIO as StringIO # pragma: no cover |
| 18 |
|
| 19 |
#import unittest |
| 20 |
from unittest import TestCase |
| 21 |
|
| 22 |
try: # pragma: no cover |
| 23 |
import unittest.mock as mock # pragma: no cover |
| 24 |
from unittest.mock import patch |
| 25 |
except: # pragma: no cover |
| 26 |
import mock # pragma: no cover |
| 27 |
from mock import patch |
| 28 |
|
| 29 |
#from mock import Mock |
| 30 |
#from mock import mock_open |
| 31 |
#from mock import MagicMock |
| 32 |
|
| 33 |
|
| 34 |
import trilinosprhelpers.gitutility as gitutility |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
#============================================================================== |
| 39 |
# |
| 40 |
# M O C K H E L P E R S |
| 41 |
# |
| 42 |
#============================================================================== |
| 43 |
|
| 44 |
def mock_subprocess_get_git_version_str(*args): |
| 45 |
output = None |
| 46 |
vstr = "2.27.0" |
| 47 |
try: |
| 48 |
output = bytes(vstr, "utf-8") |
| 49 |
except: # pragma: no cover |
| 50 |
output = bytes(vstr) # pragma: no cover |
| 51 |
return output |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
#============================================================================== |
| 56 |
# |
| 57 |
# T E S T S |
| 58 |
# |
| 59 |
#============================================================================== |
| 60 |
class GitUtilityTest(TestCase): |
| 61 |
""" |
| 62 |
""" |
| 63 |
def setUp(self): |
| 64 |
pass |
| 65 |
|
| 66 |
|
| 67 |
def test_version_str(self): |
| 68 |
print("") |
| 69 |
gitutil = gitutility.GitUtility() |
| 70 |
with patch('subprocess.check_output', side_effect=mock_subprocess_get_git_version_str) as m: |
| 71 |
version_str = gitutil.version_str |
| 72 |
m.assert_called_once_with(["git", "--version"]) |
| 73 |
print("version_str = {}".format(version_str)) |
| 74 |
|
| 75 |
|
| 76 |
def test_version(self): |
| 77 |
print("") |
| 78 |
gitutil = gitutility.GitUtility() |
| 79 |
with patch('subprocess.check_output', side_effect=mock_subprocess_get_git_version_str) as m: |
| 80 |
version = gitutil.version |
| 81 |
m.assert_called_once_with(["git", "--version"]) |
| 82 |
|
| 83 |
self.assertIsInstance(version, dict) |
| 84 |
self.assertEqual(2, version['major']) |
| 85 |
self.assertEqual(27, version['minor']) |
| 86 |
self.assertEqual(0, version['patch']) |
| 87 |
|
| 88 |
print("version = {}".format(version)) |
| 89 |
|
| 90 |
|
| 91 |
def test_check_minimum_version(self): |
| 92 |
print("") |
| 93 |
gitutil = gitutility.GitUtility() |
| 94 |
with patch('subprocess.check_output', side_effect=mock_subprocess_get_git_version_str) as m: |
| 95 |
version = gitutil.version |
| 96 |
m.assert_called_once_with(["git", "--version"]) |
| 97 |
|
| 98 |
ret = gitutil.check_minimum_version(1) |
| 99 |
self.assertEqual(0, ret) |
| 100 |
|
| 101 |
ret = gitutil.check_minimum_version(1, 20) |
| 102 |
self.assertEqual(0, ret) |
| 103 |
|
| 104 |
ret = gitutil.check_minimum_version(1, 30) |
| 105 |
self.assertEqual(0, ret) |
| 106 |
|
| 107 |
ret = gitutil.check_minimum_version(2) |
| 108 |
self.assertEqual(0, ret) |
| 109 |
|
| 110 |
ret = gitutil.check_minimum_version(2, 20) |
| 111 |
self.assertEqual(0, ret) |
| 112 |
|
| 113 |
with self.assertRaises( SystemExit ) as m: |
| 114 |
gitutil.check_minimum_version(2, 30) |
| 115 |
|
| 116 |
with self.assertRaises( SystemExit ) as m: |
| 117 |
gitutil.check_minimum_version(3) |
| 118 |
|
| 119 |
with self.assertRaises( SystemExit ) as m: |
| 120 |
gitutil.check_minimum_version(3, 20) |
| 121 |
|
| 122 |
with self.assertRaises( SystemExit ) as m: |
| 123 |
gitutil.check_minimum_version(3, 30) |
| 124 |
|
| 125 |
with self.assertRaises( TypeError ) as m: |
| 126 |
gitutil.check_minimum_version("3") |
| 127 |
|
| 128 |
with self.assertRaises( TypeError ) as m: |
| 129 |
gitutil.check_minimum_version(2, "27") |
| 130 |
|
| 131 |
|
| 132 |
def test_pretty_print(self): |
| 133 |
print("") |
| 134 |
gitutil = gitutility.GitUtility() |
| 135 |
with patch('subprocess.check_output', side_effect=mock_subprocess_get_git_version_str) as m: |
| 136 |
version = gitutil.version |
| 137 |
m.assert_called_once_with(["git", "--version"]) |
| 138 |
|
| 139 |
with patch('sys.stdout', new = StringIO()) as m_out: |
| 140 |
gitutil.pretty_print() |
| 141 |
|
| 142 |
self.assertEqual(m_out.getvalue().strip(), "Git Version Detected: 2.27.0") |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|