Skip to content

Commit

Permalink
fix: proper support of toolchain with cargo-hack (#204)
Browse files Browse the repository at this point in the history
* chore: update `rs-actions-core` to 5.0.0, run `npm update`

* fix: proper support of `toolchain` with `cargo-hack`

* chore(ci): test `toolchain` with non-`cargo` tools

* chore(ci): do not use rust-cache on CI to test our own caching

* chore: add JS tests for `run`

* chore: fix `package-lock.json`
  • Loading branch information
clechasseur authored Jan 10, 2025
1 parent 8653300 commit ba11837
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 65 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ jobs:
with:
node-version-file: '.nvmrc'
cache: 'npm'
- uses: actions-rust-lang/setup-rust-toolchain@11df97af8e8102fd60b60a77dfbf58d40cd843b8 # v1.10.1
with:
components: clippy,rustfmt
matcher: false
rustflags: ""
target: x86_64-unknown-linux-gnu,aarch64-unknown-linux-gnu
- run: npm ci
- run: npm run lint
- run: npm run build
Expand Down Expand Up @@ -60,9 +66,11 @@ jobs:
- uses: ./
with:
command: build
toolchain: stable
args: --target aarch64-unknown-linux-gnu
working-directory: rust_tests/working
tool: cross
cache-key: 'rs-cargo-tests'

rust-tests-cargo-hack:
runs-on: ubuntu-24.04
Expand All @@ -75,15 +83,18 @@ jobs:
- uses: ./
with:
command: check
toolchain: stable
working-directory: rust_tests/working
tool: cargo-hack
cache-key: 'rs-cargo-tests'

rust-clippy-warnings:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- uses: actions-rust-lang/setup-rust-toolchain@11df97af8e8102fd60b60a77dfbf58d40cd843b8 # v1.10.1
with:
components: clippy
matcher: false
rustflags: ""
- uses: ./
Expand All @@ -97,6 +108,7 @@ jobs:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- uses: actions-rust-lang/setup-rust-toolchain@11df97af8e8102fd60b60a77dfbf58d40cd843b8 # v1.10.1
with:
components: rustfmt
matcher: false
rustflags: ""
- uses: ./
Expand Down
82 changes: 82 additions & 0 deletions __tests__/run.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { Input } from "../src/input";
import { run } from "../src/run";

const SECONDS = 1000;

describe("run", () => {
it.each([
{
name: "rust-tests-working",
input: {
command: "check",
toolchain: "stable",
args: ["--package", "working", "--all-targets", "--all-features"],
},
},
{
name: "rust-tests-not-working-default-features",
input: {
command: "build",
args: ["--package", "not_working"],
},
},
{
name: "rust-tests-not-working-all-features",
input: {
command: "build",
args: ["--package", "not_working", "--all-features"],
},
shouldThrow: true,
},
{
name: "rust-tests-cross",
input: {
command: "build",
toolchain: "stable",
args: ["--target", "aarch64-unknown-linux-gnu"],
workingDirectory: "rust_tests/working",
tool: "cross",
},
},
{
name: "rust-tests-cargo-hack",
input: {
command: "check",
toolchain: "stable",
args: [],
workingDirectory: "rust_tests/working",
tool: "cargo-hack",
},
},
{
name: "rust-clippy-warnings",
input: {
command: "clippy",
args: ["--package", "clippy_warnings"],
},
},
{
name: " rust-fmt-warnings",
input: {
command: "fmt",
args: ["--package", "fmt_warnings", "--check"],
},
shouldThrow: true,
},
])(
"$name",
async ({ input, shouldThrow }: { input: Input; shouldThrow?: boolean }) => {
const inputWithoutCache: Input = {
...input,
...(!process.env.CI && { cacheKey: "no-cache" }),
};

if (shouldThrow) {
await expect(run(inputWithoutCache)).rejects.toThrow();
} else {
await expect(run(inputWithoutCache)).resolves.not.toThrow();
}
},
240 * SECONDS,
);
});
4 changes: 2 additions & 2 deletions dist/index.js

Large diffs are not rendered by default.

50 changes: 29 additions & 21 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,40 @@ import { FlatCompat } from "@eslint/eslintrc";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
allConfig: js.configs.all
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
allConfig: js.configs.all
});

