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