簡體   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