Modernize stat usage
[scons.git] / test / Actions / pre-post.py
blob0d9f36fce936e25256c5b9336b5196cd99836ab5
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 This test exercises the AddPreAction() and AddPostAction() API
28 functions, which add pre-build and post-build actions to nodes.
29 """
31 import os
33 import TestSCons
35 _exe = TestSCons._exe
36 _python_ = TestSCons._python_
38 test = TestSCons.TestSCons()
40 test.dir_fixture('pre-post-fixture')
42 test.write(['work1', 'SConstruct'], """
43 import os.path
44 import stat
46 DefaultEnvironment(tools=[])
47 env = Environment(XXX='bar%(_exe)s')
49 def before(env, target, source):
50 a=str(target[0])
51 with open(a, "wb") as f:
52 f.write(b"Foo\\n")
53 os.chmod(a, os.stat(a).st_mode | stat.S_IXUSR)
54 with open("before.txt", "ab") as f:
55 f.write((os.path.splitext(str(target[0]))[0] + "\\n").encode())
57 def after(env, target, source):
58 t = str(target[0])
59 a = "after_" + t
60 with open(t, "rb") as fin, open(a, "wb") as fout:
61 fout.write(fin.read())
62 os.chmod(a, os.stat(a).st_mode | stat.S_IXUSR)
64 foo = env.Program(source='foo.c', target='foo')
65 AddPreAction(foo, before)
66 AddPostAction('foo%(_exe)s', after)
68 bar = env.Program(source='bar.c', target='bar')
69 env.AddPreAction('$XXX', before)
70 env.AddPostAction('$XXX', after)
71 """ % locals())
73 test.run(chdir='work1', arguments='.')
75 test.run(program=test.workpath('work1', 'foo'+ _exe), stdout="foo.c\n")
76 test.run(program=test.workpath('work1', 'bar'+ _exe), stdout="bar.c\n")
78 test.must_match(['work1', 'before.txt'], "bar\nfoo\n")
80 after_foo_exe = test.workpath('work1', 'after_foo' + _exe)
81 test.run(program=after_foo_exe, stdout="foo.c\n")
83 after_bar_exe = test.workpath('work1', 'after_bar' + _exe)
84 test.run(program=after_bar_exe, stdout="bar.c\n")
86 # work2 start
87 test.run(chdir='work2', arguments = '.')
89 test.must_match(['work2', 'file1.out'], "111\n")
90 test.must_match(['work2', 'file2.out'], "222\n")
91 test.must_match(['work2', 'file3.out'], "333\n")
93 # work3 start
94 test.run(chdir = 'work3', arguments = 'dir/file', stdout=test.wrap_stdout("""\
95 pre(["dir"], [])
96 post(["dir"], [])
97 build(["%s"], [])
98 """ % os.path.join('dir', 'file')))
100 test.must_match(['work3', 'dir', 'file'], "build()\n")
102 # work4 start
103 test.write(['work4', 'SConstruct'], """\
104 DefaultEnvironment(tools=[])
106 def pre_action(target, source, env):
107 with open(str(target[0]), 'ab') as f:
108 f.write(('pre %%s\\n' %% source[0]).encode())
110 def post_action(target, source, env):
111 with open(str(target[0]), 'ab') as f:
112 f.write(('post %%s\\n' %% source[0]).encode())
114 env = Environment(tools=[])
115 o = env.Command(
116 ['pre-post', 'file.out'],
117 'file.in',
118 r'%(_python_)s build.py ${TARGETS[1]} $SOURCE'
120 env.AddPreAction(o, pre_action)
121 env.AddPostAction(o, post_action)
122 """ % locals())
124 test.run(chdir='work4', arguments='.')
126 test.must_match(['work4', 'file.out'], "file.in\n")
127 test.must_match(['work4', 'pre-post'], "pre file.in\npost file.in\n")
129 test.pass_test()
131 # Local Variables:
132 # tab-width:4
133 # indent-tabs-mode:nil
134 # End:
135 # vim: set expandtab tabstop=4 shiftwidth=4: