From aa613979507b3fcf2c84d46581616ccb64aaa03f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 1 Sep 2022 15:35:39 +0200 Subject: [PATCH] Add: Add tests for all testing functions Ensure all testing functions are working as expected. --- tests/testing/__init__.py | 16 +++ tests/testing/test_testing.py | 187 ++++++++++++++++++++++++++++++++++ 2 files changed, 203 insertions(+) create mode 100644 tests/testing/__init__.py create mode 100644 tests/testing/test_testing.py diff --git a/tests/testing/__init__.py b/tests/testing/__init__.py new file mode 100644 index 000000000..9bb9516b6 --- /dev/null +++ b/tests/testing/__init__.py @@ -0,0 +1,16 @@ +# Copyright (C) 2022 Greenbone Networks GmbH +# +# SPDX-License-Identifier: GPL-3.0-or-later +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . diff --git a/tests/testing/test_testing.py b/tests/testing/test_testing.py new file mode 100644 index 000000000..f05430fc4 --- /dev/null +++ b/tests/testing/test_testing.py @@ -0,0 +1,187 @@ +# Copyright (C) 2022 Greenbone Networks GmbH +# +# SPDX-License-Identifier: GPL-3.0-or-later +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +import unittest +from pathlib import Path + +from pontos.git.git import Git +from pontos.testing import ( + add_sys_path, + ensure_unload_module, + temp_directory, + temp_file, + temp_git_repository, + temp_python_module, + unload_module, +) + + +class AddSysPathTestCase(unittest.TestCase): + def test_add_sys_path(self): + with self.assertRaises(ImportError): + # pylint: disable=import-error,import-outside-toplevel,unused-import + import mymodule + + with temp_file("", name="mymodule.py") as module_path, add_sys_path( + module_path.parent + ): + # pylint: disable=import-error,import-outside-toplevel,unused-import + import mymodule + + unload_module("mymodule") + + +class TempDirectoryTestCase(unittest.TestCase): + def test_temp_directory(self): + with temp_directory() as tmp_dir: + tmp_file = tmp_dir / "test.txt" + tmp_file.write_text("Lorem Ipsum", encoding="utf8") + + self.assertTrue(tmp_dir.exists()) + self.assertTrue(tmp_file.exists()) + + self.assertFalse(tmp_dir.exists()) + self.assertFalse(tmp_file.exists()) + + def test_temp_directory_change_into(self): + old_cwd = Path.cwd() + with temp_directory(change_into=True) as tmp_dir: + new_cwd = Path.cwd() + + self.assertEqual(new_cwd, tmp_dir) + self.assertNotEqual(old_cwd, new_cwd) + + def test_temp_directory_add_to_sys_path(self): + with self.assertRaises(ImportError): + # pylint: disable=import-error,import-outside-toplevel,unused-import + import mymodule2 + + with temp_directory(add_to_sys_path=True) as module_path: + mymodule_file = module_path / "mymodule2.py" + mymodule_file.touch() + + # pylint: disable=import-error,import-outside-toplevel,unused-import + import mymodule2 + + unload_module("mymodule") + + +class TempGitRepositoryTestCase(unittest.TestCase): + def test_temp_git_repository(self): + with temp_git_repository(branch="foo") as git_directory: + test_file = git_directory / "test.txt" + test_file.write_text("Lorem Ipsum", encoding="utf8") + + self.assertTrue(git_directory.exists()) + self.assertTrue(test_file.exists()) + + git = Git(git_directory) + git.add(test_file) + + self.assertFalse(git_directory.exists()) + self.assertFalse(test_file.exists()) + + +class TempFileTestCase(unittest.TestCase): + def test_temp_file(self): + with temp_file("my content") as test_file: + self.assertTrue(test_file.exists()) + self.assertEqual("my content", test_file.read_text(encoding="utf8")) + + self.assertFalse(test_file.exists()) + + def test_temp_file_name(self): + with temp_file("my content", name="foo.txt") as test_file: + self.assertTrue(test_file.exists()) + self.assertEqual(test_file.name, "foo.txt") + self.assertEqual("my content", test_file.read_text(encoding="utf8")) + + self.assertFalse(test_file.exists()) + + def test_temp_file_change_into(self): + old_cwd = Path.cwd() + with temp_file("my content", change_into=True) as test_file: + new_cwd = Path.cwd() + + self.assertTrue(test_file.exists()) + self.assertEqual("my content", test_file.read_text(encoding="utf8")) + + self.assertEqual(new_cwd, test_file.parent) + self.assertNotEqual(old_cwd, new_cwd) + + self.assertFalse(test_file.exists()) + + +class TempPythonModuleTestCase(unittest.TestCase): + def test_temp_python_module(self): + with self.assertRaises(ImportError): + # pylint: disable=import-error,import-outside-toplevel,unused-import + import foo + + with temp_python_module("def foo():\n pass") as module_path: + self.assertTrue(module_path.exists()) + + # pylint: disable=import-error,import-outside-toplevel,unused-import + import foo + + self.assertFalse(module_path.exists()) + + unload_module("foo") + + def test_temp_python_module_name(self): + with self.assertRaises(ImportError): + # pylint: disable=import-error,import-outside-toplevel,unused-import + import mymodule3 + + with temp_python_module( + "def foo():\n pass", name="mymodule3" + ) as module_path: + self.assertTrue(module_path.exists()) + + # pylint: disable=import-error,import-outside-toplevel,unused-import + import mymodule3 + + self.assertFalse(module_path.exists()) + + unload_module("foo") + + +class EnsureUnloadModuleTestCase(unittest.TestCase): + def test_ensure_unload_module(self): + with temp_python_module( + "def foo():\n pass", name="bar" + ), ensure_unload_module("bar"): + # pylint: disable=import-error,import-outside-toplevel,unused-import + import bar + + with self.assertRaises(ImportError): + # pylint: disable=import-error,import-outside-toplevel,unused-import + import bar + + def test_ensure_unload_module_exception(self): + with self.assertRaisesRegex(ValueError, "Ipsum"): + with temp_python_module( + "def func():\n raise ValueError('Ipsum')", name="bar" + ), ensure_unload_module("bar"): + # pylint: disable=import-error,import-outside-toplevel,unused-import + import bar + + bar.func() + + with self.assertRaises(ImportError): + # pylint: disable=import-error,import-outside-toplevel,unused-import + import bar