Add main module attributes.
[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 the generated *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.util import files_walker
16 from aces_ocio.create_aces_config import (
17     ACES_OCIO_CTL_DIRECTORY_ENVIRON,
18     createACESConfig)
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     """
46
47     def setUp(self):
48         """
49         Initialises common tests attributes.
50         """
51
52         self.__aces_ocio_ctl_directory = os.environ.get(
53             ACES_OCIO_CTL_DIRECTORY_ENVIRON, None)
54
55         assert self.__aces_ocio_ctl_directory is not None, (
56             'Undefined "{0}" environment variable!'.format(
57                 ACES_OCIO_CTL_DIRECTORY_ENVIRON))
58
59         assert os.path.exists(self.__aces_ocio_ctl_directory) is True, (
60             '"{0}" directory does not exists!'.format(
61                 self.__aces_ocio_ctl_directory))
62
63         self.maxDiff = None
64         self.__temporary_directory = tempfile.mkdtemp()
65
66     def tearDown(self):
67         """
68         Post tests actions.
69         """
70
71         shutil.rmtree(self.__temporary_directory)
72
73     @staticmethod
74     def directory_hashes(directory, filters_in=None, filters_out=None):
75         """
76         """
77
78         hashes = {}
79         for path in files_walker(directory,
80                                  filters_in=filters_in,
81                                  filters_out=filters_out):
82             with open(path) as file:
83                 hash = hashlib.md5(
84                     re.sub('\s', '', file.read())).hexdigest()
85             hashes[path.replace(directory, '')] = hash
86         return hashes
87
88     def test_ACES_config(self):
89         """
90         """
91
92         self.assertTrue(createACESConfig(self.__aces_ocio_ctl_directory,
93                                          self.__temporary_directory))
94
95         reference_hashes = self.directory_hashes(
96             REFERENCE_CONFIG_ROOT_DIRECTORY,
97             HASH_TEST_PATTERNS)
98         test_hashes = self.directory_hashes(
99             self.__temporary_directory,
100             HASH_TEST_PATTERNS)
101
102         self.assertDictEqual(reference_hashes, test_hashes)
103
104         # Checking that unashable files ('.icc', '.ocio') are generated.
105         unashable = lambda x: (
106             sorted([file.replace(x, '') for file in
107                     files_walker(x, UNHASHABLE_TEST_PATTERNS)]))
108
109         self.assertListEqual(unashable(REFERENCE_CONFIG_ROOT_DIRECTORY),
110                              unashable(self.__temporary_directory))
111
112
113 if __name__ == '__main__':
114     unittest.main()