如何解决使用Rust,您如何在发布到crate.io之前执行平台测试?
我正在研究framework
上的Mac
。最终,我将要发布到crate.io
。我希望它不会因平台测试不佳而爆炸。是否有方法可以在所有或至少大多数当前部署平台上进行测试而无需直接访问这些平台?例如,我无权访问Windows
框。
解决方法
如果您将代码托管在github上,则可以设置github操作以在多个平台上构建和测试代码。
我在我的代码上运行了两组操作。
- 仅运行一次测试,然后在Linux上运行clippy并检查
rust fmt
中是否存在正常的推送和提取请求 - 另一个在设置发行版分支并创建发行版时运行,运行测试并生成和上传Windows,Linux和macOS的发行版二进制文件。
您可以在完整的here中看到完整的文件。
但是将这些和简化结合起来意味着,要对每个推送请求进行测试,您将在.github/workflows/testing.yml
处创建一个文件,如下所示(未测试):
name: Run Tests
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build_matrix:
name: Run tests for ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
name: [linux,windows,macos]
include:
- name: linux
os: ubuntu-latest
- name: windows
os: windows-latest
- name: macos
os: macos-latest
steps:
- uses: actions/checkout@v1
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
override: true
- name: Test
run: cargo test
,
我实际上是在我的一个项目中这样做的,altbinutils。我使用 Github 操作。我测试的是:
- 针对每个平台的测试
- 为每个平台构建
- 覆盖率测试
- 格式测试
我将每个平台的工作流程分开到不同的配置文件中,因为 (i) 我不想在一个失败时全部失败,并且 (ii) 我实际上想将它们作为 Shields 徽章单独呈现在我的 README.md
提供全面的详细信息。
您可以在 .github/workflows 目录下看到工作流,但同样,让我在这里分享配置,因为将来可能会发生变化。
构建工作流
构建工作流确保您的代码将在目标平台上编译,但不能确保其正常工作。
我有三个独立的构建工作流程。它们是:
build.linux.yml
build.windows.yml
build.macos.yml
内容是:
# build.linux.yml
name: build_linux
on: [push,pull_request]
jobs:
Test:
strategy:
matrix:
os: [ubuntu-latest]
rust: [stable,beta]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v1
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.rust }}
override: true
- uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- uses: actions-rs/cargo@v1
with:
command: build
# build.windows.yml
name: build_windows
on: [push,pull_request]
jobs:
Test:
strategy:
matrix:
os: [windows-latest]
rust: [stable]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v1
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.rust }}
override: true
- uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- uses: actions-rs/cargo@v1
with:
command: build
# build.macos.yml
name: build_macos
on: [push,pull_request]
jobs:
Test:
strategy:
matrix:
os: [macos-latest]
rust: [stable]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v1
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.rust }}
override: true
- uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- uses: actions-rs/cargo@v1
with:
command: build
如果你仔细看,你会发现我设置了 stable
和 beta
Rust 矩阵以确保我的代码在未来不会被破坏,并且只在 Linux 平台上,因为在其他平台上这样做是很友好的多余的。
测试工作流程
测试工作流确保您的代码行为正确。
我为每个平台设置了三个三个独立的工作流程。这些是:
# test.linux.yml
name: test_linux
on: [push,beta]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v1
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.rust }}
override: true
- uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- uses: actions-rs/cargo@v1
with:
command: test
args: --lib --workspace
# test.windows.yml
name: test_windows
on: [push,pull_request]
jobs:
Test:
strategy:
matrix:
os: [windows-latest]
rust: [stable]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v1
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.rust }}
override: true
- uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- uses: actions-rs/cargo@v1
with:
command: test
args: --lib --workspace -- --nocapture
# test.macos.yml
name: test_macos
on: [push,pull_request]
jobs:
Test:
strategy:
matrix:
os: [macos-latest]
rust: [stable]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v1
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.rust }}
override: true
- uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- uses: actions-rs/cargo@v1
with:
command: test
args: --lib --workspace -- --nocapture
您可以在这些配置中看到的额外内容是 --workspace
标志,如果您的项目有子成员,它将测试您的所有工作区。就我而言,我在子目录下有单独的应用程序,这些子目录实际上是工作区的成员。
还要注意,对于 Windows 和 MacOS,我还添加了 -- --nocapture
标志,它实际上会打印出这些平台上的所有日志和打印语句。我对 Linux 很有信心,但对于其他平台,我没有。
覆盖工作流程
Coverage 工作流运行测试并计算您的测试实际命中的代码行的百分比。然后它会将统计信息推送到 Codecov,以便您可以查看全面的详细信息。
我只有一个工作流,它在 Linux 上运行。一个就够了,因为覆盖其他平台会导致(大致)相同的结果。
工作流配置文件为:
# coverage.yml
name: coverage
on: [push,pull_request]
jobs:
Test:
strategy:
matrix:
os: [ubuntu-latest]
rust: [stable]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v1
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.rust }}
override: true
- uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Generate Coverage
run: |
cargo install cargo-tarpaulin
cargo tarpaulin --verbose --all-features --workspace --timeout 120 --out Xml
- name: Upload Coverage
uses: codecov/codecov-action@v1
with:
fail_ci_if_error: true
格式化工作流程
格式化工作流测试代码是否在新的拉取请求中格式正确。如果您认为其他人会为您的代码库贡献代码,并且您希望尽可能保持其整洁和可读,则可以使用它。
工作流配置文件为:
# format.yml
name: format
on: [push,pull_request]
jobs:
Test:
strategy:
matrix:
os: [ubuntu-latest]
rust: [stable]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v1
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.rust }}
override: true
- uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- uses: actions-rs/cargo@v1
with:
command: fmt
args: -- --check
展示结果
您可以使用 Shields 将结果显示为徽章。我将它们呈现在 Markdown 表格中,如下所示:
| | Build | Test | Format | Coverage |
| ----------- | ------------------------------------- | ----------------------------------- | ----------------------- | --------------------------- |
| **Linux** | ![linux build][linux_build_badge] | ![linux test][linux_test_badge] | ![format][format_badge] | ![coverage][coverage_badge] |
| **Windows** | ![windows build][windows_build_badge] | ![windows test][windows_test_badge] | - | - |
| **MacOS** | ![macos build][macos_build_badge] | ![macos test][macos_test_badge] | - | - |
<!-- START BADGES -->
<!-- linux -->
[linux_build_badge]: https://img.shields.io/github/workflow/status/erayerdin/altbinutils/build_linux/master?logo=linux&logoColor=white&style=flat-square
[linux_test_badge]: https://img.shields.io/github/workflow/status/erayerdin/altbinutils/test_linux/master?logo=linux&logoColor=white&style=flat-square&label=test
[format_badge]: https://img.shields.io/github/workflow/status/erayerdin/altbinutils/format/master?logo=linux&logoColor=white&style=flat-square&label=format
[coverage_badge]: https://img.shields.io/codecov/c/github/erayerdin/altbinutils/master?style=flat-square&token=71P6IZY43W&logo=linux&logoColor=white
<!-- windows -->
[windows_build_badge]: https://img.shields.io/github/workflow/status/erayerdin/altbinutils/build_windows/master?logo=windows&logoColor=white&style=flat-square
[windows_test_badge]: https://img.shields.io/github/workflow/status/erayerdin/altbinutils/test_windows/master?logo=windows&logoColor=white&style=flat-square&label=test
<!-- macos -->
[macos_build_badge]: https://img.shields.io/github/workflow/status/erayerdin/altbinutils/build_macos/master?logo=apple&logoColor=white&style=flat-square
[macos_test_badge]: https://img.shields.io/github/workflow/status/erayerdin/altbinutils/test_macos/master?logo=apple&logoColor=white&style=flat-square&label=test
<!-- END BADGES -->
您实际上可以通过根据需要更改以下 URL 来显示您的工作流程之一的状态(以显示它是成功还是失败):
https://img.shields.io/github/workflow/status/[your-username]/[your-reponame]/[name-attr-of-config-file]/[branch]
这将为您生成一个徽章,以便人们可以确定您的代码是否有效。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。