blob: 6229d6a9fffafbd79ef4a557ef316a292c8dbf76 [file] [log] [blame]
scroggo@google.com4fed6432013-05-14 17:50:02 +00001#!/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.com74feb152013-06-13 19:12:05 +00008import filecmp
scroggo@google.com4fed6432013-05-14 17:50:02 +00009import os
10import subprocess
11import sys
scroggo@google.com2b9424b2013-06-21 19:12:47 +000012import tempfile
scroggo@google.com4fed6432013-05-14 17:50:02 +000013
14class 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.
22def 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.com74feb152013-06-13 19:12:05 +000035# Quit early if two files have different content.
36def 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.com36c5bdb2013-10-15 20:29:37 +000042def 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)
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
80def 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
scroggo@google.com4fed6432013-05-14 17:50:02 +0000102def main():
103 # Use the directory of this file as the out directory
104 file_dir = os.path.abspath(os.path.dirname(__file__))
105
106 trunk_dir = os.path.normpath(os.path.join(file_dir, os.pardir, os.pardir))
107
108 # Find the binary
109 skimage_binary = PickBinaryPath(trunk_dir)
110 print "Running " + skimage_binary
111
epoger@google.com74feb152013-06-13 19:12:05 +0000112 # Generate an expectations file from known images.
113 images_dir = os.path.join(file_dir, "skimage", "input",
114 "images-with-known-hashes")
115 expectations_path = os.path.join(file_dir, "skimage", "output-actual",
116 "create-expectations", "expectations.json")
117 subprocess.check_call([skimage_binary, "--readPath", images_dir,
118 "--createExpectationsPath", expectations_path])
scroggo@google.com4fed6432013-05-14 17:50:02 +0000119
epoger@google.com74feb152013-06-13 19:12:05 +0000120 # Make sure the expectations file was generated correctly.
121 golden_expectations = os.path.join(file_dir, "skimage", "output-expected",
122 "create-expectations",
123 "expectations.json")
124 DieIfFilesMismatch(expected=golden_expectations, actual=expectations_path)
scroggo@google.com4fed6432013-05-14 17:50:02 +0000125
epoger@google.com74feb152013-06-13 19:12:05 +0000126 # Tell skimage to read back the expectations file it just wrote, and
127 # confirm that the images in images_dir match it.
128 subprocess.check_call([skimage_binary, "--readPath", images_dir,
129 "--readExpectationsPath", expectations_path])
scroggo@google.com4fed6432013-05-14 17:50:02 +0000130
scroggo@google.com36c5bdb2013-10-15 20:29:37 +0000131 test_incorrect_expectations(file_dir=file_dir,
132 skimage_binary=skimage_binary)
epoger@google.com74feb152013-06-13 19:12:05 +0000133
scroggo@google.com2b9424b2013-06-21 19:12:47 +0000134 # Generate an expectations file from an empty directory.
135 empty_dir = tempfile.mkdtemp()
136 expectations_path = os.path.join(file_dir, "skimage", "output-actual",
137 "empty-dir", "expectations.json")
138 subprocess.check_call([skimage_binary, "--readPath", empty_dir,
139 "--createExpectationsPath", expectations_path])
140 golden_expectations = os.path.join(file_dir, "skimage", "output-expected",
141 "empty-dir", "expectations.json")
142 DieIfFilesMismatch(expected=golden_expectations, actual=expectations_path)
143 os.rmdir(empty_dir)
144
145 # Generate an expectations file from a nonexistent directory.
146 expectations_path = os.path.join(file_dir, "skimage", "output-actual",
147 "nonexistent-dir", "expectations.json")
148 subprocess.check_call([skimage_binary, "--readPath", "/nonexistent/dir",
149 "--createExpectationsPath", expectations_path])
150 golden_expectations = os.path.join(file_dir, "skimage", "output-expected",
151 "nonexistent-dir", "expectations.json")
152 DieIfFilesMismatch(expected=golden_expectations, actual=expectations_path)
153
scroggo@google.com36c5bdb2013-10-15 20:29:37 +0000154 test_invalid_file(file_dir=file_dir, skimage_binary=skimage_binary)
155
epoger@google.com74feb152013-06-13 19:12:05 +0000156 # Done with all tests.
scroggo@google.com4fed6432013-05-14 17:50:02 +0000157 print "Self tests succeeded!"
158
159if __name__ == "__main__":
160 main()