Update items for 0.97.0d20071212.
[scons.git] / test / Chmod.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 """
28 Verify that the Chmod() Action works.
29 """
30
31 import os
32 import os.path
33 import stat
34
35 import TestSCons
36
37 test = TestSCons.TestSCons()
38
39 # Note:  Windows basically has two modes that it can os.chmod() files to
40 # 0444 and 0666, and directories to 0555 and 0777, so we can only really
41 # oscillate between those values.
42 test.write('SConstruct', """
43 Execute(Chmod('f1', 0666))
44 Execute(Chmod('d2', 0777))
45 def cat(env, source, target):
46     target = str(target[0])
47     source = map(str, source)
48     f = open(target, "wb")
49     for src in source:
50         f.write(open(src, "rb").read())
51     f.close()
52 Cat = Action(cat)
53 env = Environment()
54 env.Command('bar.out', 'bar.in', [Cat,
55                                   Chmod("f3", 0666),
56                                   Chmod("d4", 0777)])
57 env = Environment(FILE = 'f5')
58 env.Command('f6.out', 'f6.in', [Chmod('$FILE', 0666), Cat])
59 env.Command('f7.out', 'f7.in', [Cat,
60                                 Chmod('Chmod-$SOURCE', 0666),
61                                 Chmod('${TARGET}-Chmod', 0666)])
62 """)
63
64 test.write('f1', "f1\n")
65 test.subdir('d2')
66 test.write(['d2', 'file'], "d2/file\n")
67 test.write('bar.in', "bar.in\n")
68 test.write('f3', "f3\n")
69 test.subdir('d4')
70 test.write(['d4', 'file'], "d4/file\n")
71 test.write('f5', "f5\n")
72 test.write('f6.in', "f6.in\n")
73 test.write('f7.in', "f7.in\n")
74 test.write('Chmod-f7.in', "Chmod-f7.in\n")
75 test.write('f7.out-Chmod', "f7.out-Chmod\n")
76
77 os.chmod(test.workpath('f1'), 0444)
78 os.chmod(test.workpath('d2'), 0555)
79 os.chmod(test.workpath('f3'), 0444)
80 os.chmod(test.workpath('d4'), 0555)
81 os.chmod(test.workpath('f5'), 0444)
82 os.chmod(test.workpath('Chmod-f7.in'), 0444)
83 os.chmod(test.workpath('f7.out-Chmod'), 0444)
84
85 expect = test.wrap_stdout(read_str = 'Chmod("f1", 0666)\nChmod("d2", 0777)\n',
86                           build_str = """\
87 cat(["bar.out"], ["bar.in"])
88 Chmod("f3", 0666)
89 Chmod("d4", 0777)
90 Chmod("f5", 0666)
91 cat(["f6.out"], ["f6.in"])
92 cat(["f7.out"], ["f7.in"])
93 Chmod("Chmod-f7.in", 0666)
94 Chmod("f7.out-Chmod", 0666)
95 """)
96 test.run(options = '-n', arguments = '.', stdout = expect)
97
98 s = stat.S_IMODE(os.stat(test.workpath('f1'))[stat.ST_MODE])
99 test.fail_test(s != 0444)
100 s = stat.S_IMODE(os.stat(test.workpath('d2'))[stat.ST_MODE])
101 test.fail_test(s != 0555)
102 test.must_not_exist('bar.out')
103 s = stat.S_IMODE(os.stat(test.workpath('f3'))[stat.ST_MODE])
104 test.fail_test(s != 0444)
105 s = stat.S_IMODE(os.stat(test.workpath('d4'))[stat.ST_MODE])
106 test.fail_test(s != 0555)
107 s = stat.S_IMODE(os.stat(test.workpath('f5'))[stat.ST_MODE])
108 test.fail_test(s != 0444)
109 test.must_not_exist('f6.out')
110 test.must_not_exist('f7.out')
111 s = stat.S_IMODE(os.stat(test.workpath('Chmod-f7.in'))[stat.ST_MODE])
112 test.fail_test(s != 0444)
113 s = stat.S_IMODE(os.stat(test.workpath('f7.out-Chmod'))[stat.ST_MODE])
114 test.fail_test(s != 0444)
115
116 test.run()
117
118 s = stat.S_IMODE(os.stat(test.workpath('f1'))[stat.ST_MODE])
119 test.fail_test(s != 0666)
120 s = stat.S_IMODE(os.stat(test.workpath('d2'))[stat.ST_MODE])
121 test.fail_test(s != 0777)
122 test.must_match('bar.out', "bar.in\n")
123 s = stat.S_IMODE(os.stat(test.workpath('f3'))[stat.ST_MODE])
124 test.fail_test(s != 0666)
125 s = stat.S_IMODE(os.stat(test.workpath('d4'))[stat.ST_MODE])
126 test.fail_test(s != 0777)
127 s = stat.S_IMODE(os.stat(test.workpath('f5'))[stat.ST_MODE])
128 test.fail_test(s != 0666)
129 test.must_match('f6.out', "f6.in\n")
130 test.must_match('f7.out', "f7.in\n")
131 s = stat.S_IMODE(os.stat(test.workpath('Chmod-f7.in'))[stat.ST_MODE])
132 test.fail_test(s != 0666)
133 s = stat.S_IMODE(os.stat(test.workpath('f7.out-Chmod'))[stat.ST_MODE])
134 test.fail_test(s != 0666)
135
136 test.pass_test()