Skip to content

Commit

Permalink
fix jscodeshift test
Browse files Browse the repository at this point in the history
  • Loading branch information
r4zendev committed Jul 23, 2024
1 parent a56c06e commit 18c0aba
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 74 deletions.
5 changes: 1 addition & 4 deletions apps/backend/src/publishHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,7 @@ export const publishHandler: RouteHandler<{
throwOnNotFound: false,
});

const built = await getCodemodExecutable({
path: unpackPath,
config: codemodRc,
}).catch(() => null);
const built = await getCodemodExecutable(unpackPath).catch(() => null);

if (path === null || built === null) {
return reply.code(400).send({
Expand Down
5 changes: 1 addition & 4 deletions apps/cli/src/commands/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,7 @@ export const handlePublishCliCommand = async (options: {
})),
);

const builtExecutable = await getCodemodExecutable({
config: codemodRc,
path: source,
}).catch(() => null);
const builtExecutable = await getCodemodExecutable(source).catch(() => null);

if (builtExecutable === null) {
throw new Error(
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "@codemod-com/codemod",
"version": "0.0.0-development",
"type": "module",
"author": "Codemod, Inc.",
"license": "Apache License, Version 2.0",
"description": "A monorepo for all the interconnected tools of codemod.com",
Expand Down Expand Up @@ -42,8 +43,8 @@
"lint:write": "biome check --write --diagnostic-level=error .",
"lint:linter": "biome lint --write --diagnostic-level=error .",
"lint:formatter": "biome format --write --diagnostic-level=error .",
"test:silent": "pnpm run test:unit --silent && pnpm run test:backend --silent",
"test": "pnpm run test:unit && pnpm run test:backend",
"test:silent": "pnpm run test:unit --silent && pnpm run test:backend --silent",
"test:unit": "vitest run --exclude \"(apps/backend/**/*.*|packages/codemods/next/13/app-directory-boilerplate/test/test.win.ts)\"",
"test:backend": "pnpm --filter @codemod-com/backend test",
"test:win": "vitest run packages/codemods/next/13/app-directory-boilerplate/test/test.win.ts",
Expand Down Expand Up @@ -77,7 +78,6 @@
"tsx": "^4.2.0",
"turbo": "^1.10.14",
"typescript": "^5.4.5",
"vite": "^5.3.4",
"vite-tsconfig-paths": "^4.2.0",
"vitest": "^2.0.4"
},
Expand Down
1 change: 0 additions & 1 deletion packages/runner/src/engines/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export const buildVmConsole =
(callback: (kind: ConsoleKind, message: string) => void) =>
(k: unknown, data: unknown, ...args: unknown[]) => {
const kind = parseConsoleKind(k);

const message = format(data, ...args);

callback(kind, message);
Expand Down
1 change: 1 addition & 0 deletions packages/runner/src/runner.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export type {};
// import { equal } from "node:assert";
// import { randomBytes } from "node:crypto";
// import { Volume, createFsFromVolume } from "memfs";
Expand Down
2 changes: 1 addition & 1 deletion packages/runner/src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ export class Runner {
return await onSuccess?.({ codemod, commands: [] });
}

const codemodSource = await getCodemodExecutable(codemod);
const codemodSource = await getCodemodExecutable(codemod.path);

if (codemod.config.engine === "workflow") {
this.printRunSummary(printer, codemod, flowSettings, {
Expand Down
10 changes: 4 additions & 6 deletions packages/runner/src/source-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import esbuild from "esbuild";
import tsmorph from "ts-morph";

import type { Filemod } from "@codemod-com/filemod";
import { type Codemod, getEntryPath } from "@codemod-com/utilities";
import { getEntryPath } from "@codemod-com/utilities";

import { readFile } from "node:fs/promises";
import type { Dependencies } from "#engines/filemod.js";
Expand Down Expand Up @@ -51,18 +51,16 @@ export const getTransformer = (source: string) => {

export const BUILT_SOURCE_PATH = "cdmd_dist/index.cjs";

export const getCodemodExecutable = async (
codemod: Pick<Codemod, "config" | "path">,
) => {
const outputFilePath = join(resolve(codemod.path), BUILT_SOURCE_PATH);
export const getCodemodExecutable = async (source: string) => {
const outputFilePath = join(resolve(source), BUILT_SOURCE_PATH);
try {
return await readFile(outputFilePath, { encoding: "utf8" });
} catch {
// continue
}

const { path: entryPoint } = await getEntryPath({
source: codemod.path,
source,
throwOnNotFound: true,
});

Expand Down
5 changes: 4 additions & 1 deletion packages/runner/test/console.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { deepStrictEqual } from "node:assert";
import vm from "node:vm";
import type { ConsoleKind } from "@codemod-com/printer";

import { describe, it } from "vitest";

import type { ConsoleKind } from "@codemod-com/printer";

import { CONSOLE_OVERRIDE } from "../src/constants.js";
import { buildVmConsole } from "../src/engines/common.js";

Expand Down
25 changes: 15 additions & 10 deletions packages/runner/test/runJscodeshiftCodemod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { afterAll, describe, it } from "vitest";

import type { ConsoleKind } from "@codemod-com/printer";

import type { CodemodConfig } from "@codemod-com/utilities";
import type { CodemodConfigInput } from "@codemod-com/utilities";
import { runJscodeshiftCodemod } from "../src/engines/jscodeshift.js";
import { getCodemodExecutable } from "../src/source-code.js";

Expand Down Expand Up @@ -67,20 +67,25 @@ export default function transform(
const testTempDir = join(homedir(), ".codemod", "test-temp");

describe("runJscodeshiftCodemod", async () => {
const codemodName = randomBytes(4).toString("hex");
const codemodName = randomBytes(8).toString("hex");
const directoryPath = join(testTempDir, codemodName);
const distPath = join(directoryPath, "cdmd_dist");
const srcPath = join(directoryPath, "src");

await mkdir(distPath, { recursive: true });
await writeFile(join(distPath, `${codemodName}.ts`), codemodSource);
await mkdir(srcPath, { recursive: true });
await writeFile(join(srcPath, "index.ts"), codemodSource);
await writeFile(
join(directoryPath, ".codemodrc.json"),
JSON.stringify({
name: "test",
engine: "jscodeshift",
version: "0.0.0",
} satisfies CodemodConfigInput),
);

const compiledSource = await getCodemodExecutable({
config: {} as CodemodConfig,
path: directoryPath,
});
const compiledSource = await getCodemodExecutable(directoryPath);

afterAll(async () => {
await rmdir(directoryPath);
await rmdir(directoryPath, { recursive: true });
});

it("should return transformed output", () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/runner/test/runTsMorphCodemod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { join } from "node:path";
import { afterAll, describe, it } from "vitest";

import type { ConsoleKind } from "@codemod-com/printer";

import type { CodemodConfig } from "@codemod-com/utilities";

import { runTsMorphCodemod } from "../src/engines/ts-morph.js";
import { getCodemodExecutable } from "../src/source-code.js";

Expand Down Expand Up @@ -51,7 +51,7 @@ describe("runTsMorphCodemod", async () => {
const messages: [ConsoleKind, string][] = [];

const fileCommands = runTsMorphCodemod(
codemodSource,
compiledSource,
"index.ts",
"",
{},
Expand Down
Loading

0 comments on commit 18c0aba

Please sign in to comment.