Skip to content

Commit

Permalink
Include file name of test case in output.
Browse files Browse the repository at this point in the history
This allows jumping directly to the test case in IDEs.
  • Loading branch information
dillof committed Apr 10, 2024
1 parent 5e5fa42 commit 7423828
Show file tree
Hide file tree
Showing 17 changed files with 46 additions and 19 deletions.
15 changes: 7 additions & 8 deletions nihtest/File.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self, name, input=None, result=None):
def file_name(self, directory):
return self.name.replace("@SANDBOX@", os.path.abspath(directory))

def compare(self, configuration, directory):
def compare(self, output, configuration, directory):
if not self.result:
return True

Expand Down Expand Up @@ -62,8 +62,8 @@ def compare(self, configuration, directory):
print(f"comparing {self.name} failed:")
print("\n".join(command.stderr))
else:
print(f"{self.name} differs:")
print("\n".join(command.stdout))
output.print(f"{self.name} differs:")
output.print("\n".join(command.stdout))
return command.exit_code == 0
output_data = self.result.data

Expand All @@ -73,21 +73,20 @@ def compare(self, configuration, directory):
# TODO: allow binary data
command = Command.Command(configuration.find_program(preprocessor[0]), arguments, environment=Environment.Environment(configuration).environment)
command.run()
return Utility.compare_lines(self.name, output_data, command.stdout, configuration.verbose != Configuration.When.NEVER)
return Utility.compare_lines(output, self.name, output_data, command.stdout)

if not output_is_binary:
try:
input_data = Utility.read_lines(input_file_name)
return Utility.compare_lines(self.name, output_data, input_data, configuration.verbose != Configuration.When.NEVER)
return Utility.compare_lines(output, self.name, output_data, input_data)
except UnicodeDecodeError:
output_data = "\n".join(output_data)

with open(input_file_name, "rb") as file:
input_data = file.read()
if input_data != output_data:
if configuration.verbose != Configuration.When.NEVER:
print(f"{self.name} differs:")
print("Binary files differ.")
output.print(f"{self.name} differs:")
output.print("Binary files differ.")
return False
return True

Expand Down
13 changes: 13 additions & 0 deletions nihtest/Output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Output:
def __init__(self, header, verbose):
self.header = header
self.header_printed = False
self.verbose = verbose

def print(self, message):
if not self.verbose:
return
if not self.header_printed:
print(self.header)
self.header_printed = True
print(message)
18 changes: 10 additions & 8 deletions nihtest/Test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from nihtest import Configuration
from nihtest import Environment
from nihtest import Features
from nihtest import Output
from nihtest import TestCase
from nihtest import Sandbox
from nihtest import Utility
Expand Down Expand Up @@ -101,20 +102,22 @@ def run(self):
st = os.stat(full_file)
os.chmod(full_file, st.st_mode | stat.S_IWRITE)

self.compare("exit code", [str(self.case.exit_code)], [str(command.exit_code)])
self.compare("output", self.case.stdout, command.stdout)
self.compare("error output", self.case.stderr, self.process_stderr(command.stderr))
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))

files_expected = []
for file in self.case.files:
if file.result:
files_expected.append(file.file_name(self.sandbox.directory))

self.compare("file list", sorted(files_expected), sorted(files_got))
self.compare(output, "file list", sorted(files_expected), sorted(files_got))

file_content_ok = True
for file in self.case.files:
if file.name in files_got and not file.compare(self.case.configuration, self.sandbox.directory):
if file.name in files_got and not file.compare(output, self.case.configuration, self.sandbox.directory):
file_content_ok = False
if not file_content_ok:
self.failed.append("file contents")
Expand All @@ -130,9 +133,8 @@ def run(self):
else:
return TestResult.OK

def compare(self, description, expected, got):
if not Utility.compare_lines(description, expected, got,
self.case.configuration.verbose != Configuration.When.NEVER):
def compare(self, output, description, expected, got):
if not Utility.compare_lines(output, description, expected, got):
self.failed.append(description)

def list_files(self):
Expand Down
6 changes: 3 additions & 3 deletions nihtest/Utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
from nihtest import CompareArrays


