blob: cb66fe1729f51c6e10c40bbed482c72b3be5e32a [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
12
13class BinaryNotFoundException(Exception):
14 def __str__ (self):
15 return ("Could not find binary!\n"
16 "Did you forget to build the tools project?\n"
17 "Self tests failed")
18
19# Find a path to the binary to use. Iterates through a list of possible
20# locations the binary may be.
21def PickBinaryPath(base_dir):
22 POSSIBLE_BINARY_PATHS = [
23 'out/Debug/skimage',
24 'out/Release/skimage',
25 'xcodebuild/Debug/skimage',
26 'xcodebuild/Release/skimage',
27 ]
28 for binary in POSSIBLE_BINARY_PATHS:
29 binary_full_path = os.path.join(base_dir, binary)
30 if (os.path.exists(binary_full_path)):
31 return binary_full_path
32 raise BinaryNotFoundException
33
epoger@google.com74feb152013-06-13 19:12:05 +000034# Quit early if two files have different content.
35def DieIfFilesMismatch(expected, actual):
36 if not filecmp.cmp(expected, actual):
37 print 'Error: file mismatch! expected=%s , actual=%s' % (
38 expected, actual)
39 exit(1)
40
scroggo@google.com4fed6432013-05-14 17:50:02 +000041def main():
42 # Use the directory of this file as the out directory
43 file_dir = os.path.abspath(os.path.dirname(__file__))
44
45 trunk_dir = os.path.normpath(os.path.join(file_dir, os.pardir, os.pardir))
46
47 # Find the binary
48 skimage_binary = PickBinaryPath(trunk_dir)
49 print "Running " + skimage_binary
50
epoger@google.com74feb152013-06-13 19:12:05 +000051 # Generate an expectations file from known images.
52 images_dir = os.path.join(file_dir, "skimage", "input",
53 "images-with-known-hashes")
54 expectations_path = os.path.join(file_dir, "skimage", "output-actual",
55 "create-expectations", "expectations.json")
56 subprocess.check_call([skimage_binary, "--readPath", images_dir,
57 "--createExpectationsPath", expectations_path])
scroggo@google.com4fed6432013-05-14 17:50:02 +000058
epoger@google.com74feb152013-06-13 19:12:05 +000059 # Make sure the expectations file was generated correctly.
60 golden_expectations = os.path.join(file_dir, "skimage", "output-expected",
61 "create-expectations",
62 "expectations.json")
63 DieIfFilesMismatch(expected=golden_expectations, actual=expectations_path)
scroggo@google.com4fed6432013-05-14 17:50:02 +000064
epoger@google.com74feb152013-06-13 19:12:05 +000065 # Tell skimage to read back the expectations file it just wrote, and
66 # confirm that the images in images_dir match it.
67 subprocess.check_call([skimage_binary, "--readPath", images_dir,
68 "--readExpectationsPath", expectations_path])
scroggo@google.com4fed6432013-05-14 17:50:02 +000069
epoger@google.com74feb152013-06-13 19:12:05 +000070 # 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.
72
73 # Done with all tests.
scroggo@google.com4fed6432013-05-14 17:50:02 +000074 print "Self tests succeeded!"
75
76if __name__ == "__main__":
77 main()