61acfa5df282fb806c33e104e7011ae4d8701cc5
[OpenColorIO-Configs.git] / aces_1.0.0 / python / aces_ocio / tests / tests_aces_config.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 """
5 Defines unit tests for *ACES* configuration.
6 """
7
8 import hashlib
9 import os
10 import re
11 import shutil
12 import sys
13
14 # TODO: Temporary ugly thing to be discussed, ideally the package should be
15 # in PYTHONPATH.
16 sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..'))
17
18 import tempfile
19 import unittest
20
21 from aces_ocio.utilities import files_walker
22 from aces_ocio.create_aces_config import (
23     ACES_OCIO_CTL_DIRECTORY_ENVIRON,
24     create_ACES_config)
25
26 __author__ = 'ACES Developers'
27 __copyright__ = 'Copyright (C) 2014 - 2015 - ACES Developers'
28 __license__ = ''
29 __maintainer__ = 'ACES Developers'
30 __email__ = 'aces@oscars.org'
31 __status__ = 'Production'
32
33 __all__ = ['REFERENCE_CONFIG_ROOT_DIRECTORY',
34            'HASH_TEST_PATTERNS',
35            'UNHASHABLE_TEST_PATTERNS',
36            'TestACESConfig']
37
38
39 # TODO: Investigate how the current config has been generated to use it for
40 # tests.
41 # REFERENCE_CONFIG_ROOT_DIRECTORY = os.path.abspath(
42 #     os.path.join(os.path.dirname(__file__), '..', '..', '..'))
43 REFERENCE_CONFIG_ROOT_DIRECTORY = '/colour-science/colour-ramblings/ocio/aces'
44
45 HASH_TEST_PATTERNS = ('\.3dl', '\.lut', '\.csp')
46 UNHASHABLE_TEST_PATTERNS = ('\.icc', '\.ocio')
47
48
49 class TestACESConfig(unittest.TestCase):
50     """
51     Performs tests on the *ACES* configuration.
52     """
53
54     def setUp(self):
55         """
56         Initialises common tests attributes.
57         """
58
59         self.__aces_ocio_ctl_directory = os.environ.get(
60             ACES_OCIO_CTL_DIRECTORY_ENVIRON, None)
61
62         assert self.__aces_ocio_ctl_directory is not None, (
63             'Undefined "{0}" environment variable!'.format(
64                 ACES_OCIO_CTL_DIRECTORY_ENVIRON))
65
66         assert os.path.exists(self.__aces_ocio_ctl_directory) is True, (
67             '"{0}" directory does not exists!'.format(
68                 self.__aces_ocio_ctl_directory))
69
70         self.maxDiff = None
71         self.__temporary_directory = tempfile.mkdtemp()
72
73     def tearDown(self):
74         """
75         Post tests actions.
76         """
77
78         shutil.rmtree(self.__temporary_directory)
79
80     @staticmethod
81     def directory_hashes(directory,
82                          filters_in=None,
83                          filters_out=None,
84                          flags=0):
85         """
86         Recursively computes the hashes from the file within given directory.
87
88         Parameters
89         ----------
90         directory : str or unicode
91             Directory to compute the file hashes.
92         filters_in : array_like
93             Included patterns.
94         filters_out : array_like
95             Excluded patterns.
96         flags : int
97             Regex flags.
98
99         Returns
100         -------
101         dict
102              Directory file hashes.
103         """
104
105         hashes = {}
106         for path in files_walker(directory,
107                                  filters_in=filters_in,
108                                  filters_out=filters_out):
109             with open(path) as file:
110                 hash = hashlib.md5(
111                     re.sub('\s', '', file.read())).hexdigest()
112             hashes[path.replace(directory, '')] = hash
113         return hashes
114
115     def test_ACES_config(self):
116         """
117         Performs tests on the *ACES* configuration by computing hashes on the
118         generated configuration and comparing them to the existing one.
119         """
120
121         self.assertTrue(create_ACES_config(self.__aces_ocio_ctl_directory,
122                                            self.__temporary_directory))
123
124         reference_hashes = self.directory_hashes(
125             REFERENCE_CONFIG_ROOT_DIRECTORY,
126             HASH_TEST_PATTERNS)
127         test_hashes = self.directory_hashes(
128             self.__temporary_directory,
129             HASH_TEST_PATTERNS)
130
131         self.assertDictEqual(reference_hashes, test_hashes)
132
133         # Checking that unashable files ('.icc', '.ocio') are generated.
134         unashable = lambda x: (
135             sorted([file.replace(x, '') for file in
136                     files_walker(x, UNHASHABLE_TEST_PATTERNS)]))
137
138         self.assertListEqual(unashable(REFERENCE_CONFIG_ROOT_DIRECTORY),
139                              unashable(self.__temporary_directory))
140
141
142 if __name__ == '__main__':
143     unittest.main()