Issue 2168: Mac OS X fixes for SWIG tests. (Greg Noel)
[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(('f1-File'), 0666))
45 Execute(Chmod('d2', 0777))
46 Execute(Chmod(Dir('d2-Dir'), 0777))
47 def cat(env, source, target):
48     target = str(target[0])
49     source = map(str, source)
50     f = open(target, "wb")
51     for src in source:
52         f.write(open(src, "rb").read())
53     f.close()
54 Cat = Action(cat)
55 env = Environment()
56 env.Command('bar.out', 'bar.in', [Cat,
57                                   Chmod("f3", 0666),
58                                   Chmod("d4", 0777)])
59 env = Environment(FILE = 'f5')
60 env.Command('f6.out', 'f6.in', [Chmod('$FILE', 0666), Cat])
61 env.Command('f7.out', 'f7.in', [Cat,
62                                 Chmod('Chmod-$SOURCE', 0666),
63                                 Chmod('${TARGET}-Chmod', 0666)])
64
65 # Make sure Chmod works with a list of arguments
66 env = Environment(FILE = 'f9')
67 env.Command('f8.out', 'f8.in', [Chmod(['$FILE', File('f10')], 0666), Cat])
68 Execute(Chmod(['d11', Dir('d12')], 0777))
69 """)
70
71 test.write('f1', "f1\n")
72 test.write('f1-File', "f1-File\n")
73 test.subdir('d2')
74 test.write(['d2', 'file'], "d2/file\n")
75 test.subdir('d2-Dir')
76 test.write(['d2-Dir', 'file'], "d2-Dir/file\n")
77 test.write('bar.in', "bar.in\n")
78 test.write('f3', "f3\n")
79 test.subdir('d4')
80 test.write(['d4', 'file'], "d4/file\n")
81 test.write('f5', "f5\n")
82 test.write('f6.in', "f6.in\n")
83 test.write('f7.in', "f7.in\n")
84 test.write('Chmod-f7.in', "Chmod-f7.in\n")
85 test.write('f7.out-Chmod', "f7.out-Chmod\n")
86 test.write('f8.in', "f8.in\n")
87 test.write('f9', "f9\n")
88 test.write('f10', "f10\n")
89 test.subdir('d11')
90 test.subdir('d12')
91
92 os.chmod(test.workpath('f1'), 0444)
93 os.chmod(test.workpath('f1-File'), 0444)
94 os.chmod(test.workpath('d2'), 0555)
95 os.chmod(test.workpath('d2-Dir'), 0555)
96 os.chmod(test.workpath('f3'), 0444)
97 os.chmod(test.workpath('d4'), 0555)
98 os.chmod(test.workpath('f5'), 0444)
99 os.chmod(test.workpath('Chmod-f7.in'), 0444)
100 os.chmod(test.workpath('f7.out-Chmod'), 0444)
101 os.chmod(test.workpath('f9'), 0444)
102 os.chmod(test.workpath('f10'), 0444)
103 os.chmod(test.workpath('d11'), 0555)
104 os.chmod(test.workpath('d12'), 0555)
105
106 expect = test.wrap_stdout(read_str = """\
107 Chmod("f1", 0666)
108 Chmod("f1-File", 0666)
109 Chmod("d2", 0777)
110 Chmod("d2-Dir", 0777)
111 Chmod(["d11", "d12"], 0777)
112 """,
113                           build_str = """\
114 cat(["bar.out"], ["bar.in"])
115 Chmod("f3", 0666)
116 Chmod("d4", 0777)
117 Chmod("f5", 0666)
118 cat(["f6.out"], ["f6.in"])
119 cat(["f7.out"], ["f7.in"])
120 Chmod("Chmod-f7.in", 0666)
121 Chmod("f7.out-Chmod", 0666)
122 Chmod(["f9", "f10"], 0666)
123 cat(["f8.out"], ["f8.in"])
124 """)
125 test.run(options = '-n', arguments = '.', stdout = expect)
126
127 s = stat.S_IMODE(os.stat(test.workpath('f1'))[stat.ST_MODE])
128 test.fail_test(s != 0444)
129 s = stat.S_IMODE(os.stat(test.workpath('f1-File'))[stat.ST_MODE])
130 test.fail_test(s != 0444)
131 s = stat.S_IMODE(os.stat(test.workpath('d2'))[stat.ST_MODE])
132 test.fail_test(s != 0555)
133 s = stat.S_IMODE(os.stat(test.workpath('d2-Dir'))[stat.ST_MODE])
134 test.fail_test(s != 0555)
135 test.must_not_exist('bar.out')
136 s = stat.S_IMODE(os.stat(test.workpath('f3'))[stat.ST_MODE])
137 test.fail_test(s != 0444)
138 s = stat.S_IMODE(os.stat(test.workpath('d4'))[stat.ST_MODE])
139 test.fail_test(s != 0555)
140 s = stat.S_IMODE(os.stat(test.workpath('f5'))[stat.ST_MODE])
141 test.fail_test(s != 0444)
142 test.must_not_exist('f6.out')
143 test.must_not_exist('f7.out')
144 s = stat.S_IMODE(os.stat(test.workpath('Chmod-f7.in'))[stat.ST_MODE])
145 test.fail_test(s != 0444)
146 s = stat.S_IMODE(os.stat(test.workpath('f7.out-Chmod'))[stat.ST_MODE])
147 test.fail_test(s != 0444)
148 test.must_not_exist('f8.out')
149 s = stat.S_IMODE(os.stat(test.workpath('f9'))[stat.ST_MODE])
150 test.fail_test(s != 0444)
151 s = stat.S_IMODE(os.stat(test.workpath('f10'))[stat.ST_MODE])
152 test.fail_test(s != 0444)
153 s = stat.S_IMODE(os.stat(test.workpath('d11'))[stat.ST_MODE])
154 test.fail_test(s != 0555)
155 s = stat.S_IMODE(os.stat(test.workpath('d12'))[stat.ST_MODE])
156 test.fail_test(s != 0555)
157
158 test.run()
159
160 s = stat.S_IMODE(os.stat(test.workpath('f1'))[stat.ST_MODE])
161 test.fail_test(s != 0666)
162 s = stat.S_IMODE(os.stat(test.workpath('f1-File'))[stat.ST_MODE])
163 test.fail_test(s != 0666)
164 s = stat.S_IMODE(os.stat(test.workpath('d2'))[stat.ST_MODE])
165 test.fail_test(s != 0777)
166 s = stat.S_IMODE(os.stat(test.workpath('d2-Dir'))[stat.ST_MODE])
167 test.fail_test(s != 0777)
168 test.must_match('bar.out', "bar.in\n")
169 s = stat.S_IMODE(os.stat(test.workpath('f3'))[stat.ST_MODE])
170 test.fail_test(s != 0666)
171 s = stat.S_IMODE(os.stat(test.workpath('d4'))[stat.ST_MODE])
172 test.fail_test(s != 0777)
173 s = stat.S_IMODE(os.stat(test.workpath('f5'))[stat.ST_MODE])
174 test.fail_test(s != 0666)
175 test.must_match('f6.out', "f6.in\n")
176 test.must_match('f7.out', "f7.in\n")
177 s = stat.S_IMODE(os.stat(test.workpath('Chmod-f7.in'))[stat.ST_MODE])
178 test.fail_test(s != 0666)
179 s = stat.S_IMODE(os.stat(test.workpath('f7.out-Chmod'))[stat.ST_MODE])
180 test.fail_test(s != 0666)
181 test.must_match('f8.out', "f8.in\n")
182 s = stat.S_IMODE(os.stat(test.workpath('f9'))[stat.ST_MODE])
183 test.fail_test(s != 0666)
184 s = stat.S_IMODE(os.stat(test.workpath('f10'))[stat.ST_MODE])
185 test.fail_test(s != 0666)
186 s = stat.S_IMODE(os.stat(test.workpath('d11'))[stat.ST_MODE])
187 test.fail_test(s != 0777)
188 s = stat.S_IMODE(os.stat(test.workpath('d12'))[stat.ST_MODE])
189 test.fail_test(s != 0777)
190
191 test.pass_test()