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 17 matching lines...) Expand all Loading... |
28 ] | 28 ] |
29 for binary in POSSIBLE_BINARY_PATHS: | 29 for binary in POSSIBLE_BINARY_PATHS: |
30 binary_full_path = os.path.join(base_dir, binary) | 30 binary_full_path = os.path.join(base_dir, binary) |
31 if (os.path.exists(binary_full_path)): | 31 if (os.path.exists(binary_full_path)): |
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 raise Exception("Error: file mismatch! expected=%s , actual=%s" % ( |
39 expected, actual) | 39 expected, actual)) |
40 exit(1) | |
41 | 40 |
42 def test_invalid_file(file_dir, skimage_binary): | 41 def test_invalid_file(file_dir, skimage_binary): |
43 """ Test the return value of skimage when an invalid file is decoded. | 42 """ 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 | 43 If there is no expectation file, or the file expects a particular |
45 result, skimage should return nonzero indicating failure. | 44 result, skimage should return nonzero indicating failure. |
46 If the file has no expectation, or ignore-failure is set to true, | 45 If the file has no expectation, or ignore-failure is set to true, |
47 skimage should return zero indicating success. """ | 46 skimage should return zero indicating success. """ |
48 invalid_file = os.path.join(file_dir, "skimage", "input", "bad-images", | 47 invalid_file = os.path.join(file_dir, "skimage", "input", "bad-images", |
49 "invalid.png") | 48 "invalid.png") |
50 # No expectations file: | 49 # No expectations file: |
51 args = [skimage_binary, "--readPath", invalid_file] | 50 args = [skimage_binary, "--readPath", invalid_file] |
52 result = subprocess.call(args) | 51 result = subprocess.call(args) |
53 if 0 == result: | 52 if 0 == result: |
54 print "'%s' should have reported failure!" % " ".join(args) | 53 raise Exception("'%s' should have reported failure!" % " ".join(args)) |
55 exit(1) | |
56 | 54 |
57 # Directory holding all expectations files | 55 # Directory holding all expectations files |
58 expectations_dir = os.path.join(file_dir, "skimage", "input", "bad-images") | 56 expectations_dir = os.path.join(file_dir, "skimage", "input", "bad-images") |
59 | 57 |
60 # Expectations file expecting a valid decode: | 58 # Expectations file expecting a valid decode: |
61 incorrect_expectations = os.path.join(expectations_dir, | 59 incorrect_expectations = os.path.join(expectations_dir, |
62 "incorrect-results.json") | 60 "incorrect-results.json") |
63 args = [skimage_binary, "--readPath", invalid_file, | 61 args = [skimage_binary, "--readPath", invalid_file, |
64 "--readExpectationsPath", incorrect_expectations] | 62 "--readExpectationsPath", incorrect_expectations] |
65 result = subprocess.call(args) | 63 result = subprocess.call(args) |
66 if 0 == result: | 64 if 0 == result: |
67 print "'%s' should have reported failure!" % " ".join(args) | 65 raise Exception("'%s' should have reported failure!" % " ".join(args)) |
68 exit(1) | |
69 | 66 |
70 # Empty expectations: | 67 # Empty expectations: |
71 empty_expectations = os.path.join(expectations_dir, "empty-results.json") | 68 empty_expectations = os.path.join(expectations_dir, "empty-results.json") |
72 output = subprocess.check_output([skimage_binary, "--readPath", invalid_file
, | 69 output = subprocess.check_output([skimage_binary, "--readPath", invalid_file
, |
73 "--readExpectationsPath", | 70 "--readExpectationsPath", |
74 empty_expectations], | 71 empty_expectations], |
75 stderr=subprocess.STDOUT) | 72 stderr=subprocess.STDOUT) |
76 if not "Missing" in output: | 73 if not "Missing" in output: |
77 # Another test (in main()) tests to ensure that "Missing" does not appear | 74 # Another test (in main()) tests to ensure that "Missing" does not appear |
78 # in the output. That test could be passed if the output changed so | 75 # in the output. That test could be passed if the output changed so |
79 # "Missing" never appears. This ensures that an error is not missed if | 76 # "Missing" never appears. This ensures that an error is not missed if |
80 # that happens. | 77 # that happens. |
81 print "skimage output changed! This may cause other self tests to fail!" | 78 raise Exception( |
82 exit(1) | 79 "skimage output changed! This may cause other self tests to fail!") |
83 | 80 |
84 # Ignore failure: | 81 # Ignore failure: |
85 ignore_expectations = os.path.join(expectations_dir, "ignore-results.json") | 82 ignore_expectations = os.path.join(expectations_dir, "ignore-results.json") |
86 output = subprocess.check_output([skimage_binary, "--readPath", invalid_file
, | 83 output = subprocess.check_output([skimage_binary, "--readPath", invalid_file
, |
87 "--readExpectationsPath", | 84 "--readExpectationsPath", |
88 ignore_expectations], | 85 ignore_expectations], |
89 stderr=subprocess.STDOUT) | 86 stderr=subprocess.STDOUT) |
90 if not "failures" in output: | 87 if not "failures" in output: |
91 # Another test (in main()) tests to ensure that "failures" does not | 88 # Another test (in main()) tests to ensure that "failures" does not |
92 # appear in the output. That test could be passed if the output changed | 89 # appear in the output. That test could be passed if the output changed |
93 # so "failures" never appears. This ensures that an error is not missed | 90 # so "failures" never appears. This ensures that an error is not missed |
94 # if that happens. | 91 # if that happens. |
95 print "skimage output changed! This may cause other self tests to fail!" | 92 raise Exception( |
96 exit(1) | 93 "skimage output changed! This may cause other self tests to fail!") |
97 | 94 |
98 def test_incorrect_expectations(file_dir, skimage_binary): | 95 def test_incorrect_expectations(file_dir, skimage_binary): |
99 """ Test that comparing to incorrect expectations fails, unless | 96 """ Test that comparing to incorrect expectations fails, unless |
100 ignore-failures is set to true. """ | 97 ignore-failures is set to true. """ |
101 valid_file = os.path.join(file_dir, "skimage", "input", | 98 valid_file = os.path.join(file_dir, "skimage", "input", |
102 "images-with-known-hashes", | 99 "images-with-known-hashes", |
103 "1209453360120438698.png") | 100 "1209453360120438698.png") |
104 expectations_dir = os.path.join(file_dir, "skimage", "input", | 101 expectations_dir = os.path.join(file_dir, "skimage", "input", |
105 "images-with-known-hashes") | 102 "images-with-known-hashes") |
106 | 103 |
107 incorrect_results = os.path.join(expectations_dir, | 104 incorrect_results = os.path.join(expectations_dir, |
108 "incorrect-results.json") | 105 "incorrect-results.json") |
109 args = [skimage_binary, "--readPath", valid_file, "--readExpectationsPath", | 106 args = [skimage_binary, "--readPath", valid_file, "--readExpectationsPath", |
110 incorrect_results] | 107 incorrect_results] |
111 result = subprocess.call(args) | 108 result = subprocess.call(args) |
112 if 0 == result: | 109 if 0 == result: |
113 print "'%s' should have reported failure!" % " ".join(args) | 110 raise Exception("'%s' should have reported failure!" % " ".join(args)) |
114 exit(1) | |
115 | 111 |
116 ignore_results = os.path.join(expectations_dir, "ignore-failures.json") | 112 ignore_results = os.path.join(expectations_dir, "ignore-failures.json") |
117 subprocess.check_call([skimage_binary, "--readPath", valid_file, | 113 subprocess.check_call([skimage_binary, "--readPath", valid_file, |
118 "--readExpectationsPath", ignore_results]) | 114 "--readExpectationsPath", ignore_results]) |
119 | 115 |
120 def main(): | 116 def main(): |
121 # Use the directory of this file as the out directory | 117 # Use the directory of this file as the out directory |
122 file_dir = os.path.abspath(os.path.dirname(__file__)) | 118 file_dir = os.path.abspath(os.path.dirname(__file__)) |
123 | 119 |
124 trunk_dir = os.path.normpath(os.path.join(file_dir, os.pardir, os.pardir)) | 120 trunk_dir = os.path.normpath(os.path.join(file_dir, os.pardir, os.pardir)) |
(...skipping 21 matching lines...) Expand all Loading... |
146 output = subprocess.check_output([skimage_binary, "--readPath", images_dir, | 142 output = subprocess.check_output([skimage_binary, "--readPath", images_dir, |
147 "--readExpectationsPath", | 143 "--readExpectationsPath", |
148 expectations_path], | 144 expectations_path], |
149 stderr=subprocess.STDOUT) | 145 stderr=subprocess.STDOUT) |
150 | 146 |
151 # Although skimage succeeded, it would have reported success if the file | 147 # Although skimage succeeded, it would have reported success if the file |
152 # was missing from the expectations file. Consider this a failure, since | 148 # was missing from the expectations file. Consider this a failure, since |
153 # the expectations file was created from this same image. (It will print | 149 # the expectations file was created from this same image. (It will print |
154 # "Missing" in this case before listing the missing expectations). | 150 # "Missing" in this case before listing the missing expectations). |
155 if "Missing" in output: | 151 if "Missing" in output: |
156 print "Expectations file was missing expectations!" | 152 raise Exception("Expectations file was missing expectations: %s" % output) |
157 print output | |
158 exit(1) | |
159 | 153 |
160 # Again, skimage would succeed if there were known failures (and print | 154 # Again, skimage would succeed if there were known failures (and print |
161 # "failures"), but there should be no failures, since the file just | 155 # "failures"), but there should be no failures, since the file just |
162 # created did not include failures to ignore. | 156 # created did not include failures to ignore. |
163 if "failures" in output: | 157 if "failures" in output: |
164 print "Image failed!" | 158 raise Exception("Image failed: %s" % output) |
165 print output | |
166 exit(1) | |
167 | 159 |
168 | 160 |
169 test_incorrect_expectations(file_dir=file_dir, | 161 test_incorrect_expectations(file_dir=file_dir, |
170 skimage_binary=skimage_binary) | 162 skimage_binary=skimage_binary) |
171 | 163 |
172 # Generate an expectations file from an empty directory. | 164 # Generate an expectations file from an empty directory. |
173 empty_dir = tempfile.mkdtemp() | 165 empty_dir = tempfile.mkdtemp() |
174 expectations_path = os.path.join(file_dir, "skimage", "output-actual", | 166 expectations_path = os.path.join(file_dir, "skimage", "output-actual", |
175 "empty-dir", "expectations.json") | 167 "empty-dir", "expectations.json") |
176 subprocess.check_call([skimage_binary, "--readPath", empty_dir, | 168 subprocess.check_call([skimage_binary, "--readPath", empty_dir, |
(...skipping 12 matching lines...) Expand all Loading... |
189 "nonexistent-dir", "expectations.json") | 181 "nonexistent-dir", "expectations.json") |
190 DieIfFilesMismatch(expected=golden_expectations, actual=expectations_path) | 182 DieIfFilesMismatch(expected=golden_expectations, actual=expectations_path) |
191 | 183 |
192 test_invalid_file(file_dir=file_dir, skimage_binary=skimage_binary) | 184 test_invalid_file(file_dir=file_dir, skimage_binary=skimage_binary) |
193 | 185 |
194 # Done with all tests. | 186 # Done with all tests. |
195 print "Self tests succeeded!" | 187 print "Self tests succeeded!" |
196 | 188 |
197 if __name__ == "__main__": | 189 if __name__ == "__main__": |
198 main() | 190 main() |
OLD | NEW |