summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam Deegan <bill@baddogconsulting.com>2019-03-02 19:57:21 -0500
committerGitHub <noreply@github.com>2019-03-02 19:57:21 -0500
commit87445ef897104a15f42be27cf9773d9fd11dca96 (patch)
tree7a63df3d1dc460cd54f190f1910b88264fd85448
parent9b44764fc8f96a1036d12e22c7b7359369f04d8d (diff)
parente7e70407bd54811604f8e907819e36a6025f7915 (diff)
downloadscons-git-87445ef897104a15f42be27cf9773d9fd11dca96.tar.gz
Merge pull request #3320 from mwichmann/test-file-closings
[PYPY] [PY 3.8] more context manager use in tests
-rw-r--r--test/Actions/pre-post.py16
-rw-r--r--test/SideEffect/basic.py7
-rw-r--r--test/SideEffect/variant_dir.py7
-rw-r--r--test/VariantDir/errors.py45
4 files changed, 37 insertions, 38 deletions
diff --git a/test/Actions/pre-post.py b/test/Actions/pre-post.py
index a5acbfbc3..cd0bfb4a4 100644
--- a/test/Actions/pre-post.py
+++ b/test/Actions/pre-post.py
@@ -47,13 +47,11 @@ env = Environment(XXX='bar%(_exe)s')
def before(env, target, source):
a=str(target[0])
- f=open(a, "wb")
- f.write(b"Foo\\n")
- f.close()
+ with open(a, "wb") as f:
+ f.write(b"Foo\\n")
os.chmod(a, os.stat(a)[stat.ST_MODE] | stat.S_IXUSR)
- f=open("before.txt", "ab")
- f.write((os.path.splitext(str(target[0]))[0] + "\\n").encode())
- f.close()
+ with open("before.txt", "ab") as f:
+ f.write((os.path.splitext(str(target[0]))[0] + "\\n").encode())
def after(env, target, source):
t = str(target[0])
@@ -106,9 +104,11 @@ test.must_match(['work3', 'dir', 'file'], "build()\n")
# work4 start
test.write(['work4', 'SConstruct'], """\
def pre_action(target, source, env):
- open(str(target[0]), 'ab').write(('pre %%s\\n' %% source[0]).encode())
+ with open(str(target[0]), 'ab') as f:
+ f.write(('pre %%s\\n' %% source[0]).encode())
def post_action(target, source, env):
- open(str(target[0]), 'ab').write(('post %%s\\n' %% source[0]).encode())
+ with open(str(target[0]), 'ab') as f:
+ f.write(('post %%s\\n' %% source[0]).encode())
env = Environment()
o = env.Command(['pre-post', 'file.out'],
'file.in',
diff --git a/test/SideEffect/basic.py b/test/SideEffect/basic.py
index af6c264bd..b5b6381e2 100644
--- a/test/SideEffect/basic.py
+++ b/test/SideEffect/basic.py
@@ -37,13 +37,14 @@ test = TestSCons.TestSCons()
test.write('SConstruct', """\
def copy(source, target):
- open(target, "wb").write(open(source, "rb").read())
+ with open(target, "wb") as f, open(source, "rb") as f2:
+ f.write(f2.read())
def build(env, source, target):
copy(str(source[0]), str(target[0]))
if target[0].side_effects:
- side_effect = open(str(target[0].side_effects[0]), "ab")
- side_effect.write(('%%s -> %%s\\n'%%(str(source[0]), str(target[0]))).encode())
+ with open(str(target[0].side_effects[0]), "ab") as side_effect:
+ side_effect.write(('%%s -> %%s\\n'%%(str(source[0]), str(target[0]))).encode())
Build = Builder(action=build)
env = Environment(BUILDERS={'Build':Build}, SUBDIR='subdir')
diff --git a/test/SideEffect/variant_dir.py b/test/SideEffect/variant_dir.py
index 711e4266c..0e9ae532a 100644
--- a/test/SideEffect/variant_dir.py
+++ b/test/SideEffect/variant_dir.py
@@ -38,13 +38,14 @@ test = TestSCons.TestSCons()
test.write('SConstruct',
"""
def copy(source, target):
- open(target, "wb").write(open(source, "rb").read())
+ with open(target, "wb") as f, open(source, "rb") as f2:
+ f.write(f2.read())
def build(env, source, target):
copy(str(source[0]), str(target[0]))
if target[0].side_effects:
- side_effect = open(str(target[0].side_effects[0]), "ab")
- side_effect.write(('%s -> %s\\n'%(str(source[0]), str(target[0]))).encode())
+ with open(str(target[0].side_effects[0]), "ab") as side_effect:
+ side_effect.write(('%s -> %s\\n'%(str(source[0]), str(target[0]))).encode())
Build = Builder(action=build)
env = Environment(BUILDERS={'Build':Build})
diff --git a/test/VariantDir/errors.py b/test/VariantDir/errors.py
index c74d10372..26ef4a231 100644
--- a/test/VariantDir/errors.py
+++ b/test/VariantDir/errors.py
@@ -57,10 +57,10 @@ def fake_scan(node, env, target):
def cat(env, source, target):
target = str(target[0])
- f = open(target, "w")
- for src in source:
- f.write(open(str(src), "r").read())
- f.close()
+ with open(target, "w") as f:
+ for src in source:
+ with open(str(src), "r") as f2:
+ f.write(f2.read())
env = Environment(BUILDERS={'Build':Builder(action=cat)},
SCANNERS=[Scanner(fake_scan, skeys = ['.in'])])
@@ -101,16 +101,15 @@ test.subdir(dir)
SConscript = test.workpath(dir, 'SConscript')
test.write(SConscript, '')
os.chmod(SConscript, os.stat(SConscript)[stat.ST_MODE] & ~stat.S_IWUSR)
-f = open(SConscript, 'r')
-os.chmod(dir, os.stat(dir)[stat.ST_MODE] & ~stat.S_IWUSR)
+with open(SConscript, 'r'):
+ os.chmod(dir, os.stat(dir)[stat.ST_MODE] & ~stat.S_IWUSR)
-test.run(chdir = 'ro-SConscript',
- arguments = ".",
- status = 2,
- stderr = "scons: *** Cannot duplicate `%s' in `build': Permission denied. Stop.\n" % os.path.join('src', 'SConscript'))
+ test.run(chdir = 'ro-SConscript',
+ arguments = ".",
+ status = 2,
+ stderr = "scons: *** Cannot duplicate `%s' in `build': Permission denied. Stop.\n" % os.path.join('src', 'SConscript'))
-os.chmod('ro-SConscript', os.stat('ro-SConscript')[stat.ST_MODE] | stat.S_IWUSR)
-f.close()
+ os.chmod('ro-SConscript', os.stat('ro-SConscript')[stat.ST_MODE] | stat.S_IWUSR)
test.run(chdir = 'ro-SConscript',
arguments = ".",
@@ -129,25 +128,23 @@ test.write([dir, 'SConscript'], '')
file_in = test.workpath(dir, 'file.in')
test.write(file_in, '')
os.chmod(file_in, os.stat(file_in)[stat.ST_MODE] & ~stat.S_IWUSR)
-f = open(file_in, 'r')
-os.chmod(dir, os.stat(dir)[stat.ST_MODE] & ~stat.S_IWUSR)
+with open(file_in, 'r'):
+ os.chmod(dir, os.stat(dir)[stat.ST_MODE] & ~stat.S_IWUSR)
-test.run(chdir = 'ro-src',
- arguments = ".",
- status = 2,
- stderr = """\
+ test.run(chdir = 'ro-src',
+ arguments = ".",
+ status = 2,
+ stderr = """\
scons: *** Cannot duplicate `%s' in `build': Permission denied. Stop.
""" % (os.path.join('src', 'file.in')))
-test.run(chdir = 'ro-src',
- arguments = "-k .",
- status = 2,
- stderr = """\
+ test.run(chdir = 'ro-src',
+ arguments = "-k .",
+ status = 2,
+ stderr = """\
scons: *** Cannot duplicate `%s' in `build': Permission denied. Stop.
""" % (os.path.join('src', 'file.in')))
-f.close()
-
# ensure that specifying multiple source directories for one
# build directory results in an error message, rather
# than just silently failing.