From: Mukund Sivaraman Date: Thu, 14 Jun 2012 05:52:45 +0000 (+0530) Subject: [1828] Make code safer by using with X-Git-Tag: trac2351_base~226^2~42^2~1^2~20 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1ece2eadd41752ca89bc9c6932942b9c39dc91ac;p=thirdparty%2Fkea.git [1828] Make code safer by using with --- diff --git a/src/bin/bindctl/tests/bindctl_test.py b/src/bin/bindctl/tests/bindctl_test.py index 6252ca9365..bcfb6c594d 100644 --- a/src/bin/bindctl/tests/bindctl_test.py +++ b/src/bin/bindctl/tests/bindctl_test.py @@ -425,6 +425,12 @@ class FakeBindCmdInterpreter(bindcmd.BindCmdInterpreter): class TestBindCmdInterpreter(unittest.TestCase): + def setUp(self): + self.old_stdout = sys.stdout + + def tearDown(self): + sys.stdout = self.old_stdout + def _create_invalid_csv_file(self, csvfilename): import csv csvfile = open(csvfilename, 'w') @@ -447,20 +453,17 @@ class TestBindCmdInterpreter(unittest.TestCase): self.assertEqual(new_csv_dir, custom_cmd.csv_file_dir) def test_get_saved_user_info(self): - old_stdout = sys.stdout - sys.stdout = open(os.devnull, 'w') - cmd = bindcmd.BindCmdInterpreter() - users = cmd._get_saved_user_info('/notexist', 'csv_file.csv') - self.assertEqual([], users) - - csvfilename = 'csv_file.csv' - self._create_invalid_csv_file(csvfilename) - users = cmd._get_saved_user_info('./', csvfilename) - self.assertEqual([], users) - os.remove(csvfilename) - sys.stdout.close() - sys.stdout = old_stdout - + with open(os.devnull, 'w') as f: + sys.stdout = f + cmd = bindcmd.BindCmdInterpreter() + users = cmd._get_saved_user_info('/notexist', 'csv_file.csv') + self.assertEqual([], users) + + csvfilename = 'csv_file.csv' + self._create_invalid_csv_file(csvfilename) + users = cmd._get_saved_user_info('./', csvfilename) + self.assertEqual([], users) + os.remove(csvfilename) class TestCommandLineOptions(unittest.TestCase): def setUp(self):