1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
# SPDX-FileCopyrightText: 2022-2023 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
import io
import unittest
from argparse import ArgumentParser
from contextlib import redirect_stderr
from pontos.github.script.errors import GitHubScriptError
from pontos.github.script.load import (
load_script,
run_add_arguments_function,
run_github_script_function,
)
from pontos.testing import temp_file, temp_python_module
from tests import IsolatedAsyncioTestCase
class LoadScriptTestCase(unittest.TestCase):
def test_load_script(self):
with (
temp_file("def foo():\n\treturn 1", name="foo.py") as f,
load_script(f) as module,
):
self.assertIsNotNone(module)
self.assertIsNotNone(module.foo)
self.assertEqual(module.foo(), 1)
def test_load_script_module(self):
with (
temp_python_module(
"def foo():\n\treturn 1", name="github-foo-script"
),
load_script("github-foo-script") as module,
):
self.assertIsNotNone(module)
self.assertIsNotNone(module.foo)
self.assertEqual(module.foo(), 1)
def test_load_script_failure(self):
with self.assertRaisesRegex(
ModuleNotFoundError, "No module named 'baz'"
):
with load_script("foo/bar/baz.py"):
pass
class RunAddArgumentsFunction(unittest.TestCase):
def test_add_arguments_function(self):
with (
temp_file(
"def add_script_arguments(parser):\n\t"
"parser.add_argument('--foo')",
name="foo.py",
) as f,
load_script(f) as module,
):
parser = ArgumentParser()
run_add_arguments_function(module, parser)
args = parser.parse_args(["--foo", "123"])
self.assertEqual(args.foo, "123")
def test_no_add_arguments_function(self):
with (
temp_file(
"def foo():\n\tpass",
name="foo.py",
) as f,
load_script(f) as module,
):
parser = ArgumentParser()
run_add_arguments_function(module, parser)
with self.assertRaises(SystemExit), redirect_stderr(io.StringIO()):
parser.parse_args(["--foo", "123"])
class RunGithubScriptFunctionTestCase(IsolatedAsyncioTestCase):
def test_run_async_github_script_function(self):
with (
temp_file(
"async def github_script(api, args):\n\treturn 1",
name="foo.py",
) as f,
load_script(f) as module,
):
self.assertEqual(
run_github_script_function(module, "123", 123, {}), 1
)
def test_sync_github_script_function_not_supported(self):
with (
temp_file(
"def github_script(api, args):\n\treturn 1",
name="foo.py",
) as f,
load_script(f) as module,
):
with self.assertRaises(GitHubScriptError):
run_github_script_function(module, "123", 123, {})
def test_no_github_script_function(self):
with (
temp_file(
"def foo():\n\tpass",
name="foo.py",
) as f,
load_script(f) as module,
):
with self.assertRaises(GitHubScriptError):
run_github_script_function(module, "123", 123, {})
|