Add Python Value Nodes. (Chad Austin)
[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 test = TestSCons.TestSCons(match=TestCmd.match_re)
35
36 test.write('SConstruct', """
37 class Custom:
38     def __init__(self, value):  self.value = value
39     def __str__(self):          return "C=" + str(self.value)
40
41 P = ARGUMENTS.get('prefix', '/usr/local')
42 L = len(P)
43 C = Custom(P)
44
45 def create(target, source, env):
46     open(str(target[0]), 'wb').write(source[0].get_contents())
47
48 env = Environment()
49 env['BUILDERS']['B'] = Builder(action = create)
50 env.B('f1.out', Value(P))
51 env.B('f2.out', Value(L))
52 env.B('f3.out', Value(C))
53 """)
54
55
56 test.run()
57 out1 = """create("f1.out", "'/usr/local'")"""
58 out2 = """create("f2.out", "10")"""
59 out3 = """create\\("f3.out", "<.*.Custom instance at """
60 test.fail_test(string.find(test.stdout(), out1) == -1)
61 test.fail_test(string.find(test.stdout(), out2) == -1)
62 test.fail_test(re.search(out3, test.stdout()) == None)
63
64 test.fail_test(not os.path.exists(test.workpath('f1.out')))
65 test.fail_test(open(test.workpath('f1.out'), 'rb').read() != '/usr/local')
66 test.fail_test(not os.path.exists(test.workpath('f2.out')))
67 test.fail_test(open(test.workpath('f2.out'), 'rb').read() != '10')
68 test.fail_test(not os.path.exists(test.workpath('f3.out')))
69 test.fail_test(open(test.workpath('f3.out'), 'rb').read() != 'C=/usr/local')
70
71 test.up_to_date(arguments = ".")
72
73 test.run(arguments = 'prefix=/usr')
74 out4 = """create("f1.out", "'/usr'")"""
75 out5 = """create("f2.out", "4")"""
76 out6 = """create\\("f3.out", "<.*.Custom instance at """
77 test.fail_test(string.find(test.stdout(), out4) == -1)
78 test.fail_test(string.find(test.stdout(), out5) == -1)
79 test.fail_test(re.search(out6, test.stdout()) == None)
80
81 test.fail_test(not os.path.exists(test.workpath('f1.out')))
82 test.fail_test(open(test.workpath('f1.out'), 'rb').read() != '/usr')
83 test.fail_test(not os.path.exists(test.workpath('f2.out')))
84 test.fail_test(open(test.workpath('f2.out'), 'rb').read() != '4')
85 test.fail_test(not os.path.exists(test.workpath('f3.out')))
86 test.fail_test(open(test.workpath('f3.out'), 'rb').read() != 'C=/usr')
87
88 test.unlink('f3.out')
89
90 test.run(arguments = 'prefix=/var')
91 out4 = """create("f1.out", "'/var'")"""
92 test.fail_test(string.find(test.stdout(), out4) == -1)
93 test.fail_test(string.find(test.stdout(), out5) != -1)
94 test.fail_test(re.search(out6, test.stdout()) == None)
95
96 test.fail_test(not os.path.exists(test.workpath('f1.out')))
97 test.fail_test(open(test.workpath('f1.out'), 'rb').read() != '/var')
98 test.fail_test(not os.path.exists(test.workpath('f2.out')))
99 test.fail_test(open(test.workpath('f2.out'), 'rb').read() != '4')
100 test.fail_test(not os.path.exists(test.workpath('f3.out')))
101 test.fail_test(open(test.workpath('f3.out'), 'rb').read() != 'C=/var')
102
103 test.pass_test()