Code formatting.
[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 from __future__ import division
9
10 import hashlib
11 import os
12 import re
13 import shutil
14 import sys
15 import tempfile
16 import unittest
17
18 sys.path.append(os.path.abspath(
19     os.path.join(os.path.dirname(__file__), '..', '..')))
20
21 from aces_ocio.utilities import files_walker
22 from aces_ocio.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                                  flags=flags):
110             with open(path) as file:
111                 digest = hashlib.md5(
112                     re.sub('\s', '', file.read())).hexdigest()
113             hashes[path.replace(directory, '')] = digest
114         return hashes
115
116     def test_ACES_config(self):
117         """
118         Performs tests on the *ACES* configuration by computing hashes on the
119         generated configuration and comparing them to the existing one.
120         """
121
122         self.assertTrue(create_ACES_config(self.__aces_ocio_ctl_directory,
123                                            self.__temporary_directory))
124
125         reference_hashes = self.directory_hashes(
126             REFERENCE_CONFIG_ROOT_DIRECTORY,
127             HASH_TEST_PATTERNS)
128         test_hashes = self.directory_hashes(
129             self.__temporary_directory,
130             HASH_TEST_PATTERNS)
131
132         self.assertDictEqual(reference_hashes, test_hashes)
133
134         # Checking that unashable files ('.icc', '.ocio') are generated.
135         unashable = lambda x: (
136             sorted([file.replace(x, '') for file in
137                     files_walker(x, UNHASHABLE_TEST_PATTERNS)]))
138
139         self.assertListEqual(unashable(REFERENCE_CONFIG_ROOT_DIRECTORY),
140                              unashable(self.__temporary_directory))
141
142
143 if __name__ == '__main__':
144     unittest.main()