Merge pull request #4655 from bdbaddog/fix_new_ninja_package
[scons.git] / test / Variables / PackageVariable.py
blob77c04e60880bfae79b8a4892877ca42ea0441c7b
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 Test the PackageVariable canned Variable type.
28 """
30 from __future__ import annotations
32 import os
34 import TestSCons
36 test = TestSCons.TestSCons()
38 SConstruct_path = test.workpath('SConstruct')
40 def check(expect: list[str]) -> None:
41 result = test.stdout().split('\n')
42 # skip first line and any lines beyond the length of expect
43 assert result[1:len(expect)+1] == expect, (result[1:len(expect)+1], expect)
45 test.write(SConstruct_path, """\
46 from SCons.Variables.PackageVariable import PackageVariable as PV
47 from SCons.Variables import PackageVariable
49 opts = Variables(args=ARGUMENTS)
50 opts.AddVariables(
51 PackageVariable('x11', 'use X11 installed here (yes = search some places', 'yes'),
52 PV('package', 'help for package', 'yes'),
55 _ = DefaultEnvironment(tools=[])
56 env = Environment(variables=opts, tools=[])
57 Help(opts.GenerateHelpText(env))
59 print(env['x11'])
60 Default(env.Alias('dummy', None))
61 """)
63 test.run()
64 check([str(True)])
66 test.run(arguments='x11=no')
67 check([str(False)])
69 test.run(arguments='x11=0')
70 check([str(False)])
72 test.run(arguments=['x11=%s' % test.workpath()])
73 check([test.workpath()])
75 space_subdir = test.workpath('space subdir')
76 test.subdir(space_subdir)
77 test.run(arguments=[f'x11={space_subdir}'])
78 check([space_subdir])
80 expect_stderr = """
81 scons: *** Path does not exist for variable 'x11': '/non/existing/path/'
82 """ + test.python_file_line(SConstruct_path, 11)
84 test.run(arguments='x11=/non/existing/path/', stderr=expect_stderr, status=2)
86 # test that an enabling value produces the default value
87 # as long as that's a path string
88 tinycbor_path = test.workpath('path', 'to', 'tinycbor')
89 test.subdir(tinycbor_path)
90 SConstruct_pathstr = test.workpath('SConstruct.path')
91 test.write(SConstruct_pathstr, f"""\
92 from SCons.Variables import PackageVariable
94 vars = Variables(args=ARGUMENTS)
95 vars.Add(
96 PackageVariable(
97 'tinycbor',
98 help="use 'tinycbor' at <path>",
99 default=r'{tinycbor_path}'
103 _ = DefaultEnvironment(tools=[])
104 env = Environment(variables=vars, tools=[])
106 print(env['tinycbor'])
107 Default(env.Alias('dummy', None))
108 """)
110 test.run(arguments=['-f', 'SConstruct.path', 'tinycbor=yes'])
111 check([tinycbor_path])
113 test.pass_test()
115 # Local Variables:
116 # tab-width:4
117 # indent-tabs-mode:nil
118 # End:
119 # vim: set expandtab tabstop=4 shiftwidth=4: