diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index afdb2d1..66f4997 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -404,3 +404,37 @@ jobs: - name: Run tests shell: bash run: ./validate-inputs.py --test + + test-other-cargo-commands: + name: Test a "cargo foo" command + runs-on: ubuntu-24.04 + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Copy test project to root + shell: bash + run: | + cp -a test-project/* . + rm -fr test-project + - name: Create cargo-foo command + shell: bash + run: | + set -e + set -x + + dir="${{ github.workspace }}/bin" + mkdir -p "$dir" + + bin="$dir/cargo-foo" + + echo "#!/bin/bash" >> "$bin" + echo "echo 'This is cargo-foo!'" >> "$bin" + echo "exit 0" >> "$bin" + chmod 0755 "$bin" + + echo "PATH=$dir:$PATH" >> "$GITHUB_ENV" + - name: Run cargo-foo command + uses: ./ + with: + command: foo + target: x86_64-unknown-linux-gnu diff --git a/Changes.md b/Changes.md index 9c5cfb6..f733946 100644 --- a/Changes.md +++ b/Changes.md @@ -4,6 +4,9 @@ [dtolnay/rust-toolchain](https://github.com/dtolnay/rust-toolchain) accepts a lot of different options that this action wasn't allowing. It's simpler and more flexible to just let that action handle validation. Requested by @axos88 (Akos Vandra-Meyer). GH #42. +- Removed validation for the `command` input. This allows you to use this action with any `cargo` + extension command, like `cargo-deb`. Setting `command` to `both` is still supported and will run + the `build` and `test` commands. Requested by @bvaisvil (Benjamin Vaisvil). GH #43. ## 1.0.3 - 2025-02-17 diff --git a/README.md b/README.md index 340c75f..fd3a51c 100644 --- a/README.md +++ b/README.md @@ -69,19 +69,19 @@ Ubuntu x86-64, Windows, and macOS runners. This action takes the following parameters: -| Key | Type | Required? | Description | -| ----------------------- | -------------------------------------------------------------------- | --------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `command` | string (one of `build`, `test`, `both` (build and test), or `bench`) | no | The command(s) to run. The default is `build`. Running the `test` command will fail with \*BSD targets and non-x86 Windows. | -| `target` | string | yes | The target triple to compile for. This should be one of the targets found by running `rustup target list`. | -| `working-directory` | string | no | The working directory in which to run the `cargo` or `cross` commands. Defaults to the current directory (`.`). | -| `toolchain` | string) | no | The Rust toolchain version to install. This is passed directly to [dtolnay/rust-toolchain](https://github.com/dtolnay/rust-toolchain), which accepts many different options. See its documentation for more details. The default is `stable`. | -| `GITHUB_TOKEN` | string | no | Defaults to the value of `${{ github.token }}`. | -| `args` | string | no | A string-separated list of arguments to be passed to `cross build`, like `--release --locked`. | -| `strip` | boolean (`true` or `false`) | no | If this is true, then the resulting binaries will be stripped if possible. This is only possible for binaries which weren't cross-compiled. | -| `cross-version` | string | no | This can be used to set the version of `cross` to use. If specified, it should be a specific `cross` release tag (like `v0.2.3`) or a git ref (commit hash, `HEAD`, etc.). If this is not set then the latest released version will always be used. If this is set to a git ref then the version corresponding to that ref will be installed. | -| `force-use-cross` | boolean (`true` or `false`) | no | If this is true, then the action will use `cross` even if it is not needed for the given target. If this is set to `true`, then the resulting binary will not be stripped, regardless of whether `strip` is `true` or not. This only works on Linux hosts. Forcing the use of `cross` on other hosts is not supported. | -| `use-rust-cache` | boolean | no | Whether or not to use [the `Swatinem/rust-cache@v2` action](https://github.com/Swatinem/rust-cache). This defaults to true. | -| `rust-cache-parameters` | string (containing JSON) | no | This must be a string containing valid JSON. The JSON should be an object where the keys are the parameters for [the `Swatinem/rust-cache@v2` action](https://github.com/Swatinem/rust-cache). | +| Key | Type | Required? | Description | +| ----------------------- | --------------------------- | --------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `command` | string | no | The command(s) to run. The default is `build`. Running the `test` command will fail with \*BSD targets and non-x86 Windows. You can use any command supported by `cargo` and/or `cross`. For example, if you install `cargo-deb`, the command can be `deb`. Use the special string "both" to run both `build` and `test. | +| `target` | string | yes | The target triple to compile for. This should be one of the targets found by running `rustup target list`. | +| `working-directory` | string | no | The working directory in which to run the `cargo` or `cross` commands. Defaults to the current directory (`.`). | +| `toolchain` | string) | no | The Rust toolchain version to install. This is passed directly to [dtolnay/rust-toolchain](https://github.com/dtolnay/rust-toolchain), which accepts many different options. See its documentation for more details. The default is `stable`. | +| `GITHUB_TOKEN` | string | no | Defaults to the value of `${{ github.token }}`. | +| `args` | string | no | A string-separated list of arguments to be passed to `cross build`, like `--release --locked`. | +| `strip` | boolean (`true` or `false`) | no | If this is true, then the resulting binaries will be stripped if possible. This is only possible for binaries which weren't cross-compiled. | +| `cross-version` | string | no | This can be used to set the version of `cross` to use. If specified, it should be a specific `cross` release tag (like `v0.2.3`) or a git ref (commit hash, `HEAD`, etc.). If this is not set then the latest released version will always be used. If this is set to a git ref then the version corresponding to that ref will be installed. | +| `force-use-cross` | boolean (`true` or `false`) | no | If this is true, then the action will use `cross` even if it is not needed for the given target. If this is set to `true`, then the resulting binary will not be stripped, regardless of whether `strip` is `true` or not. This only works on Linux hosts. Forcing the use of `cross` on other hosts is not supported. | +| `use-rust-cache` | boolean | no | Whether or not to use [the `Swatinem/rust-cache@v2` action](https://github.com/Swatinem/rust-cache). This defaults to true. | +| `rust-cache-parameters` | string (containing JSON) | no | This must be a string containing valid JSON. The JSON should be an object where the keys are the parameters for [the `Swatinem/rust-cache@v2` action](https://github.com/Swatinem/rust-cache). | ### Setting Environment Variables diff --git a/action.yml b/action.yml index 677460f..0facaf4 100644 --- a/action.yml +++ b/action.yml @@ -15,7 +15,7 @@ inputs: command: description: | - The commands to run. This must be one of "build", "test", "both" (build and test), or "bench". + The commands to run. Use "both" to run both "build" and "test". default: build toolchain: @@ -191,51 +191,35 @@ runs: with: ${{ steps.parse-rust-cache-parameters.outputs }} if: inputs.use-rust-cache == 'true' - - name: Run tests (*nix) + - name: Run cargo test working-directory: ${{ inputs.working-directory }} - shell: bash + # We want to run in Powershell on Windows to make sure we compile in a native Windows + # environment. Some things won't compile properly under msys, notably OpenSSL, which is + # compiled locally when using the `openssl` crate with the `vendored` feature. + shell: ${{ runner.os == 'Windows' && 'powershell' || 'bash' }} run: | ${{ steps.set-build-command.outputs.build-command }} test --target ${{ inputs.target }} ${{ inputs.args }} - if: steps.determine-cargo-commands.outputs.test == 'true' && runner.os != 'Windows' + if: steps.determine-cargo-commands.outputs.test == 'true' - # We want to run in Powershell on Windows to make sure we compile in a - # native Windows environment. Some things won't compile properly under - # msys, notably OpenSSL, which is compiled locally when using the - # `openssl` crate with the `vendored` feature. - - name: Run tests (Windows) + - name: Run cargo build working-directory: ${{ inputs.working-directory }} - shell: powershell + # We want to run in Powershell on Windows to make sure we compile in a native Windows + # environment. Some things won't compile properly under msys, notably OpenSSL, which is + # compiled locally when using the `openssl` crate with the `vendored` feature. + shell: ${{ runner.os == 'Windows' && 'powershell' || 'bash' }} run: | - & ${{ steps.set-build-command.outputs.build-command }} test --target ${{ inputs.target }} ${{ inputs.args }} - if: steps.determine-cargo-commands.outputs.test == 'true' && runner.os == 'Windows' + ${{ steps.set-build-command.outputs.build-command }} build --target ${{ inputs.target }} ${{ inputs.args }} + if: steps.determine-cargo-commands.outputs.build == 'true' - - name: Run benchmarks (*nix) + - name: Run cargo ${{ steps.determine-cargo-commands.outputs.command }} working-directory: ${{ inputs.working-directory }} - shell: bash + # We want to run in Powershell on Windows to make sure we compile in a native Windows + # environment. Some things won't compile properly under msys, notably OpenSSL, which is + # compiled locally when using the `openssl` crate with the `vendored` feature. + shell: ${{ runner.os == 'Windows' && 'powershell' || 'bash' }} run: | - ${{ steps.set-build-command.outputs.build-command }} bench --target ${{ inputs.target }} ${{ inputs.args }} - if: steps.determine-cargo-commands.outputs.bench == 'true' && runner.os != 'Windows' - - - name: Run benchmarks (Windows) - working-directory: ${{ inputs.working-directory }} - shell: powershell - run: | - & ${{ steps.set-build-command.outputs.build-command }} bench --target ${{ inputs.target }} ${{ inputs.args }} - if: steps.determine-cargo-commands.outputs.bench == 'true' && runner.os == 'Windows' - - - name: Build binary (*nix) - working-directory: ${{ inputs.working-directory }} - shell: bash - run: | - ${{ steps.set-build-command.outputs.build-command }} build ${{ inputs.args }} --target ${{ inputs.target }} - if: steps.determine-cargo-commands.outputs.build == 'true' && runner.os != 'Windows' - - - name: Build binary (Windows) - working-directory: ${{ inputs.working-directory }} - shell: powershell - run: | - & ${{ steps.set-build-command.outputs.build-command }} build ${{ inputs.args }} --target ${{ inputs.target }} - if: steps.determine-cargo-commands.outputs.build == 'true' && runner.os == 'Windows' + ${{ steps.set-build-command.outputs.build-command }} ${{ steps.determine-cargo-commands.outputs.command }} --target ${{ inputs.target }} ${{ inputs.args }} + if: steps.determine-cargo-commands.outputs.command != '' - name: Strip binary working-directory: ${{ inputs.working-directory }} diff --git a/determine-cargo-commands.sh b/determine-cargo-commands.sh index e596ba7..19696fc 100755 --- a/determine-cargo-commands.sh +++ b/determine-cargo-commands.sh @@ -9,5 +9,5 @@ if [ "$COMMAND" == 'both' ]; then echo "build=true" >>"$GITHUB_OUTPUT" echo "test=true" >>"$GITHUB_OUTPUT" else - echo "$COMMAND=true" >>"$GITHUB_OUTPUT" + echo "command=$COMMAND" >>"$GITHUB_OUTPUT" fi diff --git a/validate-inputs.py b/validate-inputs.py index 96e7213..adbbaed 100755 --- a/validate-inputs.py +++ b/validate-inputs.py @@ -42,14 +42,6 @@ class InputValidator: if "target" not in self.inputs: validation_errors.append("'target' is a required parameter") - # Validate command if present - if "command" in self.inputs: - valid_commands = {"build", "test", "both", "bench"} - if self.inputs["command"] not in valid_commands: - validation_errors.append( - f"Invalid 'command'. Must be one of {sorted(valid_commands)}" - ) - # Validate working directory if present if "working_directory" in self.inputs: path = Path(self.inputs["working_directory"]) @@ -135,23 +127,6 @@ class TestInputValidator(unittest.TestCase): errors = validator.validate() self.assertTrue(errors) - def test_validate_valid_command(self) -> None: - """Test validation of valid commands.""" - valid_commands = ["build", "test", "both", "bench"] - - for command in valid_commands: - self.setup_env({"target": "x86_64-unknown-linux-gnu", "command": command}) - validator = InputValidator("/root") - errors = validator.validate() - self.assertFalse(errors, f"Command '{command}' should be valid") - - def test_validate_invalid_command(self) -> None: - """Test validation of invalid command.""" - self.setup_env({"target": "x86_64-unknown-linux-gnu", "command": "invalid"}) - validator = InputValidator("/root") - errors = validator.validate() - self.assertTrue(errors) - def test_validate_valid_toolchain(self) -> None: """Test validation of valid toolchains.""" valid_toolchains = [