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 | 4fed643 | 2013-05-14 17:50:02 +0000 | [diff] [blame] | 42 | def main(): |
| 43 | # Use the directory of this file as the out directory |
| 44 | file_dir = os.path.abspath(os.path.dirname(__file__)) |
| 45 | |
| 46 | trunk_dir = os.path.normpath(os.path.join(file_dir, os.pardir, os.pardir)) |
| 47 | |
| 48 | # Find the binary |
| 49 | skimage_binary = PickBinaryPath(trunk_dir) |
| 50 | print "Running " + skimage_binary |
| 51 | |
epoger@google.com | 74feb15 | 2013-06-13 19:12:05 +0000 | [diff] [blame] | 52 | # Generate an expectations file from known images. |
| 53 | images_dir = os.path.join(file_dir, "skimage", "input", |
| 54 | "images-with-known-hashes") |
| 55 | expectations_path = os.path.join(file_dir, "skimage", "output-actual", |
| 56 | "create-expectations", "expectations.json") |
| 57 | subprocess.check_call([skimage_binary, "--readPath", images_dir, |
| 58 | "--createExpectationsPath", expectations_path]) |
scroggo@google.com | 4fed643 | 2013-05-14 17:50:02 +0000 | [diff] [blame] | 59 | |
epoger@google.com | 74feb15 | 2013-06-13 19:12:05 +0000 | [diff] [blame] | 60 | # Make sure the expectations file was generated correctly. |
| 61 | golden_expectations = os.path.join(file_dir, "skimage", "output-expected", |
| 62 | "create-expectations", |
| 63 | "expectations.json") |
| 64 | DieIfFilesMismatch(expected=golden_expectations, actual=expectations_path) |
scroggo@google.com | 4fed643 | 2013-05-14 17:50:02 +0000 | [diff] [blame] | 65 | |
epoger@google.com | 74feb15 | 2013-06-13 19:12:05 +0000 | [diff] [blame] | 66 | # Tell skimage to read back the expectations file it just wrote, and |
| 67 | # confirm that the images in images_dir match it. |
| 68 | subprocess.check_call([skimage_binary, "--readPath", images_dir, |
| 69 | "--readExpectationsPath", expectations_path]) |
scroggo@google.com | 4fed643 | 2013-05-14 17:50:02 +0000 | [diff] [blame] | 70 | |
epoger@google.com | 74feb15 | 2013-06-13 19:12:05 +0000 | [diff] [blame] | 71 | # TODO(scroggo): Add a test that compares expectations and image files that |
| 72 | # are known to NOT match, and make sure it returns an error. |
| 73 | |
scroggo@google.com | 2b9424b | 2013-06-21 19:12:47 +0000 | [diff] [blame] | 74 | # Generate an expectations file from an empty directory. |
| 75 | empty_dir = tempfile.mkdtemp() |
| 76 | expectations_path = os.path.join(file_dir, "skimage", "output-actual", |
| 77 | "empty-dir", "expectations.json") |
| 78 | subprocess.check_call([skimage_binary, "--readPath", empty_dir, |
| 79 | "--createExpectationsPath", expectations_path]) |
| 80 | golden_expectations = os.path.join(file_dir, "skimage", "output-expected", |
| 81 | "empty-dir", "expectations.json") |
| 82 | DieIfFilesMismatch(expected=golden_expectations, actual=expectations_path) |
| 83 | os.rmdir(empty_dir) |
| 84 | |
| 85 | # Generate an expectations file from a nonexistent directory. |
| 86 | expectations_path = os.path.join(file_dir, "skimage", "output-actual", |
| 87 | "nonexistent-dir", "expectations.json") |
| 88 | subprocess.check_call([skimage_binary, "--readPath", "/nonexistent/dir", |
| 89 | "--createExpectationsPath", expectations_path]) |
| 90 | golden_expectations = os.path.join(file_dir, "skimage", "output-expected", |
| 91 | "nonexistent-dir", "expectations.json") |
| 92 | DieIfFilesMismatch(expected=golden_expectations, actual=expectations_path) |
| 93 | |
epoger@google.com | 74feb15 | 2013-06-13 19:12:05 +0000 | [diff] [blame] | 94 | # Done with all tests. |
scroggo@google.com | 4fed643 | 2013-05-14 17:50:02 +0000 | [diff] [blame] | 95 | print "Self tests succeeded!" |
| 96 | |
| 97 | if __name__ == "__main__": |
| 98 | main() |