def compare_lines(description, expected, got, verbose):
if not verbose:
def compare_lines(output, description, expected, got):
if not output.verbose:
return expected == got

compare = CompareArrays.CompareArrays(expected, got)
diff = compare.get_diff()
if diff:
print(f"{description} differs:")
output.print(f"{description} differs:")
write_lines(sys.stdout, diff)
return False
return True
Expand Down
1 change: 1 addition & 0 deletions tests/compare-binary-fail.test
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ file nihtest.conf nihtest-conf
file binary-1 binary-1
file binary-2 binary-2
stdout
test.test:1: test case failed
a differs:
Binary files differ.
test -- FAIL: file contents
Expand Down
1 change: 1 addition & 0 deletions tests/diff-1.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ return 1
file diff-1.test diff-1.input
file nihtest.conf nihtest-conf
stdout
diff-1.test:1: test case failed
output differs:
-E
A
Expand Down
1 change: 1 addition & 0 deletions tests/diff-2.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ return 1
file diff-2.test diff-2.input
file nihtest.conf nihtest-conf
stdout
diff-2.test:1: test case failed
output differs:
+E
A
Expand Down
1 change: 1 addition & 0 deletions tests/false-fail.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ return 1
file false-fail.test false-fail.input
file nihtest.conf nihtest-conf
stdout
false-fail.test:1: test case failed
exit code differs:
-0
+1
Expand Down
1 change: 1 addition & 0 deletions tests/features-fail.test
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ file features-fail.test features-fail.input
file nihtest.conf nihtest-conf
file config.h config.h
stdout
features-fail.test:1: test case failed
exit code differs:
-0
+1
Expand Down
1 change: 1 addition & 0 deletions tests/file-del-fail.test
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ file file-del-fail.test file-del-fail.input
file success.txt success.txt
file nihtest.conf nihtest-conf
stdout
file-del-fail.test:1: test case failed
file list differs:
+testfile
file-del-fail -- FAIL: file list
Expand Down
1 change: 1 addition & 0 deletions tests/file-fail.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ return 1
file file-fail.test file-fail.input
file nihtest.conf nihtest-conf
stdout
file-fail.test:1: test case failed
testfile differs:
-This is a successful test.
+This is not a successful test.
Expand Down
1 change: 1 addition & 0 deletions tests/file-inline-fail.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ return 1
file file-inline-fail.test file-inline-fail.input
file nihtest.conf nihtest-conf
stdout
file-inline-fail.test:1: test case failed
inline-both differs:
-This is the second inline data.
+This is the first inline data.
Expand Down
1 change: 1 addition & 0 deletions tests/file-new-fail.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ return 1
file file-new-fail.test file-new-fail.input
file nihtest.conf nihtest-conf
stdout
file-new-fail.test:1: test case failed
file list differs:
-testfile
file-new-fail -- FAIL: file list
Expand Down
1 change: 1 addition & 0 deletions tests/precheck-fail.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ return 1
file precheck-fail.test precheck-fail.input
file nihtest.conf nihtest-conf
stdout
precheck-fail.test:1: test case failed
exit code differs:
-0
+1
Expand Down
1 change: 1 addition & 0 deletions tests/stderr-fail.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ return 1
file stderr-fail.test stderr-fail.input
file nihtest.conf nihtest-conf
stdout
stderr-fail.test:1: test case failed
error output differs:
-This is a successful test.
+This is not a successful test.
Expand Down
1 change: 1 addition & 0 deletions tests/stdout-fail.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ return 1
file stdout-fail.test stdout-fail.input
file nihtest.conf nihtest-conf
stdout
stdout-fail.test:1: test case failed
output differs:
-This is a successful test.
+This is not a successful test.
Expand Down
1 change: 1 addition & 0 deletions tests/true-fail.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ return 1
file true-fail.test true-fail.input
file nihtest.conf nihtest-conf
stdout
true-fail.test:1: test case failed
exit code differs:
-1
+0
Expand Down

0 comments on commit 7423828

Please sign in to comment.