Implement code review notes.
[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.generate_config import (
23     ACES_OCIO_CTL_DIRECTORY_ENVIRON,
24     generate_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
44 HASH_TEST_PATTERNS = ('\.3dl', '\.lut', '\.csp')
45 UNHASHABLE_TEST_PATTERNS = ('\.icc', '\.ocio')
46
47
48 class TestACESConfig(unittest.TestCase):
49     """
50     Performs tests on the *ACES* configuration.
51     """
52
53     def setUp(self):
54         """
55         Initialises common tests attributes.
56         """
57
58         self.__aces_ocio_ctl_directory = os.environ.get(
59             ACES_OCIO_CTL_DIRECTORY_ENVIRON, None)
60
61         assert self.__aces_ocio_ctl_directory is not None, (
62             'Undefined "{0}" environment variable!'.format(
63                 ACES_OCIO_CTL_DIRECTORY_ENVIRON))
64
65         assert os.path.exists(self.__aces_ocio_ctl_directory) is True, (
66             '"{0}" directory does not exists!'.format(
67                 self.__aces_ocio_ctl_directory))
68
69         self.maxDiff = None
70         self.__temporary_directory = tempfile.mkdtemp()
71
72     def tearDown(self):
73         """
74         Post tests actions.
75         """
76
77         shutil.rmtree(self.__temporary_directory)
78
79     @staticmethod
80     def directory_hashes(directory,
81                          filters_in=None,
82                          filters_out=None,
83                          flags=0):
84         """
85         Recursively computes the hashes from the file within given directory.
86
87         Parameters
88         ----------
89         directory : str or unicode
90             Directory to compute the file hashes.
91         filters_in : array_like
92             Included patterns.
93         filters_out : array_like
94             Excluded patterns.
95         flags : int
96             Regex flags.
97
98         Returns
99         -------
100         dict
101              Directory file hashes.
102         """
103
104         hashes = {}
105         for path in files_walker(directory,
106                                  filters_in=filters_in,
107                                  filters_out=filters_out,
108                                  flags=flags):
109             with open(path) as file:
110                 digest = hashlib.md5(
111                     re.sub('\s', '', file.read())).hexdigest()
112             hashes[path.replace(directory, '')] = digest
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(generate_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()