minimize tool initialization in tests
[scons.git] / test / Configure / custom-tests.py
bloba4c29c78e1db2a1b3b644b4e419626445ba0fed3
1 #!/usr/bin/env python
3 # MIT License
5 # Copyright The SCons Foundation
7 # Permission is hereby granted, free of charge, to any person obtaining
8 # a copy of this software and associated documentation files (the
9 # "Software"), to deal in the Software without restriction, including
10 # without limitation the rights to use, copy, modify, merge, publish,
11 # distribute, sublicense, and/or sell copies of the Software, and to
12 # permit persons to whom the Software is furnished to do so, subject to
13 # the following conditions:
15 # The above copyright notice and this permission notice shall be included
16 # in all copies or substantial portions of the Software.
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
19 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
20 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
26 """
27 Verify execution of custom test cases.
28 """
31 import TestSCons
33 _exe = TestSCons._exe
34 _obj = TestSCons._obj
35 _python_ = TestSCons._python_
37 test = TestSCons.TestSCons()
39 NCR = test.NCR # non-cached rebuild
40 CR = test.CR # cached rebuild (up to date)
41 NCF = test.NCF # non-cached build failure
42 CF = test.CF # cached build failure
44 compileOK = '#include <stdio.h>\\nint main(void) {printf("Hello");return 0;}'
45 compileFAIL = "syntax error"
46 linkOK = compileOK
47 linkFAIL = "void myFunc(); int main(void) { myFunc(); }"
48 runOK = compileOK
49 runFAIL = "int main(void) { return 1; }"
51 test.write('pyAct.py', """\
52 import sys
53 print(sys.argv[1])
54 sys.exit(int(sys.argv[1]))
55 """)
57 test.write('SConstruct', """\
58 DefaultEnvironment(tools=[])
59 def CheckCustom(test):
60 test.Message( 'Executing MyTest ... ' )
61 retCompileOK = test.TryCompile( '%(compileOK)s', '.c' )
62 retCompileFAIL = test.TryCompile( '%(compileFAIL)s', '.c' )
63 retLinkOK = test.TryLink( '%(linkOK)s', '.c' )
64 retLinkFAIL = test.TryLink( '%(linkFAIL)s', '.c' )
65 (retRunOK, outputRunOK) = test.TryRun( '%(runOK)s', '.c' )
66 (retRunFAIL, outputRunFAIL) = test.TryRun( '%(runFAIL)s', '.c' )
67 (retActOK, outputActOK) = test.TryAction( r'%(_python_)s pyAct.py 0 > $TARGET' )
68 (retActFAIL, outputActFAIL) = test.TryAction( r'%(_python_)s pyAct.py 1 > $TARGET' )
69 resOK = retCompileOK and retLinkOK and retRunOK and outputRunOK=="Hello"
70 resOK = resOK and retActOK and int(outputActOK)==0
71 resFAIL = retCompileFAIL or retLinkFAIL or retRunFAIL or outputRunFAIL!=""
72 resFAIL = resFAIL or retActFAIL or outputActFAIL!=""
73 test.Result( resOK and not resFAIL )
74 return resOK and not resFAIL
76 env = Environment()
77 import os
78 env.AppendENVPath('PATH', os.environ['PATH'])
79 conf = Configure( env, custom_tests={'CheckCustom' : CheckCustom} )
80 conf.CheckCustom()
81 env = conf.Finish()
82 """ % locals())
84 test.run()
86 test.checkLogAndStdout(["Executing MyTest ... "],
87 ["yes"],
88 [[(('.c', NCR), (_obj, NCR)),
89 (('.c', NCR), (_obj, NCF)),
90 (('.c', NCR), (_obj, NCR), (_exe, NCR)),
91 (('.c', NCR), (_obj, NCR), (_exe, NCF)),
92 (('.c', NCR), (_obj, NCR), (_exe, NCR), (_exe + '.out', NCR)),
93 (('.c', NCR), (_obj, NCR), (_exe, NCR), (_exe + '.out', NCF)),
94 (('', NCR),),
95 (('', NCF),)]],
96 "config.log", ".sconf_temp", "SConstruct")
98 test.run()
100 # Try again to check caching
101 test.checkLogAndStdout(["Executing MyTest ... "],
102 ["yes"],
103 [[(('.c', CR), (_obj, CR)),
104 (('.c', CR), (_obj, CF)),
105 (('.c', CR), (_obj, CR), (_exe, CR)),
106 (('.c', CR), (_obj, CR), (_exe, CF)),
107 (('.c', CR), (_obj, CR), (_exe, CR), (_exe + '.out', CR)),
108 (('.c', CR), (_obj, CR), (_exe, CR), (_exe + '.out', CF)),
109 (('', CR),),
110 (('', CF),)]],
111 "config.log", ".sconf_temp", "SConstruct")
113 # Test other customs:
114 test.write('SConstruct', """\
115 def CheckList(test):
116 test.Message( 'Display of list ...' )
117 res = [1, 2, 3, 4]
118 test.Result( res )
119 return res
121 def CheckEmptyList(test):
122 test.Message( 'Display of empty list ...' )
123 res = list()
124 test.Result( res )
125 return res
127 def CheckRandomStr(test):
128 test.Message( 'Display of random string ...' )
129 res = "a random string"
130 test.Result( res )
131 return res
133 def CheckEmptyStr(test):
134 test.Message( 'Display of empty string ...' )
135 res = ""
136 test.Result( res )
137 return res
139 def CheckDict(test):
140 test.Message( 'Display of dictionary ...' )
141 res = {"key1" : 1, "key2" : "text"}
142 test.Result( res )
143 return res
145 def CheckEmptyDict(test):
146 test.Message( 'Display of empty dictionary ...' )
147 res = dict
148 test.Result( res )
149 return res
151 env = Environment()
152 import os
153 env.AppendENVPath('PATH', os.environ['PATH'])
154 conf = Configure( env, custom_tests={'CheckList' : CheckList,
155 'CheckEmptyList' : CheckEmptyList,
156 'CheckRandomStr' : CheckRandomStr,
157 'CheckEmptyStr' : CheckEmptyStr,
158 'CheckDict' : CheckDict,
159 'CheckEmptyDict' : CheckEmptyDict} )
160 conf.CheckList()
161 conf.CheckEmptyList()
162 conf.CheckRandomStr()
163 conf.CheckEmptyStr()
164 conf.CheckDict()
165 conf.CheckEmptyDict()
166 env = conf.Finish()
167 """ % locals())
169 test.run()
171 test.must_match('config.log',
172 r""".*
174 scons: Configure: Display of list ...
175 scons: Configure: \(cached\) yes
177 scons: Configure: Display of empty list ...
178 scons: Configure: \(cached\) no
180 scons: Configure: Display of random string ...
181 scons: Configure: \(cached\) a random string
183 scons: Configure: Display of empty string ...
184 scons: Configure: \(cached\) *
186 scons: Configure: Display of dictionary ...
187 scons: Configure: \(cached\) yes
189 scons: Configure: Display of empty dictionary ...
190 scons: Configure: \(cached\) yes
193 """,
194 match=TestSCons.match_re)
196 test.pass_test()
198 # Local Variables:
199 # tab-width:4
200 # indent-tabs-mode:nil
201 # End:
202 # vim: set expandtab tabstop=4 shiftwidth=4: