Skip to content

Commit

Permalink
Add stdout-replace directive.
Browse files Browse the repository at this point in the history
  • Loading branch information
dillof committed Apr 10, 2024
1 parent 7423828 commit 22b63ba
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 11 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -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]

Expand Down
10 changes: 9 additions & 1 deletion manpages/nihtest-case.mdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions nihtest/Test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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))
7 changes: 7 additions & 0 deletions nihtest/TestCase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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]

Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion nihtest/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from nihtest import Test
from nihtest import Configuration

VERSION = "1.5.2"
VERSION = "1.6.0"


def main():
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand All @@ -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]
Expand Down
8 changes: 8 additions & 0 deletions tests/stdout-replace-pass.input
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions tests/stdout-replace-pass.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
program nihtest
arguments test.test
return 0
file test.test stdout-replace-pass.input
file nihtest.conf nihtest-conf

0 comments on commit 22b63ba

Please sign in to comment.