From 467bcd17312214158556909a06298edc4ffeeaf5 Mon Sep 17 00:00:00 2001 From: Craig Rodrigues Date: Sat, 25 Feb 2017 14:31:44 -0800 Subject: [PATCH] Use context-managers to close open file descriptors --- worker/buildbot_worker/test/unit/test_bot.py | 9 ++++++--- .../test/unit/test_commands_fs.py | 18 ++++++++++++------ .../test/unit/test_commands_transfer.py | 14 +++++++++----- .../test/unit/test_commands_utils.py | 12 ++++++++---- .../test/unit/test_runprocess.py | 6 ++++-- 5 files changed, 39 insertions(+), 20 deletions(-) diff --git a/worker/buildbot_worker/test/unit/test_bot.py b/worker/buildbot_worker/test/unit/test_bot.py index 85017eac740..b0014b11f9d 100644 --- a/worker/buildbot_worker/test/unit/test_bot.py +++ b/worker/buildbot_worker/test/unit/test_bot.py @@ -80,9 +80,12 @@ def check(vers): def test_getWorkerInfo(self): infodir = os.path.join(self.basedir, "info") os.makedirs(infodir) - open(os.path.join(infodir, "admin"), "w").write("testy!") - open(os.path.join(infodir, "foo"), "w").write("bar") - open(os.path.join(infodir, "environ"), "w").write("something else") + with open(os.path.join(infodir, "admin"), "w") as f: + f.write("testy!") + with open(os.path.join(infodir, "foo"), "w") as f: + f.write("bar") + with open(os.path.join(infodir, "environ"), "w") as f: + f.write("something else") d = self.bot.callRemote("getWorkerInfo") diff --git a/worker/buildbot_worker/test/unit/test_commands_fs.py b/worker/buildbot_worker/test/unit/test_commands_fs.py index 84a0568b0f7..6fe26e59772 100644 --- a/worker/buildbot_worker/test/unit/test_commands_fs.py +++ b/worker/buildbot_worker/test/unit/test_commands_fs.py @@ -176,7 +176,8 @@ def test_existing_file(self): self.make_command(fs.MakeDirectory, dict( dir='test-file', ), True) - open(os.path.join(self.basedir, 'test-file'), "w") + with open(os.path.join(self.basedir, 'test-file'), "w"): + pass d = self.run_command() def check(_): @@ -228,7 +229,8 @@ def test_file(self): self.make_command(fs.StatFile, dict( file='test-file', ), True) - open(os.path.join(self.basedir, 'test-file'), "w") + with open(os.path.join(self.basedir, 'test-file'), "w"): + pass d = self.run_command() @@ -248,7 +250,8 @@ def test_file_workdir(self): workdir='wd' ), True) os.mkdir(os.path.join(self.basedir, 'wd')) - open(os.path.join(self.basedir, 'wd', 'test-file'), "w") + with open(os.path.join(self.basedir, 'wd', 'test-file'), "w"): + pass d = self.run_command() @@ -304,7 +307,8 @@ def test_file(self): self.make_command(fs.GlobPath, dict( path='t*-file', ), True) - open(os.path.join(self.basedir, 'test-file'), "w") + with open(os.path.join(self.basedir, 'test-file'), "w"): + pass d = self.run_command() @@ -344,8 +348,10 @@ def test_dir(self): dir='workdir', ), True) workdir = os.path.join(self.basedir, 'workdir') - open(os.path.join(workdir, 'file1'), "w") - open(os.path.join(workdir, 'file2'), "w") + with open(os.path.join(workdir, 'file1'), "w"): + pass + with open(os.path.join(workdir, 'file2'), "w"): + pass d = self.run_command() diff --git a/worker/buildbot_worker/test/unit/test_commands_transfer.py b/worker/buildbot_worker/test/unit/test_commands_transfer.py index 177cff464ec..b6c6db0746d 100644 --- a/worker/buildbot_worker/test/unit/test_commands_transfer.py +++ b/worker/buildbot_worker/test/unit/test_commands_transfer.py @@ -122,7 +122,8 @@ def setUp(self): self.datafile = os.path.join(self.datadir, 'data') # note: use of 'wb' here ensures newlines aren't translated on the # upload - open(self.datafile, mode="wb").write(b"this is some data\n" * 10) + with open(self.datafile, mode="wb") as f: + f.write(b"this is some data\n" * 10) def tearDown(self): self.tearDownCommand() @@ -297,9 +298,10 @@ def setUp(self): if os.path.exists(self.datadir): shutil.rmtree(self.datadir) os.makedirs(self.datadir) - open(os.path.join(self.datadir, "aa"), mode="wb").write(b"lots of a" * 100) - open(os.path.join(self.datadir, "bb"), mode="wb").write( - b"and a little b" * 17) + with open(os.path.join(self.datadir, "aa"), mode="wb") as f: + f.write(b"lots of a" * 100) + with open(os.path.join(self.datadir, "bb"), mode="wb") as f: + f.write(b"and a little b" * 17) def tearDown(self): self.tearDownCommand() @@ -505,7 +507,9 @@ def check(_): ]) datafile = os.path.join(self.basedir, 'data') self.assertTrue(os.path.exists(datafile)) - self.assertEqual(open(datafile, mode="rb").read(), test_data[:50]) + with open(datafile, mode="rb") as f: + data = f.read() + self.assertEqual(data, test_data[:50]) d.addCallback(check) return d diff --git a/worker/buildbot_worker/test/unit/test_commands_utils.py b/worker/buildbot_worker/test/unit/test_commands_utils.py index 25d02ff1011..35146bd020e 100644 --- a/worker/buildbot_worker/test/unit/test_commands_utils.py +++ b/worker/buildbot_worker/test/unit/test_commands_utils.py @@ -102,11 +102,14 @@ def setUp(self): # fill it with some files os.mkdir(os.path.join(self.target)) - open(os.path.join(self.target, "a"), "w") + with open(os.path.join(self.target, "a"), "w"): + pass os.mkdir(os.path.join(self.target, "d")) - open(os.path.join(self.target, "d", "a"), "w") + with open(os.path.join(self.target, "d", "a"), "w"): + pass os.mkdir(os.path.join(self.target, "d", "d")) - open(os.path.join(self.target, "d", "d", "a"), "w") + with open(os.path.join(self.target, "d", "d", "a"), "w"): + pass def tearDown(self): try: @@ -127,7 +130,8 @@ def test_rmdirRecursive_symlink(self): if runtime.platformType == 'win32': raise unittest.SkipTest("no symlinks on this platform") os.mkdir("noperms") - open("noperms/x", "w") + with open("noperms/x", "w"): + pass os.chmod("noperms/x", 0) try: os.symlink("../noperms", os.path.join(self.target, "link")) diff --git a/worker/buildbot_worker/test/unit/test_runprocess.py b/worker/buildbot_worker/test/unit/test_runprocess.py index 2928e0269ea..b1fdbc23c92 100644 --- a/worker/buildbot_worker/test/unit/test_runprocess.py +++ b/worker/buildbot_worker/test/unit/test_runprocess.py @@ -512,7 +512,8 @@ def poll(): return if os.path.exists(pidfile): try: - pid = int(open(pidfile).read()) + with open(pidfile) as f: + pid = int(f.read()) except (IOError, TypeError, ValueError): pid = None @@ -814,7 +815,8 @@ def test_statFile_missing(self): def test_statFile_exists(self): rp = self.makeRP() - open('statfile.log', 'w').write('hi') + with open('statfile.log', 'w') as f: + f.write('hi') lf = runprocess.LogFileWatcher(rp, 'test', 'statfile.log', False) st = lf.statFile() self.assertEqual(