From 22b63ba94c7073f53afded70d37e53a5e35d2fa7 Mon Sep 17 00:00:00 2001 From: Dieter Baron Date: Wed, 10 Apr 2024 11:26:41 +0200 Subject: [PATCH] Add stdout-replace directive. --- CMakeLists.txt | 2 +- NEWS.md | 4 +++- manpages/nihtest-case.mdoc | 10 +++++++++- nihtest/Test.py | 10 +++++----- nihtest/TestCase.py | 7 +++++++ nihtest/__main__.py | 2 +- pyproject.toml | 4 ++-- tests/stdout-replace-pass.input | 8 ++++++++ tests/stdout-replace-pass.test | 5 +++++ 9 files changed, 41 insertions(+), 11 deletions(-) create mode 100644 tests/stdout-replace-pass.input create mode 100644 tests/stdout-replace-pass.test diff --git a/CMakeLists.txt b/CMakeLists.txt index 876565f..3822faa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.12) # Also update version in nihtest/__main__.py and pyproject.toml project(nihtest - VERSION 1.5.2 + VERSION 1.6.0 DESCRIPTION "NiH testing framework" HOMEPAGE_URL "https://github.com/nih-at/nihtest" LANGUAGES C) diff --git a/NEWS.md b/NEWS.md index d80b441..db4ce8e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,8 @@ -# 1.5.3 [Unreleased] +# 1.6.0 [2024-04-10] - Print command line for `--verbose --setup-only`. +- Add `stdout-replace` directive. +- Better integration with IDEs. # 1.5.2 [2024-03-20] diff --git a/manpages/nihtest-case.mdoc b/manpages/nihtest-case.mdoc index 6549679..2e35fbb 100644 --- a/manpages/nihtest-case.mdoc +++ b/manpages/nihtest-case.mdoc @@ -29,7 +29,7 @@ .\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN .\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. .\" -.Dd November 20, 2023 +.Dd April 10, 2024 .Dt NIHTEST-CASE 5 .Os .Sh NAME @@ -197,6 +197,14 @@ If is given, the output is compared with that file, otherwise the expected text is taken from the following lines of the test case, up to a line consisting of .Dq end-of-inline-data . +.It Ic stdout-replace Ar pattern replacement +Run regular expression replacement over the standard output +and the expected output as provided by +.Ic stdout +before comparing them. +See +.Ic stderr-replace +for details. .\" .It Ic ulimit Ar C VALUE .\" Set .\" .Xr ulimit 1 diff --git a/nihtest/Test.py b/nihtest/Test.py index 1ee30c8..2a61a80 100644 --- a/nihtest/Test.py +++ b/nihtest/Test.py @@ -15,7 +15,7 @@ from nihtest import Utility -def process_stderr_line(line, replacements): +def process_output_line(line, replacements): for replacement in replacements: line = re.sub(replacement[0], replacement[1], line) return line @@ -105,8 +105,8 @@ def run(self): output = Output.Output(self.case.file_name + ":1: test case failed", self.case.configuration.verbose != Configuration.When.NEVER) self.compare(output, "exit code", [str(self.case.exit_code)], [str(command.exit_code)]) - self.compare(output,"output", self.case.stdout, command.stdout) - self.compare(output, "error output", self.case.stderr, self.process_stderr(command.stderr)) + self.compare(output,"output", self.case.stdout, self.process_output_replace(command.stdout, self.case.stdout_replace)) + self.compare(output, "error output", self.case.stderr, self.process_output_replace(command.stderr, self.case.stderr_replace)) files_expected = [] for file in self.case.files: @@ -176,5 +176,5 @@ def precheck_passed(self): command.run() return command.exit_code == 0 - def process_stderr(self, lines): - return list(map(lambda line: process_stderr_line(line, self.case.stderr_replace), lines)) + def process_output_replace(self, lines, replacements): + return list(map(lambda line: process_output_line(line, replacements), lines)) diff --git a/nihtest/TestCase.py b/nihtest/TestCase.py index 8df7c53..5b5c97d 100644 --- a/nihtest/TestCase.py +++ b/nihtest/TestCase.py @@ -54,6 +54,7 @@ def __init__(self, configuration, args): self.stderr_replace = [] self.stdin = [] self.stdout = [] + self.stdout_replace = [] self.ok = True self.directories = [] self.modification_times = {} @@ -203,6 +204,9 @@ def directive_stdin(self, arguments): def directive_stdout(self, arguments): self.stdout = self.io_data(arguments) + def directive_stdout_replace(self, arguments): + self.stdout_replace.append((re.compile(arguments[0]), arguments[1])) + def directive_working_directory(self, arguments): self.working_directory = arguments[0] @@ -270,6 +274,9 @@ def directive_working_directory(self, arguments): usage="[file]", minimum_arguments=0, maximum_arguments=1, only_once=True), + "stdout-replace": Directive(method=directive_stdout_replace, + usage="pattern replacement", + minimum_arguments=2), "working-directory": Directive(method=directive_working_directory, usage="directory", minimum_arguments=1, diff --git a/nihtest/__main__.py b/nihtest/__main__.py index 8ebf62f..553ff39 100644 --- a/nihtest/__main__.py +++ b/nihtest/__main__.py @@ -4,7 +4,7 @@ from nihtest import Test from nihtest import Configuration -VERSION = "1.5.2" +VERSION = "1.6.0" def main(): diff --git a/pyproject.toml b/pyproject.toml index 7206b47..e971bb6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "nihtest" -version = "1.5.2" +version = "1.6.0" authors = [ { name="Dieter Baron", email="dillo@nih.at" }, { name="Thomas Klausner", email="wiz@gatalith.at"} @@ -30,7 +30,7 @@ dependencies = [ nihtest = "nihtest.__main__:main" [tool.bumpver] -current_version = "1.5.2" +current_version = "1.6.0" version_pattern = "MAJOR.MINOR.PATCH" [tool.bumpver.file_patterns] diff --git a/tests/stdout-replace-pass.input b/tests/stdout-replace-pass.input new file mode 100644 index 0000000..0c17aaa --- /dev/null +++ b/tests/stdout-replace-pass.input @@ -0,0 +1,8 @@ +program nihtest-echo +return 0 +arguments /some/path/tool: This is not a very successful test. +stdout-replace "not (.) very" "\1" +stdout-replace "^/.*: " "" +stdout +This is a successful test. +end-of-inline-data diff --git a/tests/stdout-replace-pass.test b/tests/stdout-replace-pass.test new file mode 100644 index 0000000..ecad578 --- /dev/null +++ b/tests/stdout-replace-pass.test @@ -0,0 +1,5 @@ +program nihtest +arguments test.test +return 0 +file test.test stdout-replace-pass.input +file nihtest.conf nihtest-conf