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.
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:
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:]]
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:]]]
50 class DefaultsTestCase(unittest
.TestCase
):
51 def test_mkdir_func0(self
) -> None:
52 test
= TestCmd
.TestCmd(workdir
='')
54 subdir2
= test
.workpath('sub', 'dir1', 'dir2')
57 mkdir_func(subdir2
) # 2nd time should be OK too
59 def test_mkdir_func1(self
) -> None:
60 test
= TestCmd
.TestCmd(workdir
='')
62 subdir1
= test
.workpath('sub', 'dir1')
63 subdir2
= test
.workpath('sub', 'dir1', 'dir2')
64 # No error if asked to create existing dir
69 def test_mkdir_func2(self
) -> None:
70 test
= TestCmd
.TestCmd(workdir
='')
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
79 test
.write(file, "test\n")
85 self
.fail("expected OSError")
87 def test__defines_no_target_or_source_arg(self
) -> None:
89 Verify that _defines() function can handle either or neither source or
90 target being specified
92 env
= DummyEnvironment()
95 # Neither source or target specified
96 x
= _defines('-D', ['A', 'B', 'C'], 'XYZ', env
)
97 self
.assertEqual(x
, ['-DAXYZ', '-DBXYZ', '-DCXYZ'])
100 # only source specified
101 y
= _defines('-D', ['AA', 'BA', 'CA'], 'XYZA', env
, 'XYZ')
102 self
.assertEqual(y
, ['-DAAXYZA', '-DBAXYZA', '-DCAXYZA'])
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()
115 rv
= processDefines('name')
116 self
.assertEqual(rv
, ['name'])
120 rv
= processDefines('name=val')
121 self
.assertEqual(rv
, ['name=val'])
124 # space-separated macros
125 rv
= processDefines('name1 name2=val2')
126 self
.assertEqual(rv
, ['name1', 'name2=val2'])
130 rv
= processDefines(['name', 'val'])
131 self
.assertEqual(rv
, ['name', 'val'])
135 rv
= processDefines(('name', 'val'))
136 self
.assertEqual(rv
, ['name=val'])
140 rv
= processDefines({'foo': None, 'name': 'val'})
141 self
.assertEqual(rv
, ['foo', 'name=val'])
145 rv
= processDefines(['foo', ('name', 'val'), ['name2', 'val2']])
146 self
.assertEqual(rv
, ['foo', 'name=val', 'name2=val2'])
150 with self
.assertRaises(
151 UserError
, msg
="Invalid tuple should throw SCons.Errors.UserError"
153 rv
= processDefines([('name', 'val', 'bad')])
156 if __name__
== "__main__":
161 # indent-tabs-mode:nil
163 # vim: set expandtab tabstop=4 shiftwidth=4: