scroggo@google.com | 4fed643 | 2013-05-14 17:50:02 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | # Self-test for skimage. |
| 7 | |
epoger@google.com | 74feb15 | 2013-06-13 19:12:05 +0000 | [diff] [blame] | 8 | import filecmp |
scroggo@google.com | 4fed643 | 2013-05-14 17:50:02 +0000 | [diff] [blame] | 9 | import os |
| 10 | import subprocess |
| 11 | import sys |
scroggo@google.com | 2b9424b | 2013-06-21 19:12:47 +0000 | [diff] [blame] | 12 | import tempfile |
scroggo@google.com | 4fed643 | 2013-05-14 17:50:02 +0000 | [diff] [blame] | 13 | |
| 14 | class BinaryNotFoundException(Exception): |
| 15 | def __str__ (self): |
| 16 | return ("Could not find binary!\n" |
| 17 | "Did you forget to build the tools project?\n" |
| 18 | "Self tests failed") |
| 19 | |
| 20 | # Find a path to the binary to use. Iterates through a list of possible |
| 21 | # locations the binary may be. |
| 22 | def PickBinaryPath(base_dir): |
| 23 | POSSIBLE_BINARY_PATHS = [ |
| 24 | 'out/Debug/skimage', |
| 25 | 'out/Release/skimage', |
| 26 | 'xcodebuild/Debug/skimage', |
| 27 | 'xcodebuild/Release/skimage', |
| 28 | ] |
| 29 | for binary in POSSIBLE_BINARY_PATHS: |
| 30 | binary_full_path = os.path.join(base_dir, binary) |
| 31 | if (os.path.exists(binary_full_path)): |
| 32 | return binary_full_path |
| 33 | raise BinaryNotFoundException |
| 34 | |
epoger@google.com | 74feb15 | 2013-06-13 19:12:05 +0000 | [diff] [blame] | 35 | # Quit early if two files have different content. |
| 36 | def DieIfFilesMismatch(expected, actual): |
| 37 | if not filecmp.cmp(expected, actual): |
| 38 | print 'Error: file mismatch! expected=%s , actual=%s' % ( |
| 39 | expected, actual) |
| 40 | exit(1) |
| 41 | |
scroggo@google.com | 36c5bdb | 2013-10-15 20:29:37 +0000 | [diff] [blame^] | 42 | def test_invalid_file(file_dir, skimage_binary): |
| 43 | """ Test the return value of skimage when an invalid file is decoded. |
| 44 | If there is no expectation file, or the file expects a particular |
| 45 | result, skimage should return nonzero indicating failure. |
| 46 | If the file has no expectation, or ignore-failure is set to true, |
| 47 | skimage should return zero indicating success. """ |
| 48 | invalid_file = os.path.join(file_dir, "skimage", "input", "bad-images", |
| 49 | "invalid.png") |
| 50 | # No expectations file: |
| 51 | args = [skimage_binary, "--readPath", invalid_file] |
| 52 | result = subprocess.call(args) |
| 53 | if 0 == result: |
| 54 | print "'%s' should have reported failure!" % " ".join(args) |
| 55 | exit(1) |
| 56 | |
| 57 | # Directory holding all expectations files |
| 58 | expectations_dir = os.path.join(file_dir, "skimage", "input", "bad-images") |
| 59 | |
| 60 | # Expectations file expecting a valid decode: |
| 61 | incorrect_expectations = os.path.join(expectations_dir, |
| 62 | "incorrect-results.json") |
| 63 | args = [skimage_binary, "--readPath", invalid_file, |
| 64 | "--readExpectationsPath", incorrect_expectations] |
| 65 | result = subprocess.call(args) |
| 66 | if 0 == result: |
| 67 | print "'%s' should have reported failure!" % " ".join(args) |
| 68 | exit(1) |
| 69 | |
| 70 | # Empty expectations: |
| 71 | empty_expectations = os.path.join(expectations_dir, "empty-results.json") |
| 72 | subprocess.check_call([skimage_binary, "--readPath", invalid_file, |
| 73 | "--readExpectationsPath", empty_expectations]) |
| 74 | |
| 75 | # Ignore failure: |
| 76 | ignore_expectations = os.path.join(expectations_dir, "ignore-results.json") |
| 77 | subprocess.check_call([skimage_binary, "--readPath", invalid_file, |
| 78 | "--readExpectationsPath", ignore_expectations]) |
| 79 | |
| 80 | def test_incorrect_expectations(file_dir, skimage_binary): |
| 81 | """ Test that comparing to incorrect expectations fails, unless |
| 82 | ignore-failures is set to true. """ |
| 83 | valid_file = os.path.join(file_dir, "skimage", "input", |
| 84 | "images-with-known-hashes", |
| 85 | "1209453360120438698.png") |
| 86 | expectations_dir = os.path.join(file_dir, "skimage", "input", |
| 87 | "images-with-known-hashes") |
| 88 | |
| 89 | incorrect_results = os.path.join(expectations_dir, |
| 90 | "incorrect-results.json") |
| 91 | args = [skimage_binary, "--readPath", valid_file, "--readExpectationsPath", |
| 92 | incorrect_results] |
| 93 | result = subprocess.call(args) |
| 94 | if 0 == result: |
| 95 | print "'%s' should have reported failure!" % " ".join(args) |
| 96 | exit(1) |
| 97 | |
| 98 | ignore_results = os.path.join(expectations_dir, "ignore-failures.json") |
| 99 | subprocess.check_call([skimage_binary, "--readPath", valid_file, |
| 100 | "--readExpectationsPath", ignore_results]) |
| 101 | |
scroggo@google.com | 4fed643 | 2013-05-14 17:50:02 +0000 | [diff] [blame] | 102 | def main(): |
| 103 | # Use the directory of this file as the out directory |
| 104 | file_dir = os.path.abspath(os.path.dirname(__file__)) |
| 105 | |
| 106 | trunk_dir = os.path.normpath(os.path.join(file_dir, os.pardir, os.pardir)) |
| 107 | |
| 108 | # Find the binary |
| 109 | skimage_binary = PickBinaryPath(trunk_dir) |
| 110 | print "Running " + skimage_binary |
| 111 | |
epoger@google.com | 74feb15 | 2013-06-13 19:12:05 +0000 | [diff] [blame] | 112 | # Generate an expectations file from known images. |
| 113 | images_dir = os.path.join(file_dir, "skimage", "input", |
| 114 | "images-with-known-hashes") |
| 115 | expectations_path = os.path.join(file_dir, "skimage", "output-actual", |
| 116 | "create-expectations", "expectations.json") |
| 117 | subprocess.check_call([skimage_binary, "--readPath", images_dir, |
| 118 | "--createExpectationsPath", expectations_path]) |
scroggo@google.com | 4fed643 | 2013-05-14 17:50:02 +0000 | [diff] [blame] | 119 | |
epoger@google.com | 74feb15 | 2013-06-13 19:12:05 +0000 | [diff] [blame] | 120 | # Make sure the expectations file was generated correctly. |
| 121 | golden_expectations = os.path.join(file_dir, "skimage", "output-expected", |
| 122 | "create-expectations", |
| 123 | "expectations.json") |
| 124 | DieIfFilesMismatch(expected=golden_expectations, actual=expectations_path) |
scroggo@google.com | 4fed643 | 2013-05-14 17:50:02 +0000 | [diff] [blame] | 125 | |
epoger@google.com | 74feb15 | 2013-06-13 19:12:05 +0000 | [diff] [blame] | 126 | # Tell skimage to read back the expectations file it just wrote, and |
| 127 | # confirm that the images in images_dir match it. |
| 128 | subprocess.check_call([skimage_binary, "--readPath", images_dir, |
| 129 | "--readExpectationsPath", expectations_path]) |
scroggo@google.com | 4fed643 | 2013-05-14 17:50:02 +0000 | [diff] [blame] | 130 | |
scroggo@google.com | 36c5bdb | 2013-10-15 20:29:37 +0000 | [diff] [blame^] | 131 | test_incorrect_expectations(file_dir=file_dir, |
| 132 | skimage_binary=skimage_binary) |
epoger@google.com | 74feb15 | 2013-06-13 19:12:05 +0000 | [diff] [blame] | 133 | |
scroggo@google.com | 2b9424b | 2013-06-21 19:12:47 +0000 | [diff] [blame] | 134 | # Generate an expectations file from an empty directory. |
| 135 | empty_dir = tempfile.mkdtemp() |
| 136 | expectations_path = os.path.join(file_dir, "skimage", "output-actual", |
| 137 | "empty-dir", "expectations.json") |
| 138 | subprocess.check_call([skimage_binary, "--readPath", empty_dir, |
| 139 | "--createExpectationsPath", expectations_path]) |
| 140 | golden_expectations = os.path.join(file_dir, "skimage", "output-expected", |
| 141 | "empty-dir", "expectations.json") |
| 142 | DieIfFilesMismatch(expected=golden_expectations, actual=expectations_path) |
| 143 | os.rmdir(empty_dir) |
| 144 | |
| 145 | # Generate an expectations file from a nonexistent directory. |
| 146 | expectations_path = os.path.join(file_dir, "skimage", "output-actual", |
| 147 | "nonexistent-dir", "expectations.json") |
| 148 | subprocess.check_call([skimage_binary, "--readPath", "/nonexistent/dir", |
| 149 | "--createExpectationsPath", expectations_path]) |
| 150 | golden_expectations = os.path.join(file_dir, "skimage", "output-expected", |
| 151 | "nonexistent-dir", "expectations.json") |
| 152 | DieIfFilesMismatch(expected=golden_expectations, actual=expectations_path) |
| 153 | |
scroggo@google.com | 36c5bdb | 2013-10-15 20:29:37 +0000 | [diff] [blame^] | 154 | test_invalid_file(file_dir=file_dir, skimage_binary=skimage_binary) |
| 155 | |
epoger@google.com | 74feb15 | 2013-06-13 19:12:05 +0000 | [diff] [blame] | 156 | # Done with all tests. |
scroggo@google.com | 4fed643 | 2013-05-14 17:50:02 +0000 | [diff] [blame] | 157 | print "Self tests succeeded!" |
| 158 | |
| 159 | if __name__ == "__main__": |
| 160 | main() |