14965: Cleans up test output by reducing resource warnings
authorEric Biagiotti <ebiagiotti@veritasgenetics.com>
Fri, 12 Jul 2019 17:35:11 +0000 (13:35 -0400)
committerEric Biagiotti <ebiagiotti@veritasgenetics.com>
Fri, 12 Jul 2019 17:35:11 +0000 (13:35 -0400)
Arvados-DCO-1.1-Signed-off-by:  <ebiagiotti@veritasgenetics.com>

sdk/python/tests/run_test_server.py
services/fuse/tests/test_command_args.py
services/fuse/tests/test_exec.py
services/fuse/tests/test_tmp_collection.py

index 786d5a26caf59f2ddb45f03e014cf936232bf2fa..510ffbeca98d1ef72e875fdc9103b20c20dd1e5f 100644 (file)
@@ -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
 
index 9f6555de33278efb1b83648826a17f527a85bc66..581f2462468c4197bec042826a58995826b07870 100644 (file)
@@ -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):
index 2111d535143307eac2b77359c8fc9cc9ac1658f9..35196033130261bb07bf2b595e97aa7f5025097f 100644 (file)
@@ -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')
index a70df8822524f66e449051092c9b06f3d6c6b325..f85cc2b384b973fd96788839df2e3b4d525e7336 100644 (file)
@@ -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):