Chromium Code Reviews| 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 |
| 11 import sys | 11 import sys |
| 12 import tempfile | |
| 12 | 13 |
| 13 class BinaryNotFoundException(Exception): | 14 class BinaryNotFoundException(Exception): |
| 14 def __str__ (self): | 15 def __str__ (self): |
| 15 return ("Could not find binary!\n" | 16 return ("Could not find binary!\n" |
| 16 "Did you forget to build the tools project?\n" | 17 "Did you forget to build the tools project?\n" |
| 17 "Self tests failed") | 18 "Self tests failed") |
| 18 | 19 |
| 19 # Find a path to the binary to use. Iterates through a list of possible | 20 # Find a path to the binary to use. Iterates through a list of possible |
| 20 # locations the binary may be. | 21 # locations the binary may be. |
| 21 def PickBinaryPath(base_dir): | 22 def PickBinaryPath(base_dir): |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 44 | 45 |
| 45 trunk_dir = os.path.normpath(os.path.join(file_dir, os.pardir, os.pardir)) | 46 trunk_dir = os.path.normpath(os.path.join(file_dir, os.pardir, os.pardir)) |
| 46 | 47 |
| 47 # Find the binary | 48 # Find the binary |
| 48 skimage_binary = PickBinaryPath(trunk_dir) | 49 skimage_binary = PickBinaryPath(trunk_dir) |
| 49 print "Running " + skimage_binary | 50 print "Running " + skimage_binary |
| 50 | 51 |
| 51 # Generate an expectations file from known images. | 52 # Generate an expectations file from known images. |
| 52 images_dir = os.path.join(file_dir, "skimage", "input", | 53 images_dir = os.path.join(file_dir, "skimage", "input", |
| 53 "images-with-known-hashes") | 54 "images-with-known-hashes") |
| 54 expectations_path = os.path.join(file_dir, "skimage", "output-actual", | 55 expectations_dir = os.path.join(file_dir, "skimage", "output-actual", |
| 55 "create-expectations", "expectations.json") | 56 "create-expectations") |
| 57 | |
| 58 if not os.path.exists(expectations_dir): | |
|
scroggo
2013/06/19 20:27:35
Patch set 3 creates the output directories if they
| |
| 59 os.mkdir(expectations_dir) | |
| 60 | |
| 61 expectations_path = os.path.join(expectations_dir, "expectations.json") | |
| 56 subprocess.check_call([skimage_binary, "--readPath", images_dir, | 62 subprocess.check_call([skimage_binary, "--readPath", images_dir, |
| 57 "--createExpectationsPath", expectations_path]) | 63 "--createExpectationsPath", expectations_path]) |
| 58 | 64 |
| 59 # Make sure the expectations file was generated correctly. | 65 # Make sure the expectations file was generated correctly. |
| 60 golden_expectations = os.path.join(file_dir, "skimage", "output-expected", | 66 golden_expectations = os.path.join(file_dir, "skimage", "output-expected", |
| 61 "create-expectations", | 67 "create-expectations", |
| 62 "expectations.json") | 68 "expectations.json") |
| 63 DieIfFilesMismatch(expected=golden_expectations, actual=expectations_path) | 69 DieIfFilesMismatch(expected=golden_expectations, actual=expectations_path) |
| 64 | 70 |
| 65 # Tell skimage to read back the expectations file it just wrote, and | 71 # Tell skimage to read back the expectations file it just wrote, and |
| 66 # confirm that the images in images_dir match it. | 72 # confirm that the images in images_dir match it. |
| 67 subprocess.check_call([skimage_binary, "--readPath", images_dir, | 73 subprocess.check_call([skimage_binary, "--readPath", images_dir, |
| 68 "--readExpectationsPath", expectations_path]) | 74 "--readExpectationsPath", expectations_path]) |
| 69 | 75 |
| 70 # TODO(scroggo): Add a test that compares expectations and image files that | 76 # 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. | 77 # are known to NOT match, and make sure it returns an error. |
| 72 | 78 |
| 79 # Generate an expectations file from an empty directory. | |
| 80 empty_dir = tempfile.mkdtemp() | |
| 81 expectations_dir = os.path.join(file_dir, "skimage", "output-actual", | |
| 82 "empty-dir") | |
| 83 if not os.path.exists(expectations_dir): | |
| 84 os.mkdir(expectations_dir) | |
| 85 | |
| 86 expectations_path = os.path.join(expectations_dir, "expectations.json") | |
| 87 subprocess.check_call([skimage_binary, "--readPath", empty_dir, | |
| 88 "--createExpectationsPath", expectations_path]) | |
| 89 golden_expectations = os.path.join(file_dir, "skimage", "output-expected", | |
| 90 "empty-dir", "expectations.json") | |
| 91 DieIfFilesMismatch(expected=golden_expectations, actual=expectations_path) | |
| 92 os.rmdir(empty_dir) | |
| 93 | |
| 94 # Generate an expectations file from a nonexistent directory. | |
| 95 expectations_dir = os.path.join(file_dir, "skimage", "output-actual", | |
| 96 "nonexistent-dir") | |
| 97 if not os.path.exists(expectations_dir): | |
| 98 os.mkdir(expectations_dir) | |
| 99 | |
| 100 expectations_path = os.path.join(expectations_dir, "expectations.json") | |
| 101 subprocess.check_call([skimage_binary, "--readPath", "/nonexistent/dir", | |
| 102 "--createExpectationsPath", expectations_path]) | |
| 103 golden_expectations = os.path.join(file_dir, "skimage", "output-expected", | |
| 104 "nonexistent-dir", "expectations.json") | |
| 105 DieIfFilesMismatch(expected=golden_expectations, actual=expectations_path) | |
| 106 | |
| 73 # Done with all tests. | 107 # Done with all tests. |
| 74 print "Self tests succeeded!" | 108 print "Self tests succeeded!" |
| 75 | 109 |
| 76 if __name__ == "__main__": | 110 if __name__ == "__main__": |
| 77 main() | 111 main() |
| OLD | NEW |