File: test_core.py

package info (click to toggle)
pontos 25.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,744 kB
  • sloc: python: 44,602; makefile: 21; sh: 10; xml: 3
file content (172 lines) | stat: -rw-r--r-- 5,518 bytes parent folder | download | duplicates (2)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# SPDX-FileCopyrightText: 2021-2023 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later
#

import unittest
from unittest.mock import patch

from pontos.github.actions.core import ActionIO, Console
from pontos.github.actions.errors import GitHubActionsError
from pontos.testing import temp_directory


@patch("builtins.print")
class ConsoleTestCase(unittest.TestCase):
    def test_start_group(self, print_mock):
        Console.start_group("Foo")
        print_mock.assert_called_once_with("::group::Foo")

    def test_end_group(self, print_mock):
        Console.end_group()
        print_mock.assert_called_once_with("::endgroup::")

    def test_group(self, print_mock):
        with Console.group("Foo"):
            print_mock.assert_called_once_with("::group::Foo")

        print_mock.assert_called_with("::endgroup::")

    def test_warning(self, print_mock):
        Console.warning(
            "foo",
            name="bar",
            line="123",
            end_line="234",
            column="1",
            end_column="2",
            title="Foo Bar",
        )
        print_mock.assert_called_once_with(
            "::warning file=bar,line=123,endLine=234,col=1,endColumn=2,title=Foo Bar::foo"  # pylint: disable=line-too-long # noqa: E501
        )

    def test_error(self, print_mock):
        Console.error(
            "foo",
            name="bar",
            line="123",
            end_line="234",
            column="1",
            end_column="2",
            title="Foo Bar",
        )
        print_mock.assert_called_once_with(
            "::error file=bar,line=123,endLine=234,col=1,endColumn=2,title=Foo Bar::foo"  # pylint: disable=line-too-long # noqa: E501
        )

    def test_notice(self, print_mock):
        Console.notice(
            "foo",
            name="bar",
            line="123",
            end_line="234",
            column="1",
            end_column="2",
            title="Foo Bar",
        )
        print_mock.assert_called_once_with(
            "::notice file=bar,line=123,endLine=234,col=1,endColumn=2,title=Foo Bar::foo"  # pylint: disable=line-too-long # noqa: E501
        )

    def test_log(self, print_mock):
        Console.log("foo")

        print_mock.assert_called_once_with("foo")

    def test_debug(self, print_mock):
        Console.debug("foo")

        print_mock.assert_called_once_with("::debug::foo")


class ActionIOTestCase(unittest.TestCase):
    @patch.dict(
        "os.environ", {"INPUT_FOO": "1234", "INPUT_FOO_BAR": "2345"}, clear=True
    )
    def test_input(self):
        self.assertEqual(ActionIO.input("foo"), "1234")
        self.assertEqual(ActionIO.input("FOO"), "1234")
        self.assertEqual(ActionIO.input("FoO"), "1234")

        self.assertEqual(ActionIO.input("foo bar"), "2345")
        self.assertEqual(ActionIO.input("FOO_BAR"), "2345")
        self.assertEqual(ActionIO.input("FoO BaR"), "2345")

    def test_output(self):
        with temp_directory() as temp_dir:
            file_path = temp_dir / "github.output"

            with patch.dict(
                "os.environ", {"GITHUB_OUTPUT": str(file_path)}, clear=True
            ):
                ActionIO.output("foo", "bar")
                ActionIO.output("lorem", "ipsum")

                output = file_path.read_text(encoding="utf8")

                self.assertEqual(output, "foo=bar\nlorem=ipsum\n")

    @patch("uuid.uuid1")
    def test_multiline_output(self, uuid_mock):
        deadbeef = "deadbeef"
        name = "foo"
        ml_string = """bar
baz
boing"""
        expected_output = f"{name}<<{deadbeef}{ml_string}{deadbeef}"
        uuid_mock.return_value = deadbeef
        with temp_directory() as temp_dir:
            file_path = temp_dir / "github.output"

            with patch.dict(
                "os.environ", {"GITHUB_OUTPUT": str(file_path)}, clear=True
            ):
                ActionIO.multiline_output("foo", ml_string)

                output = file_path.read_text(encoding="utf8")

                self.assertEqual(output, expected_output)

    @patch.dict("os.environ", {}, clear=True)
    def test_output_no_env(self):
        with self.assertRaises(GitHubActionsError):
            ActionIO.output("foo", "bar")

    @patch.dict("os.environ", {"GITHUB_OUTPUT": ""}, clear=True)
    def test_output_empty_env(self):
        with self.assertRaises(GitHubActionsError):
            ActionIO.output("foo", "bar")

    @patch.dict("os.environ", {}, clear=True)
    def test_no_github_output(self):
        self.assertFalse(ActionIO.has_output())

    @patch.dict(
        "os.environ", {"GITHUB_OUTPUT": "/foo/github.output"}, clear=True
    )
    def test_has_github_output(self):
        self.assertTrue(ActionIO.has_output())

    def test_out(self):
        with temp_directory() as temp_dir:
            outfile = temp_dir / "github.output"
            with patch.dict(
                "os.environ",
                {"GITHUB_OUTPUT": str(outfile.absolute())},
                clear=True,
            ):
                with ActionIO.out() as output:
                    output.write("foo", "bar")

            self.assertEqual(outfile.read_text(encoding="utf8"), "foo=bar\n")

    @patch.dict("os.environ", {}, clear=True)
    def test_out_failure(self):
        with self.assertRaisesRegex(
            GitHubActionsError,
            "GITHUB_OUTPUT environment variable not set. Can't write "
            "action output.",
        ):
            with ActionIO.out():
                pass