minimize tool initialization in tests
[scons.git] / test / Install / Install.py
blob2857c729013a3c4c59d8da26665cd96cd286a0e1
1 #!/usr/bin/env python
3 # MIT Licenxe
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 Install() Builder works
28 """
30 import os.path
31 import time
33 import TestSCons
35 test = TestSCons.TestSCons()
37 test.subdir('outside', 'work', ['work', 'sub'])
39 f1_out = test.workpath('work', 'export', 'f1.out')
40 f2_out = test.workpath('work', 'export', 'f2.out')
41 f3_out = test.workpath('work', 'export', 'f3.out')
42 f4_out = test.workpath('work', 'export', 'f4.out')
43 f5_txt = test.workpath('outside', 'f5.txt')
44 f6_txt = test.workpath('outside', 'f6.txt')
45 f6_sep = f6_txt.replace(os.sep, '/')
47 _SUBDIR_f4_out = os.path.join('$SUBDIR', 'f4.out')
49 test.write(['work', 'SConstruct'], """\
50 DefaultEnvironment(tools=[])
51 def cat(env, source, target):
52 target = str(target[0])
53 with open(target, 'wb') as ofp:
54 for src in source:
55 with open(str(src), 'rb') as ifp:
56 ofp.write(ifp.read())
58 def my_install(dest, source, env):
59 import shutil
60 shutil.copy2(source, dest)
61 with open('my_install.out', 'a') as f:
62 f.write(dest)
64 env1 = Environment(tools=[])
65 env1.Append(BUILDERS={'Cat':Builder(action=cat)})
66 env3 = env1.Clone(INSTALL = my_install)
68 t = env1.Cat(target='f1.out', source='f1.in')
69 env1.Install(dir='export', source=t)
70 t = env1.Cat(target='f2.out', source='f2.in')
71 Install(dir='export', source=t)
73 t = env3.Cat(target='f3.out', source='f3.in')
74 env3.Install(dir='export', source=t)
76 env4 = env1.Clone(EXPORT='export', SUBDIR='sub')
77 t = env4.Cat(target='sub/f4.out', source='sub/f4.in')
78 env4.Install(dir='$EXPORT', source=r'%(_SUBDIR_f4_out)s')
80 env1.Install('.', r'%(f5_txt)s')
81 env1.Install('export', r'%(f5_txt)s')
82 env1.Install('.', r'%(f6_sep)s')
83 env1.Install('export', r'%(f6_sep)s')
85 # test passing a keyword arg (not used, but should be accepted)
86 env7 = env1.Clone(EXPORT='export')
87 env7.Install(dir='$EXPORT', source='./f1.in', FOO="bar")
89 """ % locals())
91 test.write(['work', 'f1.in'], "f1.in\n")
92 test.write(['work', 'f2.in'], "f2.in\n")
93 test.write(['work', 'f3.in'], "f3.in\n")
94 test.write(['work', 'sub', 'f4.in'], "sub/f4.in\n")
95 test.write(f5_txt, "f5.txt\n")
96 test.write(f6_txt, "f6.txt\n")
98 test.run(chdir = 'work', arguments = '.')
100 test.must_match(f1_out, "f1.in\n", mode='r')
101 test.must_match(f2_out, "f2.in\n", mode='r')
102 test.must_match(f3_out, "f3.in\n", mode='r')
103 test.must_match(f4_out, "sub/f4.in\n", mode='r')
104 test.must_match(['work', 'f5.txt'], "f5.txt\n", mode='r')
105 test.must_match(['work', 'export', 'f5.txt'], "f5.txt\n", mode='r')
106 test.must_match(['work', 'f6.txt'], "f6.txt\n", mode='r')
107 test.must_match(['work', 'export', 'f6.txt'], "f6.txt\n", mode='r')
109 test.must_match(['work', 'my_install.out'], os.path.join('export', 'f3.out'), mode='r')
110 test.must_match(['work', 'export', 'f1.in'], "f1.in\n", mode='r')
112 # make sure the programs didn't get rebuilt, because nothing changed:
113 oldtime1 = os.path.getmtime(f1_out)
114 oldtime2 = os.path.getmtime(f2_out)
116 test.write(['work', 'f1.in'], "f1.in again\n")
118 time.sleep(2) # introduce a small delay, to make the test valid
120 test.run(chdir = 'work', arguments = '.')
122 test.fail_test(oldtime1 == os.path.getmtime(f1_out))
123 test.fail_test(oldtime2 != os.path.getmtime(f2_out))
125 # Verify that we didn't link to the Installed file.
126 with open(f2_out, 'w') as f:
127 f.write("xyzzy\n")
128 test.must_match(['work', 'f2.out'], "f2.in\n", mode='r')
130 # Verify that scons prints an error message
131 # if a target can not be unlinked before building it:
132 test.write(['work', 'f1.in'], "f1.in again again\n")
134 os.chmod(test.workpath('work', 'export'), 0o555)
135 with open(f1_out, 'rb'):
136 expect = [
137 "Permission denied",
138 "The process cannot access the file because it is being used by another process",
139 "Der Prozess kann nicht auf die Datei zugreifen, da sie von einem anderen Prozess verwendet wird",
142 test.run(chdir='work', arguments=f1_out, stderr=None, status=2)
144 test.must_contain_any_line(test.stderr(), expect)
146 test.pass_test()
148 # Local Variables:
149 # tab-width:4
150 # indent-tabs-mode:nil
151 # End:
152 # vim: set expandtab tabstop=4 shiftwidth=4: