[Python-checkins] bpo-37400: Fix test_os.test_chown() (GH-14374) (GH-14378)

Victor Stinner webhook-mailer at python.org
Tue Jun 25 16:25:13 EDT 2019


https://github.com/python/cpython/commit/1d4b6ba19466aba0eb91c4ba01ba509acf18c723
commit: 1d4b6ba19466aba0eb91c4ba01ba509acf18c723
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: Victor Stinner <vstinner at redhat.com>
date: 2019-06-25T22:25:01+02:00
summary:

bpo-37400: Fix test_os.test_chown() (GH-14374) (GH-14378)

Use os.getgroups() rather than grp.getgrall() to get groups.
Rename also the test to test_chown_gid().
(cherry picked from commit d7c87d982d4ec4ba201bcee14324ae5e0e90581f)

Co-authored-by: Victor Stinner <vstinner at redhat.com>

files:
A Misc/NEWS.d/next/Tests/2019-06-25-16-02-43.bpo-37400.cx_EWv.rst
M Lib/test/test_os.py

diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
index a9170504004d..8032da053067 100644
--- a/Lib/test/test_os.py
+++ b/Lib/test/test_os.py
@@ -41,15 +41,6 @@
     import _winapi
 except ImportError:
     _winapi = None
-try:
-    import grp
-    groups = [g.gr_gid for g in grp.getgrall() if getpass.getuser() in g.gr_mem]
-    if hasattr(os, 'getgid'):
-        process_gid = os.getgid()
-        if process_gid not in groups:
-            groups.append(process_gid)
-except ImportError:
-    groups = []
 try:
     import pwd
     all_users = [u.pw_uid for u in pwd.getpwall()]
@@ -1238,13 +1229,19 @@ def test_chown_uid_gid_arguments_must_be_index(self):
         self.assertIsNone(os.chown(support.TESTFN, uid, gid))
         self.assertIsNone(os.chown(support.TESTFN, -1, -1))
 
-    @unittest.skipUnless(len(groups) > 1, "test needs more than one group")
-    def test_chown(self):
+    @unittest.skipUnless(hasattr(os, 'getgroups'), 'need os.getgroups')
+    def test_chown_gid(self):
+        groups = os.getgroups()
+        if len(groups) < 2:
+            self.skipTest("test needs at least 2 groups")
+
         gid_1, gid_2 = groups[:2]
         uid = os.stat(support.TESTFN).st_uid
+
         os.chown(support.TESTFN, uid, gid_1)
         gid = os.stat(support.TESTFN).st_gid
         self.assertEqual(gid, gid_1)
+
         os.chown(support.TESTFN, uid, gid_2)
         gid = os.stat(support.TESTFN).st_gid
         self.assertEqual(gid, gid_2)
diff --git a/Misc/NEWS.d/next/Tests/2019-06-25-16-02-43.bpo-37400.cx_EWv.rst b/Misc/NEWS.d/next/Tests/2019-06-25-16-02-43.bpo-37400.cx_EWv.rst
new file mode 100644
index 000000000000..737c78189009
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2019-06-25-16-02-43.bpo-37400.cx_EWv.rst
@@ -0,0 +1,2 @@
+Fix test_os.test_chown(): use os.getgroups() rather than grp.getgrall()
+to get groups. Rename also the test to test_chown_gid().



More information about the Python-checkins mailing list