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 |
| 12 | |
| 13 | class BinaryNotFoundException(Exception): |
| 14 | def __str__ (self): |
| 15 | return ("Could not find binary!\n" |
| 16 | "Did you forget to build the tools project?\n" |
| 17 | "Self tests failed") |
| 18 | |
| 19 | # Find a path to the binary to use. Iterates through a list of possible |
| 20 | # locations the binary may be. |
| 21 | def PickBinaryPath(base_dir): |
| 22 | POSSIBLE_BINARY_PATHS = [ |
| 23 | 'out/Debug/skimage', |
| 24 | 'out/Release/skimage', |
| 25 | 'xcodebuild/Debug/skimage', |
| 26 | 'xcodebuild/Release/skimage', |
| 27 | ] |
| 28 | for binary in POSSIBLE_BINARY_PATHS: |
| 29 | binary_full_path = os.path.join(base_dir, binary) |
| 30 | if (os.path.exists(binary_full_path)): |
| 31 | return binary_full_path |
| 32 | raise BinaryNotFoundException |
| 33 | |
epoger@google.com | 74feb15 | 2013-06-13 19:12:05 +0000 | [diff] [blame^] | 34 | # Quit early if two files have different content. |
| 35 | def DieIfFilesMismatch(expected, actual): |
| 36 | if not filecmp.cmp(expected, actual): |
| 37 | print 'Error: file mismatch! expected=%s , actual=%s' % ( |
| 38 | expected, actual) |
| 39 | exit(1) |
| 40 | |
scroggo@google.com | 4fed643 | 2013-05-14 17:50:02 +0000 | [diff] [blame] | 41 | def main(): |
| 42 | # Use the directory of this file as the out directory |
| 43 | file_dir = os.path.abspath(os.path.dirname(__file__)) |
| 44 | |
| 45 | trunk_dir = os.path.normpath(os.path.join(file_dir, os.pardir, os.pardir)) |
| 46 | |
| 47 | # Find the binary |
| 48 | skimage_binary = PickBinaryPath(trunk_dir) |
| 49 | print "Running " + skimage_binary |
| 50 | |
epoger@google.com | 74feb15 | 2013-06-13 19:12:05 +0000 | [diff] [blame^] | 51 | # Generate an expectations file from known images. |
| 52 | images_dir = os.path.join(file_dir, "skimage", "input", |
| 53 | "images-with-known-hashes") |
| 54 | expectations_path = os.path.join(file_dir, "skimage", "output-actual", |
| 55 | "create-expectations", "expectations.json") |
| 56 | subprocess.check_call([skimage_binary, "--readPath", images_dir, |
| 57 | "--createExpectationsPath", expectations_path]) |
scroggo@google.com | 4fed643 | 2013-05-14 17:50:02 +0000 | [diff] [blame] | 58 | |
epoger@google.com | 74feb15 | 2013-06-13 19:12:05 +0000 | [diff] [blame^] | 59 | # Make sure the expectations file was generated correctly. |
| 60 | golden_expectations = os.path.join(file_dir, "skimage", "output-expected", |
| 61 | "create-expectations", |
| 62 | "expectations.json") |
| 63 | DieIfFilesMismatch(expected=golden_expectations, actual=expectations_path) |
scroggo@google.com | 4fed643 | 2013-05-14 17:50:02 +0000 | [diff] [blame] | 64 | |
epoger@google.com | 74feb15 | 2013-06-13 19:12:05 +0000 | [diff] [blame^] | 65 | # Tell skimage to read back the expectations file it just wrote, and |
| 66 | # confirm that the images in images_dir match it. |
| 67 | subprocess.check_call([skimage_binary, "--readPath", images_dir, |
| 68 | "--readExpectationsPath", expectations_path]) |
scroggo@google.com | 4fed643 | 2013-05-14 17:50:02 +0000 | [diff] [blame] | 69 | |
epoger@google.com | 74feb15 | 2013-06-13 19:12:05 +0000 | [diff] [blame^] | 70 | # TODO(scroggo): Add a test that compares expectations and image files that |
| 71 | # are known to NOT match, and make sure it returns an error. |
| 72 | |
| 73 | # Done with all tests. |
scroggo@google.com | 4fed643 | 2013-05-14 17:50:02 +0000 | [diff] [blame] | 74 | print "Self tests succeeded!" |
| 75 | |
| 76 | if __name__ == "__main__": |
| 77 | main() |