Merge pull request #4674 from bdbaddog/fix_2281_Aliases_ignore_pre_post_add_actions
[scons.git] / test / VariantDir / errors.py
blob7400056e7ee3c38b31d7d626a5bedaf660f22126
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 Validate successful handling of errors when duplicating things in
28 VariantDirs. This is generally when the VariantDir, or something in it,
29 is read-only.
30 """
32 import os
33 import stat
34 import sys
35 import TestSCons
36 from TestCmd import IS_ROOT
38 test = TestSCons.TestSCons()
40 if IS_ROOT:
41 test.skip_test('SConscript permissions meaningless when running as root; skipping test.\n')
43 for dir in ['normal', 'ro-dir', 'ro-SConscript', 'ro-src']:
44 test.subdir(dir, [dir, 'src'])
46 test.write([dir, 'SConstruct'], """\
47 import os.path
48 VariantDir('build', 'src')
49 SConscript(os.path.join('build', 'SConscript'))
50 """)
52 test.write([dir, 'src', 'SConscript'], """\
53 def fake_scan(node, env, target):
54 # We fetch the contents here, even though we don't examine
55 # them, because get_contents() will cause the engine to
56 # try to link the source file into the build directory,
57 # potentially triggering a different failure case.
58 contents = node.get_contents()
59 return []
61 def cat(env, source, target):
62 target = str(target[0])
63 with open(target, "w") as f:
64 for src in source:
65 with open(str(src), "r") as f2:
66 f.write(f2.read())
68 DefaultEnvironment(tools=[]) # test speedup
69 env = Environment(BUILDERS={'Build':Builder(action=cat)},
70 SCANNERS=[Scanner(fake_scan, skeys = ['.in'])])
72 # Do some Node test operations to ensure no side-effects cause failures
73 File('file.in').exists()
74 File('file.in').is_derived()
76 env.Build('file.out', 'file.in')
77 """)
79 test.write([dir, 'src', 'file.in'], dir + "/src/file.in\n")
81 # Just verify that the normal case works fine.
82 test.run(chdir = 'normal', arguments = ".")
84 test.must_match(['normal', 'build', 'file.out'], "normal/src/file.in\n", mode='r')
86 # Verify the error when the VariantDir itself is read-only. Don't bother
87 # to test this on Windows, because the ACL (I think) still allows the
88 # owner to create files in the directory even when it's read-only.
89 if sys.platform != 'win32':
90 dir = os.path.join('ro-dir', 'build')
91 test.subdir(dir)
92 os.chmod(dir, os.stat(dir).st_mode & ~stat.S_IWUSR)
94 test.run(chdir = 'ro-dir',
95 arguments = ".",
96 status = 2,
97 stderr = "scons: *** Cannot duplicate `%s' in `build': Permission denied. Stop.\n" % os.path.join('src', 'SConscript'))
99 # Verify the error when the SConscript file within the VariantDir is
100 # read-only. Note that we have to make the directory read-only too,
101 # because otherwise our duplication logic will be able to unlink
102 # the read-only SConscript and duplicate the new one.
103 dir = os.path.join('ro-SConscript', 'build')
104 test.subdir(dir)
105 SConscript = test.workpath(dir, 'SConscript')
106 test.write(SConscript, '')
107 os.chmod(SConscript, os.stat(SConscript).st_mode & ~stat.S_IWUSR)
108 with open(SConscript, 'r'):
109 os.chmod(dir, os.stat(dir).st_mode & ~stat.S_IWUSR)
111 test.run(chdir = 'ro-SConscript',
112 arguments = ".",
113 status = 2,
114 stderr = "scons: *** Cannot duplicate `%s' in `build': Permission denied. Stop.\n" % os.path.join('src', 'SConscript'))
116 os.chmod('ro-SConscript', os.stat('ro-SConscript').st_mode | stat.S_IWUSR)
118 test.run(chdir = 'ro-SConscript',
119 arguments = ".",
120 status = 2,
121 stderr = "scons: *** Cannot duplicate `%s' in `build': Permission denied. Stop.\n" % os.path.join('src', 'SConscript'))
123 # Verify the error when the source file within the VariantDir is
124 # read-only. Note that we have to make the directory read-only too,
125 # because otherwise our duplication logic will be able to unlink the
126 # read-only source file and duplicate the new one. But because we've
127 # made the VariantDir read-only, we must also create a writable SConscript
128 # file there so it can be duplicated from the source directory.
129 dir = os.path.join('ro-src', 'build')
130 test.subdir(dir)
131 test.write([dir, 'SConscript'], '')
132 file_in = test.workpath(dir, 'file.in')
133 test.write(file_in, '')
134 os.chmod(file_in, os.stat(file_in).st_mode & ~stat.S_IWUSR)
135 with open(file_in, 'r'):
136 os.chmod(dir, os.stat(dir).st_mode & ~stat.S_IWUSR)
138 test.run(chdir = 'ro-src',
139 arguments = ".",
140 status = 2,
141 stderr = """\
142 scons: *** Cannot duplicate `%s' in `build': Permission denied. Stop.
143 """ % (os.path.join('src', 'file.in')))
145 test.run(chdir = 'ro-src',
146 arguments = "-k .",
147 status = 2,
148 stderr = """\
149 scons: *** Cannot duplicate `%s' in `build': Permission denied. Stop.
150 """ % (os.path.join('src', 'file.in')))
152 # ensure that specifying multiple source directories for one
153 # build directory results in an error message, rather
154 # than just silently failing.
155 test.subdir('duplicate', ['duplicate', 'src1'], ['duplicate', 'src2'])
157 duplicate_SConstruct_path = test.workpath('duplicate', 'SConstruct')
159 test.write(duplicate_SConstruct_path, """\
160 VariantDir('build', 'src1')
161 VariantDir('build', 'src2')
162 """)
164 expect_stderr = """
165 scons: *** 'build' already has a source directory: 'src1'.
166 """ + test.python_file_line(duplicate_SConstruct_path, 2)
168 test.run(chdir = 'duplicate',
169 arguments = ".",
170 status = 2,
171 stderr = expect_stderr)
173 test.pass_test()
175 # Local Variables:
176 # tab-width:4
177 # indent-tabs-mode:nil
178 # End:
179 # vim: set expandtab tabstop=4 shiftwidth=4: