[ci skip] Add note that this change may break SetOption() + ninja usage with fix
[scons.git] / test / Value / Value.py
blob7bb48ca9f18605911bb90312f3fb041d68a7665d
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 """Test Value() with different deciders."""
28 import re
30 import TestSCons
31 import TestCmd
33 _python_ = TestSCons._python_
35 test = TestSCons.TestSCons(match=TestCmd.match_re)
37 python = TestSCons.python
39 # do not f-string, "source_signature" substituted in a loop below
40 SConstruct_content = """
41 Decider(r'%(source_signature)s')
43 class Custom:
44 def __init__(self, value):
45 self.value = value
47 def __str__(self):
48 return "C=" + str(self.value)
50 P = ARGUMENTS.get('prefix', '/usr/local')
51 L = len(P)
52 C = Custom(P)
54 def create(target, source, env):
55 with open(str(target[0]), 'wb') as f:
56 f.write(source[0].get_contents())
58 DefaultEnvironment(tools=[]) # test speedup
59 env = Environment()
60 env['BUILDERS']['B'] = Builder(action=create)
61 env['BUILDERS']['S'] = Builder(action=r'%(_python_)s put.py $SOURCES into $TARGET')
62 env.B('f1.out', Value(P))
63 env.B('f2.out', env.Value(L))
64 env.B('f3.out', Value(C))
65 env.S('f4.out', Value(L))
67 def create_value(target, source, env):
68 target[0].write(source[0].get_contents())
70 def create_value_file(target, source, env):
71 with open(str(target[0]), 'wb') as f:
72 f.write(source[0].read())
74 env['BUILDERS']['B2'] = Builder(action=create_value)
75 env['BUILDERS']['B3'] = Builder(action=create_value_file)
77 V = Value('my value')
78 env.B2(V, 'f3.out')
79 env.B3('f5.out', V)
80 """
82 test.write('put.py', """\
83 import os
84 import sys
85 with open(sys.argv[-1],'w') as f:
86 f.write(" ".join(sys.argv[1:-2]))
87 """)
89 # Run all of the tests with both types of source signature
90 # to make sure there's no difference in behavior.
91 for source_signature in ['MD5', 'timestamp-newer']:
93 print("Testing Value node with source signatures:", source_signature)
95 test.write('SConstruct', SConstruct_content % locals())
97 test.run(arguments='-c')
98 test.run()
100 out7 = """create_value(['my value'], ["f3.out"])"""
101 out8 = """create_value_file(["f5.out"], ['my value'])"""
103 out1 = """create(["f1.out"], ['/usr/local'])"""
104 out2 = """create(["f2.out"], [10])"""
105 out3 = """create\\(\\["f3.out"\\], \\[<.*.Custom (instance|object) at """
106 #" <- unconfuses emacs syntax highlighting
108 test.must_contain_all_lines(test.stdout(), [out1, out2, out7, out8])
109 #print test.stdout()
110 test.fail_test(re.search(out3, test.stdout()) is None)
112 test.must_match('f1.out', "/usr/local")
113 test.must_match('f2.out', "10")
114 test.must_match('f3.out', "C=/usr/local")
115 test.must_match('f4.out', '10')
116 test.must_match('f5.out', "C=/usr/local")
118 test.up_to_date(arguments='.')
120 test.run(options='prefix=/usr')
121 out4 = """create(["f1.out"], ['/usr'])"""
122 out5 = """create(["f2.out"], [4])"""
123 out6 = """create\\(\\["f3.out"\\], \\[<.*.Custom (instance|object) at """
124 #" <- unconfuses emacs syntax highlighting
125 test.must_contain_all_lines(test.stdout(), [out4, out5])
126 test.fail_test(re.search(out6, test.stdout()) is None)
128 test.must_match('f1.out', "/usr")
129 test.must_match('f2.out', "4")
130 test.must_match('f3.out', "C=/usr")
131 test.must_match('f4.out', '4')
133 test.up_to_date(options='prefix=/usr', arguments='.')
135 test.unlink('f3.out')
137 test.run(options='prefix=/var')
138 out4 = """create(["f1.out"], ['/var'])"""
140 test.must_contain_all_lines(test.stdout(), [out4, out7, out8])
141 test.must_not_contain_any_line(test.stdout(), [out5])
142 test.fail_test(re.search(out6, test.stdout()) is None)
144 test.up_to_date(options='prefix=/var', arguments='.')
146 test.must_match('f1.out', "/var")
147 test.must_match('f2.out', "4")
148 test.must_match('f3.out', "C=/var")
149 test.must_match('f4.out', "4")
150 test.must_match('f5.out', "C=/var")
152 test.pass_test()
154 # Local Variables:
155 # tab-width:4
156 # indent-tabs-mode:nil
157 # End:
158 # vim: set expandtab tabstop=4 shiftwidth=4: