Lit: Introduce "%/[STpst] into parseIntegratedTestScript(), to normalize substitutions.
[oota-llvm.git] / utils / lit / lit / TestingConfig.py
1 import os
2 import sys
3
4 PY2 = sys.version_info[0] < 3
5
6 class TestingConfig:
7     """"
8     TestingConfig - Information on the tests inside a suite.
9     """
10
11     @staticmethod
12     def fromdefaults(litConfig):
13         """
14         fromdefaults(litConfig) -> TestingConfig
15
16         Create a TestingConfig object with default values.
17         """
18         # Set the environment based on the command line arguments.
19         environment = {
20             'LIBRARY_PATH' : os.environ.get('LIBRARY_PATH',''),
21             'LD_LIBRARY_PATH' : os.environ.get('LD_LIBRARY_PATH',''),
22             'PATH' : os.pathsep.join(litConfig.path +
23                                      [os.environ.get('PATH','')]),
24             'SYSTEMROOT' : os.environ.get('SYSTEMROOT',''),
25             'TERM' : os.environ.get('TERM',''),
26             'LLVM_DISABLE_CRASH_REPORT' : '1',
27             }
28
29         if sys.platform == 'win32':
30             environment.update({
31                     'INCLUDE' : os.environ.get('INCLUDE',''),
32                     'PATHEXT' : os.environ.get('PATHEXT',''),
33                     'PYTHONUNBUFFERED' : '1',
34                     'TEMP' : os.environ.get('TEMP',''),
35                     'TMP' : os.environ.get('TMP',''),
36                     })
37
38         # Set the default available features based on the LitConfig.
39         available_features = []
40         if litConfig.useValgrind:
41             available_features.append('valgrind')
42             if litConfig.valgrindLeakCheck:
43                 available_features.append('vg_leak')
44
45         return TestingConfig(None,
46                              name = '<unnamed>',
47                              suffixes = set(),
48                              test_format = None,
49                              environment = environment,
50                              substitutions = [],
51                              unsupported = False,
52                              test_exec_root = None,
53                              test_source_root = None,
54                              excludes = [],
55                              available_features = available_features,
56                              pipefail = True)
57
58     def load_from_path(self, path, litConfig):
59         """
60         load_from_path(path, litConfig)
61
62         Load the configuration module at the provided path into the given config
63         object.
64         """
65
66         # Load the config script data.
67         f = open(path)
68         try:
69             data = f.read()
70         except:
71             litConfig.fatal('unable to load config file: %r' % (path,))
72         f.close()
73
74         # Execute the config script to initialize the object.
75         cfg_globals = dict(globals())
76         cfg_globals['config'] = self
77         cfg_globals['lit_config'] = litConfig
78         cfg_globals['__file__'] = path
79         try:
80             if PY2:
81                 exec("exec data in cfg_globals")
82             else:
83                 exec(data, cfg_globals)
84             if litConfig.debug:
85                 litConfig.note('... loaded config %r' % path)
86         except SystemExit:
87             e = sys.exc_info()[1]
88             # We allow normal system exit inside a config file to just
89             # return control without error.
90             if e.args:
91                 raise
92         except:
93             import traceback
94             litConfig.fatal(
95                 'unable to parse config file %r, traceback: %s' % (
96                     path, traceback.format_exc()))
97
98         self.finish(litConfig)
99
100     def __init__(self, parent, name, suffixes, test_format,
101                  environment, substitutions, unsupported,
102                  test_exec_root, test_source_root, excludes,
103                  available_features, pipefail):
104         self.parent = parent
105         self.name = str(name)
106         self.suffixes = set(suffixes)
107         self.test_format = test_format
108         self.environment = dict(environment)
109         self.substitutions = list(substitutions)
110         self.unsupported = unsupported
111         self.test_exec_root = test_exec_root
112         self.test_source_root = test_source_root
113         self.excludes = set(excludes)
114         self.available_features = set(available_features)
115         self.pipefail = pipefail
116
117     def clone(self):
118         # FIXME: Chain implementations?
119         #
120         # FIXME: Allow extra parameters?
121         return TestingConfig(self, self.name, self.suffixes, self.test_format,
122                              self.environment, self.substitutions,
123                              self.unsupported,
124                              self.test_exec_root, self.test_source_root,
125                              self.excludes, self.available_features,
126                              self.pipefail)
127
128     def finish(self, litConfig):
129         """finish() - Finish this config object, after loading is complete."""
130
131         self.name = str(self.name)
132         self.suffixes = set(self.suffixes)
133         self.environment = dict(self.environment)
134         self.substitutions = list(self.substitutions)
135         if self.test_exec_root is not None:
136             # FIXME: This should really only be suite in test suite config
137             # files. Should we distinguish them?
138             self.test_exec_root = str(self.test_exec_root)
139         if self.test_source_root is not None:
140             # FIXME: This should really only be suite in test suite config
141             # files. Should we distinguish them?
142             self.test_source_root = str(self.test_source_root)
143         self.excludes = set(self.excludes)
144
145     @property
146     def root(self):
147         """root attribute - The root configuration for the test suite."""
148         if self.parent is None:
149             return self
150         else:
151             return self.parent.root
152