Skip to content
Open
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
23 changes: 23 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { createDefaultPreset } from "ts-jest";

const presetConfig = createDefaultPreset();

const jestConfig = {
...presetConfig,
resetMocks: true,
testEnvironment: "node",
extensionsToTreatAsEsm: [".ts"],
moduleNameMapper: {
"^(\\.{1,2}/.*)\\.js$": "$1",
},
transform: {
"^.+\\.ts$": [
"ts-jest",
{
useESM: true,
},
],
},
};

export default jestConfig;
13 changes: 0 additions & 13 deletions jest.config.ts

This file was deleted.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "stack-action",
"version": "0.0.0",
"type": "module",
"description": "Build and test stack-based Haskell projects",
"main": "lib/main.js",
"scripts": {
"build": "tsc && ncc build lib/main.js && sed -i 's/\\x0D$//' ./dist/index.js",
"format": "prettier --write \"**/*.ts\"",
"format-check": "prettier --check \"**/*.ts\"",
"test": "jest",
"test": "NODE_OPTIONS='--experimental-vm-modules' jest",
"readme": "npx action-docs -u && prettier --write README.md"
},
"repository": {
Expand Down
4 changes: 3 additions & 1 deletion src/dirty-files.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { parseGitStatus, isInterestingFile } from "./dirty-files";
import { jest } from "@jest/globals";

import { parseGitStatus, isInterestingFile } from "./dirty-files.js";

describe("parseGitStatus", () => {
test("parse file name, and filters untracked", () => {
Expand Down
4 changes: 3 additions & 1 deletion src/envsubst.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { envsubst } from "./envsubst";
import { jest } from "@jest/globals";

import { envsubst } from "./envsubst.js";

const HOME = process.env.HOME;

Expand Down
4 changes: 3 additions & 1 deletion src/get-cache-keys.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { getCacheKeys } from "./get-cache-keys";
import { jest } from "@jest/globals";

import { getCacheKeys } from "./get-cache-keys.js";

test("getCacheKeys", () => {
const keys = getCacheKeys(["prefix-os-compiler", "package", "source"]);
Expand Down
2 changes: 1 addition & 1 deletion src/hie.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from "fs";
import * as core from "@actions/core";

import { StackCLI } from "./stack-cli";
import { StackCLI } from "./stack-cli.js"

export const HIE_YAML: string = "hie.yaml";

Expand Down
4 changes: 2 additions & 2 deletions src/inputs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as core from "@actions/core";
import * as Shellwords from "shellwords-ts";
import { envsubst } from "./envsubst";
import { type OnDirtyFiles, parseOnDirtyFiles } from "./dirty-files";
import { envsubst } from "./envsubst.js"
import { type OnDirtyFiles, parseOnDirtyFiles } from "./dirty-files.js"

export type Inputs = {
workingDirectory: string | null;
Expand Down
16 changes: 8 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as core from "@actions/core";

import { checkDirtyFiles } from "./dirty-files";
import { StackCLI } from "./stack-cli";
import { getCacheKeys } from "./get-cache-keys";
import { hashProject } from "./hash-project";
import { getInputs } from "./inputs";
import { readStackYamlSync, getStackDirectories } from "./stack-yaml";
import { DEFAULT_CACHE_OPTIONS, withCache } from "./with-cache";
import { GenHIE } from "./hie";
import { checkDirtyFiles } from "./dirty-files.js"
import { StackCLI } from "./stack-cli.js"
import { getCacheKeys } from "./get-cache-keys.js"
import { hashProject } from "./hash-project.js"
import { getInputs } from "./inputs.js"
import { readStackYamlSync, getStackDirectories } from "./stack-yaml.js"
import { DEFAULT_CACHE_OPTIONS, withCache } from "./with-cache.js"
import { GenHIE } from "./hie.js"

async function run() {
try {
Expand Down
4 changes: 3 additions & 1 deletion src/parse-stack-path.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { parseStackPath } from "./parse-stack-path";
import { jest } from "@jest/globals";

import { parseStackPath } from "./parse-stack-path.js";

const EXAMPLE = [
"snapshot-doc-root: /home/patrick/.stack/snapshots/x86_64-linux-tinfo6/0dd02c1d8a380045321779c4567c2aa8873910743eac342f72d56f3d26881028/9.2.7/doc",
Expand Down
4 changes: 3 additions & 1 deletion src/parse-stack-query.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { parseStackQuery } from "./parse-stack-query";
import { jest } from "@jest/globals";

import { parseStackQuery } from "./parse-stack-query.js";

const EXAMPLE = [
"compiler:",
Expand Down
27 changes: 17 additions & 10 deletions src/stack-cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import * as exec from "@actions/exec";
import { ExecOptions } from "@actions/exec";
import { jest } from "@jest/globals";

import { StackCLI } from "./stack-cli";
import { ExecDelegate, StackCLI } from "./stack-cli.js";

jest.spyOn(exec, "exec");
const exec: ExecDelegate = {
exec: jest.fn((command: string, args: string[], options?: ExecOptions) =>
Promise.resolve(0),
),
};

describe("StackCLI", () => {
test("Respects --resolver given", async () => {
const stackCLI = new StackCLI(["--resolver", "lts"], false);
const stackCLI = new StackCLI(["--resolver", "lts"], false, exec);

await stackCLI.setup([]);

Expand All @@ -21,6 +26,7 @@ describe("StackCLI", () => {
const stackCLI = new StackCLI(
["--stack-yaml", "sub/stack-nightly.yaml"],
false,
exec,
);

await stackCLI.setup([]);
Expand All @@ -47,6 +53,7 @@ describe("StackCLI", () => {
"nightly-20240201",
],
false,
exec,
);

await stackCLI.setup([]);
Expand All @@ -65,7 +72,7 @@ describe("StackCLI", () => {
});

test("installCompilerTools", async () => {
const stackCLI = new StackCLI([], false);
const stackCLI = new StackCLI([], false, exec);
await stackCLI.installCompilerTools(["hlint", "weeder"]);

expect(exec.exec).toHaveBeenCalledWith(
Expand All @@ -76,14 +83,14 @@ describe("StackCLI", () => {
});

test("installCompilerTools with empty arguments", async () => {
const stackCLI = new StackCLI([], false);
const stackCLI = new StackCLI([], false, exec);
await stackCLI.installCompilerTools([]);

expect(exec.exec).not.toHaveBeenCalled();
});

test("buildDependencies", async () => {
const stackCLI = new StackCLI([], false);
const stackCLI = new StackCLI([], false, exec);

await stackCLI.buildDependencies(["--coverage"]);

Expand All @@ -101,7 +108,7 @@ describe("StackCLI", () => {
});

test("buildNoTest", async () => {
const stackCLI = new StackCLI([], false);
const stackCLI = new StackCLI([], false, exec);

await stackCLI.buildNoTest(["--coverage"]);

Expand All @@ -113,7 +120,7 @@ describe("StackCLI", () => {
});

test("buildTest", async () => {
const stackCLI = new StackCLI([], false);
const stackCLI = new StackCLI([], false, exec);

await stackCLI.buildTest(["--coverage"]);

Expand All @@ -125,7 +132,7 @@ describe("StackCLI", () => {
});

test("build", async () => {
const stackCLI = new StackCLI([], false);
const stackCLI = new StackCLI([], false, exec);

await stackCLI.build(["--coverage"]);

Expand Down
28 changes: 17 additions & 11 deletions src/stack-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import * as fs from "fs";
import * as path from "path";
import { devNull } from "os";
import type { ExecOptions } from "@actions/exec";
import * as exec from "@actions/exec";
import * as realExec from "@actions/exec";

import type { StackPath } from "./parse-stack-path";
import { parseStackPath } from "./parse-stack-path";
import type { StackQuery } from "./parse-stack-query";
import { parseStackQuery } from "./parse-stack-query";
import type { StackPath } from "./parse-stack-path.js";
import { parseStackPath } from "./parse-stack-path.js";
import type { StackQuery } from "./parse-stack-query.js";
import { parseStackQuery } from "./parse-stack-query.js";

export interface ExecDelegate {
exec: (
Expand All @@ -23,10 +23,12 @@ export class StackCLI {

private debug: boolean;
private globalArgs: string[];
private execImpl: ExecDelegate;

constructor(args: string[], debug?: boolean) {
constructor(args: string[], debug?: boolean, exec?: ExecDelegate) {
this.debug = debug ?? false;
this.globalArgs = args;
this.execImpl = exec ?? realExec;

// Capture --stack-yaml if given
const stackYamlIdx = args.indexOf("--stack-yaml");
Expand All @@ -49,7 +51,7 @@ export class StackCLI {
}

async installed(): Promise<boolean> {
const ec = await exec.exec("which", ["stack"], {
const ec = await this.execImpl.exec("which", ["stack"], {
silent: true,
ignoreReturnCode: true,
});
Expand All @@ -59,14 +61,14 @@ export class StackCLI {
async install(): Promise<void> {
const url = "https://get.haskellstack.org";
const tmp = "install-stack.sh";
await exec.exec("curl", ["-sSL", "-o", tmp, url]);
await exec.exec("sh", [tmp]);
await this.execImpl.exec("curl", ["-sSL", "-o", tmp, url]);
await this.execImpl.exec("sh", [tmp]);
fs.rmSync(tmp);
}

async upgrade(): Promise<number> {
// Avoid this.exec because we don't need/want globalArgs
return await exec.exec("stack", ["upgrade"]);
return await this.execImpl.exec("stack", ["upgrade"]);
}

async setup(args: string[]): Promise<number> {
Expand Down Expand Up @@ -139,6 +141,10 @@ export class StackCLI {
}

private async exec(args: string[], options?: ExecOptions): Promise<number> {
return await exec.exec("stack", this.globalArgs.concat(args), options);
return await this.execImpl.exec(
"stack",
this.globalArgs.concat(args),
options,
);
}
}
6 changes: 4 additions & 2 deletions src/stack-yaml.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { parseStackYaml, getStackDirectories } from "./stack-yaml";
import { StackCLI } from "./stack-cli";
import { jest } from "@jest/globals";

import { parseStackYaml, getStackDirectories } from "./stack-yaml.js";
import { StackCLI } from "./stack-cli.js";

const testStackRoot = "/home/me/.stack";
const testPrograms = `${testStackRoot}/programs/x86_64-linux`;
Expand Down
2 changes: 1 addition & 1 deletion src/stack-yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as fs from "fs";
import { join as pathJoin } from "path";
import * as yaml from "js-yaml";

import { StackCLI } from "./stack-cli";
import { StackCLI } from "./stack-cli.js"

export type StackYaml = {
resolver: string;
Expand Down
Loading
Loading