mirror of
https://github.com/cloudflare/wrangler-action.git
synced 2024-11-21 09:33:23 +01:00
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:
parent
5517edbb28
commit
2962e94ac8
6 changed files with 39 additions and 60 deletions
|
@ -1 +0,0 @@
|
||||||
process.env.INPUT_QUIET ??= "false";
|
|
39
src/index.ts
39
src/index.ts
|
@ -9,8 +9,7 @@ import {
|
||||||
getBooleanInput,
|
getBooleanInput,
|
||||||
} from "@actions/core";
|
} from "@actions/core";
|
||||||
import { execSync, exec } from "node:child_process";
|
import { execSync, exec } from "node:child_process";
|
||||||
import { existsSync } from "node:fs";
|
import { checkWorkingDirectory, getNpxCmd, semverCompare } from "./utils";
|
||||||
import * as path from "node:path";
|
|
||||||
import * as util from "node:util";
|
import * as util from "node:util";
|
||||||
const execAsync = util.promisify(exec);
|
const execAsync = util.promisify(exec);
|
||||||
|
|
||||||
|
@ -31,10 +30,6 @@ const config = {
|
||||||
QUIET_MODE: getBooleanInput("quiet"),
|
QUIET_MODE: getBooleanInput("quiet"),
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
function getNpxCmd() {
|
|
||||||
return process.env.RUNNER_OS === "Windows" ? "npx.cmd" : "npx";
|
|
||||||
}
|
|
||||||
|
|
||||||
function info(message: string, bypass?: boolean): void {
|
function info(message: string, bypass?: boolean): void {
|
||||||
if (!config.QUIET_MODE || bypass) {
|
if (!config.QUIET_MODE || bypass) {
|
||||||
originalInfo(message);
|
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() {
|
async function main() {
|
||||||
try {
|
try {
|
||||||
installWrangler();
|
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() {
|
function installWrangler() {
|
||||||
if (config["WRANGLER_VERSION"].startsWith("1")) {
|
if (config["WRANGLER_VERSION"].startsWith("1")) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
|
@ -310,7 +276,4 @@ export {
|
||||||
uploadSecrets,
|
uploadSecrets,
|
||||||
authenticationSetup,
|
authenticationSetup,
|
||||||
installWrangler,
|
installWrangler,
|
||||||
checkWorkingDirectory,
|
|
||||||
getNpxCmd,
|
|
||||||
semverCompare,
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,18 +1,7 @@
|
||||||
import { expect, test, describe } from "vitest";
|
import { expect, test, describe } from "vitest";
|
||||||
import { checkWorkingDirectory, getNpxCmd, semverCompare } from "./index";
|
import { checkWorkingDirectory, getNpxCmd, semverCompare } from "./utils";
|
||||||
import path from "node:path";
|
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 () => {
|
test("getNpxCmd ", async () => {
|
||||||
process.env.RUNNER_OS = "Windows";
|
process.env.RUNNER_OS = "Windows";
|
||||||
expect(getNpxCmd()).toBe("npx.cmd");
|
expect(getNpxCmd()).toBe("npx.cmd");
|
35
src/utils.ts
Normal file
35
src/utils.ts
Normal 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.`);
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,12 +9,12 @@
|
||||||
"isolatedModules": true,
|
"isolatedModules": true,
|
||||||
"target": "ESNext",
|
"target": "ESNext",
|
||||||
"module": "ESNext",
|
"module": "ESNext",
|
||||||
"moduleResolution": "NodeNext",
|
"moduleResolution": "Bundler",
|
||||||
"rootDir": "./src",
|
"rootDir": "./src",
|
||||||
"outDir": "./dist",
|
"outDir": "./dist",
|
||||||
"lib": ["ESNext"],
|
"lib": ["ESNext"],
|
||||||
"types": ["node", "@cloudflare/workers-types"]
|
"types": ["node", "@cloudflare/workers-types"]
|
||||||
},
|
},
|
||||||
"exclude": ["node_modules", "**/*.test.ts"],
|
"exclude": ["node_modules", "**/*.test.ts"],
|
||||||
"include": ["src/index.ts"]
|
"include": ["src"]
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +0,0 @@
|
||||||
import { defineConfig } from "vitest/dist/config";
|
|
||||||
|
|
||||||
export default defineConfig({
|
|
||||||
test: {
|
|
||||||
setupFiles: ["./action-env-setup.ts"],
|
|
||||||
},
|
|
||||||
});
|
|
Loading…
Reference in a new issue