繁体   English   中英

对于 Rust,您如何在发布到 crate.io 之前执行平台测试?

[英]With Rust how do you perform platform testing before publishing to crate.io?

我正在开发一个正在我的Mac上测试的framework 最后,我想发布到crate.io 我希望它不会因为平台测试不佳而崩溃。 是否有一种方法可以在不直接访问这些平台的情况下测试所有或至少大多数当前部署平台? 例如,我无法访问Windows框。

如果您在 github 上托管您的代码,您可以设置 github 操作以在多个平台上构建和测试您的代码。

我有两组在我的代码上运行的操作。

  • 一个运行测试,并且 clippy 并检查rust fmt仅用于正常的推送和拉取请求
  • 另一个在设置发布分支并创建发布时运行,运行测试并构建和上传 windows、Linux 和 macOS 的发布二进制文件。

您可以在此处查看完整的文件。

但是结合这些并简化意味着要对每个推送和拉取请求进行测试,您将在.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) 我实际上想在我的README.md它们作为 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

如果你仔细看,你会发现我设置了stablebeta 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 表中,如下所示:

|             | 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]

这将为您生成一个徽章,以便人们可以确定您的代码是否正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM