test(annotations): cover secret masking

This commit is contained in:
evangelostsak committed 2026-10-25 15:10:44 +01:00
1 parent 0d01df182b
commit 55940aef88
1 file changed
+38 -1
+38 -1
View File
@@ -5,7 +5,14 @@ import unittest
from io import StringIO
from unittest.mock import patch
from utils.annotations.message import error, error_each, notice, warning, warning_each
from utils.annotations.message import (
error,
error_each,
mask,
notice,
warning,
warning_each,
)
GHA_ENV = {"GITHUB_ACTIONS": "true"}
NO_GHA_ENV = {"GITHUB_ACTIONS": ""}
@@ -112,5 +119,35 @@ class TestAnnotationsPlainText(unittest.TestCase):
self.assertIn("beta", lines[1])
class TestMask(unittest.TestCase):
def _capture(self, env: dict[str, str], value: str) -> tuple[str, str]:
with (
patch.dict(os.environ, env),
patch("sys.stdout", new_callable=StringIO) as out,
patch("sys.stderr", new_callable=StringIO) as err,
):
mask(value)
return out.getvalue(), err.getvalue()
def test_a_secret_is_registered_on_stderr_only(self) -> None:
out, err = self._capture(GHA_ENV, "s3cr€t")
self.assertEqual(out, "")
self.assertEqual(err, "::add-mask::s3cr€t\n")
def test_each_line_of_a_multiline_secret_is_registered(self) -> None:
_, err = self._capture(GHA_ENV, "-----BEGIN-----\nabc\n\n-----END-----\n")
self.assertEqual(
err.splitlines(),
[
"::add-mask::-----BEGIN-----",
"::add-mask::abc",
"::add-mask::-----END-----",
],
)
def test_nothing_is_printed_outside_github_actions(self) -> None:
self.assertEqual(self._capture(NO_GHA_ENV, "s3cret"), ("", ""))
if __name__ == "__main__":
unittest.main()