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