export default [...compat.extends(
"eslint:recommended",
"plugin:@typescript-eslint/recommended-type-checked",
"plugin:@typescript-eslint/stylistic-type-checked",
"plugin:prettier/recommended",
"eslint:recommended",
"plugin:@typescript-eslint/recommended-type-checked",
"plugin:@typescript-eslint/stylistic-type-checked",
"plugin:prettier/recommended",
), {
plugins: {
"@typescript-eslint": typescriptEslint,
},
plugins: {
"@typescript-eslint": typescriptEslint,
},

languageOptions: {
parser: tsParser,
ecmaVersion: 5,
sourceType: "script",
languageOptions: {
parser: tsParser,
ecmaVersion: 5,
sourceType: "script",

parserOptions: {
project: "./tsconfig.eslint.json",
},
parserOptions: {
project: "./tsconfig.eslint.json",
},
},

rules: {
"@typescript-eslint/ban-ts-ignore": "off",
},
}];
rules: {
"@typescript-eslint/ban-ts-ignore": "off",
"@typescript-eslint/no-unused-vars": [
"warn",
{
argsIgnorePattern: "^_",
varsIgnorePattern: "^_",
caughtErrorsIgnorePattern: "^_",
},
],
},
}];
20 changes: 11 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rs-cargo",
"version": "3.0.0",
"version": "3.0.1",
"private": false,
"description": "Run cargo command",
"main": "dist/index.js",
Expand Down Expand Up @@ -32,10 +32,12 @@
"url": "https://github.com/clechasseur/rs-cargo/issues"
},
"dependencies": {
"@clechasseur/rs-actions-core": "^4.0.2",
"@clechasseur/rs-actions-core": "^5.0.0",
"string-argv": "^0.3.2"
},
"devDependencies": {
"@eslint/js": "^9.17.0",
"@eslint/eslintrc": "^3.2.0",
"@types/jest": "^29.5.14",
"@types/node": "^20.17.12",
"@typescript-eslint/eslint-plugin": "^8.19.1",
Expand Down
32 changes: 1 addition & 31 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,10 @@
import path from "path";

import * as core from "@actions/core";
import * as exec from "@actions/exec";
import { issueCommand } from "@actions/core/lib/command";

import * as input from "./input";
import { Cargo, Cross, CargoHack } from "@clechasseur/rs-actions-core";

async function getProgram(actionInput: input.Input) {
switch (actionInput.tool) {
case "cross":
return await Cross.getOrInstall(actionInput.cacheKey);
case "cargo-hack":
return await CargoHack.getOrInstall(actionInput.cacheKey);
default:
return await Cargo.get();
}
}

export async function run(actionInput: input.Input): Promise<void> {
const program = await getProgram(actionInput);

let args: string[] = [];
if (actionInput.toolchain) {
args.push(`+${actionInput.toolchain}`);
}
args.push(actionInput.command);
args = args.concat(actionInput.args);

const options: exec.ExecOptions = {};
if (actionInput.workingDirectory) {
options.cwd = path.join(process.cwd(), actionInput.workingDirectory);
}

await program.call(args, options);
}
import { run } from "./run";

async function main(): Promise<void> {
// Note: the matchers used here were copied from here:
Expand Down
46 changes: 46 additions & 0 deletions src/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import path from "path";

import * as exec from "@actions/exec";

import * as input from "./input";
import {
Cargo,
CargoHack,
CargoHackOptions,
Cross,
CrossOptions,
} from "@clechasseur/rs-actions-core";

export async function getProgram(actionInput: input.Input) {
switch (actionInput.tool) {
case "cross": {
const options: CrossOptions = {
toolchain: actionInput.toolchain,
primaryKey: actionInput.cacheKey,
};
return await Cross.getOrInstall(options);
}
case "cargo-hack": {
const options: CargoHackOptions = {
toolchain: actionInput.toolchain,
primaryKey: actionInput.cacheKey,
};
return await CargoHack.getOrInstall(options);
}
default:
return await Cargo.get(actionInput.toolchain);
}
}

export async function run(actionInput: input.Input): Promise<void> {
const program = await getProgram(actionInput);

const args = [actionInput.command, ...actionInput.args];

const options: exec.ExecOptions = {};
if (actionInput.workingDirectory) {
options.cwd = path.join(process.cwd(), actionInput.workingDirectory);
}

await program.call(args, options);
}

0 comments on commit ba11837

Please sign in to comment.