在 macOS 上如何用 Apple container 容器运行 RustPython 的 Linux 测试?
【免费下载链接】RustPythonA Python Interpreter written in Rust项目地址: https://gitcode.com/GitHub_Trending/ru/RustPython
RustPython 是用 Rust 编写的 Python 解释器,开发过程中需要跑它的 Python 标准库测试套件(-m test)和 Rust 单元测试。在 macOS 上想获得一个 Linux 环境来执行这些测试时,不必搭建虚拟机:CONTRIBUTING.md 的 “Testing on Linux from macOS” 小节给出了完整流程——用 Apple 的containerCLI 构建一个 Linux 开发镜像,启动一个常驻容器,然后在容器内执行测试命令。注意文档给出的 setup 命令固定了--arch arm64,且注释明确该配置面向 arm64-only builds(禁用 Rosetta 要求),因此这套命令对应 Apple 芯片的 Mac。
前置条件
- 本地已克隆 RustPython 仓库,后续所有命令都在仓库根目录下执行(该目录包含
Cargo.toml和.devcontainer/Dockerfile); - 系统已安装 Homebrew,用于安装
containerCLI。
开发镜像由 .devcontainer/Dockerfile 构建,其基础镜像是FROM rust:bullseye并安装clang,也就是说容器内的 Rust 工具链来自这个镜像,与宿主机上是否装有 Rust 无关。
一次性设置:安装 CLI 并构建开发镜像
在仓库根目录执行(对应文档 “Setup (one-time)”):
# Install container CLI brew install container # Disable Rosetta requirement for arm64-only builds defaults write com.apple.container.defaults build.rosetta -bool false # Build the development image container build --arch arm64 -t rustpython-dev -f .devcontainer/Dockerfile .三条命令的作用依次是:通过 Homebrew 安装containerCLI;关闭 arm64-only 构建对 Rosetta 的要求;以仓库根目录为构建上下文、按.devcontainer/Dockerfile构建一个名为rustpython-dev的 arm64 开发镜像。这三步只需做一次。
启动常驻测试容器
镜像就绪后,启动一个后台常驻容器用于反复执行命令:
# Start a persistent container in background (8GB memory, 4 CPUs for compilation) container run -d --name rustpython-test -m 8G -c 4 \ --mount type=bind,source=$(pwd),target=/workspace \ -w /workspace rustpython-dev sleep infinity各参数对应文档注释的说明:
-d:后台启动常驻容器;--name rustpython-test:容器名,后续container exec用它定位容器;-m 8G -c 4:按文档注释,分配 8GB 内存、4 个 CPU,用于编译;--mount type=bind,source=$(pwd),target=/workspace:把当前目录(即仓库根目录)绑定挂载到容器内的/workspace,容器里编译出的产物和读取的源码都指向这份本地目录;-w /workspace:把工作目录设为/workspace;rustpython-dev sleep infinity:使用上一步构建的镜像,并以sleep infinity保持容器不退出,以便随时exec进容器执行命令。
在容器内运行测试
通过container exec在容器内执行测试。文档给出的示例是运行单个标准库测试模块(test_ensurepip,对应Lib/test下的同名模块):
# Run tests inside the container container exec rustpython-test sh -c "cargo run --release -- -m test test_ensurepip"同一条exec模式可用于执行容器内的任意命令,文档中演示的是一条 workspace Rust 单元测试:
# Run any command container exec rustpython-test sh -c "cargo test --workspace"如果目标是按文档 “Testing” 一节的标准方式跑 Rust 单元测试,应使用带排除项的命令(rustpython_wasm、rustpython-venvlauncher、rustpython-capi不在其中测试),把上面exec的示例换成:
container exec rustpython-test sh -c "cargo test --workspace --exclude rustpython_wasm --exclude rustpython-venvlauncher --exclude rustpython-capi"而rustpython-capi有独立的cargo配置,需要进入crates/capi目录单独测试:
container exec rustpython-test sh -c "cd crates/capi && cargo test"标准库测试(-m test)支持文档列出的几个选项,在容器里同样适用:-j <n>并行测试(文档建议线程数尽量等于 CPU 核数,不确定时用-j 4或-j 8;本容器按文档配置为 4 个 CPU),-v打开详细模式,<test_name>指定只跑单个测试模块。例如全量并行:
container exec rustpython-test sh -c "cargo run --release -- -m test -j 4"或者按文档示例只跑test_cmath(位于Lib/test/test_cmath)并输出详细信息:
container exec rustpython-test sh -c "cargo run --release -- -m test test_cmath -v"文档没有给出这些测试命令的示例输出,是否通过以测试命令自身打印的结果为准;加-v可以看到正在运行的测试的更多信息。
收尾:停止并删除容器
文档的说明是 “Stop and remove the container when done”:
# Stop and remove the container when done container rm -f rustpython-test该命令会强制停止并删除rustpython-test容器。仓库目录是通过 bind mount 映射进容器的,删除容器不会影响本地仓库中的源码。
限制说明
- 文档中的 setup 命令固定
--arch arm64并关闭 arm64-only 构建的 Rosetta 要求,文档未提供面向 Intel Mac 的替代流程; - 容器内工具链来自
.devcontainer/Dockerfile(rust:bullseye+clang)构建的rustpython-dev镜像;若该 Dockerfile 有变更,需要重新执行container build再启动容器; - 本流程覆盖在 macOS 上用
containerCLI 跑 RustPython 的 Linux 测试这一任务;若要在 macOS 宿主机上直接开发(而非容器内),环境要求见 CONTRIBUTING.md 的 “Setting up a development environment” 一节。
【免费下载链接】RustPythonA Python Interpreter written in Rust项目地址: https://gitcode.com/GitHub_Trending/ru/RustPython
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考