From b3a29b15b1b87907b6e26161c26ff6157ddd0413 Mon Sep 17 00:00:00 2001 From: Tom <25043847+Douile@users.noreply.github.com> Date: Fri, 24 Nov 2023 22:34:26 +0000 Subject: [PATCH] [CI] Improvement and fixes (#161) * protocols: Fix building without the "games" feature * crate/lib: Add required features for examples This prevents cargo from running the examples if the required features aren't enabled. * ci: Run if ANY Cargo.toml files are changed * ci: Make sure to run unit tests * ci: Separate checks for library and CLI * ci: Add slightly better comments * ci: Only run deeper tests for CLI or LIB when their files were changed * ci: Improve act arguments for testing actions locally * ci: Fix pre-commit not running tests * ci: Only update shared cache after the initial build * ci: Make sure that rustup downloads get cached * tidy: Clean up file formatting * ci: Fix issue with audit --- .actrc | 3 + .github/.act-event.json | 6 ++ .github/workflows/audit.yml | 1 - .github/workflows/ci.yml | 93 +++++++++++++++++++++++++++---- .pre-commit-config.yaml | 11 +++- crates/lib/Cargo.toml | 19 ++++++- crates/lib/src/protocols/types.rs | 5 +- 7 files changed, 123 insertions(+), 15 deletions(-) create mode 100644 .github/.act-event.json diff --git a/.actrc b/.actrc index b455111..8af6fb7 100644 --- a/.actrc +++ b/.actrc @@ -3,3 +3,6 @@ # Swap docker image for the one containing the rust toolchain -P ubuntu-latest=ghcr.io/catthehacker/ubuntu:rust-latest + +# Load custom event +-e .github/.act-event.json diff --git a/.github/.act-event.json b/.github/.act-event.json new file mode 100644 index 0000000..8925958 --- /dev/null +++ b/.github/.act-event.json @@ -0,0 +1,6 @@ +{ + "act": true, + "repository": { + "default_branch": "main" + } +} diff --git a/.github/workflows/audit.yml b/.github/workflows/audit.yml index a553af6..826e66e 100644 --- a/.github/workflows/audit.yml +++ b/.github/workflows/audit.yml @@ -13,4 +13,3 @@ jobs: - uses: rustsec/audit-check@v1.4.1 with: token: ${{ secrets.GITHUB_TOKEN }} - if: ${{ !env.ACT }} # skip during local actions testing diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1a79b82..94d5509 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,14 +6,14 @@ on: branches: [ "main" ] paths: - "**.rs" # Any rust file - - "Cargo.toml" + - "**/Cargo.toml" # Any Cargo.toml - ".rustfmt.toml" - ".github/workflows/ci.yml" # This action pull_request: branches: [ "main" ] paths: - "**.rs" # Any rust file - - "Cargo.toml" + - "**/Cargo.toml" # Any Cargo.toml - ".rustfmt.toml" - ".github/workflows/ci.yml" # This action @@ -21,9 +21,13 @@ env: CARGO_TERM_COLOR: always jobs: + # First check that we can build EVERYTHING and that tests pass build_first: name: "Build, check, and test with all features" runs-on: ubuntu-latest + outputs: + cli: ${{ steps.filter.outputs.cli }} + lib: ${{ steps.filter.outputs.lib }} steps: - uses: actions/checkout@v4 - uses: Swatinem/rust-cache@v2 @@ -33,10 +37,23 @@ jobs: - name: Run Build run: cargo check --verbose --workspace --bins --lib --examples --all-features - name: Run Tests - run: cargo test --verbose --workspace --bins --lib --examples --all-features - build: + run: cargo test --verbose --workspace --bins --lib --examples --tests --all-features + # Check what paths were modified so we only run the required tests + - uses: dorny/paths-filter@v2 + id: filter + with: + filters: | + cli: + - 'crates/cli/**' + lib: + - 'crates/lib/**' + + # If we were able to build then test different feature combinations compile with the library + build_lib: runs-on: ubuntu-latest needs: [ "build_first" ] + # Only run if library files were modified + if: ${{ needs.build_first.outputs.lib == 'true' }} strategy: fail-fast: false matrix: @@ -53,21 +70,61 @@ jobs: build_name: "Just Game definitions" - build_type: "--no-default-features --features serde" build_name: "Just serde" - name: "Build ${{ matrix.build_name }}" + name: "Build library ${{ matrix.build_name }}" steps: - uses: actions/checkout@v4 - uses: Swatinem/rust-cache@v2 with: shared-key: "cargo-deps" cache-targets: false + save-if: false - name: Run Build - run: cargo check --workspace --verbose --bins --lib ${{ matrix.build_type }} - build_msrv: - name: "Build using MSRV (lib only)" + run: cargo check -p gamedig --verbose --lib --examples --tests ${{ matrix.build_type }} + + # If we were able to build then test different feature combinations compile with the CLI + build_cli: runs-on: ubuntu-latest + needs: [ "build_first" ] + # Only run if CLI files were modified + if: ${{ needs.build_first.outputs.cli == 'true' }} + strategy: + fail-fast: false + matrix: + include: + - build_type: "" + build_name: "Default" + - build_type: "--no-default-features" + build_name: "No features" + name: "Build CLI ${{ matrix.build_name }}" steps: - uses: actions/checkout@v4 - uses: Swatinem/rust-cache@v2 + with: + shared-key: "cargo-deps" + cache-targets: false + save-if: false + - name: Run Build + run: cargo check -p gamedig-cli --verbose --bins --examples --tests ${{ matrix.build_type }} + + # If we were able to build then test the MSRV compiles (for the libary as not enforced for CLI) + build_msrv: + name: "Build using MSRV (lib only)" + runs-on: ubuntu-latest + needs: [ "build_first" ] + # Only run if library files were modified + if: ${{ needs.build_first.outputs.lib == 'true' }} + # Unfortunate hard-coding of rustup directory so that rust-cache caches it + env: + RUSTUP_HOME: /home/runner/.rustup + steps: + # Act's rust runner has rustup in a different place + - if: ${{ env.ACT }} + run: mkdir -p /home/runner && ln -s /usr/share/rust/.rustup /home/runner/.rustup + - uses: actions/checkout@v4 + - uses: Swatinem/rust-cache@v2 + with: + cache-targets: false + cache-directories: ${{ env.RUSTUP_HOME }}/toolchains - name: Install MSRV uses: actions-rs/toolchain@v1 with: @@ -75,14 +132,22 @@ jobs: override: true - name: Run MSRV run: cargo check -p gamedig + # Check the code is formatted properly formatting: name: "Check code formatting" runs-on: ubuntu-latest + # Unfortunate hard-coding of rustup directory so that rust-cache caches it + env: + RUSTUP_HOME: /home/runner/.rustup steps: + # Act's rust runner has rustup in a different place + - if: ${{ env.ACT }} + run: mkdir -p /home/runner && ln -s /usr/share/rust/.rustup /home/runner/.rustup - uses: actions/checkout@v4 - uses: Swatinem/rust-cache@v2 with: cache-targets: false + cache-directories: ${{ env.RUSTUP_HOME }}/toolchains - name: Install Formatting nightly uses: actions-rs/toolchain@v1 with: @@ -91,6 +156,8 @@ jobs: override: true - name: Run Formatting check run: cargo fmt --check --verbose + + # If we were able to build then lint the codebase with clippy clippy: name: "Run clippy tests" runs-on: ubuntu-latest @@ -101,15 +168,20 @@ jobs: with: shared-key: "cargo-deps" cache-targets: false + save-if: false + # Run github actions version of clippy that adds annotations - name: Run Clippy uses: actions-rs/clippy-check@v1 with: token: ${{ secrets.GITHUB_TOKEN }} - args: --workspace --bins --lib --examples --all-features + args: --workspace --bins --lib --examples --tests --all-features if: ${{ !env.ACT }} # skip during local actions testing + # Run clippy binary - name: Run clippy (local) - run: cargo clippy --workspace --verbose --bins --lib --examples --all-features + run: cargo clippy --verbose --workspace --bins --lib --examples --tests --all-features if: ${{ env.ACT }} # only run during local actions testing + + # If we were able to build then test that rustdoc (and rustdoc examples) compile doc: name: "Check rustdoc" runs-on: ubuntu-latest @@ -120,6 +192,7 @@ jobs: with: shared-key: "cargo-deps" cache-targets: false + save-if: false - name: "Run cargo doc" run: cargo doc --workspace env: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 6e7adc3..f7f95bc 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -9,45 +9,52 @@ repos: files: '[.]rs$' pass_filenames: false entry: rustup run --install nightly-2023-07-09 cargo-clippy -- --workspace --all-features -- -D warnings + - id: build-no-features name: Check crate build with no features language: system files: '[.]rs$' pass_filenames: false entry: cargo check --workspace --no-default-features + - id: build-all-features name: Check crate builds with all features language: system files: '[.]rs$' pass_filenames: false entry: cargo check --workspace --all-features --lib --bins --examples + - id: test name: Check tests pass language: system files: '[.]rs$' pass_filenames: false - entry: cargo test --workspace --bins --lib --examples --all-features + entry: cargo test --workspace --bins --lib --examples --tests --all-features + - id: format name: Check rustfmt language: system files: '[.]rs$' pass_filenames: false entry: rustup run --install nightly-2023-07-09 cargo-fmt --check + - id: msrv name: Check MSRV compiles (lib only) language: system files: '[.]rs$' pass_filenames: false entry: rustup run --install 1.65 cargo check -p gamedig + - id: docs name: Check rustdoc compiles language: system files: '[.]rs$' pass_filenames: false entry: env RUSTDOCFLAGS="-D warnings" cargo doc + - id: actions name: Check actions work language: system files: '^[.]github/workflows/' pass_filenames: false - entry: act -q + entry: act --rm diff --git a/crates/lib/Cargo.toml b/crates/lib/Cargo.toml index f3199b3..fe9cae9 100644 --- a/crates/lib/Cargo.toml +++ b/crates/lib/Cargo.toml @@ -32,4 +32,21 @@ encoding_rs = "0.8" serde = { version = "1.0", optional = true } -phf = { version = "0.11", optional = true, features = ["macros"] } \ No newline at end of file +phf = { version = "0.11", optional = true, features = ["macros"] } + +# Examples +[[example]] +name = "minecraft" +required-features = ["games"] + +[[example]] +name = "teamfortress2" +required-features = ["games"] + +[[example]] +name = "valve_master_server_query" +required-features = ["services"] + +[[example]] +name = "generic" +required-features = ["games", "game_defs"] diff --git a/crates/lib/src/protocols/types.rs b/crates/lib/src/protocols/types.rs index b4abcf8..35c7c9f 100644 --- a/crates/lib/src/protocols/types.rs +++ b/crates/lib/src/protocols/types.rs @@ -1,6 +1,8 @@ +#[cfg(feature = "games")] +use crate::games::minecraft; use crate::protocols::{gamespy, quake, unreal2, valve}; use crate::GDErrorKind::InvalidInput; -use crate::{minecraft, GDResult}; +use crate::GDResult; use std::time::Duration; @@ -8,6 +10,7 @@ use std::time::Duration; use serde::{Deserialize, Serialize}; /// Enumeration of all custom protocols +#[cfg(feature = "games")] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] pub enum ProprietaryProtocol {