Skip to content

Commit 8778209

Browse files
authored
Refine venv test to use Activate.ps1 and check mode (gh-145417)
Update the mtime test case based on feedback
1 parent 1428e75 commit 8778209

1 file changed

Lines changed: 26 additions & 16 deletions

File tree

Lib/test/test_venv.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -378,31 +378,41 @@ def test_install_scripts_mtime(self):
378378
Test that install_scripts does not preserve mtime when copying scripts.
379379
Using mtime serves as a proxy to verify that shutil.copy2 (and thus
380380
SELinux bin_t contexts) is not being used during script installation.
381+
See gh-145417.
381382
"""
382383
import time
383384

384-
builder = venv.EnvBuilder()
385-
builder.create(self.env_dir)
386-
context = builder.ensure_directories(self.env_dir)
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+
if abs(time.time() - src_mtime) < 1.0:
389+
time.sleep(1.1)
390+
391+
rmtree(self.env_dir)
392+
venv.create(self.env_dir)
387393

388-
with tempfile.TemporaryDirectory() as script_dir:
389-
common_dir = os.path.join(script_dir, 'common')
390-
os.mkdir(common_dir)
391-
script_path = os.path.join(common_dir, 'test_script.sh')
394+
dst_path = os.path.join(self.env_dir, self.bindir, 'Activate.ps1')
395+
self.assertTrue(os.path.exists(dst_path), "Activate.ps1 not found in venv")
396+
dst_mtime = os.path.getmtime(dst_path)
392397

393-
with open(script_path, 'wb') as f:
394-
f.write(b'echo Hello')
398+
# shutil.copy should update mtime, whereas shutil.copy2 would preserve it
399+
self.assertNotEqual(src_mtime, dst_mtime,
400+
"mtime was preserved, meaning shutil.copy2 was used")
395401

396-
past_time = time.time() - 10_000_000
397-
os.utime(script_path, (past_time, past_time))
402+
# Permissions and content should still match
403+
src_stat = os.stat(src_path)
404+
dst_stat = os.stat(dst_path)
405+
self.assertEqual(src_stat.st_mode, dst_stat.st_mode, "File modes do not match")
398406

399-
builder.install_scripts(context, script_dir)
407+
with open(src_path, 'rb') as f:
408+
src_data = f.read()
409+
with open(dst_path, 'rb') as f:
410+
dst_data = f.read()
411+
self.assertEqual(src_data, dst_data, "File contents do not match")
400412

401-
dst_path = os.path.join(context.bin_path, 'test_script.sh')
402-
self.assertTrue(os.path.exists(dst_path))
413+
self.assertNotIn(b'__VENV_PYTHON__', src_data,
414+
"Test assumes Activate.ps1 is a static file, not a template")
403415

404-
new_mtime = os.path.getmtime(dst_path)
405-
self.assertGreater(new_mtime, past_time + 1000)
406416

407417
def test_overwrite_existing(self):
408418
"""

0 commit comments

Comments
 (0)