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