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 os
import subprocess import subprocess
import sys import sys
import tempfile
from subset_test_suite import SubsetTestSuite from subset_test_suite import SubsetTestSuite
@ -20,21 +21,39 @@ def cmd(command):
print (p.stderr.read (), end="") # file=sys.stderr print (p.stderr.read (), end="") # file=sys.stderr
return p.stdout.read (), p.returncode 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): def run_test(test):
result, return_code = cmd([hb_subset, out_file = os.path.join(tempfile.mkdtemp(), test.get_font_name() + '-subset.ttf')
test.font_path, cli_args = [hb_subset, test.font_path, out_file, "--unicodes=%s" % test.unicodes()]
"--unicodes=%s" % test.unicodes()]) _, return_code = cmd(cli_args)
if return_code: if return_code:
print ("ERROR: hb-subset failed for %s, %s, %s" % (test.font_path, test.profile_path, test.unicodes())) return fail_test(test, cli_args, "%s returned %d" % (' '.join(cli_args), return_code))
return 1
with open(os.path.join(test_suite.get_output_directory(), expected = read_binary(os.path.join(test_suite.get_output_directory(),
test.get_font_name())) as expected: test.get_font_name()))
if not result == expected.read(): actual = read_binary(out_file)
print ("ERROR: hb-subset %s, %s, %s does not match expected value." % (test.font_path, test.profile_path, test.unicodes()))
return 1 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 return 0

View File

@ -13,37 +13,52 @@ int
main (int argc, char **argv) main (int argc, char **argv)
{ {
int exit_code = 0; 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) { if (fd == -1) {
perror("Unable to open file"); perror("Unable to open font file");
exit(1); exit(1);
} }
void *mapped_file = MAP_FAILED; void *mapped_file = MAP_FAILED;
char *raw_font = nullptr; int fd_out = -1;
struct stat stat; struct stat stat;
if (fstat(fd, &stat) != -1) { 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); mapped_file = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (mapped_file != MAP_FAILED) { if (mapped_file == MAP_FAILED) {
raw_font = static_cast<char*>(mapped_file);
} else {
perror("Failed to map file"); perror("Failed to map file");
} }
} else { } else {
perror("Unable to fstat"); perror("Unable to fstat");
} }
if (raw_font) { if (mapped_file != MAP_FAILED) {
printf("Mapped file\n"); hb_blob_t *font_blob = hb_blob_create(static_cast<const char*>(mapped_file),
for (int i = 0; i < 4; i++) { stat.st_size,
printf("%02x", *(raw_font + i)); HB_MEMORY_MODE_READONLY, nullptr,
} nullptr);
printf("\n");
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) { 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"); perror("Unable to close file");
exit_code = 1; exit_code = 1;
} }
return exit_code; return exit_code;
} }