Merged revisions 1582-1665 via svnmerge from
[scons.git] / test / Value.py
1 #!/usr/bin/env python
2 #
3 # __COPYRIGHT__
4 #
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:
12 #
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
15 #
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.
23 #
24
25 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
26
27 import os
28 import re
29 import string
30 import sys
31 import TestSCons
32 import TestCmd
33
34 _python_ = TestSCons._python_
35
36 test = TestSCons.TestSCons(match=TestCmd.match_re)
37
38 # Run all of the tests with both types of source signature
39 # to make sure there's no difference in behavior.
40 for source_signature in ['MD5', 'timestamp']:
41
42     print "Testing Value node with source signatures:", source_signature
43
44     test.write('SConstruct', """
45 SourceSignatures(r'%(source_signature)s')
46
47 class Custom:
48     def __init__(self, value):  self.value = value
49     def __str__(self):          return "C=" + str(self.value)
50
51 P = ARGUMENTS.get('prefix', '/usr/local')
52 L = len(P)
53 C = Custom(P)
54
55 def create(target, source, env):
56     open(str(target[0]), 'wb').write(source[0].get_contents())
57
58 env = Environment()
59 env['BUILDERS']['B'] = Builder(action = create)
60 env['BUILDERS']['S'] = Builder(action = '%(_python_)s put $SOURCES into $TARGET')
61 env.B('f1.out', Value(P))
62 env.B('f2.out', env.Value(L))
63 env.B('f3.out', Value(C))
64 env.S('f4.out', Value(L))
65
66 def create_value (target, source, env):
67     target[0].write(source[0].get_contents ())
68
69 def create_value_file (target, source, env):
70     open(str(target[0]), 'wb').write(source[0].read())
71
72 env['BUILDERS']['B2'] = Builder(action = create_value)
73 env['BUILDERS']['B3'] = Builder(action = create_value_file)
74
75 V = Value('my value')
76 env.B2(V, 'f3.out')
77 env.B3('f5.out', V)
78 """ % locals())
79
80     test.write('put', """
81 import os
82 import string
83 import sys
84 open(sys.argv[-1],'wb').write(string.join(sys.argv[1:-2]))
85 """)
86
87     test.run(arguments='-c')
88     test.run()
89
90     out7 = """create_value(["'my value'"], ["f3.out"])"""
91     out8 = """create_value_file(["f5.out"], ["'my value'"])"""
92
93     out1 = """create(["f1.out"], ["'/usr/local'"])"""
94     out2 = """create(["f2.out"], ["10"])"""
95     out3 = """create\\(\\["f3.out"\\], \\["<.*.Custom instance at """
96     #" <- unconfuses emacs syntax highlighting
97
98     test.fail_test(string.find(test.stdout(), out1) == -1)
99     test.fail_test(string.find(test.stdout(), out2) == -1)
100     test.fail_test(string.find(test.stdout(), out7) == -1)
101     test.fail_test(string.find(test.stdout(), out8) == -1)
102     test.fail_test(re.search(out3, test.stdout()) == None)
103
104     test.must_match('f1.out', "/usr/local")
105     test.must_match('f2.out', "10")
106     test.must_match('f3.out', "C=/usr/local")
107     test.must_match('f4.out', '10')
108     test.must_match('f5.out', "C=/usr/local")
109
110     test.up_to_date(arguments='.')
111
112     test.run(arguments='prefix=/usr')
113     out4 = """create(["f1.out"], ["'/usr'"])"""
114     out5 = """create(["f2.out"], ["4"])"""
115     out6 = """create\\(\\["f3.out"\\], \\["<.*.Custom instance at """
116     #" <- unconfuses emacs syntax highlighting
117     test.fail_test(string.find(test.stdout(), out4) == -1)
118     test.fail_test(string.find(test.stdout(), out5) == -1)
119     test.fail_test(re.search(out6, test.stdout()) == None)
120
121     test.must_match('f1.out', "/usr")
122     test.must_match('f2.out', "4")
123     test.must_match('f3.out', "C=/usr")
124     test.must_match('f4.out', '4')
125
126     test.up_to_date('prefix=/usr', '.')
127
128     test.unlink('f3.out')
129
130     test.run(arguments='prefix=/var')
131     out4 = """create(["f1.out"], ["'/var'"])"""
132
133     test.fail_test(string.find(test.stdout(), out4) == -1)
134     test.fail_test(string.find(test.stdout(), out5) != -1)
135     test.fail_test(string.find(test.stdout(), out7) == -1)
136     test.fail_test(string.find(test.stdout(), out8) == -1)
137     test.fail_test(re.search(out6, test.stdout()) == None)
138
139     test.up_to_date('prefix=/var', '.')
140
141     test.must_match('f1.out', "/var")
142     test.must_match('f2.out', "4")
143     test.must_match('f3.out', "C=/var")
144     test.must_match('f4.out', "4")
145     test.must_match('f5.out', "C=/var")
146
147 test.pass_test()