From 7cac2b42ab530880a9ac6bff59909f960ea106e8 Mon Sep 17 00:00:00 2001 From: Eric Biagiotti Date: Fri, 12 Jul 2019 13:35:11 -0400 Subject: [PATCH] 14965: Cleans up test output by reducing resource warnings Arvados-DCO-1.1-Signed-off-by: --- sdk/python/tests/run_test_server.py | 13 ++++++++----- services/fuse/tests/test_command_args.py | 11 ++++++----- services/fuse/tests/test_exec.py | 7 ++++--- services/fuse/tests/test_tmp_collection.py | 5 ++--- 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/sdk/python/tests/run_test_server.py b/sdk/python/tests/run_test_server.py index 786d5a26ca..510ffbeca9 100644 --- a/sdk/python/tests/run_test_server.py +++ b/sdk/python/tests/run_test_server.py @@ -503,9 +503,10 @@ def _start_keep(n, keep_args): for arg, val in keep_args.items(): keep_cmd.append("{}={}".format(arg, val)) - logf = open(_logfilename('keep{}'.format(n)), 'a') - kp0 = subprocess.Popen( - keep_cmd, stdin=open('/dev/null'), stdout=logf, stderr=logf, close_fds=True) + with open(_logfilename('keep{}'.format(n)), 'a') as logf: + with open('/dev/null') as _stdin: + kp0 = subprocess.Popen( + keep_cmd, stdin=_stdin, stdout=logf, stderr=logf, close_fds=True) with open(_pidfile('keep{}'.format(n)), 'w') as f: f.write(str(kp0.pid)) @@ -562,7 +563,8 @@ def run_keep(blob_signing_key=None, enforce_permissions=False, num_servers=2): pidfile = _pidfile('keepproxy') if os.path.exists(pidfile): try: - os.kill(int(open(pidfile).read()), signal.SIGHUP) + with open(pidfile) as pid: + os.kill(int(pid.read()), signal.SIGHUP) except OSError: os.remove(pidfile) @@ -733,7 +735,8 @@ def _setport(program, port): # Returns 9 if program is not up. def _getport(program): try: - return int(open(_portfile(program)).read()) + with open(_portfile(program)) as prog: + return int(prog.read()) except IOError: return 9 diff --git a/services/fuse/tests/test_command_args.py b/services/fuse/tests/test_command_args.py index 9f6555de33..581f246246 100644 --- a/services/fuse/tests/test_command_args.py +++ b/services/fuse/tests/test_command_args.py @@ -34,11 +34,12 @@ def noexit(func): @contextlib.contextmanager def nostderr(): - orig, sys.stderr = sys.stderr, open(os.devnull, 'w') - try: - yield - finally: - sys.stderr = orig + with open(os.devnull, 'w') as dn: + orig, sys.stderr = sys.stderr, dn + try: + yield + finally: + sys.stderr = orig class MountArgsTest(unittest.TestCase): diff --git a/services/fuse/tests/test_exec.py b/services/fuse/tests/test_exec.py index 2111d53514..3519603313 100644 --- a/services/fuse/tests/test_exec.py +++ b/services/fuse/tests/test_exec.py @@ -59,6 +59,7 @@ class ExecMode(unittest.TestCase): quote(os.path.join(self.mnt, 'zzz', 'foo.txt')), quote(os.path.join(self.mnt, 'zzz', '.arvados#collection')), quote(os.path.join(self.okfile)))])) - self.assertRegexpMatches( - json.load(open(self.okfile))['manifest_text'], - r' 0:3:foo.txt\n') + with open(self.okfile) as f: + self.assertRegexpMatches( + json.load(f)['manifest_text'], + r' 0:3:foo.txt\n') diff --git a/services/fuse/tests/test_tmp_collection.py b/services/fuse/tests/test_tmp_collection.py index a70df88225..f85cc2b384 100644 --- a/services/fuse/tests/test_tmp_collection.py +++ b/services/fuse/tests/test_tmp_collection.py @@ -54,9 +54,8 @@ class TmpCollectionArgsTest(unittest.TestCase): def current_manifest(tmpdir): - return json.load(open( - os.path.join(tmpdir, '.arvados#collection') - ))['manifest_text'] + with open(os.path.join(tmpdir, '.arvados#collection')) as tmp: + return json.load(tmp)['manifest_text'] class TmpCollectionTest(IntegrationTest): -- 2.30.2