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 filecmp |
9 import os | 9 import os |
10 import subprocess | 10 import subprocess |
(...skipping 21 matching lines...) Expand all Loading... | |
32 return binary_full_path | 32 return binary_full_path |
33 raise BinaryNotFoundException | 33 raise BinaryNotFoundException |
34 | 34 |
35 # Quit early if two files have different content. | 35 # Quit early if two files have different content. |
36 def DieIfFilesMismatch(expected, actual): | 36 def DieIfFilesMismatch(expected, actual): |
37 if not filecmp.cmp(expected, actual): | 37 if not filecmp.cmp(expected, actual): |
38 print 'Error: file mismatch! expected=%s , actual=%s' % ( | 38 print 'Error: file mismatch! expected=%s , actual=%s' % ( |
39 expected, actual) | 39 expected, actual) |
40 exit(1) | 40 exit(1) |
41 | 41 |
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) | |
epoger
2013/10/15 15:31:03
Personally, I would write it this way to avoid the
scroggo
2013/10/15 19:50:59
Done.
epoger
2013/10/15 20:20:55
Right. I think consistency within the file is mor
| |
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 | |
42 def main(): | 102 def main(): |
43 # Use the directory of this file as the out directory | 103 # Use the directory of this file as the out directory |
44 file_dir = os.path.abspath(os.path.dirname(__file__)) | 104 file_dir = os.path.abspath(os.path.dirname(__file__)) |
45 | 105 |
46 trunk_dir = os.path.normpath(os.path.join(file_dir, os.pardir, os.pardir)) | 106 trunk_dir = os.path.normpath(os.path.join(file_dir, os.pardir, os.pardir)) |
47 | 107 |
48 # Find the binary | 108 # Find the binary |
49 skimage_binary = PickBinaryPath(trunk_dir) | 109 skimage_binary = PickBinaryPath(trunk_dir) |
50 print "Running " + skimage_binary | 110 print "Running " + skimage_binary |
51 | 111 |
52 # Generate an expectations file from known images. | 112 # Generate an expectations file from known images. |
53 images_dir = os.path.join(file_dir, "skimage", "input", | 113 images_dir = os.path.join(file_dir, "skimage", "input", |
54 "images-with-known-hashes") | 114 "images-with-known-hashes") |
55 expectations_path = os.path.join(file_dir, "skimage", "output-actual", | 115 expectations_path = os.path.join(file_dir, "skimage", "output-actual", |
56 "create-expectations", "expectations.json") | 116 "create-expectations", "expectations.json") |
57 subprocess.check_call([skimage_binary, "--readPath", images_dir, | 117 subprocess.check_call([skimage_binary, "--readPath", images_dir, |
58 "--createExpectationsPath", expectations_path]) | 118 "--createExpectationsPath", expectations_path]) |
59 | 119 |
60 # Make sure the expectations file was generated correctly. | 120 # Make sure the expectations file was generated correctly. |
61 golden_expectations = os.path.join(file_dir, "skimage", "output-expected", | 121 golden_expectations = os.path.join(file_dir, "skimage", "output-expected", |
62 "create-expectations", | 122 "create-expectations", |
63 "expectations.json") | 123 "expectations.json") |
64 DieIfFilesMismatch(expected=golden_expectations, actual=expectations_path) | 124 DieIfFilesMismatch(expected=golden_expectations, actual=expectations_path) |
65 | 125 |
66 # Tell skimage to read back the expectations file it just wrote, and | 126 # Tell skimage to read back the expectations file it just wrote, and |
67 # confirm that the images in images_dir match it. | 127 # confirm that the images in images_dir match it. |
68 subprocess.check_call([skimage_binary, "--readPath", images_dir, | 128 subprocess.check_call([skimage_binary, "--readPath", images_dir, |
69 "--readExpectationsPath", expectations_path]) | 129 "--readExpectationsPath", expectations_path]) |
70 | 130 |
71 # TODO(scroggo): Add a test that compares expectations and image files that | 131 test_incorrect_expectations(file_dir=file_dir, |
72 # are known to NOT match, and make sure it returns an error. | 132 skimage_binary=skimage_binary) |
73 | 133 |
74 # Generate an expectations file from an empty directory. | 134 # Generate an expectations file from an empty directory. |
75 empty_dir = tempfile.mkdtemp() | 135 empty_dir = tempfile.mkdtemp() |
76 expectations_path = os.path.join(file_dir, "skimage", "output-actual", | 136 expectations_path = os.path.join(file_dir, "skimage", "output-actual", |
77 "empty-dir", "expectations.json") | 137 "empty-dir", "expectations.json") |
78 subprocess.check_call([skimage_binary, "--readPath", empty_dir, | 138 subprocess.check_call([skimage_binary, "--readPath", empty_dir, |
79 "--createExpectationsPath", expectations_path]) | 139 "--createExpectationsPath", expectations_path]) |
80 golden_expectations = os.path.join(file_dir, "skimage", "output-expected", | 140 golden_expectations = os.path.join(file_dir, "skimage", "output-expected", |
81 "empty-dir", "expectations.json") | 141 "empty-dir", "expectations.json") |
82 DieIfFilesMismatch(expected=golden_expectations, actual=expectations_path) | 142 DieIfFilesMismatch(expected=golden_expectations, actual=expectations_path) |
83 os.rmdir(empty_dir) | 143 os.rmdir(empty_dir) |
84 | 144 |
85 # Generate an expectations file from a nonexistent directory. | 145 # Generate an expectations file from a nonexistent directory. |
86 expectations_path = os.path.join(file_dir, "skimage", "output-actual", | 146 expectations_path = os.path.join(file_dir, "skimage", "output-actual", |
87 "nonexistent-dir", "expectations.json") | 147 "nonexistent-dir", "expectations.json") |
88 subprocess.check_call([skimage_binary, "--readPath", "/nonexistent/dir", | 148 subprocess.check_call([skimage_binary, "--readPath", "/nonexistent/dir", |
89 "--createExpectationsPath", expectations_path]) | 149 "--createExpectationsPath", expectations_path]) |
90 golden_expectations = os.path.join(file_dir, "skimage", "output-expected", | 150 golden_expectations = os.path.join(file_dir, "skimage", "output-expected", |
91 "nonexistent-dir", "expectations.json") | 151 "nonexistent-dir", "expectations.json") |
92 DieIfFilesMismatch(expected=golden_expectations, actual=expectations_path) | 152 DieIfFilesMismatch(expected=golden_expectations, actual=expectations_path) |
93 | 153 |
154 test_invalid_file(file_dir=file_dir, skimage_binary=skimage_binary) | |
155 | |
94 # Done with all tests. | 156 # Done with all tests. |
95 print "Self tests succeeded!" | 157 print "Self tests succeeded!" |
96 | 158 |
97 if __name__ == "__main__": | 159 if __name__ == "__main__": |
98 main() | 160 main() |
OLD | NEW |