blob: 24164d4a5190b7bd397965127cedcab0e533ed3a [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.com4fed6432013-05-14 17:50:02 +000042def main():
43 # Use the directory of this file as the out directory
44 file_dir = os.path.abspath(os.path.dirname(__file__))
45
46 trunk_dir = os.path.normpath(os.path.join(file_dir, os.pardir, os.pardir))
47
48 # Find the binary
49 skimage_binary = PickBinaryPath(trunk_dir)
50 print "Running " + skimage_binary
51
epoger@google.com74feb152013-06-13 19:12:05 +000052 # Generate an expectations file from known images.
53 images_dir = os.path.join(file_dir, "skimage", "input",
54 "images-with-known-hashes")
55 expectations_path = os.path.join(file_dir, "skimage", "output-actual",
56 "create-expectations", "expectations.json")
57 subprocess.check_call([skimage_binary, "--readPath", images_dir,
58 "--createExpectationsPath", expectations_path])
scroggo@google.com4fed6432013-05-14 17:50:02 +000059
epoger@google.com74feb152013-06-13 19:12:05 +000060 # Make sure the expectations file was generated correctly.
61 golden_expectations = os.path.join(file_dir, "skimage", "output-expected",
62 "create-expectations",
63 "expectations.json")
64 DieIfFilesMismatch(expected=golden_expectations, actual=expectations_path)
scroggo@google.com4fed6432013-05-14 17:50:02 +000065
epoger@google.com74feb152013-06-13 19:12:05 +000066 # Tell skimage to read back the expectations file it just wrote, and
67 # confirm that the images in images_dir match it.
68 subprocess.check_call([skimage_binary, "--readPath", images_dir,
69 "--readExpectationsPath", expectations_path])
scroggo@google.com4fed6432013-05-14 17:50:02 +000070
epoger@google.com74feb152013-06-13 19:12:05 +000071 # TODO(scroggo): Add a test that compares expectations and image files that
72 # are known to NOT match, and make sure it returns an error.
73
scroggo@google.com2b9424b2013-06-21 19:12:47 +000074 # Generate an expectations file from an empty directory.
75 empty_dir = tempfile.mkdtemp()
76 expectations_path = os.path.join(file_dir, "skimage", "output-actual",
77 "empty-dir", "expectations.json")
78 subprocess.check_call([skimage_binary, "--readPath", empty_dir,
79 "--createExpectationsPath", expectations_path])
80 golden_expectations = os.path.join(file_dir, "skimage", "output-expected",
81 "empty-dir", "expectations.json")
82 DieIfFilesMismatch(expected=golden_expectations, actual=expectations_path)
83 os.rmdir(empty_dir)
84
85 # Generate an expectations file from a nonexistent directory.
86 expectations_path = os.path.join(file_dir, "skimage", "output-actual",
87 "nonexistent-dir", "expectations.json")
88 subprocess.check_call([skimage_binary, "--readPath", "/nonexistent/dir",
89 "--createExpectationsPath", expectations_path])
90 golden_expectations = os.path.join(file_dir, "skimage", "output-expected",
91 "nonexistent-dir", "expectations.json")
92 DieIfFilesMismatch(expected=golden_expectations, actual=expectations_path)
93
epoger@google.com74feb152013-06-13 19:12:05 +000094 # Done with all tests.
scroggo@google.com4fed6432013-05-14 17:50:02 +000095 print "Self tests succeeded!"
96
97if __name__ == "__main__":
98 main()