Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing tool tests #795

Merged
merged 4 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions tests/driver/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export interface SetupSettings {
trunkVersion?: string;
}

export type CustomExecOptions = ExecOptions & { stdin?: string };

const execFilePromise = util.promisify(execFile);

const TEMP_SUBDIR = "tmp";
Expand Down Expand Up @@ -378,13 +380,41 @@ export abstract class GenericTrunkDriver {
* Run a command inside the sandbox test repo.
* @param bin command to run
* @param args arguments to run
* @param execOptions
* @param execOptions options to pass the stdin to exec
*/
async run(bin: string, args: string[], execOptions?: ExecOptions) {
return await execFilePromise(bin, args, {
async run(bin: string, args: string[], execOptions?: CustomExecOptions) {
const exec = execFile(bin, args, {
cwd: this.sandboxPath,
env: executionEnv(this.sandboxPath ?? ""),
...execOptions,
});
exec.stdin?.write(execOptions?.stdin ?? "");
exec.stdin?.end();

return await new Promise<{ stdout: string; stderr: string }>((resolve, reject) => {
let stdout = "";
let stderr = "";

exec.stdout?.on("data", (chunk: string) => {
stdout += chunk;
});

exec.stderr?.on("data", (chunk: string) => {
stderr += chunk;
});

exec.on("error", (err: any) => {
// trunk-ignore(eslint/@typescript-eslint/prefer-promise-reject-errors)
reject(err);
});
exec.on("exit", (code: number) => {
if (code === 0) {
resolve({ stdout, stderr });
} else {
// trunk-ignore(eslint/@typescript-eslint/prefer-promise-reject-errors)
reject({ error: new Error(`Process exited with code ${code}`), code, stdout, stderr });
}
});
});
}
}
12 changes: 7 additions & 5 deletions tests/driver/tool_driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,17 @@ lint:

/**** Execution methods ****/

async runTool(command: string[]): Promise<TrunkToolRunResult> {
async runTool(command: string[], stdin?: string): Promise<TrunkToolRunResult> {
const tools_subdir = fs.existsSync(path.resolve(this.sandboxPath ?? "", ".trunk/dev-tools"))
? "dev-tools"
: "tools";
try {
if (process.platform == "win32") {
const { stdout, stderr } = await this.run("powershell", [
`.trunk/${tools_subdir}/${command[0]}.bat`,
...command.slice(1),
]);
const { stdout, stderr } = await this.run(
"powershell",
[`.trunk/${tools_subdir}/${command[0]}.bat`, ...command.slice(1)],
{ stdin },
);
return {
exitCode: 0,
stdout,
Expand All @@ -215,6 +216,7 @@ lint:
const { stdout, stderr } = await this.run(
`.trunk/${tools_subdir}/${command[0]}`,
command.slice(1),
{ stdin },
);
return {
exitCode: 0,
Expand Down
7 changes: 5 additions & 2 deletions tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,18 +267,21 @@ interface ToolTestConfig {
expectedOut?: string;
expectedErr?: string;
expectedExitCode?: number;
stdin?: string;
}

export const makeToolTestConfig = ({
command,
expectedOut = "",
expectedErr = "",
expectedExitCode = 0,
stdin = "",
}: ToolTestConfig) => ({
command,
expectedOut,
expectedErr,
expectedExitCode,
stdin,
});

export const toolTest = ({
Expand All @@ -298,9 +301,9 @@ export const toolTest = ({
}) => {
describe(`Testing tool ${toolName}`, () => {
const driver = setupTrunkToolDriver(dirName, {}, toolName, toolVersion, preCheck);
testConfigs.forEach(({ command, expectedOut, expectedErr, expectedExitCode }) => {
testConfigs.forEach(({ command, expectedOut, expectedErr, expectedExitCode, stdin }) => {
conditionalTest(skipTestIf(toolVersion), command.join(" "), async () => {
const { stdout, stderr, exitCode } = await driver.runTool(command);
const { stdout, stderr, exitCode } = await driver.runTool(command, stdin);
expect(stdout).toContain(expectedOut);
expect(stderr).toContain(expectedErr);
expect(exitCode).toEqual(expectedExitCode);
Expand Down
33 changes: 30 additions & 3 deletions tools/diff-so-fancy/diff_so_fancy.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
import { toolInstallTest } from "tests";
import { makeToolTestConfig, toolTest } from "tests";

// TODO(Tyler): tool def is missing healthcheck
toolInstallTest({
const sampleDiff = `diff --git a/.trunk/trunk.yaml b/.trunk/trunk.yaml
index dc4f1f8..fb8fe54 100644
--- a/.trunk/trunk.yaml
+++ b/.trunk/trunk.yaml
@@ -13,3 +13,6 @@ lint:
- node_modules/**
- .trunk/configs/**
- .gitattributes
+tools:
+ enabled:
+ - [email protected]`;

let exitCode = 25;
if (process.platform === "darwin") {
exitCode = 102;
} else if (process.platform === "win32") {
exitCode = 0;
}
// diff-so-fancy returns a nonzero exit code for its version command, so we have to
// use the custom test constructor.
toolTest({
toolName: "diff-so-fancy",
toolVersion: "1.4.3",
testConfigs: [
makeToolTestConfig({
command: ["diff-so-fancy"],
expectedExitCode: exitCode,
expectedOut: "modified:",
stdin: sampleDiff,
}),
],
});
1 change: 0 additions & 1 deletion tools/difft/difft.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { toolInstallTest } from "tests";

// TODO(Tyler): tool def is missing healthcheck
toolInstallTest({
toolName: "difft",
toolVersion: "0.56.1",
Expand Down
3 changes: 3 additions & 0 deletions tools/difft/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ tools:
download: difft
known_good_version: 0.56.1
shims: [difft]
health_checks:
- command: difft --version
parse_regex: Difftastic ${semver}
7 changes: 4 additions & 3 deletions tools/gk/gk.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { toolInstallTest } from "tests";
import { makeToolTestConfig, toolTest } from "tests";

// TODO(Tyler): tool def is missing healthcheck
toolInstallTest({
// No version command for gk
toolTest({
toolName: "gk",
toolVersion: "1.2.2",
testConfigs: [makeToolTestConfig({ command: ["gk", "-h"], expectedErr: "Usage" })],
});
8 changes: 5 additions & 3 deletions tools/goreleaser/goreleaser.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { toolInstallTest } from "tests";
import { makeToolTestConfig, toolTest } from "tests";
import { osTimeoutMultiplier } from "tests/utils";

// This install is quite slow on some Linux machines.
jest.setTimeout(600000 * osTimeoutMultiplier);

// TODO(Tyler): tool def is missing healthcheck
toolInstallTest({
toolTest({
toolName: "goreleaser",
toolVersion: "1.25.1",
testConfigs: [
makeToolTestConfig({ command: ["goreleaser", "--version"], expectedOut: "goreleaser" }),
],
});
3 changes: 3 additions & 0 deletions tools/tsc/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ tools:
package: typescript
known_good_version: 5.2.2
shims: [tsc]
health_checks:
- command: tsc --version
parse_regex: Version ${semver}
1 change: 0 additions & 1 deletion tools/tsc/tsc.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { toolInstallTest } from "tests";
import { skipOS } from "tests/utils";

// TODO(Tyler): tool def is missing healthcheck
toolInstallTest({
toolName: "tsc",
toolVersion: "5.2.2",
Expand Down
9 changes: 6 additions & 3 deletions tools/yq/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ downloads:
cpu:
x86_64: 386
arm_64: arm64
url: https://github.com/mikefarah/yq/releases/download/v${version}/yq_${os}_${cpu}.tar.gz
url: https://github.com/mikefarah/yq/releases/download/v${version}/yq_${os}_${cpu}
- os:
windows: windows
cpu:
x86_64: 386
arm_64: arm64
url: https://github.com/mikefarah/yq/releases/download/v${version}/yq_${os}_${cpu}.zip
url: https://github.com/mikefarah/yq/releases/download/v${version}/yq_${os}_${cpu}.exe
tools:
definitions:
- name: yq
download: yq
known_good_version: 4.40.5
known_good_version: 4.44.1
shims: [yq]
health_checks:
- command: yq --version
parse_regex: version v${semver}
4 changes: 2 additions & 2 deletions tools/yq/yq.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { toolInstallTest } from "tests";

// TODO(Tyler): tool def is missing healthcheck
// The binary name varies by platform so we can't roll this into a health_check as-is.
toolInstallTest({
toolName: "yq",
toolVersion: "4.40.5",
toolVersion: "4.44.1",
});
Loading