File size: 28,424 Bytes
5980447
1
2
{"repo": "greenbone/python-gvm", "pull_number": 198, "instance_id": "greenbone__python-gvm-198", "issue_numbers": "", "base_commit": "5a85d62b76db8cc726864e981e4a1f1fbd6bb3f9", "patch": "diff --git a/gvm/__init__.py b/gvm/__init__.py\n--- a/gvm/__init__.py\n+++ b/gvm/__init__.py\n@@ -18,23 +18,7 @@\n \"\"\"\n Main module of python-gvm.\n \"\"\"\n-from pathlib import Path\n-\n-from pkg_resources import safe_version\n-\n-import toml\n-\n-\n-def get_version_from_pyproject_toml() -> str:\n-    path = Path(__file__)\n-    pyproject_toml_path = path.parent.parent / 'pyproject.toml'\n-\n-    if pyproject_toml_path.exists():\n-        pyproject_toml = toml.loads(pyproject_toml_path.read_text())\n-        if 'tool' in pyproject_toml and 'poetry' in pyproject_toml['tool']:\n-            return pyproject_toml['tool']['poetry']['version']\n-\n-    raise RuntimeError('Version information not found in pyproject.toml file.')\n+from .__version__ import __version__\n \n \n def get_version() -> str:\n@@ -47,5 +31,4 @@ def get_version() -> str:\n     .. _PEP440:\n        https://www.python.org/dev/peps/pep-0440\n     \"\"\"\n-    str_version = get_version_from_pyproject_toml()\n-    return safe_version(str_version)\n+    return __version__\ndiff --git a/gvm/__version__.py b/gvm/__version__.py\nnew file mode 100644\n--- /dev/null\n+++ b/gvm/__version__.py\n@@ -0,0 +1,5 @@\n+# pylint: disable=invalid-name\n+\n+# THIS IS AN AUTOGENERATED FILE. DO NOT TOUCH!\n+\n+__version__ = \"20.4.dev1\"\ndiff --git a/gvm/utils.py b/gvm/utils.py\n--- a/gvm/utils.py\n+++ b/gvm/utils.py\n@@ -1,5 +1,5 @@\n # -*- coding: utf-8 -*-\n-# Copyright (C) 2018 - 2019 Greenbone Networks GmbH\n+# Copyright (C) 2018 - 2020 Greenbone Networks GmbH\n #\n # SPDX-License-Identifier: GPL-3.0-or-later\n #\n@@ -21,25 +21,3 @@\n \n def deprecation(message: str):\n     warnings.warn(message, DeprecationWarning, stacklevel=2)\n-\n-\n-def get_version_string(version: tuple) -> str:\n-    \"\"\"Create a version string from a version tuple\n-\n-    Arguments:\n-        version: version as tuple e.g. (1, 2, 0, dev, 5)\n-\n-    Returns:\n-        The version tuple converted into a string representation\n-    \"\"\"\n-    if len(version) > 4:\n-        ver = \".\".join(str(x) for x in version[:4])\n-        ver += str(version[4])\n-\n-        if len(version) > 5:\n-            # support (1, 2, 3, 'beta', 2, 'dev', 1)\n-            ver += \".{0}{1}\".format(str(version[5]), str(version[6]))\n-\n-        return ver\n-    else:\n-        return \".\".join(str(x) for x in version)\ndiff --git a/gvm/version.py b/gvm/version.py\nnew file mode 100644\n--- /dev/null\n+++ b/gvm/version.py\n@@ -0,0 +1,280 @@\n+# -*- coding: utf-8 -*-\n+# Copyright (C) 2020 Greenbone Networks GmbH\n+#\n+# SPDX-License-Identifier: GPL-3.0-or-later\n+#\n+# This program is free software: you can redistribute it and/or modify\n+# it under the terms of the GNU General Public License as published by\n+# the Free Software Foundation, either version 3 of the License, or\n+# (at your option) any later version.\n+#\n+# This program is distributed in the hope that it will be useful,\n+# but WITHOUT ANY WARRANTY; without even the implied warranty of\n+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n+# GNU General Public License for more details.\n+#\n+# You should have received a copy of the GNU General Public License\n+# along with this program.  If not, see <http://www.gnu.org/licenses/>.\n+\n+import argparse\n+import re\n+import sys\n+\n+from pathlib import Path\n+\n+import tomlkit\n+\n+from packaging.version import Version, InvalidVersion\n+\n+\n+from gvm import get_version\n+\n+\n+def strip_version(version: str) -> str:\n+    \"\"\"\n+    Strips a leading 'v' from a version string\n+\n+    E.g. v1.2.3 will be converted to 1.2.3\n+    \"\"\"\n+    if version and version[0] == 'v':\n+        return version[1:]\n+\n+    return version\n+\n+\n+def safe_version(version: str) -> str:\n+    \"\"\"\n+    Returns the version as a string in `PEP440`_ compliant\n+    format.\n+\n+    .. _PEP440:\n+       https://www.python.org/dev/peps/pep-0440\n+    \"\"\"\n+    try:\n+        return str(Version(version))\n+    except InvalidVersion:\n+        version = version.replace(' ', '.')\n+        return re.sub('[^A-Za-z0-9.]+', '-', version)\n+\n+\n+def get_version_from_pyproject_toml(pyproject_toml_path: Path = None) -> str:\n+    \"\"\"\n+    Return the version information from the [tool.poetry] section of the\n+    pyproject.toml file. The version may be in non standardized form.\n+    \"\"\"\n+    if not pyproject_toml_path:\n+        path = Path(__file__)\n+        pyproject_toml_path = path.parent.parent / 'pyproject.toml'\n+\n+    if not pyproject_toml_path.exists():\n+        raise RuntimeError('pyproject.toml file not found.')\n+\n+    pyproject_toml = tomlkit.parse(pyproject_toml_path.read_text())\n+    if (\n+        'tool' in pyproject_toml\n+        and 'poetry' in pyproject_toml['tool']\n+        and 'version' in pyproject_toml['tool']['poetry']\n+    ):\n+        return pyproject_toml['tool']['poetry']['version']\n+\n+    raise RuntimeError('Version information not found in pyproject.toml file.')\n+\n+\n+def get_version_string(version: tuple) -> str:\n+    \"\"\"Create a version string from a version tuple\n+\n+    Arguments:\n+        version: version as tuple e.g. (1, 2, 0, dev, 5)\n+\n+    Returns:\n+        The version tuple converted into a string representation\n+    \"\"\"\n+    if len(version) > 4:\n+        ver = \".\".join(str(x) for x in version[:4])\n+        ver += str(version[4])\n+\n+        if len(version) > 5:\n+            # support (1, 2, 3, 'beta', 2, 'dev', 1)\n+            ver += \".{0}{1}\".format(str(version[5]), str(version[6]))\n+\n+        return ver\n+    else:\n+        return \".\".join(str(x) for x in version)\n+\n+\n+def print_version(pyproject_toml_path: Path = None) -> None:\n+    pyproject_version = get_version_from_pyproject_toml(\n+        pyproject_toml_path=pyproject_toml_path\n+    )\n+\n+    print(pyproject_version)\n+\n+\n+def versions_equal(new_version: str, old_version: str) -> bool:\n+    \"\"\"\n+    Checks if new_version and old_version are equal\n+    \"\"\"\n+    return safe_version(old_version) == safe_version(new_version)\n+\n+\n+def is_version_pep440_compliant(version: str) -> bool:\n+    \"\"\"\n+    Checks if the provided version is a PEP 440 compliant version string\n+    \"\"\"\n+    return version == safe_version(version)\n+\n+\n+def update_pyproject_version(\n+    new_version: str, pyproject_toml_path: Path,\n+) -> None:\n+    \"\"\"\n+    Update the version in the pyproject.toml file\n+    \"\"\"\n+    version = safe_version(new_version)\n+\n+    pyproject_toml = tomlkit.parse(pyproject_toml_path.read_text())\n+\n+    if 'tool' not in pyproject_toml:\n+        tool_table = tomlkit.table()\n+        pyproject_toml['tool'] = tool_table\n+\n+    if 'poetry' not in pyproject_toml['tool']:\n+        poetry_table = tomlkit.table()\n+        pyproject_toml['tool'].add('poetry', poetry_table)\n+\n+    pyproject_toml['tool']['poetry']['version'] = version\n+\n+    pyproject_toml_path.write_text(tomlkit.dumps(pyproject_toml))\n+\n+\n+def update_version_file(new_version: str, version_file_path: Path) -> None:\n+    \"\"\"\n+    Update the version file with the new version\n+    \"\"\"\n+    version = safe_version(new_version)\n+\n+    text = \"\"\"# pylint: disable=invalid-name\n+\n+# THIS IS AN AUTOGENERATED FILE. DO NOT TOUCH!\n+\n+__version__ = \"{}\"\\n\"\"\".format(\n+        version\n+    )\n+    version_file_path.write_text(text)\n+\n+\n+def _update_python_gvm_version(\n+    new_version: str, pyproject_toml_path: Path, *, force: bool = False\n+):\n+    if not pyproject_toml_path.exists():\n+        sys.exit(\n+            'Could not find pyproject.toml file in the current working dir.'\n+        )\n+\n+    cwd_path = Path.cwd()\n+    python_gvm_version = get_version()\n+    pyproject_version = get_version_from_pyproject_toml(\n+        pyproject_toml_path=pyproject_toml_path\n+    )\n+    version_file_path = cwd_path / 'gvm' / '__version__.py'\n+\n+    if not pyproject_toml_path.exists():\n+        sys.exit(\n+            'Could not find __version__.py file at {}.'.format(\n+                version_file_path\n+            )\n+        )\n+\n+    if not force and versions_equal(new_version, python_gvm_version):\n+        print('Version is already up-to-date.')\n+        sys.exit(0)\n+\n+    update_pyproject_version(\n+        new_version=new_version, pyproject_toml_path=pyproject_toml_path\n+    )\n+\n+    update_version_file(\n+        new_version=new_version, version_file_path=version_file_path,\n+    )\n+\n+    print(\n+        'Updated version from {} to {}'.format(\n+            pyproject_version, safe_version(new_version)\n+        )\n+    )\n+\n+\n+def _verify_version(version: str, pyproject_toml_path: Path) -> None:\n+    python_gvm_version = get_version()\n+    pyproject_version = get_version_from_pyproject_toml(\n+        pyproject_toml_path=pyproject_toml_path\n+    )\n+    if not is_version_pep440_compliant(python_gvm_version):\n+        sys.exit(\"The version in gvm/__version__.py is not PEP 440 compliant.\")\n+\n+    if pyproject_version != python_gvm_version:\n+        sys.exit(\n+            \"The version set in the pyproject.toml file \\\"{}\\\" doesn't \"\n+            \"match the python-gvm version \\\"{}\\\"\".format(\n+                pyproject_version, python_gvm_version\n+            )\n+        )\n+\n+    if version != 'current':\n+        provided_version = strip_version(version)\n+        if provided_version != python_gvm_version:\n+            sys.exit(\n+                \"Provided version \\\"{}\\\" does not match the python-gvm \"\n+                \"version \\\"{}\\\"\".format(provided_version, python_gvm_version)\n+            )\n+\n+    print('OK')\n+\n+\n+def main():\n+    parser = argparse.ArgumentParser(\n+        description='Version handling utilities for python-gvm.', prog='version'\n+    )\n+\n+    subparsers = parser.add_subparsers(\n+        title='subcommands',\n+        description='valid subcommands',\n+        help='additional help',\n+        dest='command',\n+    )\n+\n+    verify_parser = subparsers.add_parser('verify')\n+    verify_parser.add_argument('version', help='version string to compare')\n+\n+    subparsers.add_parser('show')\n+\n+    update_parser = subparsers.add_parser('update')\n+    update_parser.add_argument('version', help='version string to use')\n+    update_parser.add_argument(\n+        '--force',\n+        help=\"don't check if version is already set\",\n+        action=\"store_true\",\n+    )\n+\n+    args = parser.parse_args()\n+\n+    if not getattr(args, 'command', None):\n+        parser.print_usage()\n+        sys.exit(0)\n+\n+    pyproject_toml_path = Path.cwd() / 'pyproject.toml'\n+\n+    if args.command == 'update':\n+        _update_python_gvm_version(\n+            args.version,\n+            pyproject_toml_path=pyproject_toml_path,\n+            force=args.force,\n+        )\n+    elif args.command == 'show':\n+        print_version(pyproject_toml_path=pyproject_toml_path)\n+    elif args.command == 'verify':\n+        _verify_version(args.version, pyproject_toml_path=pyproject_toml_path)\n+\n+\n+if __name__ == '__main__':\n+    main()\n", "test_patch": "diff --git a/tests/version/__init__.py b/tests/version/__init__.py\nnew file mode 100644\ndiff --git a/tests/version/test_get_version_from_pyproject_toml.py b/tests/version/test_get_version_from_pyproject_toml.py\nnew file mode 100644\n--- /dev/null\n+++ b/tests/version/test_get_version_from_pyproject_toml.py\n@@ -0,0 +1,79 @@\n+# -*- coding: utf-8 -*-\n+# Copyright (C) 2020 Greenbone Networks GmbH\n+#\n+# SPDX-License-Identifier: GPL-3.0-or-later\n+#\n+# This program is free software: you can redistribute it and/or modify\n+# it under the terms of the GNU General Public License as published by\n+# the Free Software Foundation, either version 3 of the License, or\n+# (at your option) any later version.\n+#\n+# This program is distributed in the hope that it will be useful,\n+# but WITHOUT ANY WARRANTY; without even the implied warranty of\n+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n+# GNU General Public License for more details.\n+#\n+# You should have received a copy of the GNU General Public License\n+# along with this program.  If not, see <http://www.gnu.org/licenses/>.\n+\n+import unittest\n+\n+from pathlib import Path\n+from unittest.mock import MagicMock\n+\n+from gvm.version import get_version_from_pyproject_toml\n+\n+\n+class GetVersionFromPyprojectTomlTestCase(unittest.TestCase):\n+    def test_pyproject_toml_file_not_exists(self):\n+        fake_path_class = MagicMock(spec=Path)\n+        fake_path = fake_path_class.return_value\n+        fake_path.exists.return_value = False\n+\n+        with self.assertRaisesRegex(\n+            RuntimeError, 'pyproject.toml file not found'\n+        ):\n+            get_version_from_pyproject_toml(fake_path)\n+\n+        fake_path.exists.assert_called_with()\n+\n+    def test_no_poerty_section(self):\n+        fake_path_class = MagicMock(spec=Path)\n+        fake_path = fake_path_class.return_value\n+        fake_path.exists.return_value = True\n+        fake_path.read_text.return_value = ''\n+\n+        with self.assertRaisesRegex(\n+            RuntimeError, 'Version information not found in pyproject.toml file'\n+        ):\n+            get_version_from_pyproject_toml(fake_path)\n+\n+        fake_path.exists.assert_called_with()\n+        fake_path.read_text.assert_called_with()\n+\n+    def test_empty_poerty_section(self):\n+        fake_path_class = MagicMock(spec=Path)\n+        fake_path = fake_path_class.return_value\n+        fake_path.exists.return_value = True\n+        fake_path.read_text.return_value = '[tool.poetry]'\n+\n+        with self.assertRaisesRegex(\n+            RuntimeError, 'Version information not found in pyproject.toml file'\n+        ):\n+            get_version_from_pyproject_toml(fake_path)\n+\n+        fake_path.exists.assert_called_with()\n+        fake_path.read_text.assert_called_with()\n+\n+    def test_get_version(self):\n+        fake_path_class = MagicMock(spec=Path)\n+        fake_path = fake_path_class.return_value\n+        fake_path.exists.return_value = True\n+        fake_path.read_text.return_value = '[tool.poetry]\\nversion = \"1.2.3\"'\n+\n+        version = get_version_from_pyproject_toml(fake_path)\n+\n+        self.assertEqual(version, '1.2.3')\n+\n+        fake_path.exists.assert_called_with()\n+        fake_path.read_text.assert_called_with()\ndiff --git a/tests/utils/test_get_version_string.py b/tests/version/test_get_version_string.py\nsimilarity index 97%\nrename from tests/utils/test_get_version_string.py\nrename to tests/version/test_get_version_string.py\n--- a/tests/utils/test_get_version_string.py\n+++ b/tests/version/test_get_version_string.py\n@@ -18,7 +18,7 @@\n \n import unittest\n \n-from gvm.utils import get_version_string\n+from gvm.version import get_version_string\n \n \n class TestGetVersionString(unittest.TestCase):\ndiff --git a/tests/version/test_is_version_pep440_compliant.py b/tests/version/test_is_version_pep440_compliant.py\nnew file mode 100644\n--- /dev/null\n+++ b/tests/version/test_is_version_pep440_compliant.py\n@@ -0,0 +1,45 @@\n+# -*- coding: utf-8 -*-\n+# Copyright (C) 2020 Greenbone Networks GmbH\n+#\n+# SPDX-License-Identifier: GPL-3.0-or-later\n+#\n+# This program is free software: you can redistribute it and/or modify\n+# it under the terms of the GNU General Public License as published by\n+# the Free Software Foundation, either version 3 of the License, or\n+# (at your option) any later version.\n+#\n+# This program is distributed in the hope that it will be useful,\n+# but WITHOUT ANY WARRANTY; without even the implied warranty of\n+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n+# GNU General Public License for more details.\n+#\n+# You should have received a copy of the GNU General Public License\n+# along with this program.  If not, see <http://www.gnu.org/licenses/>.\n+\n+import unittest\n+\n+from gvm.version import is_version_pep440_compliant\n+\n+\n+class IsVersionPep440CompliantTestCase(unittest.TestCase):\n+    def test_is_compliant(self):\n+        self.assertTrue(is_version_pep440_compliant('1.2.3.dev1'))\n+        self.assertTrue(is_version_pep440_compliant('1.2.3.dev0'))\n+        self.assertTrue(is_version_pep440_compliant('20.4'))\n+        self.assertTrue(is_version_pep440_compliant('1.2'))\n+        self.assertTrue(is_version_pep440_compliant('1.2.0a0'))\n+        self.assertTrue(is_version_pep440_compliant('1.2.0a1'))\n+        self.assertTrue(is_version_pep440_compliant('1.2.0b0'))\n+        self.assertTrue(is_version_pep440_compliant('1.2.0b1'))\n+\n+    def test_is_not_compliant(self):\n+        self.assertFalse(is_version_pep440_compliant('1.2.3dev1'))\n+        self.assertFalse(is_version_pep440_compliant('1.2.3dev'))\n+        self.assertFalse(is_version_pep440_compliant('1.2.3dev0'))\n+        self.assertFalse(is_version_pep440_compliant('1.2.3alpha'))\n+        self.assertFalse(is_version_pep440_compliant('1.2.3alpha0'))\n+        self.assertFalse(is_version_pep440_compliant('1.2.3.a0'))\n+        self.assertFalse(is_version_pep440_compliant('1.2.3beta'))\n+        self.assertFalse(is_version_pep440_compliant('1.2.3beta0'))\n+        self.assertFalse(is_version_pep440_compliant('1.2.3.b0'))\n+        self.assertFalse(is_version_pep440_compliant('20.04'))\ndiff --git a/tests/version/test_safe_version.py b/tests/version/test_safe_version.py\nnew file mode 100644\n--- /dev/null\n+++ b/tests/version/test_safe_version.py\n@@ -0,0 +1,55 @@\n+# -*- coding: utf-8 -*-\n+# Copyright (C) 2020 Greenbone Networks GmbH\n+#\n+# SPDX-License-Identifier: GPL-3.0-or-later\n+#\n+# This program is free software: you can redistribute it and/or modify\n+# it under the terms of the GNU General Public License as published by\n+# the Free Software Foundation, either version 3 of the License, or\n+# (at your option) any later version.\n+#\n+# This program is distributed in the hope that it will be useful,\n+# but WITHOUT ANY WARRANTY; without even the implied warranty of\n+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n+# GNU General Public License for more details.\n+#\n+# You should have received a copy of the GNU General Public License\n+# along with this program.  If not, see <http://www.gnu.org/licenses/>.\n+\n+import unittest\n+\n+from gvm.version import safe_version\n+\n+\n+class SafeVersionTestCase(unittest.TestCase):\n+    def test_dev_versions(self):\n+        self.assertEqual(safe_version('1.2.3dev'), '1.2.3.dev0')\n+        self.assertEqual(safe_version('1.2.3dev1'), '1.2.3.dev1')\n+        self.assertEqual(safe_version('1.2.3.dev'), '1.2.3.dev0')\n+\n+    def test_alpha_versions(self):\n+        self.assertEqual(safe_version('1.2.3alpha'), '1.2.3a0')\n+        self.assertEqual(safe_version('1.2.3.alpha'), '1.2.3a0')\n+        self.assertEqual(safe_version('1.2.3a'), '1.2.3a0')\n+        self.assertEqual(safe_version('1.2.3.a1'), '1.2.3a1')\n+        self.assertEqual(safe_version('1.2.3a1'), '1.2.3a1')\n+\n+    def test_beta_versions(self):\n+        self.assertEqual(safe_version('1.2.3beta'), '1.2.3b0')\n+        self.assertEqual(safe_version('1.2.3.beta'), '1.2.3b0')\n+        self.assertEqual(safe_version('1.2.3b'), '1.2.3b0')\n+        self.assertEqual(safe_version('1.2.3.b1'), '1.2.3b1')\n+        self.assertEqual(safe_version('1.2.3b1'), '1.2.3b1')\n+\n+    def test_caldav_versions(self):\n+        self.assertEqual(safe_version('22.04'), '22.4')\n+        self.assertEqual(safe_version('22.4'), '22.4')\n+        self.assertEqual(safe_version('22.10'), '22.10')\n+        self.assertEqual(safe_version('22.04dev1'), '22.4.dev1')\n+        self.assertEqual(safe_version('22.10dev1'), '22.10.dev1')\n+\n+    def test_release_versions(self):\n+        self.assertEqual(safe_version('1'), '1')\n+        self.assertEqual(safe_version('1.2'), '1.2')\n+        self.assertEqual(safe_version('1.2.3'), '1.2.3')\n+        self.assertEqual(safe_version('22.4'), '22.4')\ndiff --git a/tests/version/test_strip_version.py b/tests/version/test_strip_version.py\nnew file mode 100644\n--- /dev/null\n+++ b/tests/version/test_strip_version.py\n@@ -0,0 +1,31 @@\n+# -*- coding: utf-8 -*-\n+# Copyright (C) 2020 Greenbone Networks GmbH\n+#\n+# SPDX-License-Identifier: GPL-3.0-or-later\n+#\n+# This program is free software: you can redistribute it and/or modify\n+# it under the terms of the GNU General Public License as published by\n+# the Free Software Foundation, either version 3 of the License, or\n+# (at your option) any later version.\n+#\n+# This program is distributed in the hope that it will be useful,\n+# but WITHOUT ANY WARRANTY; without even the implied warranty of\n+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n+# GNU General Public License for more details.\n+#\n+# You should have received a copy of the GNU General Public License\n+# along with this program.  If not, see <http://www.gnu.org/licenses/>.\n+\n+import unittest\n+\n+from gvm.version import strip_version\n+\n+\n+class StripVersionTestCase(unittest.TestCase):\n+    def test_version_string_without_v(self):\n+        self.assertEqual(strip_version('1.2.3'), '1.2.3')\n+        self.assertEqual(strip_version('1.2.3dev'), '1.2.3dev')\n+\n+    def test_version_string_with_v(self):\n+        self.assertEqual(strip_version('v1.2.3'), '1.2.3')\n+        self.assertEqual(strip_version('v1.2.3dev'), '1.2.3dev')\ndiff --git a/tests/version/test_update_pyproject_version.py b/tests/version/test_update_pyproject_version.py\nnew file mode 100644\n--- /dev/null\n+++ b/tests/version/test_update_pyproject_version.py\n@@ -0,0 +1,79 @@\n+# -*- coding: utf-8 -*-\n+# Copyright (C) 2020 Greenbone Networks GmbH\n+#\n+# SPDX-License-Identifier: GPL-3.0-or-later\n+#\n+# This program is free software: you can redistribute it and/or modify\n+# it under the terms of the GNU General Public License as published by\n+# the Free Software Foundation, either version 3 of the License, or\n+# (at your option) any later version.\n+#\n+# This program is distributed in the hope that it will be useful,\n+# but WITHOUT ANY WARRANTY; without even the implied warranty of\n+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n+# GNU General Public License for more details.\n+#\n+# You should have received a copy of the GNU General Public License\n+# along with this program.  If not, see <http://www.gnu.org/licenses/>.\n+\n+import unittest\n+\n+from pathlib import Path\n+from unittest.mock import MagicMock\n+\n+import tomlkit\n+from gvm.version import update_pyproject_version\n+\n+\n+class UpdatePyprojectVersionTestCase(unittest.TestCase):\n+    def test_empty_pyproject_toml(self):\n+        fake_path_class = MagicMock(spec=Path)\n+        fake_path = fake_path_class.return_value\n+        fake_path.read_text.return_value = \"\"\n+\n+        update_pyproject_version('20.04dev1', pyproject_toml_path=fake_path)\n+\n+        text = fake_path.write_text.call_args[0][0]\n+\n+        toml = tomlkit.parse(text)\n+\n+        self.assertEqual(toml['tool']['poetry']['version'], '20.4.dev1')\n+\n+    def test_empty_tool_section(self):\n+        fake_path_class = MagicMock(spec=Path)\n+        fake_path = fake_path_class.return_value\n+        fake_path.read_text.return_value = \"[tool]\"\n+\n+        update_pyproject_version('20.04dev1', pyproject_toml_path=fake_path)\n+\n+        text = fake_path.write_text.call_args[0][0]\n+\n+        toml = tomlkit.parse(text)\n+\n+        self.assertEqual(toml['tool']['poetry']['version'], '20.4.dev1')\n+\n+    def test_empty_tool_poetry_section(self):\n+        fake_path_class = MagicMock(spec=Path)\n+        fake_path = fake_path_class.return_value\n+        fake_path.read_text.return_value = \"[tool.poetry]\"\n+\n+        update_pyproject_version('20.04dev1', pyproject_toml_path=fake_path)\n+\n+        text = fake_path.write_text.call_args[0][0]\n+\n+        toml = tomlkit.parse(text)\n+\n+        self.assertEqual(toml['tool']['poetry']['version'], '20.4.dev1')\n+\n+    def test_override_existing_version(self):\n+        fake_path_class = MagicMock(spec=Path)\n+        fake_path = fake_path_class.return_value\n+        fake_path.read_text.return_value = '[tool.poetry]\\nversion = \"1.2.3\"'\n+\n+        update_pyproject_version('20.04dev1', pyproject_toml_path=fake_path)\n+\n+        text = fake_path.write_text.call_args[0][0]\n+\n+        toml = tomlkit.parse(text)\n+\n+        self.assertEqual(toml['tool']['poetry']['version'], '20.4.dev1')\ndiff --git a/verify-version.py b/tests/version/test_update_version_file.py\nsimilarity index 57%\nrename from verify-version.py\nrename to tests/version/test_update_version_file.py\n--- a/verify-version.py\n+++ b/tests/version/test_update_version_file.py\n@@ -16,32 +16,23 @@\n # You should have received a copy of the GNU General Public License\n # along with this program.  If not, see <http://www.gnu.org/licenses/>.\n \n-import sys\n+import unittest\n \n-from gvm import get_version\n+from pathlib import Path\n+from unittest.mock import MagicMock\n \n+from gvm.version import update_version_file\n \n-def strip_version(version: str) -> str:\n-    if not version:\n-        return version\n \n-    if version[0] == 'v':\n-        return version[1:]\n+class UpdateVersionFileTestCase(unittest.TestCase):\n+    def test_update_version_file(self):\n+        fake_path_class = MagicMock(spec=Path)\n+        fake_path = fake_path_class.return_value\n \n+        update_version_file('22.04dev1', fake_path)\n \n-def main():\n-    if len(sys.argv) < 2:\n-        sys.exit('Missing argument for version.')\n-        return\n+        text = fake_path.write_text.call_args[0][0]\n \n-    p_version = strip_version(sys.argv[1])\n-    version = get_version()\n-    if p_version != version:\n-        sys.exit(\n-            \"Provided version: {} does not match the python-gvm \"\n-            \"version: {}\".format(p_version, version)\n-        )\n+        *_, version_line, _last_line = text.split('\\n')\n \n-\n-if __name__ == '__main__':\n-    main()\n+        self.assertEqual(version_line, '__version__ = \"22.4.dev1\"')\ndiff --git a/tests/version/test_versions_equal.py b/tests/version/test_versions_equal.py\nnew file mode 100644\n--- /dev/null\n+++ b/tests/version/test_versions_equal.py\n@@ -0,0 +1,37 @@\n+# -*- coding: utf-8 -*-\n+# Copyright (C) 2020 Greenbone Networks GmbH\n+#\n+# SPDX-License-Identifier: GPL-3.0-or-later\n+#\n+# This program is free software: you can redistribute it and/or modify\n+# it under the terms of the GNU General Public License as published by\n+# the Free Software Foundation, either version 3 of the License, or\n+# (at your option) any later version.\n+#\n+# This program is distributed in the hope that it will be useful,\n+# but WITHOUT ANY WARRANTY; without even the implied warranty of\n+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n+# GNU General Public License for more details.\n+#\n+# You should have received a copy of the GNU General Public License\n+# along with this program.  If not, see <http://www.gnu.org/licenses/>.\n+\n+import unittest\n+\n+from gvm.version import versions_equal\n+\n+\n+class VersionEqualTestCase(unittest.TestCase):\n+    def test_version_equal(self):\n+        self.assertTrue(versions_equal('1.2.3', '1.2.3'))\n+        self.assertTrue(versions_equal('1.2.3a', '1.2.3a0'))\n+        self.assertTrue(versions_equal('1.2.3a0', '1.2.3.a0'))\n+        self.assertTrue(versions_equal('1.2.3dev1', '1.2.3.dev1'))\n+\n+    def test_version_not_equal(self):\n+        self.assertFalse(versions_equal('1.2.3', '1.2'))\n+        self.assertFalse(versions_equal('1.2.3a', '1.2.3a1'))\n+        self.assertFalse(versions_equal('1.2.3a0', '1.2.3.a1'))\n+        self.assertFalse(versions_equal('1.2.3dev', '1.2.3dev1'))\n+        self.assertFalse(versions_equal('1.2.3dev', '1.2.3.dev1'))\n+        self.assertFalse(versions_equal('1.2.3.dev1', '1.2.3.dev2'))\n", "problem_statement": "", "hints_text": "", "created_at": "2020-04-01T13:55:00Z"}