Skip to content

Commit

Permalink
chore: add JS tests for run
Browse files Browse the repository at this point in the history
  • Loading branch information
clechasseur committed Jan 10, 2025
1 parent f2cb12d commit 27b95aa
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 75 deletions.
13 changes: 6 additions & 7 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 All @@ -26,8 +32,6 @@ jobs:
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- uses: actions-rust-lang/setup-rust-toolchain@11df97af8e8102fd60b60a77dfbf58d40cd843b8 # v1.10.1
with:
cache: 'false'
- uses: ./
with:
command: check
Expand All @@ -42,7 +46,6 @@ jobs:
with:
matcher: false
rustflags: ""
cache: 'false'
- uses: ./
with:
command: build
Expand All @@ -60,7 +63,6 @@ jobs:
- uses: actions-rust-lang/setup-rust-toolchain@11df97af8e8102fd60b60a77dfbf58d40cd843b8 # v1.10.1
with:
target: aarch64-unknown-linux-gnu
cache: 'false'
- uses: ./
with:
command: build
Expand All @@ -78,7 +80,6 @@ jobs:
with:
matcher: false
rustflags: ""
cache: 'false'
- uses: ./
with:
command: check
Expand All @@ -96,7 +97,6 @@ jobs:
components: clippy
matcher: false
rustflags: ""
cache: 'false'
- uses: ./
with:
command: clippy
Expand All @@ -111,7 +111,6 @@ jobs:
components: rustfmt
matcher: false
rustflags: ""
cache: 'false'
- uses: ./
with:
command: fmt
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,
);
});
2 changes: 1 addition & 1 deletion 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: "^_",
},
],
},
}];
6 changes: 3 additions & 3 deletions package-lock.json

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

4 changes: 3 additions & 1 deletion 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 @@ -36,6 +36,8 @@
"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
43 changes: 1 addition & 42 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,51 +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,
CargoHack,
CargoHackOptions,
Cross,
CrossOptions,
} from "@clechasseur/rs-actions-core";

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);
}
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 27b95aa

Please sign in to comment.