Move unit-tested modules into utils file

This fixes the issue where running the unit tests invoked the entire action since index.ts calls `main()` at the top-level scope.
This commit is contained in:
Cina Saffary 2023-08-29 18:16:41 -05:00
parent 5517edbb28
commit 2962e94ac8
6 changed files with 39 additions and 60 deletions

View file

@ -1 +0,0 @@
process.env.INPUT_QUIET ??= "false";

View file

@ -9,8 +9,7 @@ import {
getBooleanInput,
} from "@actions/core";
import { execSync, exec } from "node:child_process";
import { existsSync } from "node:fs";
import * as path from "node:path";
import { checkWorkingDirectory, getNpxCmd, semverCompare } from "./utils";
import * as util from "node:util";
const execAsync = util.promisify(exec);
@ -31,10 +30,6 @@ const config = {
QUIET_MODE: getBooleanInput("quiet"),
} as const;
function getNpxCmd() {
return process.env.RUNNER_OS === "Windows" ? "npx.cmd" : "npx";
}
function info(message: string, bypass?: boolean): void {
if (!config.QUIET_MODE || bypass) {
originalInfo(message);
@ -59,26 +54,6 @@ function endGroup(): void {
}
}
/**
* A helper function to compare two semver versions. If the second arg is greater than the first arg, it returns true.
*/
function semverCompare(version1: string, version2: string) {
if (version2 === "latest") return true;
const version1Parts = version1.split(".");
const version2Parts = version2.split(".");
for (const version1Part of version1Parts) {
const version2Part = version2Parts.shift();
if (version1Part !== version2Part && version2Part) {
return version1Part < version2Part ? true : false;
}
}
return false;
}
async function main() {
try {
installWrangler();
@ -112,15 +87,6 @@ async function runProcess(
}
}
function checkWorkingDirectory(workingDirectory = ".") {
const normalizedPath = path.normalize(workingDirectory);
if (existsSync(normalizedPath)) {
return normalizedPath;
} else {
throw new Error(`Directory ${workingDirectory} does not exist.`);
}
}
function installWrangler() {
if (config["WRANGLER_VERSION"].startsWith("1")) {
throw new Error(
@ -310,7 +276,4 @@ export {
uploadSecrets,
authenticationSetup,
installWrangler,
checkWorkingDirectory,
getNpxCmd,
semverCompare,
};

View file

@ -1,18 +1,7 @@
import { expect, test, describe } from "vitest";
import { checkWorkingDirectory, getNpxCmd, semverCompare } from "./index";
import { checkWorkingDirectory, getNpxCmd, semverCompare } from "./utils";
import path from "node:path";
const config = {
WRANGLER_VERSION: "mockVersion",
secrets: ["mockSercret", "mockSecretAgain"],
workingDirectory: "./mockWorkingDirectory",
CLOUDFLARE_API_TOKEN: "mockAPIToken",
CLOUDFLARE_ACCOUNT_ID: "mockAccountID",
ENVIRONMENT: undefined,
VARS: ["mockVar", "mockVarAgain"],
COMMANDS: ["mockCommand", "mockCommandAgain"],
};
test("getNpxCmd ", async () => {
process.env.RUNNER_OS = "Windows";
expect(getNpxCmd()).toBe("npx.cmd");

35
src/utils.ts Normal file
View file

@ -0,0 +1,35 @@
import { existsSync } from "node:fs";
import * as path from "node:path";
export function getNpxCmd() {
return process.env.RUNNER_OS === "Windows" ? "npx.cmd" : "npx";
}
/**
* A helper function to compare two semver versions. If the second arg is greater than the first arg, it returns true.
*/
export function semverCompare(version1: string, version2: string) {
if (version2 === "latest") return true;
const version1Parts = version1.split(".");
const version2Parts = version2.split(".");
for (const version1Part of version1Parts) {
const version2Part = version2Parts.shift();
if (version1Part !== version2Part && version2Part) {
return version1Part < version2Part ? true : false;
}
}
return false;
}
export function checkWorkingDirectory(workingDirectory = ".") {
const normalizedPath = path.normalize(workingDirectory);
if (existsSync(normalizedPath)) {
return normalizedPath;
} else {
throw new Error(`Directory ${workingDirectory} does not exist.`);
}
}

View file

@ -9,12 +9,12 @@
"isolatedModules": true,
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "NodeNext",
"moduleResolution": "Bundler",
"rootDir": "./src",
"outDir": "./dist",
"lib": ["ESNext"],
"types": ["node", "@cloudflare/workers-types"]
},
"exclude": ["node_modules", "**/*.test.ts"],
"include": ["src/index.ts"]
"include": ["src"]
}

View file

@ -1,7 +0,0 @@
import { defineConfig } from "vitest/dist/config";
export default defineConfig({
test: {
setupFiles: ["./action-env-setup.ts"],
},
});