tweak test failure output. write to a temp file not stdout. test still fails because expected is not just an identical copy of input

This commit is contained in:
Rod Sheeter 2018-01-30 19:27:11 -08:00
parent cf403e1a53
commit e9d154ac8d
2 changed files with 66 additions and 26 deletions

View File

@ -9,6 +9,7 @@ import io
import os
import subprocess
import sys
import tempfile
from subset_test_suite import SubsetTestSuite
@ -20,21 +21,39 @@ def cmd(command):
print (p.stderr.read (), end="") # file=sys.stderr
return p.stdout.read (), p.returncode
def read_binary(file_path):
with open(file_path, 'rb') as f:
return f.read()
def fail_test(test, cli_args, message):
print ('ERROR: %s' % message)
print ('Test State:')
print (' test.font_path %s' % os.path.abspath(test.font_path))
print (' test.profile_path %s' % os.path.abspath(test.profile_path))
print (' test.unicodes %s' % test.unicodes())
expected_file = os.path.join(test_suite.get_output_directory(),
test.get_font_name())
print (' expected_file %s' % os.path.abspath(expected_file))
return 1
def run_test(test):
result, return_code = cmd([hb_subset,
test.font_path,
"--unicodes=%s" % test.unicodes()])
out_file = os.path.join(tempfile.mkdtemp(), test.get_font_name() + '-subset.ttf')
cli_args = [hb_subset, test.font_path, out_file, "--unicodes=%s" % test.unicodes()]
_, return_code = cmd(cli_args)
if return_code:
print ("ERROR: hb-subset failed for %s, %s, %s" % (test.font_path, test.profile_path, test.unicodes()))
return 1
return fail_test(test, cli_args, "%s returned %d" % (' '.join(cli_args), return_code))
with open(os.path.join(test_suite.get_output_directory(),
test.get_font_name())) as expected:
if not result == expected.read():
print ("ERROR: hb-subset %s, %s, %s does not match expected value." % (test.font_path, test.profile_path, test.unicodes()))
return 1
expected = read_binary(os.path.join(test_suite.get_output_directory(),
test.get_font_name()))
actual = read_binary(out_file)
if len(actual) != len(expected):
return fail_test(test, cli_args, "expected %d bytes, actual %d: %s" % (
len(expected), len(actual), ' '.join(cli_args)))
if not actual == expected:
return fail_test(test, cli_args, 'files are the same length but not the same bytes')
return 0

View File

@ -13,37 +13,52 @@ int
main (int argc, char **argv)
{
int exit_code = 0;
int fd = open("/tmp/Lobster-Regular.ttf", O_RDONLY);
if (argc != 4) {
fprintf(stderr, "Must have 4 args\n");
exit(1);
}
int fd = open(argv[1], O_RDONLY);
if (fd == -1) {
perror("Unable to open file");
perror("Unable to open font file");
exit(1);
}
void *mapped_file = MAP_FAILED;
char *raw_font = nullptr;
int fd_out = -1;
struct stat stat;
if (fstat(fd, &stat) != -1) {
printf("File is %zu bytes\n", stat.st_size);
void *mapped_file = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (mapped_file != MAP_FAILED) {
raw_font = static_cast<char*>(mapped_file);
} else {
mapped_file = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (mapped_file == MAP_FAILED) {
perror("Failed to map file");
}
} else {
perror("Unable to fstat");
}
if (raw_font) {
printf("Mapped file\n");
for (int i = 0; i < 4; i++) {
printf("%02x", *(raw_font + i));
}
printf("\n");
if (mapped_file != MAP_FAILED) {
hb_blob_t *font_blob = hb_blob_create(static_cast<const char*>(mapped_file),
stat.st_size,
HB_MEMORY_MODE_READONLY, nullptr,
nullptr);
hb_blob_t *font_blob = hb_blob_create(raw_font, stat.st_size, HB_MEMORY_MODE_READONLY, nullptr, nullptr);
fd_out = open(argv[2], O_CREAT | O_WRONLY, S_IRWXU);
if (fd_out != -1) {
ssize_t bytes_written = write(fd_out, mapped_file, stat.st_size);
if (bytes_written == -1) {
perror("Unable to write output file");
exit_code = 1;
} else if (bytes_written != stat.st_size) {
fprintf(stderr, "Wrong number of bytes written");
exit_code = 1;
}
} else {
perror("Unable to open output file");
exit_code = 1;
}
}
if (mapped_file != MAP_FAILED) {
@ -53,9 +68,15 @@ main (int argc, char **argv)
}
}
if (close(fd) == -1) {
if (fd_out != -1 && close(fd_out) == -1) {
perror("Unable to close output file");
exit_code = 1;
}
if (fd != -1 && close(fd) == -1) {
perror("Unable to close file");
exit_code = 1;
}
return exit_code;
}