Passed
Pull Request — master (#198)
by
unknown
01:30
created

tests.version.test_get_version_from_pyproject_toml   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 80
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A GetVersionFromPyprojectTomlTestCase.test_pyproject_toml_file_not_exists() 0 11 2
A GetVersionFromPyprojectTomlTestCase.test_get_version() 0 12 1
A GetVersionFromPyprojectTomlTestCase.test_empty_poerty_section() 0 13 2
A GetVersionFromPyprojectTomlTestCase.test_no_poerty_section() 0 13 2
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2020 Greenbone Networks GmbH
3
#
4
# SPDX-License-Identifier: GPL-3.0-or-later
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
import unittest
20
21
from pathlib import Path
22
from unittest.mock import MagicMock
23
24
from gvm.version import get_version_from_pyproject_toml
25
26
27
class GetVersionFromPyprojectTomlTestCase(unittest.TestCase):
28
    def test_pyproject_toml_file_not_exists(self):
29
        fake_path_class = MagicMock(spec=Path)
30
        fake_path = fake_path_class.return_value
31
        fake_path.exists.return_value = False
32
33
        with self.assertRaisesRegex(
34
            RuntimeError, 'pyproject.toml file not found'
35
        ):
36
            get_version_from_pyproject_toml(fake_path)
37
38
        fake_path.exists.assert_called_with()
39
40
    def test_no_poerty_section(self):
41
        fake_path_class = MagicMock(spec=Path)
42
        fake_path = fake_path_class.return_value
43
        fake_path.exists.return_value = True
44
        fake_path.read_text.return_value = ''
45
46
        with self.assertRaisesRegex(
47
            RuntimeError, 'Version information not found in pyproject.toml file'
48
        ):
49
            get_version_from_pyproject_toml(fake_path)
50
51
        fake_path.exists.assert_called_with()
52
        fake_path.read_text.assert_called_with()
53
54
    def test_empty_poerty_section(self):
55
        fake_path_class = MagicMock(spec=Path)
56
        fake_path = fake_path_class.return_value
57
        fake_path.exists.return_value = True
58
        fake_path.read_text.return_value = '[tool.poetry]'
59
60
        with self.assertRaisesRegex(
61
            RuntimeError, 'Version information not found in pyproject.toml file'
62
        ):
63
            get_version_from_pyproject_toml(fake_path)
64
65
        fake_path.exists.assert_called_with()
66
        fake_path.read_text.assert_called_with()
67
68
    def test_get_version(self):
69
        fake_path_class = MagicMock(spec=Path)
70
        fake_path = fake_path_class.return_value
71
        fake_path.exists.return_value = True
72
        fake_path.read_text.return_value = '[tool.poetry]\nversion = "1.2.3"'
73
74
        version = get_version_from_pyproject_toml(fake_path)
75
76
        self.assertEqual(version, '1.2.3')
77
78
        fake_path.exists.assert_called_with()
79
        fake_path.read_text.assert_called_with()
80