Updates for CHANGES/RELEASE.txt
[scons.git] / SCons / DefaultsTests.py
bloba59b8b00980990507962084787c1951e8a01936d
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
26 import collections
28 import TestCmd
30 from SCons.Defaults import mkdir_func, _defines, processDefines
31 from SCons.Errors import UserError
34 class DummyEnvironment(collections.UserDict):
35 def __init__(self, **kwargs) -> None:
36 super().__init__()
37 self.data.update(kwargs)
39 def subst(self, str_subst, target=None, source=None, conv=None):
40 if str_subst[0] == '$':
41 return self.data[str_subst[1:]]
42 return str_subst
44 def subst_list(self, str_subst, target=None, source=None, conv=None):
45 if str_subst[0] == '$':
46 return [self.data[str_subst[1:]]]
47 return [[str_subst]]
50 class DefaultsTestCase(unittest.TestCase):
51 def test_mkdir_func0(self) -> None:
52 test = TestCmd.TestCmd(workdir='')
53 test.subdir('sub')
54 subdir2 = test.workpath('sub', 'dir1', 'dir2')
55 # Simple smoke test
56 mkdir_func(subdir2)
57 mkdir_func(subdir2) # 2nd time should be OK too
59 def test_mkdir_func1(self) -> None:
60 test = TestCmd.TestCmd(workdir='')
61 test.subdir('sub')
62 subdir1 = test.workpath('sub', 'dir1')
63 subdir2 = test.workpath('sub', 'dir1', 'dir2')
64 # No error if asked to create existing dir
65 os.makedirs(subdir2)
66 mkdir_func(subdir2)
67 mkdir_func(subdir1)
69 def test_mkdir_func2(self) -> None:
70 test = TestCmd.TestCmd(workdir='')
71 test.subdir('sub')
72 subdir1 = test.workpath('sub', 'dir1')
73 subdir2 = test.workpath('sub', 'dir1', 'dir2')
74 file = test.workpath('sub', 'dir1', 'dir2', 'file')
76 # make sure it does error if asked to create a dir
77 # where there's already a file
78 os.makedirs(subdir2)
79 test.write(file, "test\n")
80 try:
81 mkdir_func(file)
82 except OSError as e:
83 pass
84 else:
85 self.fail("expected OSError")
87 def test__defines_no_target_or_source_arg(self) -> None:
88 """
89 Verify that _defines() function can handle either or neither source or
90 target being specified
91 """
92 env = DummyEnvironment()
94 with self.subTest():
95 # Neither source or target specified
96 x = _defines('-D', ['A', 'B', 'C'], 'XYZ', env)
97 self.assertEqual(x, ['-DAXYZ', '-DBXYZ', '-DCXYZ'])
99 with self.subTest():
100 # only source specified
101 y = _defines('-D', ['AA', 'BA', 'CA'], 'XYZA', env, 'XYZ')
102 self.assertEqual(y, ['-DAAXYZA', '-DBAXYZA', '-DCAXYZA'])
104 with self.subTest():
105 # source and target specified
106 z = _defines('-D', ['AAB', 'BAB', 'CAB'], 'XYZAB', env, 'XYZ', 'abc')
107 self.assertEqual(z, ['-DAABXYZAB', '-DBABXYZAB', '-DCABXYZAB'])
109 def test_processDefines(self) -> None:
110 """Verify correct handling in processDefines."""
111 env = DummyEnvironment()
113 with self.subTest():
114 # macro name only
115 rv = processDefines('name')
116 self.assertEqual(rv, ['name'])
118 with self.subTest():
119 # macro with value
120 rv = processDefines('name=val')
121 self.assertEqual(rv, ['name=val'])
123 with self.subTest():
124 # space-separated macros
125 rv = processDefines('name1 name2=val2')
126 self.assertEqual(rv, ['name1', 'name2=val2'])
128 with self.subTest():
129 # single list
130 rv = processDefines(['name', 'val'])
131 self.assertEqual(rv, ['name', 'val'])
133 with self.subTest():
134 # single tuple
135 rv = processDefines(('name', 'val'))
136 self.assertEqual(rv, ['name=val'])
138 with self.subTest():
139 # single dict
140 rv = processDefines({'foo': None, 'name': 'val'})
141 self.assertEqual(rv, ['foo', 'name=val'])
143 with self.subTest():
144 # compound list
145 rv = processDefines(['foo', ('name', 'val'), ['name2', 'val2']])
146 self.assertEqual(rv, ['foo', 'name=val', 'name2=val2'])
148 with self.subTest():
149 # invalid tuple
150 with self.assertRaises(
151 UserError, msg="Invalid tuple should throw SCons.Errors.UserError"
153 rv = processDefines([('name', 'val', 'bad')])
156 if __name__ == "__main__":
157 unittest.main()
159 # Local Variables:
160 # tab-width:4
161 # indent-tabs-mode:nil
162 # End:
163 # vim: set expandtab tabstop=4 shiftwidth=4: