[Python-checkins] bpo-31904: Fix test_netrc for VxWorks RTOS (GH-21675)

vstinner webhook-mailer at python.org
Tue Dec 1 15:35:04 EST 2020


https://github.com/python/cpython/commit/e483d281bd55be0beee3e62d18e0719175bde671
commit: e483d281bd55be0beee3e62d18e0719175bde671
branch: master
author: pxinwr <peixing.xin at windriver.com>
committer: vstinner <vstinner at python.org>
date: 2020-12-01T21:34:42+01:00
summary:

bpo-31904: Fix test_netrc for VxWorks RTOS (GH-21675)

Fix test_netrc on VxWorks: create temporary directories using temp_cwd().

files:
A Misc/NEWS.d/next/Tests/2020-12-01-15-51-19.bpo-31904.iwetj4.rst
M Lib/test/test_netrc.py

diff --git a/Lib/test/test_netrc.py b/Lib/test/test_netrc.py
index 2bd46aa745ff2..90ef5cd363b3f 100644
--- a/Lib/test/test_netrc.py
+++ b/Lib/test/test_netrc.py
@@ -109,62 +109,56 @@ def test_comment_at_end_of_machine_line_pass_has_hash(self):
     def test_security(self):
         # This test is incomplete since we are normally not run as root and
         # therefore can't test the file ownership being wrong.
-        d = os_helper.TESTFN
-        os.mkdir(d)
-        self.addCleanup(os_helper.rmtree, d)
-        fn = os.path.join(d, '.netrc')
-        with open(fn, 'wt') as f:
-            f.write("""\
-                machine foo.domain.com login bar password pass
-                default login foo password pass
-                """)
-        with os_helper.EnvironmentVarGuard() as environ:
-            environ.set('HOME', d)
-            os.chmod(fn, 0o600)
-            nrc = netrc.netrc()
-            self.assertEqual(nrc.hosts['foo.domain.com'],
-                             ('bar', None, 'pass'))
-            os.chmod(fn, 0o622)
-            self.assertRaises(netrc.NetrcParseError, netrc.netrc)
+        with os_helper.temp_cwd(None) as d:
+            fn = os.path.join(d, '.netrc')
+            with open(fn, 'wt') as f:
+                f.write("""\
+                    machine foo.domain.com login bar password pass
+                    default login foo password pass
+                    """)
+            with os_helper.EnvironmentVarGuard() as environ:
+                environ.set('HOME', d)
+                os.chmod(fn, 0o600)
+                nrc = netrc.netrc()
+                self.assertEqual(nrc.hosts['foo.domain.com'],
+                                 ('bar', None, 'pass'))
+                os.chmod(fn, 0o622)
+                self.assertRaises(netrc.NetrcParseError, netrc.netrc)
 
     def test_file_not_found_in_home(self):
-        d = os_helper.TESTFN
-        os.mkdir(d)
-        self.addCleanup(os_helper.rmtree, d)
-        with os_helper.EnvironmentVarGuard() as environ:
-            environ.set('HOME', d)
-            self.assertRaises(FileNotFoundError, netrc.netrc)
+        with os_helper.temp_cwd(None) as d:
+            with os_helper.EnvironmentVarGuard() as environ:
+                environ.set('HOME', d)
+                self.assertRaises(FileNotFoundError, netrc.netrc)
 
     def test_file_not_found_explicit(self):
         self.assertRaises(FileNotFoundError, netrc.netrc,
                           file='unlikely_netrc')
 
     def test_home_not_set(self):
-        fake_home = os_helper.TESTFN
-        os.mkdir(fake_home)
-        self.addCleanup(os_helper.rmtree, fake_home)
-        fake_netrc_path = os.path.join(fake_home, '.netrc')
-        with open(fake_netrc_path, 'w') as f:
-            f.write('machine foo.domain.com login bar password pass')
-        os.chmod(fake_netrc_path, 0o600)
-
-        orig_expanduser = os.path.expanduser
-        called = []
-
-        def fake_expanduser(s):
-            called.append(s)
-            with os_helper.EnvironmentVarGuard() as environ:
-                environ.set('HOME', fake_home)
-                environ.set('USERPROFILE', fake_home)
-                result = orig_expanduser(s)
-                return result
-
-        with support.swap_attr(os.path, 'expanduser', fake_expanduser):
-            nrc = netrc.netrc()
-            login, account, password = nrc.authenticators('foo.domain.com')
-            self.assertEqual(login, 'bar')
-
-        self.assertTrue(called)
+        with os_helper.temp_cwd(None) as fake_home:
+            fake_netrc_path = os.path.join(fake_home, '.netrc')
+            with open(fake_netrc_path, 'w') as f:
+                f.write('machine foo.domain.com login bar password pass')
+            os.chmod(fake_netrc_path, 0o600)
+
+            orig_expanduser = os.path.expanduser
+            called = []
+
+            def fake_expanduser(s):
+                called.append(s)
+                with os_helper.EnvironmentVarGuard() as environ:
+                    environ.set('HOME', fake_home)
+                    environ.set('USERPROFILE', fake_home)
+                    result = orig_expanduser(s)
+                    return result
+
+            with support.swap_attr(os.path, 'expanduser', fake_expanduser):
+                nrc = netrc.netrc()
+                login, account, password = nrc.authenticators('foo.domain.com')
+                self.assertEqual(login, 'bar')
+
+            self.assertTrue(called)
 
 
 if __name__ == "__main__":
diff --git a/Misc/NEWS.d/next/Tests/2020-12-01-15-51-19.bpo-31904.iwetj4.rst b/Misc/NEWS.d/next/Tests/2020-12-01-15-51-19.bpo-31904.iwetj4.rst
new file mode 100644
index 0000000000000..49e9892e9ed7c
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2020-12-01-15-51-19.bpo-31904.iwetj4.rst
@@ -0,0 +1 @@
+Fix test_netrc on VxWorks: create temporary directories using temp_cwd().



More information about the Python-checkins mailing list