Updates and slight refactor in fix
[scons.git] / test / Copy-Action.py
blob1b1356e30e3e71f209e33703b6be1ed568b255d7
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 """
27 Verify that the Copy() Action works, and preserves file modification
28 times and modes.
29 """
31 import os
32 import stat
33 import sys
35 import TestSCons
37 test = TestSCons.TestSCons()
39 test.write('SConstruct', """\
40 Execute(Copy('f1.out', 'f1.in'))
41 Execute(Copy(File('d2.out'), 'd2.in'))
42 Execute(Copy('d3.out', File('f3.in')))
43 # Issue #3009: make sure it's not mangled if src is a list.
44 # make sure both list-of-str and list-of-Node work
45 Execute(Copy('d7.out', ['f10.in', 'f11.in']))
46 Execute(Copy('d7.out', Glob('f?.in')))
48 def cat(env, source, target):
49 target = str(target[0])
50 with open(target, "w") as f:
51 for src in source:
52 with open(str(src), "r") as ifp:
53 f.write(ifp.read())
55 Cat = Action(cat)
56 env = Environment()
57 env.Command(
58 'bar.out',
59 'bar.in',
60 [Cat, Copy("f4.out", "f4.in"), Copy("d5.out", "d5.in"), Copy("d6.out", "f6.in")],
62 env = Environment(OUTPUT='f7.out', INPUT='f7.in')
63 env.Command('f8.out', 'f8.in', [Copy('$OUTPUT', '$INPUT'), Cat])
64 env.Command('f9.out', 'f9.in', [Cat, Copy('${TARGET}-Copy', '$SOURCE')])
66 env.CopyTo('d4', 'f10.in')
67 env.CopyAs('d4/f11.out', 'f11.in')
68 env.CopyAs('d4/f12.out', 'd5/f12.in')
70 env.Command('f 13.out', 'f 13.in', Copy('$TARGET', '$SOURCE'))
71 """)
73 test.write('f1.in', "f1.in\n")
74 test.subdir('d2.in')
75 test.write(['d2.in', 'file'], "d2.in/file\n")
76 test.write('f3.in', "f3.in\n")
77 test.subdir('d3.out')
78 test.subdir('d7.out')
79 test.write('bar.in', "bar.in\n")
80 test.write('f4.in', "f4.in\n")
81 test.subdir('d5.in')
82 test.write(['d5.in', 'file'], "d5.in/file\n")
83 test.write('f6.in', "f6.in\n")
84 test.subdir('d6.out')
85 test.write('f7.in', "f7.in\n")
86 test.write('f8.in', "f8.in\n")
87 test.write('f9.in', "f9.in\n")
88 test.write('f10.in', "f10.in\n")
89 test.write('f11.in', "f11.in\n")
90 test.subdir('d5')
91 test.write(['d5', 'f12.in'], "f12.in\n")
92 test.write('f 13.in', "f 13.in\n")
94 os.chmod('f1.in', 0o646)
95 os.chmod('f4.in', 0o644)
97 test.sleep() # delay for timestamps
99 d4_f10_in = os.path.join('d4', 'f10.in')
100 d4_f11_out = os.path.join('d4', 'f11.out')
101 d4_f12_out = os.path.join('d4', 'f12.out')
102 d5_f12_in = os.path.join('d5', 'f12.in')
104 expect = test.wrap_stdout(
105 read_str="""\
106 Copy("f1.out", "f1.in")
107 Copy("d2.out", "d2.in")
108 Copy("d3.out", "f3.in")
109 Copy("d7.out", ["f10.in", "f11.in"])
110 Copy("d7.out", ["f1.in", "f3.in", "f4.in", "f6.in", "f7.in", "f8.in", "f9.in"])
111 """,
112 build_str="""\
113 cat(["bar.out"], ["bar.in"])
114 Copy("f4.out", "f4.in")
115 Copy("d5.out", "d5.in")
116 Copy("d6.out", "f6.in")
117 Copy file(s): "f10.in" to "%(d4_f10_in)s"
118 Copy file(s): "f11.in" to "%(d4_f11_out)s"
119 Copy file(s): "%(d5_f12_in)s" to "%(d4_f12_out)s"
120 Copy("f 13.out", "f 13.in")
121 Copy("f7.out", "f7.in")
122 cat(["f8.out"], ["f8.in"])
123 cat(["f9.out"], ["f9.in"])
124 Copy("f9.out-Copy", "f9.in")
125 """ % locals(),
128 test.run(options='-n', arguments='.', stdout=expect)
130 test.must_not_exist('f1.out')
131 test.must_not_exist('d2.out')
132 test.must_not_exist(os.path.join('d3.out', 'f3.in'))
133 test.must_not_exist(os.path.join('d7.out', 'f7.in'))
134 test.must_not_exist(os.path.join('d7.out', 'f11.in'))
135 test.must_not_exist('f4.out')
136 test.must_not_exist('d5.out')
137 test.must_not_exist(os.path.join('d6.out', 'f6.in'))
138 test.must_not_exist('f7.out')
139 test.must_not_exist('f8.out')
140 test.must_not_exist('f9.out')
141 test.must_not_exist('f9.out-Copy')
142 test.must_not_exist('d4/f10.in')
143 test.must_not_exist('d4/f11.out')
144 test.must_not_exist('d4/f12.out')
145 test.must_not_exist('f 13.out')
146 test.must_not_exist('f 13.out')
148 test.run()
150 test.must_match('f1.out', "f1.in\n", mode='r')
151 test.must_match(['d2.out', 'file'], "d2.in/file\n", mode='r')
152 test.must_match(['d3.out', 'f3.in'], "f3.in\n", mode='r')
153 test.must_match(['d7.out', 'f7.in'], "f7.in\n", mode='r')
154 test.must_match(['d7.out', 'f11.in'], "f11.in\n", mode='r')
155 test.must_match('f4.out', "f4.in\n", mode='r')
156 test.must_match(['d5.out', 'file'], "d5.in/file\n", mode='r')
157 test.must_match(['d6.out', 'f6.in'], "f6.in\n", mode='r')
158 test.must_match('f7.out', "f7.in\n", mode='r')
159 test.must_match('f8.out', "f8.in\n", mode='r')
160 test.must_match('f9.out', "f9.in\n", mode='r')
161 test.must_match('f9.out-Copy', "f9.in\n", mode='r')
162 test.must_match('d4/f10.in', 'f10.in\n', mode='r')
163 test.must_match('d4/f11.out', 'f11.in\n', mode='r')
164 test.must_match('d4/f12.out', 'f12.in\n', mode='r')
165 test.must_match('f 13.out', 'f 13.in\n', mode='r')
167 errors = 0
169 def must_be_same(f1, f2):
170 global errors
171 if isinstance(f1, list):
172 f1 = os.path.join(*f1)
173 if isinstance(f2, list):
174 f2 = os.path.join(*f2)
175 s1 = os.stat(f1)
176 s2 = os.stat(f2)
177 for value in ['ST_MODE', 'ST_MTIME']:
178 v = getattr(stat, value)
179 if s1[v] != s2[v]:
180 msg = f"{f1!r}[{value}] {s1[v]} != {f2!r}[{value}] {s2[v]}\n"
181 sys.stderr.write(msg)
182 errors += 1
184 must_be_same('f1.out', 'f1.in')
185 must_be_same(['d2.out', 'file'], ['d2.in', 'file'])
186 must_be_same(['d3.out', 'f3.in'], 'f3.in')
187 must_be_same('f4.out', 'f4.in')
188 must_be_same(['d5.out', 'file'], ['d5.in', 'file'])
189 must_be_same(['d6.out', 'f6.in'], 'f6.in')
190 must_be_same('f7.out', 'f7.in')
191 must_be_same(['d4', 'f10.in'], 'f10.in')
192 must_be_same(['d4', 'f11.out'], 'f11.in')
193 must_be_same(['d4', 'f12.out'], ['d5', 'f12.in'])
194 must_be_same('f 13.out', 'f 13.in')
196 if errors:
197 test.fail_test()
199 test.pass_test()
201 # Local Variables:
202 # tab-width:4
203 # indent-tabs-mode:nil
204 # End:
205 # vim: set expandtab tabstop=4 shiftwidth=4: