1# SPDX-License-Identifier: Apache-2.0
2# -----------------------------------------------------------------------------
3# Copyright 2020 Arm Limited
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may not
6# use this file except in compliance with the License. You may obtain a copy
7# of the License at:
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
16# -----------------------------------------------------------------------------
17"""
18An ASTC TestSet is comprised of a set of TestImages. Images are stored in a
19structured directory layout. This structure encodes important metadata about
20each image - such as color profile and data encoding - in the directory and
21file names used.
22
23TestSets are built by using reflection on a root directory to automatically
24find all of the test images that comprise the set.
25"""
26
27import os
28
29from testlib.image import TestImage
30
31
32class TSetException(Exception):
33    """
34    Exception thrown for bad test set specification.
35    """
36
37
38class TestSet():
39    """
40    Generate a list of images that are test candidates.
41
42    This reflection is built automatically based on a directory of images on
43    disk, provided that the images follow a standard structure.
44
45    Attributes:
46        name: The name of the test set.
47        tests: The list of TestImages forming the set.
48    """
49
50    def __init__(self, name, rootDir, profiles, formats, imageFilter=None):
51        """
52        Create a new TestSet through reflection.
53
54        Args:
55            name (str): The name of the test set.
56            rootDir (str): The root directory of the test set.
57            profiles (list(str)): The ASTC profiles to allow.
58            formats (list(str)): The image formats to allow.
59            imageFilter (str): The name of the image to include (for bug repo).
60
61        Raises:
62            TSetException: The specified TestSet could not be loaded.
63        """
64        self.name = name
65
66        if not os.path.exists(rootDir) and not os.path.isdir(rootDir):
67            raise TSetException("Bad test set root directory (%s)" % rootDir)
68
69        self.tests = []
70
71        for (dirPath, dirNames, fileNames) in os.walk(rootDir):
72            for fileName in fileNames:
73                # Only select image files
74                fileExt = os.path.splitext(fileName)[1]
75                if fileExt not in TestImage.TEST_EXTS:
76                    continue
77
78                # Create the TestImage for each file on disk
79                filePath = os.path.join(dirPath, fileName)
80                image = TestImage(filePath)
81
82                # Filter out the ones we don't want to allow
83                if image.colorProfile not in profiles:
84                    continue
85
86                if image.colorFormat not in formats:
87                    continue
88
89                if imageFilter and image.testFile != imageFilter:
90                    continue
91
92                self.tests.append((filePath, image))
93
94        # Sort the TestImages so they are in a stable order
95        self.tests.sort()
96        self.tests = [x[1] for x in self.tests]
97