Followon to PR #4348: more bool fixes
[scons.git] / SCons / Tool / ToolTests.py
blobe87a9725f4b44a8c1c8e56d05e1f188785a1cd6d
1 # MIT License
3 # Copyright The SCons Foundation
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:
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
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.
24 import os
25 import unittest
27 import TestUnit
29 import SCons.Errors
30 import SCons.Tool
33 class DummyEnvironment:
34 def __init__(self) -> None:
35 self.dict = {}
36 def Detect(self, progs):
37 if not SCons.Util.is_List(progs):
38 progs = [ progs ]
39 return progs[0]
40 def Append(self, **kw) -> None:
41 self.dict.update(kw)
42 def __getitem__(self, key):
43 return self.dict[key]
44 def __setitem__(self, key, val) -> None:
45 self.dict[key] = val
46 def __contains__(self, key) -> bool:
47 return key in self.dict
48 def subst(self, string, *args, **kwargs):
49 return string
51 PHONY_PATH = "/usr/phony/bin"
52 def WhereIs(self, key_program):
53 # for pathfind test for Issue #3336:
54 # need to fake the case where extra paths are searched, and
55 # if one has a "hit" after some fails, the fails are left in
56 # the environment's PATH. So construct a positive answer if
57 # we see a magic known path component in PATH; answer in
58 # the negative otherwise.
59 paths = self['ENV']['PATH']
60 if self.PHONY_PATH in paths:
61 return os.path.join(self.PHONY_PATH, key_program)
62 return None
63 def AppendENVPath(self, pathvar, path) -> None:
64 # signature matches how called from find_program_path()
65 self['ENV'][pathvar] = self['ENV'][pathvar] + os.pathsep + path
68 class ToolTestCase(unittest.TestCase):
69 def test_Tool(self) -> None:
70 """Test the Tool() function"""
72 env = DummyEnvironment()
73 env['BUILDERS'] = {}
74 env['ENV'] = {}
75 env['PLATFORM'] = 'test'
76 t = SCons.Tool.Tool('g++')
77 t(env)
78 assert (env['CXX'] == 'c++' or env['CXX'] == 'g++'), env['CXX']
79 assert env['INCPREFIX'] == '-I', env['INCPREFIX']
80 assert env['TOOLS'] == ['g++'], env['TOOLS']
82 exc_caught = None
83 try:
84 SCons.Tool.Tool()
85 except TypeError:
86 exc_caught = 1
87 assert exc_caught, "did not catch expected UserError"
89 exc_caught = None
90 try:
91 p = SCons.Tool.Tool('_does_not_exist_')
92 except SCons.Errors.UserError as e:
93 exc_caught = 1
94 # Old msg was Python-style "No tool named", check for new msg:
95 assert "No tool module" in str(e), e
96 assert exc_caught, "did not catch expected UserError"
99 def test_pathfind(self) -> None:
100 """Test that find_program_path() alters PATH only if add_path is true"""
102 env = DummyEnvironment()
103 PHONY_PATHS = [
104 r'C:\cygwin64\bin',
105 r'C:\cygwin\bin',
106 '/usr/local/dummy/bin',
107 env.PHONY_PATH, # will be recognized by dummy WhereIs
109 env['ENV'] = {}
110 env['ENV']['PATH'] = '/usr/local/bin:/opt/bin:/bin:/usr/bin'
111 pre_path = env['ENV']['PATH']
112 _ = SCons.Tool.find_program_path(env, 'no_tool', default_paths=PHONY_PATHS)
113 assert env['ENV']['PATH'] == pre_path, env['ENV']['PATH']
115 _ = SCons.Tool.find_program_path(env, 'no_tool', default_paths=PHONY_PATHS, add_path=True)
116 assert env.PHONY_PATH in env['ENV']['PATH'], env['ENV']['PATH']
119 if __name__ == "__main__":
120 loader = unittest.TestLoader()
121 loader.testMethodPrefix = 'test_'
122 suite = loader.loadTestsFromTestCase(ToolTestCase)
123 TestUnit.run(suite)
125 # Local Variables:
126 # tab-width:4
127 # indent-tabs-mode:nil
128 # End:
129 # vim: set expandtab tabstop=4 shiftwidth=4: