使用 Rust,您如何在发布到 crate.io 之前执行平台测试?
With Rust how do you perform platform testing before publishing to crate.io?
我正在开发 framework
,我正在 Mac
上进行测试。最后,我想发布到 crate.io
。我希望它不会因为平台测试不佳而崩溃。是否有一种方法可以在不直接访问这些平台的情况下测试所有或至少大多数当前部署平台?例如,我无法访问 Windows
框。
如果您在 github 上托管您的代码,您可以设置 github 操作以在多个平台上构建和测试您的代码。
我的代码中有两组 运行 操作。
- 一个 运行 测试,并且在 Linux 上检查
rust fmt
仅用于正常的推送和拉取请求
- 其他 运行s 在设置发布分支并创建发布时,运行s 测试并构建和上传 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) 我实际上想在我的 [=16] 上将它们作为 Shields 徽章单独展示=] 以提供全面的详细信息。
您可以在 .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, 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: 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 很有信心,但对于其他平台,我不是。
覆盖率工作流程
覆盖率工作流运行测试并计算测试实际命中的代码行的百分比。然后它将统计信息推送到 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 table 中,如下所示:
| | 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]
这将为您生成一个徽章,以便人们可以确定您的代码是否正常工作。
我正在开发 framework
,我正在 Mac
上进行测试。最后,我想发布到 crate.io
。我希望它不会因为平台测试不佳而崩溃。是否有一种方法可以在不直接访问这些平台的情况下测试所有或至少大多数当前部署平台?例如,我无法访问 Windows
框。
如果您在 github 上托管您的代码,您可以设置 github 操作以在多个平台上构建和测试您的代码。
我的代码中有两组 运行 操作。
- 一个 运行 测试,并且在 Linux 上检查
rust fmt
仅用于正常的推送和拉取请求 - 其他 运行s 在设置发布分支并创建发布时,运行s 测试并构建和上传 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) 我实际上想在我的 [=16] 上将它们作为 Shields 徽章单独展示=] 以提供全面的详细信息。
您可以在 .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, 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: 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 很有信心,但对于其他平台,我不是。
覆盖率工作流程
覆盖率工作流运行测试并计算测试实际命中的代码行的百分比。然后它将统计信息推送到 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 table 中,如下所示:
| | 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]
这将为您生成一个徽章,以便人们可以确定您的代码是否正常工作。