Update regression tests to match changes in runtest.py
[scons.git] / test / textfile.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 TestSCons
28
29 import os
30 import string
31
32 test = TestSCons.TestSCons()
33
34 foo1  = test.workpath('foo1.txt')
35 #foo2  = test.workpath('foo2.txt')
36 #foo1a = test.workpath('foo1a.txt')
37 #foo2a = test.workpath('foo2a.txt')
38
39 test.write('SConstruct', """
40 env = Environment(tools=['textfile'])
41 data0 = ['Goethe', 'Schiller']
42 data  = ['lalala', 42, data0, 'tanteratei']
43
44 env.Textfile('foo1', data)
45 env.Textfile('foo2', data, LINESEPARATOR='|*')
46 env.Textfile('foo1a.txt', data + [''])
47 env.Textfile('foo2a.txt', data + [''], LINESEPARATOR='|*')
48
49 # recreate the list with the data wrapped in Value()
50 data0 = map(Value, data0)
51 data = map(Value, data)
52 data[2] = data0
53
54 env.Substfile('bar1', data)
55 env.Substfile('bar2', data, LINESEPARATOR='|*')
56 data.append(Value(''))
57 env.Substfile('bar1a.txt', data)
58 env.Substfile('bar2a.txt', data, LINESEPARATOR='|*')
59 """)
60
61 test.run(arguments = '.')
62
63 textparts = ['lalala', '42',
64              'Goethe', 'Schiller',
65              'tanteratei']
66 foo1Text  = string.join(textparts, os.linesep)
67 foo2Text  = string.join(textparts, '|*')
68 foo1aText = foo1Text + os.linesep
69 foo2aText = foo2Text + '|*'
70
71 test.up_to_date(arguments = '.')
72
73 files = map(test.workpath, (
74             'foo1.txt', 'foo2.txt', 'foo1a.txt', 'foo2a.txt',
75             'bar1',     'bar2',     'bar1a.txt', 'bar2a.txt',
76         ))
77 def check_times():
78     # make sure the files didn't get rewritten, because nothing changed:
79     before = map(os.path.getmtime, files)
80     # introduce a small delay, to make the test valid
81     test.sleep()
82     # should still be up-to-date
83     test.up_to_date(arguments = '.')
84     after = map(os.path.getmtime, files)
85     test.fail_test(before != after)
86
87 # make sure that the file content is as expected
88 test.must_match('foo1.txt',  foo1Text)
89 test.must_match('bar1',      foo1Text)
90 test.must_match('foo2.txt',  foo2Text)
91 test.must_match('bar2',      foo2Text)
92 test.must_match('foo1a.txt', foo1aText)
93 test.must_match('bar1a.txt', foo1aText)
94 test.must_match('foo2a.txt', foo2aText)
95 test.must_match('bar2a.txt', foo2aText)
96 check_times()
97
98 # write the contents and make sure the files
99 # didn't get rewritten, because nothing changed:
100 test.write('foo1.txt',  foo1Text)
101 test.write('bar1',      foo1Text)
102 test.write('foo2.txt',  foo2Text)
103 test.write('bar2',      foo2Text)
104 test.write('foo1a.txt', foo1aText)
105 test.write('bar1a.txt', foo1aText)
106 test.write('foo2a.txt', foo2aText)
107 test.write('bar2a.txt', foo2aText)
108 check_times()
109
110 test.write('SConstruct', """
111 textlist = ['This line has no substitutions',
112             'This line has @subst@ substitutions',
113             'This line has %subst% substitutions',
114            ]
115
116 sub1 = { '@subst@' : 'most' }
117 sub2 = { '%subst%' : 'many' }
118 sub3 = { '@subst@' : 'most' , '%subst%' : 'many' }
119
120 env = Environment(tools = ['textfile'])
121
122 t = env.Textfile('text', textlist)
123 # no substitutions
124 s = env.Substfile('sub1', t)
125 # one substitution
126 s = env.Substfile('sub2', s, SUBST_DICT = sub1)
127 # the other substution
128 s = env.Substfile('sub3', s, SUBST_DICT = sub2)
129 # the reverse direction
130 s = env.Substfile('sub4', t, SUBST_DICT = sub2)
131 s = env.Substfile('sub5', s, SUBST_DICT = sub1)
132 # both
133 s = env.Substfile('sub6', t, SUBST_DICT = sub3)
134 """)
135
136 test.run(arguments = '.')
137
138 line1  = 'This line has no substitutions'
139 line2a = 'This line has @subst@ substitutions'
140 line2b = 'This line has most substitutions'
141 line3a = 'This line has %subst% substitutions'
142 line3b = 'This line has many substitutions'
143
144 def matchem(file, lines):
145     lines = string.join(lines, '\n')
146     test.must_match(file, lines)
147
148 matchem('text.txt', [line1, line2a, line3a])
149 matchem('sub1', [line1, line2a, line3a])
150 matchem('sub2', [line1, line2b, line3a])
151 matchem('sub3', [line1, line2b, line3b])
152 matchem('sub4', [line1, line2a, line3b])
153 matchem('sub5', [line1, line2b, line3b])
154 matchem('sub6', [line1, line2b, line3b])
155
156 test.up_to_date(arguments = '.')
157
158 test.pass_test()