OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 # Self-test for skimage. | 6 # Self-test for skimage. |
7 | 7 |
| 8 import filecmp |
8 import os | 9 import os |
9 import subprocess | 10 import subprocess |
10 import sys | 11 import sys |
11 | 12 |
12 class BinaryNotFoundException(Exception): | 13 class BinaryNotFoundException(Exception): |
13 def __str__ (self): | 14 def __str__ (self): |
14 return ("Could not find binary!\n" | 15 return ("Could not find binary!\n" |
15 "Did you forget to build the tools project?\n" | 16 "Did you forget to build the tools project?\n" |
16 "Self tests failed") | 17 "Self tests failed") |
17 | 18 |
18 # Find a path to the binary to use. Iterates through a list of possible | 19 # Find a path to the binary to use. Iterates through a list of possible |
19 # locations the binary may be. | 20 # locations the binary may be. |
20 def PickBinaryPath(base_dir): | 21 def PickBinaryPath(base_dir): |
21 POSSIBLE_BINARY_PATHS = [ | 22 POSSIBLE_BINARY_PATHS = [ |
22 'out/Debug/skimage', | 23 'out/Debug/skimage', |
23 'out/Release/skimage', | 24 'out/Release/skimage', |
24 'xcodebuild/Debug/skimage', | 25 'xcodebuild/Debug/skimage', |
25 'xcodebuild/Release/skimage', | 26 'xcodebuild/Release/skimage', |
26 ] | 27 ] |
27 for binary in POSSIBLE_BINARY_PATHS: | 28 for binary in POSSIBLE_BINARY_PATHS: |
28 binary_full_path = os.path.join(base_dir, binary) | 29 binary_full_path = os.path.join(base_dir, binary) |
29 if (os.path.exists(binary_full_path)): | 30 if (os.path.exists(binary_full_path)): |
30 return binary_full_path | 31 return binary_full_path |
31 raise BinaryNotFoundException | 32 raise BinaryNotFoundException |
32 | 33 |
| 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 |
33 def main(): | 41 def main(): |
34 # Use the directory of this file as the out directory | 42 # Use the directory of this file as the out directory |
35 file_dir = os.path.abspath(os.path.dirname(__file__)) | 43 file_dir = os.path.abspath(os.path.dirname(__file__)) |
36 | 44 |
37 trunk_dir = os.path.normpath(os.path.join(file_dir, os.pardir, os.pardir)) | 45 trunk_dir = os.path.normpath(os.path.join(file_dir, os.pardir, os.pardir)) |
38 | 46 |
39 # Find the binary | 47 # Find the binary |
40 skimage_binary = PickBinaryPath(trunk_dir) | 48 skimage_binary = PickBinaryPath(trunk_dir) |
41 print "Running " + skimage_binary | 49 print "Running " + skimage_binary |
42 | 50 |
43 # Run skimage twice, first to create an expectations file, and then | 51 # Generate an expectations file from known images. |
44 # comparing to it. | 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]) |
45 | 58 |
46 # Both commands will run the binary, reading from resources. | 59 # Make sure the expectations file was generated correctly. |
47 cmd_line = [skimage_binary] | 60 golden_expectations = os.path.join(file_dir, "skimage", "output-expected", |
48 resources_dir = os.path.join(trunk_dir, 'resources') | 61 "create-expectations", |
49 cmd_line.extend(["-r", resources_dir]) | 62 "expectations.json") |
| 63 DieIfFilesMismatch(expected=golden_expectations, actual=expectations_path) |
50 | 64 |
51 # Create the expectations file | 65 # Tell skimage to read back the expectations file it just wrote, and |
52 results_file = os.path.join(file_dir, "skimage", "self_test_results.json") | 66 # confirm that the images in images_dir match it. |
53 create_expectations_cmd = cmd_line + ["--createExpectationsPath", | 67 subprocess.check_call([skimage_binary, "--readPath", images_dir, |
54 results_file] | 68 "--readExpectationsPath", expectations_path]) |
55 subprocess.check_call(create_expectations_cmd) | |
56 | 69 |
57 # Now read from the expectations file | 70 # TODO(scroggo): Add a test that compares expectations and image files that |
58 check_expectations_cmd = cmd_line + ["--readExpectationsPath", | 71 # are known to NOT match, and make sure it returns an error. |
59 results_file] | 72 |
60 subprocess.check_call(check_expectations_cmd) | 73 # Done with all tests. |
61 print "Self tests succeeded!" | 74 print "Self tests succeeded!" |
62 | 75 |
63 if __name__ == "__main__": | 76 if __name__ == "__main__": |
64 main() | 77 main() |
OLD | NEW |