|
| 1 | +import unittest.mock |
| 2 | +import sarge |
| 3 | +from queue import Queue |
| 4 | + |
| 5 | +from bears.general.OutdatedDependencyBear import OutdatedDependencyBear |
| 6 | +from coalib.testing.LocalBearTestHelper import LocalBearTestHelper |
| 7 | +from coalib.results.Result import Result |
| 8 | +from coalib.settings.Section import Section |
| 9 | +from coalib.settings.Setting import Setting |
| 10 | + |
| 11 | + |
| 12 | +test_file = """ |
| 13 | +foo==1.0 |
| 14 | +bar==2.0 |
| 15 | +""" |
| 16 | + |
| 17 | + |
| 18 | +class OutdatedDependencyBearTest(LocalBearTestHelper): |
| 19 | + |
| 20 | + def setUp(self): |
| 21 | + self.section = Section('') |
| 22 | + self.uut = OutdatedDependencyBear(self.section, Queue()) |
| 23 | + |
| 24 | + @unittest.mock.patch('bears.general.OutdatedDependencyBear.' |
| 25 | + 'PipRequirement.get_latest_version') |
| 26 | + def test_pip_outdated_requirement(self, _mock): |
| 27 | + self.section.append(Setting('requirement_type', 'pip')) |
| 28 | + _mock.return_value = '3.0' |
| 29 | + with unittest.mock.patch('bears.general.OutdatedDependencyBear.' |
| 30 | + 'run') as mock: |
| 31 | + patched = unittest.mock.Mock(spec=sarge.Pipeline) |
| 32 | + patched.stdout = unittest.mock.Mock(spec=sarge.Capture) |
| 33 | + patched.stdout.text = 'foo==1.0\nbar==2.0' |
| 34 | + mock.return_value = patched |
| 35 | + message = ('The requirement {} with version {} is not ' |
| 36 | + 'pinned to its latest version 3.0.') |
| 37 | + self.check_results(self.uut, |
| 38 | + test_file.splitlines(True), |
| 39 | + [Result.from_values( |
| 40 | + origin='OutdatedDependencyBear', |
| 41 | + message=message.format('foo', '1.0'), |
| 42 | + file='default', |
| 43 | + line=2, end_line=2, |
| 44 | + ), |
| 45 | + Result.from_values( |
| 46 | + origin='OutdatedDependencyBear', |
| 47 | + message=message.format('bar', '2.0'), |
| 48 | + file='default', |
| 49 | + line=3, end_line=3, |
| 50 | + )], |
| 51 | + filename='default', |
| 52 | + ) |
| 53 | + |
| 54 | + @unittest.mock.patch('bears.general.OutdatedDependencyBear.' |
| 55 | + 'PipRequirement.get_latest_version') |
| 56 | + def test_pip_latest_requirement(self, _mock): |
| 57 | + self.section.append(Setting('requirement_type', 'pip')) |
| 58 | + _mock.return_value = '1.0' |
| 59 | + with unittest.mock.patch('bears.general.OutdatedDependencyBear.' |
| 60 | + 'run') as mock: |
| 61 | + patched = unittest.mock.Mock(spec=sarge.Pipeline) |
| 62 | + patched.stdout = unittest.mock.Mock(spec=sarge.Capture) |
| 63 | + patched.stdout.text = 'foo==1.0' |
| 64 | + mock.return_value = patched |
| 65 | + self.check_results(self.uut, |
| 66 | + [test_file.splitlines()[0]], |
| 67 | + [], |
| 68 | + filename='default') |
| 69 | + |
| 70 | + def test_requirement_type_value_error(self): |
| 71 | + self.section.append(Setting('requirement_type', 'blabla')) |
| 72 | + error = ('ValueError: Currently the bear only supports pip as ' |
| 73 | + 'requirement_type.') |
| 74 | + with self.assertRaisesRegex(AssertionError, error): |
| 75 | + self.check_validity(self.uut, [], filename='default') |
0 commit comments