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.
27 This test exercises the AddPreAction() and AddPostAction() API
28 functions, which add pre-build and post-build actions to nodes.
36 _python_
= TestSCons
._python
_
38 test
= TestSCons
.TestSCons()
40 test
.dir_fixture('pre-post-fixture')
42 test
.write(['work1', 'SConstruct'], """
46 DefaultEnvironment(tools=[])
47 env = Environment(XXX='bar%(_exe)s')
49 def before(env, target, source):
51 with open(a, "wb") as f:
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):
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)
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")
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")
94 test
.run(chdir
= 'work3', arguments
= 'dir/file', stdout
=test
.wrap_stdout("""\
98 """ % os
.path
.join('dir', 'file')))
100 test
.must_match(['work3', 'dir', 'file'], "build()\n")
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=[])
116 ['pre-post', 'file.out'],
118 r'%(_python_)s build.py ${TARGETS[1]} $SOURCE'
120 env.AddPreAction(o, pre_action)
121 env.AddPostAction(o, post_action)
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")
133 # indent-tabs-mode:nil
135 # vim: set expandtab tabstop=4 shiftwidth=4: