Added fix for TeX includes with same name as subdirs.
[scons.git] / test / import.py
1 #!/usr/bin/env python
2 #
3 # __COPYRIGHT__
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #
24
25 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
26
27 """
28 Verify that we can import and use the contents of Platform and Tool
29 modules directly.
30 """
31
32 import os
33 import re
34 import sys
35
36 # must do this here, since TestSCons will chdir
37 tooldir = os.path.join(os.getcwd(), 'src', 'engine', 'SCons', 'Tool')
38
39 import TestSCons
40 test = TestSCons.TestSCons()
41
42 # Before executing the any of the platform or tool modules, add some
43 # null entries to the environment $PATH variable to make sure there's
44 # no code that tries to index elements from the list before making sure
45 # they're non-null.
46 # (This was a problem in checkpoint release 0.97.d020070809.)
47 os.environ['PATH'] = os.pathsep + os.environ['PATH'] + \
48                      os.pathsep + os.pathsep + '/no/such/dir' + os.pathsep
49
50 SConstruct_path = test.workpath('SConstruct')
51
52 platforms = [
53     'aix',
54     'cygwin',
55     'darwin',
56     'hpux',
57     'irix',
58     'os2',
59     'posix',
60     'sunos',
61     'win32'
62 ]
63
64 for platform in platforms:
65     test.write('SConstruct', """
66 print "Platform %(platform)s"
67 env = Environment(platform = '%(platform)s')
68 import SCons.Platform.%(platform)s
69 x = SCons.Platform.%(platform)s.generate
70 """ % locals())
71     test.run()
72
73 ignore = ('__init__.py',
74         # Can't import these everywhere due to Windows registry dependency.
75         '386asm.py', 'linkloc.py',
76         # Directory of common stuff for MSVC and MSVS
77         'MSCommon',
78         # Sun pkgchk and pkginfo common stuff
79         'sun_pkg.py',
80         )
81 tools = []
82 for name in os.listdir(tooldir):
83     if name in ignore: continue
84     if name[-3:] == '.py':
85         if name[-8:] not in ('Tests.py', 'ommon.py'):
86             tools.append(name[:-3])
87     elif os.path.exists(os.path.join(tooldir,name,'__init__.py')):
88         tools.append(name)
89 tools.sort()    # why not?
90
91 #if sys.platform == 'win32':
92 # Just comment out (for now?) due to registry dependency.
93 #    tools.extend([
94 #        '386asm',
95 #        'linkloc',
96 #    ])
97
98 # Intel no compiler warning..
99 intel_no_compiler_warning = """
100 scons: warning: Failed to find Intel compiler for version='None', abi='[^']*'
101 """ + TestSCons.file_expr
102
103 # Intel no top dir warning.
104 intel_no_top_dir_warning = """
105 scons: warning: Can't find Intel compiler top dir for version='None', abi='[^']*'
106 """ + TestSCons.file_expr
107
108 # Intel no license directory warning
109 intel_license_warning = re.escape("""
110 scons: warning: Intel license dir was not found.  Tried using the INTEL_LICENSE_FILE environment variable (), the registry () and the default path (C:\Program Files\Common Files\Intel\Licenses).  Using the default path as a last resort.
111 """) + TestSCons.file_expr
112
113 intel_warnings = [
114     re.compile(intel_license_warning),
115     re.compile(intel_no_compiler_warning),
116     re.compile(intel_no_compiler_warning + intel_license_warning),
117     re.compile(intel_no_top_dir_warning),
118     re.compile(intel_no_top_dir_warning + intel_license_warning),
119 ]
120
121 moc = test.where_is('moc')
122 if moc:
123     import os.path
124
125     qtdir = os.path.dirname(os.path.dirname(moc))
126
127     qt_err = r"""
128 scons: warning: Could not detect qt, using moc executable as a hint \(QTDIR=%(qtdir)s\)
129 """ % locals()
130
131 else:
132
133     qt_err = """
134 scons: warning: Could not detect qt, using empty QTDIR
135 """
136
137 qt_warnings = [ re.compile(qt_err + TestSCons.file_expr) ]
138
139 error_output = {
140     'icl' : intel_warnings,
141     'intelc' : intel_warnings,
142     'qt' : qt_warnings,
143 }
144
145 # An SConstruct for importing Tool names that have illegal characters
146 # for Python variable names.
147 indirect_import = """\
148 print "Tool %(tool)s (indirect)"
149 env = Environment(tools = ['%(tool)s'])
150
151 SCons = __import__('SCons.Tool.%(tool)s', globals(), locals(), [])
152 m = getattr(SCons.Tool, '%(tool)s')
153 env = Environment()
154 m.generate(env)
155 """
156
157 # An SConstruct for importing Tool names "normally."
158 direct_import = """\
159 print "Tool %(tool)s (direct)"
160 env = Environment(tools = ['%(tool)s'])
161
162 import SCons.Tool.%(tool)s
163 env = Environment()
164 SCons.Tool.%(tool)s.exists(env)
165 SCons.Tool.%(tool)s.generate(env)
166 """
167
168 failures = []
169 for tool in tools:
170     if tool[0] in '0123456789' or '+' in tool or tool in ('as',):
171         test.write('SConstruct', indirect_import % locals())
172     else:
173         test.write('SConstruct', direct_import % locals())
174     test.run(stderr=None)
175     stderr = test.stderr()
176     if stderr:
177         matched = None
178         for expression in error_output.get(tool, []):
179             if expression.match(stderr):
180                 matched = 1
181                 break
182         if not matched:
183             print "Failed importing '%s', stderr:" % tool
184             print stderr
185             failures.append(tool)
186
187 test.fail_test(len(failures))
188
189 test.pass_test()
190
191 # Local Variables:
192 # tab-width:4
193 # indent-tabs-mode:nil
194 # End:
195 # vim: set expandtab tabstop=4 shiftwidth=4: