Add inputs to control the Rust toolchain (#34)

* Add inputs for controlling rust toolchain

* Add tests for new inputs

* Negated ifs!

* Fix action input name

* Better description of rust-override

* New logic of choosing toolchain

* Add explaining comment.

* Add rust-toolchain.toml test
This commit is contained in:
Mieszko Grodzicki 2023-04-08 16:21:06 +02:00 committed by GitHub
parent 43d6c89249
commit f9ffdc4a3d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 173 additions and 7 deletions

View file

@ -186,3 +186,157 @@ jobs:
run: | run: |
echo "Error! The action should have failed because of the breaking change, but it has not." echo "Error! The action should have failed because of the breaking change, but it has not."
exit 1 exit 1
test-rust-toolchain-against-directory-override:
name: Test the action against toolchain override set using rustup
runs-on: ubuntu-latest
steps:
- name: Checkout the test repository
uses: actions/checkout@v3
with:
repository: mgr0dzicki/cargo-semver-action-ref-slice
ref: patch_change
- name: Checkout the action
uses: actions/checkout@v3
with:
path: action
- name: Install latest beta
uses: dtolnay/rust-toolchain@beta
- name: Set local override to beta
run: rustup override set beta
- name: Run the action with rust-toolchain set to nightly
uses: ./action/
with:
rust-toolchain: nightly
- name: Get cache key
uses: actions/cache/restore@v3
id: get-cache-key
with:
path: ${{ github.workspace }}/semver-checks/target/semver-checks/cache
fail-on-cache-miss: true
lookup-only: true
key: -${{ github.job }}
restore-keys: |
-${{ github.job }}
- name: Fail if the toolchain used by the action is invalid
if: ${{ ! contains(steps.get-cache-key.outputs.cache-matched-key, 'nightly') }}
run: |
echo "The toolchain used by the action should be nightly, but it is not!"
exit 1
- name: Check the active toolchain
run: |
if ! rustup show active-toolchain | grep -q "beta"; then
echo "The active toolchain should be still beta, but it is $(rustup show active-toolchain)!"
exit 1
fi
test-rust-toolchain-against-toml-override:
name: Test the action against toolchain override specified in rust-toolchain.toml file
runs-on: ubuntu-latest
steps:
- name: Checkout the test repository
uses: actions/checkout@v3
with:
repository: mgr0dzicki/cargo-semver-action-ref-slice
ref: patch_change
- name: Checkout the action
uses: actions/checkout@v3
with:
path: action
- name: Install latest beta
uses: dtolnay/rust-toolchain@beta
- name: Set override to beta in rust-toolchain.toml file
run: echo -e "[toolchain]\nchannel = \"beta\"" > rust-toolchain.toml
- name: Run the action with rust-toolchain set to nightly
uses: ./action/
with:
rust-toolchain: nightly
- name: Get cache key
uses: actions/cache/restore@v3
id: get-cache-key
with:
path: ${{ github.workspace }}/semver-checks/target/semver-checks/cache
fail-on-cache-miss: true
lookup-only: true
key: -${{ github.job }}
restore-keys: |
-${{ github.job }}
- name: Fail if the toolchain used by the action is invalid
if: ${{ ! contains(steps.get-cache-key.outputs.cache-matched-key, 'nightly') }}
run: |
echo "The toolchain used by the action should be nightly, but it is not!"
exit 1
- name: Check the active toolchain
run: |
if ! rustup show active-toolchain | grep -q "beta"; then
echo "The active toolchain should be still beta, but it is $(rustup show active-toolchain)!"
exit 1
fi
test-rust-toolchain-empty:
name: Test whether action works properly with rust-toolchain set to an empty string
runs-on: ubuntu-latest
steps:
- name: Checkout the test repository
uses: actions/checkout@v3
with:
repository: mgr0dzicki/cargo-semver-action-ref-slice
ref: patch_change
- name: Checkout the action
uses: actions/checkout@v3
with:
path: action
- name: Install latest beta
uses: dtolnay/rust-toolchain@beta
- name: Set local override to beta
run: rustup override set beta
- name: Run the action with empty rust-toolchain
uses: ./action/
with:
rust-toolchain: ''
- name: Get cache key
uses: actions/cache/restore@v3
id: get-cache-key
with:
path: ${{ github.workspace }}/semver-checks/target/semver-checks/cache
fail-on-cache-miss: true
lookup-only: true
key: -${{ github.job }}
restore-keys: |
-${{ github.job }}
- name: Fail if the toolchain used by the action is invalid
if: ${{ ! contains(steps.get-cache-key.outputs.cache-matched-key, 'beta') }}
run: |
echo "The toolchain used by the action should be beta, but it is not!"
exit 1
- name: Uninstall Rust
run: rustup self uninstall -y
- name: Run the action with empty rust-toolchain (allowed to fail)
id: action_without_rust
uses: ./action/
with:
rust-toolchain: ''
continue-on-error: true
- name: Fail if the action has not returned any errors (but it should have)
if: steps.action_without_rust.outcome != 'failure'
run: |
echo "Error! The action should have failed because the Rust is not installed and rust-toolchain was set to '', but it has not."
exit 1
test-rust-toolchain-installs-rust:
name: Test whether action installs Rust toolchain by default
runs-on: ubuntu-latest
steps:
- name: Checkout the test repository
uses: actions/checkout@v3
with:
repository: mgr0dzicki/cargo-semver-action-ref-slice
ref: patch_change
- name: Checkout the action
uses: actions/checkout@v3
with:
path: action
- name: Uninstall Rust
run: rustup self uninstall -y
- name: Run the action with default arguments
uses: ./action/

View file

@ -19,6 +19,7 @@ Every argument is optional.
| `package` | The package whose API to check for semver (in Package Id Specification format, see https://doc.rust-lang.org/cargo/reference/pkgid-spec.html for reference). If not set, all packages defined in the Cargo.toml file are processed. | | | `package` | The package whose API to check for semver (in Package Id Specification format, see https://doc.rust-lang.org/cargo/reference/pkgid-spec.html for reference). If not set, all packages defined in the Cargo.toml file are processed. | |
| `manifest-path` | Path to Cargo.toml of crate or workspace to check. If not specified, the action assumes the manifest is under the default [`GITHUB_WORKSPACE`](https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables) path. | | | `manifest-path` | Path to Cargo.toml of crate or workspace to check. If not specified, the action assumes the manifest is under the default [`GITHUB_WORKSPACE`](https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables) path. | |
| `verbose` | Enables verbose output of `cargo-semver-checks`. | `false` | | `verbose` | Enables verbose output of `cargo-semver-checks`. | `false` |
| `rust-toolchain` | Rust toolchain name to use, e.g. `stable`, `nightly` or `1.68.0`. It will be installed if necessary and used regardless of local overrides and the `rust-toolchain.toml` file. However, if the input is set to be an empty string, the action assumes some Rust toolchain is already installed and uses the default one. | `stable` |
| `cache-key` | Rustdoc baseline is cached separately for runs that differ in at least one of the following: runner OS, `rustc` version, `cargo-semver-checks` version, or any of the `Cargo.lock` files in the current workspace. This input might be used to further separate the caches by providing the unique key (by default the job name) for each of them. | `${{ github.job }}` | | `cache-key` | Rustdoc baseline is cached separately for runs that differ in at least one of the following: runner OS, `rustc` version, `cargo-semver-checks` version, or any of the `Cargo.lock` files in the current workspace. This input might be used to further separate the caches by providing the unique key (by default the job name) for each of them. | `${{ github.job }}` |
| `prefix-key` | Additional prefix of the cache key, can be set to start a new cache manually. | | | `prefix-key` | Additional prefix of the cache key, can be set to start a new cache manually. | |
| `github-token` | The `GITHUB_TOKEN` secret used to download precompiled binaries from GitHub API. If not specified, the [automatic GitHub token](https://docs.github.com/en/actions/security-guides/automatic-token-authentication) provided to the workflow will be used. The token may be alternatively passed in an environment variable `GITHUB_TOKEN`. | `${{ github.token }}` | | `github-token` | The `GITHUB_TOKEN` secret used to download precompiled binaries from GitHub API. If not specified, the [automatic GitHub token](https://docs.github.com/en/actions/security-guides/automatic-token-authentication) provided to the workflow will be used. The token may be alternatively passed in an environment variable `GITHUB_TOKEN`. | `${{ github.token }}` |

View file

@ -15,6 +15,10 @@ inputs:
type: boolean type: boolean
required: true required: true
default: false default: false
rust-toolchain:
description: 'Rust toolchain name to use, e.g. `stable`, `nightly` or `1.68.0`. It will be installed if necessary and used regardless of local overrides and the `rust-toolchain.toml` file. However, if the input is set to be an empty string, the action assumes some Rust toolchain is already installed and uses the default one.'
required: false
default: 'stable'
cache-key: cache-key:
description: 'Rustdoc baseline is cached separately for runs that differ in at least one of the following: runner OS, `rustc` version, `cargo-semver-checks` version, or any of the `Cargo.lock` files in the current workspace. This input might be used to further separate the caches by providing the unique key (by default the job name) for each of them.' description: 'Rustdoc baseline is cached separately for runs that differ in at least one of the following: runner OS, `rustc` version, `cargo-semver-checks` version, or any of the `Cargo.lock` files in the current workspace. This input might be used to further separate the caches by providing the unique key (by default the job name) for each of them.'
required: false required: false

2
dist/index.js vendored

File diff suppressed because one or more lines are too long

View file

@ -72,11 +72,18 @@ async function getCargoSemverChecksDownloadURL(target: string): Promise<string>
return asset.url; return asset.url;
} }
async function installRustUp(): Promise<void> { async function installRustUpIfRequested(): Promise<void> {
const rustup = await rustCore.RustUp.getOrInstall(); const toolchain = rustCore.input.getInput("rust-toolchain");
await rustup.call(["show"]); if (toolchain) {
await rustup.setProfile("minimal"); const rustup = await rustCore.RustUp.getOrInstall();
await rustup.installToolchain("stable"); await rustup.call(["show"]);
await rustup.setProfile("minimal");
await rustup.installToolchain(toolchain);
// Setting the environment variable here affects only processes spawned
// by this action, so it will not override the default toolchain globally.
process.env["RUSTUP_TOOLCHAIN"] = toolchain;
}
} }
async function runCargoSemverChecks(cargo: rustCore.Cargo): Promise<void> { async function runCargoSemverChecks(cargo: rustCore.Cargo): Promise<void> {
@ -127,7 +134,7 @@ async function run(): Promise<void> {
const manifestPath = path.resolve(rustCore.input.getInput("manifest-path") || "./"); const manifestPath = path.resolve(rustCore.input.getInput("manifest-path") || "./");
const manifestDir = path.extname(manifestPath) ? path.dirname(manifestPath) : manifestPath; const manifestDir = path.extname(manifestPath) ? path.dirname(manifestPath) : manifestPath;
await installRustUp(); await installRustUpIfRequested();
const cargo = await rustCore.Cargo.get(); const cargo = await rustCore.Cargo.get();