Skip to content

Commit 4e127d8

Browse files
authored
Refactor test for SELinux acc to suggestion
1 parent 97652be commit 4e127d8

File tree

1 file changed

+7
-43
lines changed

1 file changed

+7
-43
lines changed

Lib/test/test_venv.py

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -374,50 +374,14 @@ def create_contents(self, paths, filename):
374374
with open(fn, 'wb') as f:
375375
f.write(b'Still here?')
376376

377-
def test_install_scripts_mtime(self):
378-
"""
379-
Test that install_scripts does not preserve mtime when copying scripts.
380-
Using mtime serves as a proxy to verify that shutil.copy2/copystat
381-
is not used during script installation,
382-
incorrectly copying e.g. SELinux bin_t context.
383-
See gh-145417.
384-
"""
385-
venv_dir = os.path.dirname(venv.__file__)
386-
src_path = os.path.join(venv_dir, 'scripts', 'common', 'Activate.ps1')
387-
src_mtime = os.path.getmtime(src_path)
388-
389-
# Ensure a temporal difference between src and dst creation
390-
if abs(time.time() - src_mtime) < 1.0:
391-
time.sleep(1.1)
392-
393-
rmtree(self.env_dir)
377+
def test_install_scripts_selinux(self):
378+
"""
379+
gh-145417: Test that install_scripts does not copy SELinux context when
380+
copying scripts.
381+
"""
382+
with patch('os.listxattr') as listxattr_mock:
394383
venv.create(self.env_dir)
395-
396-
dst_path = os.path.join(self.env_dir, self.bindir, 'Activate.ps1')
397-
self.assertTrue(os.path.exists(dst_path), "Activate.ps1 not found in venv")
398-
dst_mtime = os.path.getmtime(dst_path)
399-
400-
# shutil.copy should update mtime, whereas shutil.copy2 would preserve it
401-
self.assertNotEqual(src_mtime, dst_mtime,
402-
"mtime was preserved, meaning shutil.copy2 was used")
403-
404-
# Permissions and content should still match
405-
src_stat = os.stat(src_path)
406-
dst_stat = os.stat(dst_path)
407-
self.assertEqual(src_stat.st_mode, dst_stat.st_mode, "File modes do not match")
408-
409-
with open(src_path, 'rb') as f:
410-
src_data = f.read()
411-
412-
# Protection against the file becoming a template in the future
413-
self.assertNotIn(b'__VENV_PYTHON__', src_data,
414-
"Test assumes Activate.ps1 is a static file, not a template")
415-
416-
with open(dst_path, 'rb') as f:
417-
dst_data = f.read()
418-
419-
self.assertEqual(src_data, dst_data, "File contents do not match")
420-
384+
listxattr_mock.assert_not_called()
421385

422386
def test_overwrite_existing(self):
423387
"""

0 commit comments

Comments
 (0)