From bc92fabb8bb657e52c5daf41ab7002a5612538f8 Mon Sep 17 00:00:00 2001 From: Ben Grande Date: Sun, 4 May 2025 01:36:00 +0200 Subject: [PATCH 1/6] Add tests --- run-tests | 2 +- usr/bin/stcat | 16 +---- usr/bin/stcatn | 22 +------ usr/bin/stecho | 15 +---- usr/bin/stprint | 14 +--- usr/bin/stsponge | 26 +------- usr/bin/sttee | 19 +----- .../python3/dist-packages/stdisplay/stcat.py | 27 ++++++++ .../python3/dist-packages/stdisplay/stcatn.py | 33 ++++++++++ .../python3/dist-packages/stdisplay/stecho.py | 24 +++++++ .../dist-packages/stdisplay/stprint.py | 23 +++++++ .../dist-packages/stdisplay/stsponge.py | 37 +++++++++++ .../python3/dist-packages/stdisplay/sttee.py | 30 +++++++++ .../dist-packages/stdisplay/tests/__init__.py | 0 .../dist-packages/stdisplay/tests/stcat.py | 65 +++++++++++++++++++ .../dist-packages/stdisplay/tests/stcatn.py | 52 +++++++++++++++ .../stdisplay/tests/stdisplay.py | 0 .../dist-packages/stdisplay/tests/stecho.py | 51 +++++++++++++++ .../dist-packages/stdisplay/tests/stprint.py | 47 +++++++++++++- .../dist-packages/stdisplay/tests/stsponge.py | 52 +++++++++++++++ .../dist-packages/stdisplay/tests/sttee.py | 52 +++++++++++++++ 21 files changed, 499 insertions(+), 108 deletions(-) create mode 100755 usr/lib/python3/dist-packages/stdisplay/stcat.py create mode 100755 usr/lib/python3/dist-packages/stdisplay/stcatn.py create mode 100755 usr/lib/python3/dist-packages/stdisplay/stecho.py create mode 100755 usr/lib/python3/dist-packages/stdisplay/stprint.py create mode 100755 usr/lib/python3/dist-packages/stdisplay/stsponge.py create mode 100755 usr/lib/python3/dist-packages/stdisplay/sttee.py mode change 100755 => 100644 usr/lib/python3/dist-packages/stdisplay/tests/__init__.py create mode 100644 usr/lib/python3/dist-packages/stdisplay/tests/stcat.py create mode 100644 usr/lib/python3/dist-packages/stdisplay/tests/stcatn.py mode change 100755 => 100644 usr/lib/python3/dist-packages/stdisplay/tests/stdisplay.py create mode 100644 usr/lib/python3/dist-packages/stdisplay/tests/stecho.py mode change 100755 => 100644 usr/lib/python3/dist-packages/stdisplay/tests/stprint.py create mode 100644 usr/lib/python3/dist-packages/stdisplay/tests/stsponge.py create mode 100644 usr/lib/python3/dist-packages/stdisplay/tests/sttee.py diff --git a/run-tests b/run-tests index 1ad52aa1..21812c2e 100755 --- a/run-tests +++ b/run-tests @@ -5,7 +5,7 @@ pyrc="${git_toplevel}/pyproject.toml" pythonpath="${git_toplevel}/usr/lib/python3/dist-packages" export PYTHONPATH="${pythonpath}${PYTHONPATH+":${PYTHONPATH}"}" -pytest=(python3 -m pytest -o 'python_files=*.py') +pytest=(python3 -m pytest -o 'python_files=tests/*.py') black=(black --config="${pyrc}" --color --diff --check) pylint=(pylint --rcfile="${pyrc}") mypy=(mypy --config-file="${pyrc}") diff --git a/usr/bin/stcat b/usr/bin/stcat index da0cd351..11e8fc17 100755 --- a/usr/bin/stcat +++ b/usr/bin/stcat @@ -5,21 +5,7 @@ ## ## SPDX-License-Identifier: AGPL-3.0-or-later -"""Safely print stdin or file to stdout.""" - -from fileinput import input as file_input -from sys import stdin, stdout -from stdisplay.stdisplay import stdisplay - - -def main() -> None: - """Safely print stdin or file to stdout.""" - stdin.reconfigure(errors="ignore") # type: ignore - ## File input reads stdin when no file is provided or file is '-'. - for untrusted_text in file_input(encoding="ascii", errors="replace"): - stdout.write(stdisplay(untrusted_text)) - stdout.flush() - +from stdisplay.stcat import main if __name__ == "__main__": main() diff --git a/usr/bin/stcatn b/usr/bin/stcatn index e649339e..89b33787 100755 --- a/usr/bin/stcatn +++ b/usr/bin/stcatn @@ -5,27 +5,7 @@ ## ## SPDX-License-Identifier: AGPL-3.0-or-later -""" -Safely print stdin or file to stdout with tweaks -(trim trailing whitespace, ensure final newline). -""" - -from fileinput import input as file_input -from sys import stdin, stdout -from stdisplay.stdisplay import stdisplay - - -def main() -> None: - """ - main - """ - stdin.reconfigure(errors="ignore") # type: ignore - - for line in file_input(encoding="ascii", errors="replace"): - stdout.write(stdisplay(line).rstrip() + "\n") - - stdout.flush() - +from stdisplay.stcatn import main if __name__ == "__main__": main() diff --git a/usr/bin/stecho b/usr/bin/stecho index 4f3c02de..b58af5e8 100755 --- a/usr/bin/stecho +++ b/usr/bin/stecho @@ -5,20 +5,7 @@ ## ## SPDX-License-Identifier: AGPL-3.0-or-later -"""Safely print argument to stdout with echo's formatting.""" - -from sys import argv, stdout -from stdisplay.stdisplay import stdisplay - - -def main() -> None: - """Safely print argument to stdout with echo's formatting.""" - if len(argv) > 1: - untrusted_text = " ".join(argv[1:]) - stdout.write(stdisplay(untrusted_text)) - stdout.write("\n") - stdout.flush() - +from stdisplay.stecho import main if __name__ == "__main__": main() diff --git a/usr/bin/stprint b/usr/bin/stprint index 6a2cc0dc..6cf48e5b 100755 --- a/usr/bin/stprint +++ b/usr/bin/stprint @@ -5,19 +5,7 @@ ## ## SPDX-License-Identifier: AGPL-3.0-or-later -"""Safely print argument to stdout.""" - -from sys import argv, stdout -from stdisplay.stdisplay import stdisplay - - -def main() -> None: - """Safely print argument to stdout.""" - if len(argv) > 1: - untrusted_text = "".join(argv[1:]) - stdout.write(stdisplay(untrusted_text)) - stdout.flush() - +from stdisplay.stprint import main if __name__ == "__main__": main() diff --git a/usr/bin/stsponge b/usr/bin/stsponge index efb4d250..29343ec3 100755 --- a/usr/bin/stsponge +++ b/usr/bin/stsponge @@ -5,31 +5,7 @@ ## ## SPDX-License-Identifier: AGPL-3.0-or-later -"""Safely print stdin to stdout or file.""" - -from sys import argv, stdin, stdout -import shutil -from tempfile import NamedTemporaryFile -from stdisplay.stdisplay import stdisplay - - -def main() -> None: - """Safely print stdin to stdout or file.""" - untrusted_text_list = [] - stdin.reconfigure(errors="ignore") # type: ignore - for untrusted_text in stdin: - untrusted_text_list.append(untrusted_text) - if len(argv) == 1: - stdout.write(stdisplay("".join(untrusted_text_list))) - stdout.flush() - else: - with NamedTemporaryFile(mode="w", delete=False) as temp_file: - temp_file.write(stdisplay("".join(untrusted_text_list))) - temp_file.flush() - for file in argv[1:]: - shutil.copy2(temp_file.name, file) - temp_file.close() - +from stdisplay.stsponge import main if __name__ == "__main__": main() diff --git a/usr/bin/sttee b/usr/bin/sttee index 585ef224..661da9c2 100755 --- a/usr/bin/sttee +++ b/usr/bin/sttee @@ -5,24 +5,7 @@ ## ## SPDX-License-Identifier: AGPL-3.0-or-later -"""Safely print stdin to stdout and file.""" -from sys import argv, stdin, stdout -from stdisplay.stdisplay import stdisplay - - -def main() -> None: - """Safely print stdin to stdout and file.""" - untrusted_text_list = [] - stdin.reconfigure(errors="ignore") # type: ignore - for untrusted_text in stdin: - untrusted_text_list.append(untrusted_text) - stdout.write(stdisplay(untrusted_text)) - stdout.flush() - if len(argv) > 1: - for file_arg in argv[1:]: - with open(file_arg, mode="w", encoding="ascii") as file: - file.write(stdisplay("".join(untrusted_text_list))) - +from stdisplay.sttee import main if __name__ == "__main__": main() diff --git a/usr/lib/python3/dist-packages/stdisplay/stcat.py b/usr/lib/python3/dist-packages/stdisplay/stcat.py new file mode 100755 index 00000000..9163d97a --- /dev/null +++ b/usr/lib/python3/dist-packages/stdisplay/stcat.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 + +## SPDX-FileCopyrightText: 2025 Benjamin Grande M. S. +## SPDX-FileCopyrightText: 2025 ENCRYPTED SUPPORT LLC +## +## SPDX-License-Identifier: AGPL-3.0-or-later + +"""Safely print stdin or file to stdout.""" + +from fileinput import input as file_input +from sys import stdin, stdout, modules +from stdisplay.stdisplay import stdisplay + + +def main() -> None: + """Safely print stdin or file to stdout.""" + # https://github.com/pytest-dev/pytest/issues/4843 + if "pytest" not in modules: + stdin.reconfigure(errors="ignore") # type: ignore + ## File input reads stdin when no file is provided or file is '-'. + for untrusted_text in file_input(encoding="ascii", errors="replace"): + stdout.write(stdisplay(untrusted_text)) + stdout.flush() + + +if __name__ == "__main__": + main() diff --git a/usr/lib/python3/dist-packages/stdisplay/stcatn.py b/usr/lib/python3/dist-packages/stdisplay/stcatn.py new file mode 100755 index 00000000..3abf9397 --- /dev/null +++ b/usr/lib/python3/dist-packages/stdisplay/stcatn.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 + +## SPDX-FileCopyrightText: 2025 Benjamin Grande M. S. +## SPDX-FileCopyrightText: 2025 ENCRYPTED SUPPORT LLC +## +## SPDX-License-Identifier: AGPL-3.0-or-later + +""" +Safely print stdin or file to stdout with tweaks +(trim trailing whitespace, ensure final newline). +""" + +from fileinput import input as file_input +from sys import stdin, stdout, modules +from stdisplay.stdisplay import stdisplay + + +def main() -> None: + """ + main + """ + # https://github.com/pytest-dev/pytest/issues/4843 + if "pytest" not in modules: + stdin.reconfigure(errors="ignore") # type: ignore + + for line in file_input(encoding="ascii", errors="replace"): + stdout.write(stdisplay(line).rstrip() + "\n") + + stdout.flush() + + +if __name__ == "__main__": + main() diff --git a/usr/lib/python3/dist-packages/stdisplay/stecho.py b/usr/lib/python3/dist-packages/stdisplay/stecho.py new file mode 100755 index 00000000..4f3c02de --- /dev/null +++ b/usr/lib/python3/dist-packages/stdisplay/stecho.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python3 + +## SPDX-FileCopyrightText: 2025 Benjamin Grande M. S. +## SPDX-FileCopyrightText: 2025 ENCRYPTED SUPPORT LLC +## +## SPDX-License-Identifier: AGPL-3.0-or-later + +"""Safely print argument to stdout with echo's formatting.""" + +from sys import argv, stdout +from stdisplay.stdisplay import stdisplay + + +def main() -> None: + """Safely print argument to stdout with echo's formatting.""" + if len(argv) > 1: + untrusted_text = " ".join(argv[1:]) + stdout.write(stdisplay(untrusted_text)) + stdout.write("\n") + stdout.flush() + + +if __name__ == "__main__": + main() diff --git a/usr/lib/python3/dist-packages/stdisplay/stprint.py b/usr/lib/python3/dist-packages/stdisplay/stprint.py new file mode 100755 index 00000000..f914f0c5 --- /dev/null +++ b/usr/lib/python3/dist-packages/stdisplay/stprint.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 + +## SPDX-FileCopyrightText: 2025 Benjamin Grande M. S. +## SPDX-FileCopyrightText: 2025 ENCRYPTED SUPPORT LLC +## +## SPDX-License-Identifier: AGPL-3.0-or-later + +## """Safely print argument to stdout.""" + +from sys import argv, stdout +from stdisplay.stdisplay import stdisplay + + +def main() -> None: + """Safely print argument to stdout.""" + if len(argv) > 1: + untrusted_text = "".join(argv[1:]) + stdout.write(stdisplay(untrusted_text)) + stdout.flush() + + +if __name__ == "__main__": + main() diff --git a/usr/lib/python3/dist-packages/stdisplay/stsponge.py b/usr/lib/python3/dist-packages/stdisplay/stsponge.py new file mode 100755 index 00000000..9bcc97e2 --- /dev/null +++ b/usr/lib/python3/dist-packages/stdisplay/stsponge.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 + +## SPDX-FileCopyrightText: 2025 Benjamin Grande M. S. +## SPDX-FileCopyrightText: 2025 ENCRYPTED SUPPORT LLC +## +## SPDX-License-Identifier: AGPL-3.0-or-later + +"""Safely print stdin to stdout or file.""" + +from sys import argv, stdin, stdout, modules +import shutil +from tempfile import NamedTemporaryFile +from stdisplay.stdisplay import stdisplay + + +def main() -> None: + """Safely print stdin to stdout or file.""" + # https://github.com/pytest-dev/pytest/issues/4843 + if "pytest" not in modules: + stdin.reconfigure(errors="ignore") # type: ignore + untrusted_text_list = [] + for untrusted_text in stdin: + untrusted_text_list.append(untrusted_text) + if len(argv) == 1: + stdout.write(stdisplay("".join(untrusted_text_list))) + stdout.flush() + else: + with NamedTemporaryFile(mode="w", delete=False) as temp_file: + temp_file.write(stdisplay("".join(untrusted_text_list))) + temp_file.flush() + for file in argv[1:]: + shutil.copy2(temp_file.name, file) + temp_file.close() + + +if __name__ == "__main__": + main() diff --git a/usr/lib/python3/dist-packages/stdisplay/sttee.py b/usr/lib/python3/dist-packages/stdisplay/sttee.py new file mode 100755 index 00000000..ec1218fa --- /dev/null +++ b/usr/lib/python3/dist-packages/stdisplay/sttee.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 + +## SPDX-FileCopyrightText: 2025 Benjamin Grande M. S. +## SPDX-FileCopyrightText: 2025 ENCRYPTED SUPPORT LLC +## +## SPDX-License-Identifier: AGPL-3.0-or-later + +"""Safely print stdin to stdout and file.""" +from sys import argv, stdin, stdout, modules +from stdisplay.stdisplay import stdisplay + + +def main() -> None: + """Safely print stdin to stdout and file.""" + # https://github.com/pytest-dev/pytest/issues/4843 + if "pytest" not in modules: + stdin.reconfigure(errors="ignore") # type: ignore + untrusted_text_list = [] + for untrusted_text in stdin: + untrusted_text_list.append(untrusted_text) + stdout.write(stdisplay(untrusted_text)) + stdout.flush() + if len(argv) > 1: + for file_arg in argv[1:]: + with open(file_arg, mode="w", encoding="ascii") as file: + file.write(stdisplay("".join(untrusted_text_list))) + + +if __name__ == "__main__": + main() diff --git a/usr/lib/python3/dist-packages/stdisplay/tests/__init__.py b/usr/lib/python3/dist-packages/stdisplay/tests/__init__.py old mode 100755 new mode 100644 diff --git a/usr/lib/python3/dist-packages/stdisplay/tests/stcat.py b/usr/lib/python3/dist-packages/stdisplay/tests/stcat.py new file mode 100644 index 00000000..463f8494 --- /dev/null +++ b/usr/lib/python3/dist-packages/stdisplay/tests/stcat.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python3 + +import unittest +import sys +from unittest.mock import patch +from test.support import captured_stdout, captured_stdin + + +class TestSTCat(unittest.TestCase): + """ + Test stcat. + """ + + def tearDown(self): + del sys.modules["stdisplay.stcat"] + + # TODO: test reading from file + + def test_stcat_stdin_no_arg(self): + """ + Test stcat sanitization. + """ + argv = ["stcat.py"] + with patch.object( + sys, "argv", argv + ), captured_stdout() as stdout, captured_stdin(): + import stdisplay.stcat # pylint: disable=import-outside-toplevel + stdisplay.stcat.main() + result = stdout.getvalue() # pylint: disable=no-member + self.assertEqual("", result) + + def test_stcat_stdin_dash_arg(self): + """ + Test stcat sanitization. + """ + argv = ["stcat.py", "-"] + with patch.object( + sys, "argv", argv + ), captured_stdout() as stdout, captured_stdin() as stdin: + import stdisplay.stcat # pylint: disable=import-outside-toplevel + stdin.write("Hello world") + stdin.seek(0) + stdisplay.stcat.main() + result = stdout.getvalue() # pylint: disable=no-member + self.assertEqual("Hello world", result) + + + def test_stcat_stdin(self): + """ + Test stcat sanitization. + """ + argv = ["stcat.py"] + with patch.object( + sys, "argv", argv + ), captured_stdout() as stdout, captured_stdin() as stdin: + import stdisplay.stcat # pylint: disable=import-outside-toplevel + stdin.write("Hello world") + stdin.seek(0) + stdisplay.stcat.main() + result = stdout.getvalue() # pylint: disable=no-member + self.assertEqual("Hello world", result) + + +if __name__ == "__main__": + unittest.main() diff --git a/usr/lib/python3/dist-packages/stdisplay/tests/stcatn.py b/usr/lib/python3/dist-packages/stdisplay/tests/stcatn.py new file mode 100644 index 00000000..dd41f9d7 --- /dev/null +++ b/usr/lib/python3/dist-packages/stdisplay/tests/stcatn.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 + +import unittest +import sys +from unittest.mock import patch +from test.support import captured_stdout + +# TODO: rewrite +class TestSTCatn(unittest.TestCase): + """ + Test stcatn. + """ + + def tearDown(self): + del sys.modules["stdisplay.stcatn"] + + def test_stcatn_no_arg(self): + """ + Test stcatn without argument. + """ + argv = ["stcatn.py"] + with patch.object(sys, "argv", argv), captured_stdout() as stdout: + import stdisplay.stcatn # pylint: disable=import-outside-toplevel + stdisplay.stcatn.main() + result = stdout.getvalue() + self.assertEqual("\n", result) + + def test_stcatn_word_split(self): + """ + Test stcatn word splitting behavior. + """ + argv = ["stcatn.py", "Hello", "world"] + with patch.object(sys, "argv", argv), captured_stdout() as stdout: + import stdisplay.stcatn # pylint: disable=import-outside-toplevel + stdisplay.stcatn.main() + result = stdout.getvalue() + self.assertEqual("Hello world\n", result) + + def test_stcatn_sanitize(self): + """ + Test stcatn sanitization. + """ + argv = ["stcatn.py", "\x1b[0mHello world\x1b[2K"] + with patch.object(sys, "argv", argv), captured_stdout() as stdout: + import stdisplay.stcatn # pylint: disable=import-outside-toplevel + stdisplay.stcatn.main() + result = stdout.getvalue() + self.assertEqual("\x1b[0mHello world_[2K\n", result) + + +if __name__ == "__main__": + unittest.main() diff --git a/usr/lib/python3/dist-packages/stdisplay/tests/stdisplay.py b/usr/lib/python3/dist-packages/stdisplay/tests/stdisplay.py old mode 100755 new mode 100644 diff --git a/usr/lib/python3/dist-packages/stdisplay/tests/stecho.py b/usr/lib/python3/dist-packages/stdisplay/tests/stecho.py new file mode 100644 index 00000000..f9ee4acf --- /dev/null +++ b/usr/lib/python3/dist-packages/stdisplay/tests/stecho.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 + +import unittest +import sys +from unittest.mock import patch +from test.support import captured_stdout + +class TestSTEcho(unittest.TestCase): + """ + Test stecho. + """ + + def tearDown(self): + del sys.modules["stdisplay.stecho"] + + def test_stecho_no_arg(self): + """ + Test stecho without argument. + """ + argv = ["stecho.py"] + with patch.object(sys, "argv", argv), captured_stdout() as stdout: + import stdisplay.stecho # pylint: disable=import-outside-toplevel + stdisplay.stecho.main() + result = stdout.getvalue() + self.assertEqual("\n", result) + + def test_stecho_word_split(self): + """ + Test stecho word splitting behavior. + """ + argv = ["stecho.py", "Hello", "world"] + with patch.object(sys, "argv", argv), captured_stdout() as stdout: + import stdisplay.stecho # pylint: disable=import-outside-toplevel + stdisplay.stecho.main() + result = stdout.getvalue() + self.assertEqual("Hello world\n", result) + + def test_stecho_sanitize(self): + """ + Test stecho sanitization. + """ + argv = ["stecho.py", "\x1b[0mHello world\x1b[2K"] + with patch.object(sys, "argv", argv), captured_stdout() as stdout: + import stdisplay.stecho # pylint: disable=import-outside-toplevel + stdisplay.stecho.main() + result = stdout.getvalue() + self.assertEqual("\x1b[0mHello world_[2K\n", result) + + +if __name__ == "__main__": + unittest.main() diff --git a/usr/lib/python3/dist-packages/stdisplay/tests/stprint.py b/usr/lib/python3/dist-packages/stdisplay/tests/stprint.py old mode 100755 new mode 100644 index 7c07d3c3..f7d5a769 --- a/usr/lib/python3/dist-packages/stdisplay/tests/stprint.py +++ b/usr/lib/python3/dist-packages/stdisplay/tests/stprint.py @@ -5,13 +5,17 @@ """ import os -import unittest import subprocess +import sys +import unittest +from unittest.mock import patch +from test.support import captured_stdout from stdisplay.stdisplay import ( get_sgr_support, ) +# TODO: stop using shell and pass arguments to sys.argv. class TestSTPrint(unittest.TestCase): """ Test stprint @@ -197,6 +201,47 @@ def test_stprint_environment_term_sgr(self) -> None: result = self.shell(text, term="xterm-direct") self.assertEqual(result.stdout, expected_result) +class TestSTPrintArgv(unittest.TestCase): + """ + Test stprint. + """ + + def tearDown(self): + del sys.modules["stdisplay.stprint"] + + def test_stprint_no_arg(self): + """ + Test stprint without argument. + """ + argv = ["stprint.py"] + with patch.object(sys, "argv", argv), captured_stdout() as stdout: + import stdisplay.stprint # pylint: disable=import-outside-toplevel + stdisplay.stprint.main() + result = stdout.getvalue() + self.assertEqual("", result) + + def test_stprint_word_split(self): + """ + Test stprint word splitting behavior. + """ + argv = ["stprint.py", "Hello", "world"] + with patch.object(sys, "argv", argv), captured_stdout() as stdout: + import stdisplay.stprint # pylint: disable=import-outside-toplevel + stdisplay.stprint.main() + result = stdout.getvalue() + self.assertEqual("Helloworld", result) + + def test_stprint_sanitize(self): + """ + Test stprint sanitization. + """ + argv = ["stprint.py", "\x1b[0mHello world\x1b[2K"] + with patch.object(sys, "argv", argv), captured_stdout() as stdout: + import stdisplay.stprint # pylint: disable=import-outside-toplevel + stdisplay.stprint.main() + result = stdout.getvalue() + self.assertEqual("\x1b[0mHello world_[2K", result) + if __name__ == "__main__": unittest.main() diff --git a/usr/lib/python3/dist-packages/stdisplay/tests/stsponge.py b/usr/lib/python3/dist-packages/stdisplay/tests/stsponge.py new file mode 100644 index 00000000..0c43636f --- /dev/null +++ b/usr/lib/python3/dist-packages/stdisplay/tests/stsponge.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 + +import unittest +import sys +from unittest.mock import patch +from test.support import captured_stdout + +# TODO: rewrite +class TestSTSponge(unittest.TestCase): + """ + Test stsponge. + """ + + def tearDown(self): + del sys.modules["stdisplay.stsponge"] + + def test_stsponge_no_arg(self): + """ + Test stsponge without argument. + """ + argv = ["stsponge.py"] + with patch.object(sys, "argv", argv), captured_stdout() as stdout: + import stdisplay.stsponge # pylint: disable=import-outside-toplevel + stdisplay.stsponge.main() + result = stdout.getvalue() + self.assertEqual("\n", result) + + def test_stsponge_word_split(self): + """ + Test stsponge word splitting behavior. + """ + argv = ["stsponge.py", "Hello", "world"] + with patch.object(sys, "argv", argv), captured_stdout() as stdout: + import stdisplay.stsponge # pylint: disable=import-outside-toplevel + stdisplay.stsponge.main() + result = stdout.getvalue() + self.assertEqual("Hello world\n", result) + + def test_stsponge_sanitize(self): + """ + Test stsponge sanitization. + """ + argv = ["stsponge.py", "\x1b[0mHello world\x1b[2K"] + with patch.object(sys, "argv", argv), captured_stdout() as stdout: + import stdisplay.stsponge # pylint: disable=import-outside-toplevel + stdisplay.stsponge.main() + result = stdout.getvalue() + self.assertEqual("\x1b[0mHello world_[2K\n", result) + + +if __name__ == "__main__": + unittest.main() diff --git a/usr/lib/python3/dist-packages/stdisplay/tests/sttee.py b/usr/lib/python3/dist-packages/stdisplay/tests/sttee.py new file mode 100644 index 00000000..e20eb855 --- /dev/null +++ b/usr/lib/python3/dist-packages/stdisplay/tests/sttee.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 + +import unittest +import sys +from unittest.mock import patch +from test.support import captured_stdout + +# TODO: rewrite +class TestSTTee(unittest.TestCase): + """ + Test sttee. + """ + + def tearDown(self): + del sys.modules["stdisplay.sttee"] + + def test_sttee_no_arg(self): + """ + Test sttee without argument. + """ + argv = ["sttee.py"] + with patch.object(sys, "argv", argv), captured_stdout() as stdout: + import stdisplay.sttee # pylint: disable=import-outside-toplevel + stdisplay.sttee.main() + result = stdout.getvalue() + self.assertEqual("\n", result) + + def test_sttee_word_split(self): + """ + Test sttee word splitting behavior. + """ + argv = ["sttee.py", "Hello", "world"] + with patch.object(sys, "argv", argv), captured_stdout() as stdout: + import stdisplay.sttee # pylint: disable=import-outside-toplevel + stdisplay.sttee.main() + result = stdout.getvalue() + self.assertEqual("Hello world\n", result) + + def test_sttee_sanitize(self): + """ + Test sttee sanitization. + """ + argv = ["sttee.py", "\x1b[0mHello world\x1b[2K"] + with patch.object(sys, "argv", argv), captured_stdout() as stdout: + import stdisplay.sttee # pylint: disable=import-outside-toplevel + stdisplay.sttee.main() + result = stdout.getvalue() + self.assertEqual("\x1b[0mHello world_[2K\n", result) + + +if __name__ == "__main__": + unittest.main() From d00ecab2c21d81e63145b6407b888544a8b683d4 Mon Sep 17 00:00:00 2001 From: Ben Grande Date: Sun, 4 May 2025 15:26:25 +0200 Subject: [PATCH 2/6] Test reading from files --- .../dist-packages/stdisplay/tests/stcat.py | 94 +++++++++++++------ 1 file changed, 63 insertions(+), 31 deletions(-) diff --git a/usr/lib/python3/dist-packages/stdisplay/tests/stcat.py b/usr/lib/python3/dist-packages/stdisplay/tests/stcat.py index 463f8494..8ece0426 100644 --- a/usr/lib/python3/dist-packages/stdisplay/tests/stcat.py +++ b/usr/lib/python3/dist-packages/stdisplay/tests/stcat.py @@ -1,7 +1,10 @@ #!/usr/bin/env python3 -import unittest +import os +import shutil import sys +import tempfile +import unittest from unittest.mock import patch from test.support import captured_stdout, captured_stdin @@ -11,54 +14,83 @@ class TestSTCat(unittest.TestCase): Test stcat. """ + def setUp(self): + self.temp_files = [] + contents = ["a b\n", "c d"] + i = 0 + self.temp_dir = tempfile.mkdtemp(prefix="test_stcat_") + while i < 3: + self.temp_files.append(os.path.join(self.temp_dir, str(i))) + with open(self.temp_files[i], "w", encoding="utf-8") as file: + if i == 0: + file.write("") + elif i == 1: + file.write("".join(contents)) + elif i == 2: + file.write("".join(contents) + "\n") + file.flush() + file.close() + i += 1 + def tearDown(self): del sys.modules["stdisplay.stcat"] + shutil.rmtree(self.temp_dir) - # TODO: test reading from file - - def test_stcat_stdin_no_arg(self): - """ - Test stcat sanitization. - """ - argv = ["stcat.py"] - with patch.object( - sys, "argv", argv - ), captured_stdout() as stdout, captured_stdin(): - import stdisplay.stcat # pylint: disable=import-outside-toplevel - stdisplay.stcat.main() - result = stdout.getvalue() # pylint: disable=no-member - self.assertEqual("", result) + def _del_module(self): + module_name = "stdisplay.stcat" + if module_name in sys.modules: + del sys.modules[module_name] + globals().pop(module_name, None) - def test_stcat_stdin_dash_arg(self): + def _test_stcat(self, argv=None, incoming=None): """ - Test stcat sanitization. + Helper function to pass stdin. """ - argv = ["stcat.py", "-"] + self._del_module() + if argv is None: + argv = ["stcat.py"] + else: + argv = ["stcat.py"] + argv with patch.object( sys, "argv", argv ), captured_stdout() as stdout, captured_stdin() as stdin: import stdisplay.stcat # pylint: disable=import-outside-toplevel - stdin.write("Hello world") + + if incoming is not None: + stdin.write(incoming) stdin.seek(0) stdisplay.stcat.main() result = stdout.getvalue() # pylint: disable=no-member - self.assertEqual("Hello world", result) + return result + def test_stcat_file(self): + """ + Test passing files. + """ + cases = [ + ("", [self.temp_files[0], self.temp_files[0]]), + ("a b\nc d", [self.temp_files[0], self.temp_files[1]]), + ("a b\nc d\n", [self.temp_files[0], self.temp_files[2]]), + ("a b\nc da b\nc d\n", [self.temp_files[1], self.temp_files[2]]), + ] + for text, argv in cases: + with self.subTest(text=text, argv=argv): + self.assertEqual(text, self._test_stcat(argv=argv)) def test_stcat_stdin(self): """ - Test stcat sanitization. + Test passing stdin. """ - argv = ["stcat.py"] - with patch.object( - sys, "argv", argv - ), captured_stdout() as stdout, captured_stdin() as stdin: - import stdisplay.stcat # pylint: disable=import-outside-toplevel - stdin.write("Hello world") - stdin.seek(0) - stdisplay.stcat.main() - result = stdout.getvalue() # pylint: disable=no-member - self.assertEqual("Hello world", result) + self.assertEqual("", self._test_stcat()) + self.assertEqual("", self._test_stcat(incoming="")) + self.assertEqual("", self._test_stcat(argv=["-"])) + self.assertEqual( + "Hello world", self._test_stcat(incoming="Hello world") + ) + self.assertEqual( + "Hello world", + self._test_stcat(argv=["-"], incoming="Hello world"), + ) if __name__ == "__main__": From 748a3bedef7cf12df5dc6bdea154d165e8bb499d Mon Sep 17 00:00:00 2001 From: Ben Grande Date: Sun, 4 May 2025 16:27:58 +0200 Subject: [PATCH 3/6] Add base test class --- .../dist-packages/stdisplay/tests/__init__.py | 64 +++++++++++++- .../dist-packages/stdisplay/tests/stcat.py | 85 ++++--------------- 2 files changed, 80 insertions(+), 69 deletions(-) diff --git a/usr/lib/python3/dist-packages/stdisplay/tests/__init__.py b/usr/lib/python3/dist-packages/stdisplay/tests/__init__.py index e5a0d9b4..608ba227 100644 --- a/usr/lib/python3/dist-packages/stdisplay/tests/__init__.py +++ b/usr/lib/python3/dist-packages/stdisplay/tests/__init__.py @@ -1 +1,63 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python3 # pylint: disable=missing-module-docstring + +import os +import shutil +import sys +import tempfile +import unittest +import importlib +from unittest.mock import patch +from test.support import captured_stdout, captured_stdin + + +class TestST(unittest.TestCase): + """ + Base class for testing safe terminal utilities. + """ + + def setUp(self): + self.temp_files = [] + contents = ["a b\n", "c d"] + i = 0 + self.temp_dir = tempfile.mkdtemp() + while i < 3: + self.temp_files.append(os.path.join(self.temp_dir, str(i))) + with open(self.temp_files[i], "w", encoding="utf-8") as file: + if i == 0: + file.write("") + elif i == 1: + file.write("".join(contents)) + elif i == 2: + file.write("".join(contents) + "\n") + file.flush() + file.close() + i += 1 + + def tearDown(self): + shutil.rmtree(self.temp_dir) + + def _del_module(self): + module_name = "stdisplay." + self.module # pylint: disable=no-member + if module_name in sys.modules: + del sys.modules[module_name] + globals().pop(module_name, None) + + def _test_util(self, argv=None, incoming=None): + """ + Helper function to pass stdin. + """ + self._del_module() + if argv is None: + argv = [self.module + ".py"] # pylint: disable=no-member + else: + argv = [self.module + ".py"] + argv # pylint: disable=no-member + with patch.object( + sys, "argv", argv + ), captured_stdout() as stdout, captured_stdin() as stdin: + module = importlib.import_module("stdisplay." + self.module) # pylint: disable=no-member + if incoming is not None: + stdin.write(incoming) + stdin.seek(0) + module.main() + result = stdout.getvalue() + return result diff --git a/usr/lib/python3/dist-packages/stdisplay/tests/stcat.py b/usr/lib/python3/dist-packages/stdisplay/tests/stcat.py index 8ece0426..7029eadd 100644 --- a/usr/lib/python3/dist-packages/stdisplay/tests/stcat.py +++ b/usr/lib/python3/dist-packages/stdisplay/tests/stcat.py @@ -1,67 +1,31 @@ #!/usr/bin/env python3 -import os -import shutil -import sys -import tempfile import unittest -from unittest.mock import patch -from test.support import captured_stdout, captured_stdin +import stdisplay.tests - -class TestSTCat(unittest.TestCase): +class TestSTCat(stdisplay.tests.TestST): """ Test stcat. """ def setUp(self): - self.temp_files = [] - contents = ["a b\n", "c d"] - i = 0 - self.temp_dir = tempfile.mkdtemp(prefix="test_stcat_") - while i < 3: - self.temp_files.append(os.path.join(self.temp_dir, str(i))) - with open(self.temp_files[i], "w", encoding="utf-8") as file: - if i == 0: - file.write("") - elif i == 1: - file.write("".join(contents)) - elif i == 2: - file.write("".join(contents) + "\n") - file.flush() - file.close() - i += 1 - - def tearDown(self): - del sys.modules["stdisplay.stcat"] - shutil.rmtree(self.temp_dir) - - def _del_module(self): - module_name = "stdisplay.stcat" - if module_name in sys.modules: - del sys.modules[module_name] - globals().pop(module_name, None) + self.module = "stcat" + super().setUp() - def _test_stcat(self, argv=None, incoming=None): + def test_stcat_stdin(self): """ - Helper function to pass stdin. + Test passing stdin. """ - self._del_module() - if argv is None: - argv = ["stcat.py"] - else: - argv = ["stcat.py"] + argv - with patch.object( - sys, "argv", argv - ), captured_stdout() as stdout, captured_stdin() as stdin: - import stdisplay.stcat # pylint: disable=import-outside-toplevel - - if incoming is not None: - stdin.write(incoming) - stdin.seek(0) - stdisplay.stcat.main() - result = stdout.getvalue() # pylint: disable=no-member - return result + self.assertEqual("", self._test_util()) + self.assertEqual("", self._test_util(incoming="")) + self.assertEqual("", self._test_util(argv=["-"])) + self.assertEqual( + "Hello world", self._test_util(incoming="Hello world") + ) + self.assertEqual( + "Hello world", + self._test_util(argv=["-"], incoming="Hello world"), + ) def test_stcat_file(self): """ @@ -75,22 +39,7 @@ def test_stcat_file(self): ] for text, argv in cases: with self.subTest(text=text, argv=argv): - self.assertEqual(text, self._test_stcat(argv=argv)) - - def test_stcat_stdin(self): - """ - Test passing stdin. - """ - self.assertEqual("", self._test_stcat()) - self.assertEqual("", self._test_stcat(incoming="")) - self.assertEqual("", self._test_stcat(argv=["-"])) - self.assertEqual( - "Hello world", self._test_stcat(incoming="Hello world") - ) - self.assertEqual( - "Hello world", - self._test_stcat(argv=["-"], incoming="Hello world"), - ) + self.assertEqual(text, self._test_util(argv=argv)) if __name__ == "__main__": From ab5e5e45257100521f520ac6d5b7710ff883b728 Mon Sep 17 00:00:00 2001 From: Ben Grande Date: Mon, 5 May 2025 13:01:59 +0200 Subject: [PATCH 4/6] Test every safe terminal tool - Check output when no argument is provided; - Check if stdin is ignored by default; - Check if stdin is ignored if file is provided; - Check how many arguments a utility accepts; - Check how word splitting works on multiple arguments; - Check if newline is present; and - How it treats the argument '-'. --- pyproject.toml | 2 + run-tests | 3 +- usr/bin/stcat | 1 + usr/bin/stcatn | 1 + usr/bin/stecho | 1 + usr/bin/stprint | 1 + usr/bin/stsponge | 1 + usr/bin/sttee | 1 + .../dist-packages/stdisplay/stprint.py | 2 +- .../dist-packages/stdisplay/tests/__init__.py | 90 +++++++++++++------ .../dist-packages/stdisplay/tests/stcat.py | 45 +++++++--- .../dist-packages/stdisplay/tests/stcatn.py | 67 +++++++------- .../stdisplay/tests/stdisplay.py | 5 ++ .../dist-packages/stdisplay/tests/stecho.py | 59 +++++------- .../dist-packages/stdisplay/tests/stprint.py | 86 ++++++++---------- .../dist-packages/stdisplay/tests/stsponge.py | 87 ++++++++++-------- .../dist-packages/stdisplay/tests/sttee.py | 89 ++++++++++-------- 17 files changed, 303 insertions(+), 238 deletions(-) mode change 100644 => 100755 usr/lib/python3/dist-packages/stdisplay/tests/stprint.py diff --git a/pyproject.toml b/pyproject.toml index 66ae5607..010fdd41 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,6 +2,8 @@ line-length = 79 [tool.mypy] strict="yes" +[tool.pylint.'SIMILARITIES'] +min-similarity-lines=10 [tool.pylint.'FORMAT'] expected-line-ending-format="LF" max-line-length=79 diff --git a/run-tests b/run-tests index 21812c2e..e9accb5c 100755 --- a/run-tests +++ b/run-tests @@ -11,7 +11,8 @@ pylint=(pylint --rcfile="${pyrc}") mypy=(mypy --config-file="${pyrc}") cd "${pythonpath}/stdisplay/" -"${pytest[@]}" "${@}" +# Ideally, these variables should be ignored by the tests... +NO_COLOR="" COLORTERM="" TERM="xterm-direct" "${pytest[@]}" "${@}" "${black[@]}" . find . -type f -name "*.py" -print0 | xargs -0 "${pylint[@]}" "${mypy[@]}" . diff --git a/usr/bin/stcat b/usr/bin/stcat index 11e8fc17..54b7f924 100755 --- a/usr/bin/stcat +++ b/usr/bin/stcat @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +# pylint: disable=missing-module-docstring ## SPDX-FileCopyrightText: 2025 Benjamin Grande M. S. ## SPDX-FileCopyrightText: 2025 ENCRYPTED SUPPORT LLC diff --git a/usr/bin/stcatn b/usr/bin/stcatn index 89b33787..a04f9fb9 100755 --- a/usr/bin/stcatn +++ b/usr/bin/stcatn @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +# pylint: disable=missing-module-docstring ## SPDX-FileCopyrightText: 2025 Benjamin Grande M. S. ## SPDX-FileCopyrightText: 2025 ENCRYPTED SUPPORT LLC diff --git a/usr/bin/stecho b/usr/bin/stecho index b58af5e8..d9820854 100755 --- a/usr/bin/stecho +++ b/usr/bin/stecho @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +# pylint: disable=missing-module-docstring ## SPDX-FileCopyrightText: 2025 Benjamin Grande M. S. ## SPDX-FileCopyrightText: 2025 ENCRYPTED SUPPORT LLC diff --git a/usr/bin/stprint b/usr/bin/stprint index 6cf48e5b..7f8c6d13 100755 --- a/usr/bin/stprint +++ b/usr/bin/stprint @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +# pylint: disable=missing-module-docstring ## SPDX-FileCopyrightText: 2025 Benjamin Grande M. S. ## SPDX-FileCopyrightText: 2025 ENCRYPTED SUPPORT LLC diff --git a/usr/bin/stsponge b/usr/bin/stsponge index 29343ec3..0af27420 100755 --- a/usr/bin/stsponge +++ b/usr/bin/stsponge @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +# pylint: disable=missing-module-docstring ## SPDX-FileCopyrightText: 2025 Benjamin Grande M. S. ## SPDX-FileCopyrightText: 2025 ENCRYPTED SUPPORT LLC diff --git a/usr/bin/sttee b/usr/bin/sttee index 661da9c2..8628a648 100755 --- a/usr/bin/sttee +++ b/usr/bin/sttee @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +# pylint: disable=missing-module-docstring ## SPDX-FileCopyrightText: 2025 Benjamin Grande M. S. ## SPDX-FileCopyrightText: 2025 ENCRYPTED SUPPORT LLC diff --git a/usr/lib/python3/dist-packages/stdisplay/stprint.py b/usr/lib/python3/dist-packages/stdisplay/stprint.py index f914f0c5..6a2cc0dc 100755 --- a/usr/lib/python3/dist-packages/stdisplay/stprint.py +++ b/usr/lib/python3/dist-packages/stdisplay/stprint.py @@ -5,7 +5,7 @@ ## ## SPDX-License-Identifier: AGPL-3.0-or-later -## """Safely print argument to stdout.""" +"""Safely print argument to stdout.""" from sys import argv, stdout from stdisplay.stdisplay import stdisplay diff --git a/usr/lib/python3/dist-packages/stdisplay/tests/__init__.py b/usr/lib/python3/dist-packages/stdisplay/tests/__init__.py index 608ba227..e6ad7620 100644 --- a/usr/lib/python3/dist-packages/stdisplay/tests/__init__.py +++ b/usr/lib/python3/dist-packages/stdisplay/tests/__init__.py @@ -1,63 +1,101 @@ -#!/usr/bin/env python3 # pylint: disable=missing-module-docstring +#!/usr/bin/env python3 +# pylint: disable=missing-module-docstring +## SPDX-FileCopyrightText: 2025 Benjamin Grande M. S. +## SPDX-FileCopyrightText: 2025 ENCRYPTED SUPPORT LLC +## +## SPDX-License-Identifier: AGPL-3.0-or-later + +import importlib import os import shutil import sys import tempfile import unittest -import importlib +from typing import Optional, Any +from test.support import captured_stdout, captured_stdin # type: ignore from unittest.mock import patch -from test.support import captured_stdout, captured_stdin +from stdisplay.stdisplay import get_sgr_support -class TestST(unittest.TestCase): +class TestSTBase(unittest.TestCase): """ Base class for testing safe terminal utilities. """ - def setUp(self): - self.temp_files = [] + def __init__(self, *args: Any, **kwargs: Any) -> None: + self.text_dirty = "\x1b[0mTest\x1b[2Kor\x1b]1;is\x1b\n[m" + self.text_dirty_sanitized = "\x1b[0mTest_[2Kor_]1;is_\n[m" + super().__init__(*args, **kwargs) + + def setUp(self) -> None: + self.tmpfiles_list = [] contents = ["a b\n", "c d"] i = 0 - self.temp_dir = tempfile.mkdtemp() - while i < 3: - self.temp_files.append(os.path.join(self.temp_dir, str(i))) - with open(self.temp_files[i], "w", encoding="utf-8") as file: + self.tmpdir = tempfile.mkdtemp() + while i < 6: + self.tmpfiles_list.append(os.path.join(self.tmpdir, str(i))) + with open(self.tmpfiles_list[i], "w", encoding="utf-8") as file: if i == 0: file.write("") elif i == 1: file.write("".join(contents)) elif i == 2: file.write("".join(contents) + "\n") + elif i == 3: + file.write(self.text_dirty) + elif i in [4, 5]: + pass file.flush() file.close() i += 1 + self.tmpfiles = { + "empty": self.tmpfiles_list[0], + "raw": self.tmpfiles_list[1], + "newline": self.tmpfiles_list[2], + "dirty": self.tmpfiles_list[3], + "fill": self.tmpfiles_list[4], + "fill2": self.tmpfiles_list[5], + } - def tearDown(self): - shutil.rmtree(self.temp_dir) + def tearDown(self) -> None: + shutil.rmtree(self.tmpdir) - def _del_module(self): - module_name = "stdisplay." + self.module # pylint: disable=no-member - if module_name in sys.modules: - del sys.modules[module_name] - globals().pop(module_name, None) + def _del_module(self) -> None: + for module in ["stdisplay." + self.module]: # type: ignore # pylint: disable=no-member + if module in sys.modules: + del sys.modules[module] + globals().pop(module, None) - def _test_util(self, argv=None, incoming=None): + # pylint: disable=too-many-arguments + def _test_util( + self, + argv: Optional[list[str]] = None, + stdin: Optional[str] = None, + ) -> str: """ Helper function to pass stdin. """ self._del_module() if argv is None: - argv = [self.module + ".py"] # pylint: disable=no-member + argv = [self.module + ".py"] # type: ignore # pylint: disable=no-member else: - argv = [self.module + ".py"] + argv # pylint: disable=no-member + argv = [self.module + ".py"] + argv # type: ignore # pylint: disable=no-member with patch.object( sys, "argv", argv - ), captured_stdout() as stdout, captured_stdin() as stdin: - module = importlib.import_module("stdisplay." + self.module) # pylint: disable=no-member - if incoming is not None: - stdin.write(incoming) - stdin.seek(0) + ), captured_stdout() as stdout, captured_stdin() as stdin_patch: + module = importlib.import_module("stdisplay." + self.module) # type: ignore # pylint: disable=no-member + if stdin is not None: + stdin_patch.write(stdin) + stdin_patch.seek(0) module.main() - result = stdout.getvalue() + result = str(stdout.getvalue()) # pylint: disable=no-member return result + + def _get_file(self, file: str) -> str: + """ + Helper function get contents of a file. + """ + with open(file, mode="r", encoding="utf-8") as fileobj: + text = fileobj.read() + return text diff --git a/usr/lib/python3/dist-packages/stdisplay/tests/stcat.py b/usr/lib/python3/dist-packages/stdisplay/tests/stcat.py index 7029eadd..df7293db 100644 --- a/usr/lib/python3/dist-packages/stdisplay/tests/stcat.py +++ b/usr/lib/python3/dist-packages/stdisplay/tests/stcat.py @@ -1,46 +1,67 @@ #!/usr/bin/env python3 +# pylint: disable=missing-module-docstring + +## SPDX-FileCopyrightText: 2025 Benjamin Grande M. S. +## SPDX-FileCopyrightText: 2025 ENCRYPTED SUPPORT LLC +## +## SPDX-License-Identifier: AGPL-3.0-or-later import unittest import stdisplay.tests -class TestSTCat(stdisplay.tests.TestST): + +class TestSTCat(stdisplay.tests.TestSTBase): """ Test stcat. """ - def setUp(self): + def setUp(self) -> None: self.module = "stcat" super().setUp() - def test_stcat_stdin(self): + def test_stcat_stdin(self) -> None: """ Test passing stdin. """ self.assertEqual("", self._test_util()) - self.assertEqual("", self._test_util(incoming="")) + self.assertEqual("", self._test_util(stdin="")) self.assertEqual("", self._test_util(argv=["-"])) + self.assertEqual("a b", self._test_util(stdin="a b")) self.assertEqual( - "Hello world", self._test_util(incoming="Hello world") + "a b", + self._test_util(argv=["-"], stdin="a b"), ) self.assertEqual( - "Hello world", - self._test_util(argv=["-"], incoming="Hello world"), + self.text_dirty_sanitized, + self._test_util(stdin=self.text_dirty), ) - def test_stcat_file(self): + def test_stcat_file(self) -> None: """ Test passing files. """ cases = [ - ("", [self.temp_files[0], self.temp_files[0]]), - ("a b\nc d", [self.temp_files[0], self.temp_files[1]]), - ("a b\nc d\n", [self.temp_files[0], self.temp_files[2]]), - ("a b\nc da b\nc d\n", [self.temp_files[1], self.temp_files[2]]), + ("", [self.tmpfiles["empty"], self.tmpfiles["empty"]]), + ("a b\nc d", [self.tmpfiles["empty"], self.tmpfiles["raw"]]), + ( + "a b\nc d\n", + [self.tmpfiles["empty"], self.tmpfiles["newline"]], + ), + ( + "a b\nc da b\nc d\n", + [self.tmpfiles["raw"], self.tmpfiles["newline"]], + ), + (self.text_dirty_sanitized, [self.tmpfiles["dirty"]]), ] for text, argv in cases: with self.subTest(text=text, argv=argv): self.assertEqual(text, self._test_util(argv=argv)) + self.assertEqual( + "a b\nc d", + self._test_util(stdin="is ignored", argv=[self.tmpfiles["raw"]]), + ) + if __name__ == "__main__": unittest.main() diff --git a/usr/lib/python3/dist-packages/stdisplay/tests/stcatn.py b/usr/lib/python3/dist-packages/stdisplay/tests/stcatn.py index dd41f9d7..9babd318 100644 --- a/usr/lib/python3/dist-packages/stdisplay/tests/stcatn.py +++ b/usr/lib/python3/dist-packages/stdisplay/tests/stcatn.py @@ -1,51 +1,44 @@ #!/usr/bin/env python3 +# pylint: disable=missing-module-docstring + +## SPDX-FileCopyrightText: 2025 Benjamin Grande M. S. +## SPDX-FileCopyrightText: 2025 ENCRYPTED SUPPORT LLC +## +## SPDX-License-Identifier: AGPL-3.0-or-later import unittest -import sys -from unittest.mock import patch -from test.support import captured_stdout +import stdisplay.tests + -# TODO: rewrite -class TestSTCatn(unittest.TestCase): +class TestSTCat(stdisplay.tests.TestSTBase): """ Test stcatn. """ - def tearDown(self): - del sys.modules["stdisplay.stcatn"] + def setUp(self) -> None: + self.module = "stcatn" + super().setUp() - def test_stcatn_no_arg(self): - """ - Test stcatn without argument. - """ - argv = ["stcatn.py"] - with patch.object(sys, "argv", argv), captured_stdout() as stdout: - import stdisplay.stcatn # pylint: disable=import-outside-toplevel - stdisplay.stcatn.main() - result = stdout.getvalue() - self.assertEqual("\n", result) - - def test_stcatn_word_split(self): - """ - Test stcatn word splitting behavior. - """ - argv = ["stcatn.py", "Hello", "world"] - with patch.object(sys, "argv", argv), captured_stdout() as stdout: - import stdisplay.stcatn # pylint: disable=import-outside-toplevel - stdisplay.stcatn.main() - result = stdout.getvalue() - self.assertEqual("Hello world\n", result) - - def test_stcatn_sanitize(self): + def test_stcatn_file(self) -> None: """ - Test stcatn sanitization. + Test passing files. """ - argv = ["stcatn.py", "\x1b[0mHello world\x1b[2K"] - with patch.object(sys, "argv", argv), captured_stdout() as stdout: - import stdisplay.stcatn # pylint: disable=import-outside-toplevel - stdisplay.stcatn.main() - result = stdout.getvalue() - self.assertEqual("\x1b[0mHello world_[2K\n", result) + cases = [ + ("a b\nc d\n", [self.tmpfiles["raw"]]), + ( + "a b\nc d\na b\nc d\n", + [self.tmpfiles["raw"], self.tmpfiles["raw"]], + ), + (self.text_dirty_sanitized + "\n", [self.tmpfiles["dirty"]]), + ] + for text, argv in cases: + with self.subTest(text=text, argv=argv): + self.assertEqual(text, self._test_util(argv=argv)) + + self.assertEqual( + "a b\nc d\n", + self._test_util(stdin="is ignored", argv=[self.tmpfiles["raw"]]), + ) if __name__ == "__main__": diff --git a/usr/lib/python3/dist-packages/stdisplay/tests/stdisplay.py b/usr/lib/python3/dist-packages/stdisplay/tests/stdisplay.py index 9d609d72..d6a61638 100644 --- a/usr/lib/python3/dist-packages/stdisplay/tests/stdisplay.py +++ b/usr/lib/python3/dist-packages/stdisplay/tests/stdisplay.py @@ -1,5 +1,10 @@ #!/usr/bin/env python3 +## SPDX-FileCopyrightText: 2025 Benjamin Grande M. S. +## SPDX-FileCopyrightText: 2025 ENCRYPTED SUPPORT LLC +## +## SPDX-License-Identifier: AGPL-3.0-or-later + """ Test the stdisplay module. """ diff --git a/usr/lib/python3/dist-packages/stdisplay/tests/stecho.py b/usr/lib/python3/dist-packages/stdisplay/tests/stecho.py index f9ee4acf..f943ce38 100644 --- a/usr/lib/python3/dist-packages/stdisplay/tests/stecho.py +++ b/usr/lib/python3/dist-packages/stdisplay/tests/stecho.py @@ -1,50 +1,37 @@ #!/usr/bin/env python3 +# pylint: disable=missing-module-docstring + +## SPDX-FileCopyrightText: 2025 Benjamin Grande M. S. +## SPDX-FileCopyrightText: 2025 ENCRYPTED SUPPORT LLC +## +## SPDX-License-Identifier: AGPL-3.0-or-later import unittest -import sys -from unittest.mock import patch -from test.support import captured_stdout +import stdisplay.tests + -class TestSTEcho(unittest.TestCase): +class TestSTEcho(stdisplay.tests.TestSTBase): """ Test stecho. """ - def tearDown(self): - del sys.modules["stdisplay.stecho"] + def setUp(self) -> None: + self.module = "stecho" + super().setUp() - def test_stecho_no_arg(self): - """ - Test stecho without argument. - """ - argv = ["stecho.py"] - with patch.object(sys, "argv", argv), captured_stdout() as stdout: - import stdisplay.stecho # pylint: disable=import-outside-toplevel - stdisplay.stecho.main() - result = stdout.getvalue() - self.assertEqual("\n", result) - - def test_stecho_word_split(self): - """ - Test stecho word splitting behavior. - """ - argv = ["stecho.py", "Hello", "world"] - with patch.object(sys, "argv", argv), captured_stdout() as stdout: - import stdisplay.stecho # pylint: disable=import-outside-toplevel - stdisplay.stecho.main() - result = stdout.getvalue() - self.assertEqual("Hello world\n", result) - - def test_stecho_sanitize(self): + def test_stecho(self) -> None: """ - Test stecho sanitization. + Test stecho. """ - argv = ["stecho.py", "\x1b[0mHello world\x1b[2K"] - with patch.object(sys, "argv", argv), captured_stdout() as stdout: - import stdisplay.stecho # pylint: disable=import-outside-toplevel - stdisplay.stecho.main() - result = stdout.getvalue() - self.assertEqual("\x1b[0mHello world_[2K\n", result) + self.assertEqual("\n", self._test_util()) + self.assertEqual("\n", self._test_util(argv=[""])) + self.assertEqual("\n", self._test_util(stdin="no stdin")) + self.assertEqual("a b\n", self._test_util(argv=["a b"])) + self.assertEqual("a b\n", self._test_util(argv=["a", "b"])) + self.assertEqual( + self.text_dirty_sanitized + "\n", + self._test_util(argv=[self.text_dirty]), + ) if __name__ == "__main__": diff --git a/usr/lib/python3/dist-packages/stdisplay/tests/stprint.py b/usr/lib/python3/dist-packages/stdisplay/tests/stprint.py old mode 100644 new mode 100755 index f7d5a769..6a3f82f2 --- a/usr/lib/python3/dist-packages/stdisplay/tests/stprint.py +++ b/usr/lib/python3/dist-packages/stdisplay/tests/stprint.py @@ -1,24 +1,51 @@ #!/usr/bin/env python3 +# pylint: disable=missing-module-docstring -""" -Test the stprint module. -""" +## SPDX-FileCopyrightText: 2025 Benjamin Grande M. S. +## SPDX-FileCopyrightText: 2025 ENCRYPTED SUPPORT LLC +## +## SPDX-License-Identifier: AGPL-3.0-or-later import os import subprocess -import sys import unittest -from unittest.mock import patch -from test.support import captured_stdout from stdisplay.stdisplay import ( get_sgr_support, ) +import stdisplay.tests -# TODO: stop using shell and pass arguments to sys.argv. -class TestSTPrint(unittest.TestCase): +class TestSTPrint(stdisplay.tests.TestSTBase): """ - Test stprint + Test stecho. + """ + + def setUp(self) -> None: + self.module = "stprint" + super().setUp() + + def test_stprint(self) -> None: + """ + Test without argument. + """ + self.assertEqual("", self._test_util()) + self.assertEqual("", self._test_util(argv=[""])) + self.assertEqual("", self._test_util(stdin="no stdin")) + self.assertEqual( + "a b", self._test_util(stdin="no stdin", argv=["a b"]) + ) + self.assertEqual("ab", self._test_util(argv=["a", "b"])) + self.assertEqual( + self.text_dirty_sanitized, self._test_util(argv=[self.text_dirty]) + ) + + +class TestSTPrintShell(unittest.TestCase): + """ + Test stdisplay with environment variables using stprint. + + This class only exists because the developer could not find a way to patch + the environment variables correctly on the base class functions. """ # pylint: disable=too-many-arguments @@ -201,47 +228,6 @@ def test_stprint_environment_term_sgr(self) -> None: result = self.shell(text, term="xterm-direct") self.assertEqual(result.stdout, expected_result) -class TestSTPrintArgv(unittest.TestCase): - """ - Test stprint. - """ - - def tearDown(self): - del sys.modules["stdisplay.stprint"] - - def test_stprint_no_arg(self): - """ - Test stprint without argument. - """ - argv = ["stprint.py"] - with patch.object(sys, "argv", argv), captured_stdout() as stdout: - import stdisplay.stprint # pylint: disable=import-outside-toplevel - stdisplay.stprint.main() - result = stdout.getvalue() - self.assertEqual("", result) - - def test_stprint_word_split(self): - """ - Test stprint word splitting behavior. - """ - argv = ["stprint.py", "Hello", "world"] - with patch.object(sys, "argv", argv), captured_stdout() as stdout: - import stdisplay.stprint # pylint: disable=import-outside-toplevel - stdisplay.stprint.main() - result = stdout.getvalue() - self.assertEqual("Helloworld", result) - - def test_stprint_sanitize(self): - """ - Test stprint sanitization. - """ - argv = ["stprint.py", "\x1b[0mHello world\x1b[2K"] - with patch.object(sys, "argv", argv), captured_stdout() as stdout: - import stdisplay.stprint # pylint: disable=import-outside-toplevel - stdisplay.stprint.main() - result = stdout.getvalue() - self.assertEqual("\x1b[0mHello world_[2K", result) - if __name__ == "__main__": unittest.main() diff --git a/usr/lib/python3/dist-packages/stdisplay/tests/stsponge.py b/usr/lib/python3/dist-packages/stdisplay/tests/stsponge.py index 0c43636f..6d6083e1 100644 --- a/usr/lib/python3/dist-packages/stdisplay/tests/stsponge.py +++ b/usr/lib/python3/dist-packages/stdisplay/tests/stsponge.py @@ -1,51 +1,64 @@ #!/usr/bin/env python3 +# pylint: disable=missing-module-docstring disable=duplicate-code + +## SPDX-FileCopyrightText: 2025 Benjamin Grande M. S. +## SPDX-FileCopyrightText: 2025 ENCRYPTED SUPPORT LLC +## +## SPDX-License-Identifier: AGPL-3.0-or-later import unittest -import sys -from unittest.mock import patch -from test.support import captured_stdout +import stdisplay.tests + -# TODO: rewrite -class TestSTSponge(unittest.TestCase): +class TestSTSponge(stdisplay.tests.TestSTBase): """ Test stsponge. """ - def tearDown(self): - del sys.modules["stdisplay.stsponge"] + def setUp(self) -> None: + self.module = "stsponge" + super().setUp() - def test_stsponge_no_arg(self): - """ - Test stsponge without argument. - """ - argv = ["stsponge.py"] - with patch.object(sys, "argv", argv), captured_stdout() as stdout: - import stdisplay.stsponge # pylint: disable=import-outside-toplevel - stdisplay.stsponge.main() - result = stdout.getvalue() - self.assertEqual("\n", result) - - def test_stsponge_word_split(self): - """ - Test stsponge word splitting behavior. - """ - argv = ["stsponge.py", "Hello", "world"] - with patch.object(sys, "argv", argv), captured_stdout() as stdout: - import stdisplay.stsponge # pylint: disable=import-outside-toplevel - stdisplay.stsponge.main() - result = stdout.getvalue() - self.assertEqual("Hello world\n", result) - - def test_stsponge_sanitize(self): + def test_stsponge(self) -> None: """ - Test stsponge sanitization. + Test stsponge. """ - argv = ["stsponge.py", "\x1b[0mHello world\x1b[2K"] - with patch.object(sys, "argv", argv), captured_stdout() as stdout: - import stdisplay.stsponge # pylint: disable=import-outside-toplevel - stdisplay.stsponge.main() - result = stdout.getvalue() - self.assertEqual("\x1b[0mHello world_[2K\n", result) + self.assertEqual("", self._test_util()) + self.assertEqual("", self._test_util(stdin="")) + self.assertEqual("stdin", self._test_util(stdin="stdin")) + # Empty stdin with file argument. + self.assertEqual("", self._test_util(argv=[self.tmpfiles["fill"]])) + self.assertEqual( + "", + self._get_file(file=self.tmpfiles["fill"]), + ) + # Empty stdin when writing to file and file sanitization. + self.assertEqual( + "", + self._test_util( + stdin=self.text_dirty, argv=[self.tmpfiles["fill"]] + ), + ) + self.assertEqual( + self.text_dirty_sanitized, + self._get_file(file=self.tmpfiles["fill"]), + ) + # Empty stdin when writing to multiple files and its sanitization. + self.assertEqual( + "", + self._test_util( + stdin=self.text_dirty, + argv=[self.tmpfiles["fill"], self.tmpfiles["fill2"]], + ), + ) + self.assertEqual( + self.text_dirty_sanitized, + self._get_file(file=self.tmpfiles["fill"]), + ) + self.assertEqual( + self.text_dirty_sanitized, + self._get_file(file=self.tmpfiles["fill2"]), + ) if __name__ == "__main__": diff --git a/usr/lib/python3/dist-packages/stdisplay/tests/sttee.py b/usr/lib/python3/dist-packages/stdisplay/tests/sttee.py index e20eb855..332ebf1e 100644 --- a/usr/lib/python3/dist-packages/stdisplay/tests/sttee.py +++ b/usr/lib/python3/dist-packages/stdisplay/tests/sttee.py @@ -1,51 +1,64 @@ #!/usr/bin/env python3 +# pylint: disable=missing-module-docstring + +## SPDX-FileCopyrightText: 2025 Benjamin Grande M. S. +## SPDX-FileCopyrightText: 2025 ENCRYPTED SUPPORT LLC +## +## SPDX-License-Identifier: AGPL-3.0-or-later import unittest -import sys -from unittest.mock import patch -from test.support import captured_stdout +import stdisplay.tests + -# TODO: rewrite -class TestSTTee(unittest.TestCase): +class TestSTTee(stdisplay.tests.TestSTBase): """ - Test sttee. + Test sttee """ - def tearDown(self): - del sys.modules["stdisplay.sttee"] + def setUp(self) -> None: + self.module = "sttee" + super().setUp() - def test_sttee_no_arg(self): - """ - Test sttee without argument. - """ - argv = ["sttee.py"] - with patch.object(sys, "argv", argv), captured_stdout() as stdout: - import stdisplay.sttee # pylint: disable=import-outside-toplevel - stdisplay.sttee.main() - result = stdout.getvalue() - self.assertEqual("\n", result) - - def test_sttee_word_split(self): - """ - Test sttee word splitting behavior. - """ - argv = ["sttee.py", "Hello", "world"] - with patch.object(sys, "argv", argv), captured_stdout() as stdout: - import stdisplay.sttee # pylint: disable=import-outside-toplevel - stdisplay.sttee.main() - result = stdout.getvalue() - self.assertEqual("Hello world\n", result) - - def test_sttee_sanitize(self): + def test_sttee(self) -> None: """ - Test sttee sanitization. + Test sttee. """ - argv = ["sttee.py", "\x1b[0mHello world\x1b[2K"] - with patch.object(sys, "argv", argv), captured_stdout() as stdout: - import stdisplay.sttee # pylint: disable=import-outside-toplevel - stdisplay.sttee.main() - result = stdout.getvalue() - self.assertEqual("\x1b[0mHello world_[2K\n", result) + self.assertEqual("", self._test_util()) + self.assertEqual("", self._test_util(stdin="")) + self.assertEqual("stdin", self._test_util(stdin="stdin")) + # Empty stdin with file argument. + self.assertEqual("", self._test_util(argv=[self.tmpfiles["fill"]])) + self.assertEqual( + "", + self._get_file(file=self.tmpfiles["fill"]), + ) + # Stdin sanitization and writing to file. + self.assertEqual( + self.text_dirty_sanitized, + self._test_util( + stdin=self.text_dirty, argv=[self.tmpfiles["fill"]] + ), + ) + self.assertEqual( + self.text_dirty_sanitized, + self._get_file(file=self.tmpfiles["fill"]), + ) + # Stdin sanitization and writing to multiple files. + self.assertEqual( + self.text_dirty_sanitized, + self._test_util( + stdin=self.text_dirty, + argv=[self.tmpfiles["fill"], self.tmpfiles["fill2"]], + ), + ) + self.assertEqual( + self.text_dirty_sanitized, + self._get_file(file=self.tmpfiles["fill"]), + ) + self.assertEqual( + self.text_dirty_sanitized, + self._get_file(file=self.tmpfiles["fill2"]), + ) if __name__ == "__main__": From 11a7d0e8006afec6abde3448ead288f7c31c6550 Mon Sep 17 00:00:00 2001 From: Ben Grande Date: Mon, 5 May 2025 17:04:32 +0200 Subject: [PATCH 5/6] Add unicode trojan test --- .github/workflows/lint.yml | 3 ++- unicode-testscript | 14 +++----------- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 96f90c98..0a7b2969 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -38,7 +38,6 @@ jobs: build-essential debhelper dh-python dh-apparmor git config --global --add safe.directory "$GITHUB_WORKSPACE" - uses: actions/checkout@v4 - - name: Test build run: | dpkg-buildpackage -b -i -us -uc @@ -47,3 +46,5 @@ jobs: run: | set -o xtrace ./run-tests + git clone https://github.com/nickboucher/trojan-source + ./unicode-testscript diff --git a/unicode-testscript b/unicode-testscript index da92c304..4f952e8e 100755 --- a/unicode-testscript +++ b/unicode-testscript @@ -3,23 +3,15 @@ ## Copyright (C) 2025 - 2025 ENCRYPTED SUPPORT LLC ## See the file COPYING for copying conditions. -## NOTE: -## Assumes folder 'trojan-source' in the home folder. -## -## cd ~ -## git clone git@github.com:nickboucher/trojan-source.git - -## TODO: Convert into proper unit test and/or CI. - #set -x set -e set -o nounset set -o pipefail set -o errtrace -if [ "$(id -u)" = 0 ]; then - printf "%s\n" "$0: ERROR: No need to run as root!" >&2 - exit 1 +if [ "$(id -u)" = 0 ] && [ -z "${GITHUB_WORKSPACE:-}" ]; then + printf "%s\n" "$0: ERROR: No need to run as root!" >&2 + exit 1 fi folder_list="Assembly Bash C C# C++ Go Java JavaScript Python RegEx Rust Solidity" From 5607d7ba97ca3a9e3fdf876511edddddb5624ee9 Mon Sep 17 00:00:00 2001 From: Ben Grande Date: Sun, 11 May 2025 10:56:40 +0200 Subject: [PATCH 6/6] Fix typos and cleaner code --- .../dist-packages/stdisplay/tests/__init__.py | 20 +++++++++---------- .../dist-packages/stdisplay/tests/stcatn.py | 2 +- .../dist-packages/stdisplay/tests/stprint.py | 2 +- .../dist-packages/stdisplay/tests/stsponge.py | 15 +++++++------- .../dist-packages/stdisplay/tests/sttee.py | 9 +++++---- 5 files changed, 24 insertions(+), 24 deletions(-) diff --git a/usr/lib/python3/dist-packages/stdisplay/tests/__init__.py b/usr/lib/python3/dist-packages/stdisplay/tests/__init__.py index e6ad7620..0c7cf256 100644 --- a/usr/lib/python3/dist-packages/stdisplay/tests/__init__.py +++ b/usr/lib/python3/dist-packages/stdisplay/tests/__init__.py @@ -21,6 +21,14 @@ class TestSTBase(unittest.TestCase): """ Base class for testing safe terminal utilities. + + Assign "self.module" to the module you want to try on the "setup()" using + "super()": + + >>> class TestSTCat(stdisplay.tests.TestSTBase): + >>> def setUp(self) -> None: + >>> self.module = "stcat" + >>> super().setUp() """ def __init__(self, *args: Any, **kwargs: Any) -> None: @@ -31,9 +39,8 @@ def __init__(self, *args: Any, **kwargs: Any) -> None: def setUp(self) -> None: self.tmpfiles_list = [] contents = ["a b\n", "c d"] - i = 0 self.tmpdir = tempfile.mkdtemp() - while i < 6: + for i in range(0, 6): self.tmpfiles_list.append(os.path.join(self.tmpdir, str(i))) with open(self.tmpfiles_list[i], "w", encoding="utf-8") as file: if i == 0: @@ -48,7 +55,6 @@ def setUp(self) -> None: pass file.flush() file.close() - i += 1 self.tmpfiles = { "empty": self.tmpfiles_list[0], "raw": self.tmpfiles_list[1], @@ -91,11 +97,3 @@ def _test_util( module.main() result = str(stdout.getvalue()) # pylint: disable=no-member return result - - def _get_file(self, file: str) -> str: - """ - Helper function get contents of a file. - """ - with open(file, mode="r", encoding="utf-8") as fileobj: - text = fileobj.read() - return text diff --git a/usr/lib/python3/dist-packages/stdisplay/tests/stcatn.py b/usr/lib/python3/dist-packages/stdisplay/tests/stcatn.py index 9babd318..b330bd28 100644 --- a/usr/lib/python3/dist-packages/stdisplay/tests/stcatn.py +++ b/usr/lib/python3/dist-packages/stdisplay/tests/stcatn.py @@ -10,7 +10,7 @@ import stdisplay.tests -class TestSTCat(stdisplay.tests.TestSTBase): +class TestSTCatn(stdisplay.tests.TestSTBase): """ Test stcatn. """ diff --git a/usr/lib/python3/dist-packages/stdisplay/tests/stprint.py b/usr/lib/python3/dist-packages/stdisplay/tests/stprint.py index 6a3f82f2..1ce9ebcd 100755 --- a/usr/lib/python3/dist-packages/stdisplay/tests/stprint.py +++ b/usr/lib/python3/dist-packages/stdisplay/tests/stprint.py @@ -17,7 +17,7 @@ class TestSTPrint(stdisplay.tests.TestSTBase): """ - Test stecho. + Test stprint. """ def setUp(self) -> None: diff --git a/usr/lib/python3/dist-packages/stdisplay/tests/stsponge.py b/usr/lib/python3/dist-packages/stdisplay/tests/stsponge.py index 6d6083e1..354b11ca 100644 --- a/usr/lib/python3/dist-packages/stdisplay/tests/stsponge.py +++ b/usr/lib/python3/dist-packages/stdisplay/tests/stsponge.py @@ -7,6 +7,7 @@ ## SPDX-License-Identifier: AGPL-3.0-or-later import unittest +from pathlib import Path import stdisplay.tests @@ -26,13 +27,13 @@ def test_stsponge(self) -> None: self.assertEqual("", self._test_util()) self.assertEqual("", self._test_util(stdin="")) self.assertEqual("stdin", self._test_util(stdin="stdin")) - # Empty stdin with file argument. + # Empty stdin with file argument produces empty stdout and file. self.assertEqual("", self._test_util(argv=[self.tmpfiles["fill"]])) self.assertEqual( "", - self._get_file(file=self.tmpfiles["fill"]), + Path(self.tmpfiles["fill"]).read_text(encoding="utf-8"), ) - # Empty stdin when writing to file and file sanitization. + # Empty stdout when writing to file and file sanitization. self.assertEqual( "", self._test_util( @@ -41,9 +42,9 @@ def test_stsponge(self) -> None: ) self.assertEqual( self.text_dirty_sanitized, - self._get_file(file=self.tmpfiles["fill"]), + Path(self.tmpfiles["fill"]).read_text(encoding="utf-8"), ) - # Empty stdin when writing to multiple files and its sanitization. + # Empty stdout when writing to multiple files and its sanitization. self.assertEqual( "", self._test_util( @@ -53,11 +54,11 @@ def test_stsponge(self) -> None: ) self.assertEqual( self.text_dirty_sanitized, - self._get_file(file=self.tmpfiles["fill"]), + Path(self.tmpfiles["fill"]).read_text(encoding="utf-8"), ) self.assertEqual( self.text_dirty_sanitized, - self._get_file(file=self.tmpfiles["fill2"]), + Path(self.tmpfiles["fill2"]).read_text(encoding="utf-8"), ) diff --git a/usr/lib/python3/dist-packages/stdisplay/tests/sttee.py b/usr/lib/python3/dist-packages/stdisplay/tests/sttee.py index 332ebf1e..c931bd25 100644 --- a/usr/lib/python3/dist-packages/stdisplay/tests/sttee.py +++ b/usr/lib/python3/dist-packages/stdisplay/tests/sttee.py @@ -7,6 +7,7 @@ ## SPDX-License-Identifier: AGPL-3.0-or-later import unittest +from pathlib import Path import stdisplay.tests @@ -30,7 +31,7 @@ def test_sttee(self) -> None: self.assertEqual("", self._test_util(argv=[self.tmpfiles["fill"]])) self.assertEqual( "", - self._get_file(file=self.tmpfiles["fill"]), + Path(self.tmpfiles["fill"]).read_text(encoding="utf-8"), ) # Stdin sanitization and writing to file. self.assertEqual( @@ -41,7 +42,7 @@ def test_sttee(self) -> None: ) self.assertEqual( self.text_dirty_sanitized, - self._get_file(file=self.tmpfiles["fill"]), + Path(self.tmpfiles["fill"]).read_text(encoding="utf-8"), ) # Stdin sanitization and writing to multiple files. self.assertEqual( @@ -53,11 +54,11 @@ def test_sttee(self) -> None: ) self.assertEqual( self.text_dirty_sanitized, - self._get_file(file=self.tmpfiles["fill"]), + Path(self.tmpfiles["fill"]).read_text(encoding="utf-8"), ) self.assertEqual( self.text_dirty_sanitized, - self._get_file(file=self.tmpfiles["fill2"]), + Path(self.tmpfiles["fill2"]).read_text(encoding="utf-8"), )