python system_error ai_generated true

PermissionError:[Errno 13] 权限被拒绝:'/tmp/pytest-of-root/pytest-0/test_x0/out.txt'

PermissionError: [Errno 13] Permission denied: '/tmp/pytest-of-root/pytest-0/test_x0/out.txt'

ID: python/pytest-tmp-path-permissions

其他格式: JSON · Markdown 中文 · English
80%修复率
85%置信度
0证据数
2024-11-04首次发现

版本兼容性

版本状态引入弃用备注
7.x active — — —
8.x active — — —

根因分析

在一个环境中以 root 运行的测试留下了由 root 拥有的 /tmp/pytest-of-root;随后以其他用户运行无法写入同一个 tmp_path 基础目录。

English

Tests run as root in one environment left /tmp/pytest-of-root owned by root; subsequent runs as a different user cannot write into the same tmp_path base.

generic

解决方案

  1. 95% 成功率
    # pytest.ini
    [pytest]
    addopts = --basetemp=/home/ci/pytest-tmp
    # ensure /home/ci/pytest-tmp exists and is writable
  2. 90% 成功率
    sudo rm -rf /tmp/pytest-of-root
    pytest tests/

无效尝试

常见但无效的做法:

  1. 70% 失败

    Security risk and the next root run recreates restrictive perms.

  2. 60% 失败

    May still collide if /var/tmp/pytest-of-root exists from a root run.

  3. 75% 失败

    Loses pytest cleanup guarantees and can leave garbage.