From 61114360a998096b492d280c39e047612f919750 Mon Sep 17 00:00:00 2001 From: Greg Pabian <35925521+grzpab@users.noreply.github.com> Date: Wed, 13 Dec 2023 16:17:26 +0100 Subject: [PATCH] fix: INT-2324 fix the broken CEN tests (#167) --- src/executeMainThread.ts | 5 +++++ src/runCodemod.ts | 8 ++++---- src/runner.ts | 4 ++++ test/runner.test.ts | 13 ++++++++++++- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/executeMainThread.ts b/src/executeMainThread.ts index 4ef6d1e..2cdcc5e 100644 --- a/src/executeMainThread.ts +++ b/src/executeMainThread.ts @@ -29,6 +29,7 @@ import { NoTelemetryService, } from './telemetryService.js'; import { APP_INSIGHTS_INSTRUMENTATION_STRING } from './constants.js'; +import { readFile } from 'node:fs/promises'; // the build script contains the version declare const __INTUITA_CLI_VERSION__: string; @@ -214,6 +215,9 @@ export const executeMainThread = async () => { tarService, ); + const getCodemodSource = (path: string) => + readFile(path, { encoding: 'utf8' }); + const runner = new Runner( fs as unknown as IFs, printer, @@ -226,6 +230,7 @@ export const executeMainThread = async () => { argumentRecord, nameOrPath, process.cwd(), + getCodemodSource, homedir(), ); diff --git a/src/runCodemod.ts b/src/runCodemod.ts index a6e2e85..3723eee 100644 --- a/src/runCodemod.ts +++ b/src/runCodemod.ts @@ -20,7 +20,6 @@ import { SafeArgumentRecord } from './safeArgumentRecord.js'; import { FlowSettings } from './schemata/flowSettingsSchema.js'; import { WorkerThreadMessage } from './workerThreadMessages.js'; import { RunSettings } from './runSettings.js'; -import { readFile } from 'node:fs/promises'; const TERMINATE_IDLE_THREADS_TIMEOUT = 30 * 1000; @@ -156,6 +155,7 @@ export const runCodemod = async ( ) => void, safeArgumentRecord: SafeArgumentRecord, currentWorkingDirectory: string, + getCodemodSource: (path: string) => Promise, ): Promise => { const name = 'name' in codemod ? codemod.name : codemod.indexPath; @@ -191,6 +191,7 @@ export const runCodemod = async ( }, safeArgumentRecord, currentWorkingDirectory, + getCodemodSource, ); for (const command of commands) { @@ -266,6 +267,7 @@ export const runCodemod = async ( }, safeArgumentRecord, currentWorkingDirectory, + getCodemodSource, ); for (const command of commands) { @@ -338,9 +340,7 @@ export const runCodemod = async ( return; } - const codemodSource = await readFile(codemod.indexPath, { - encoding: 'utf8', - }); + const codemodSource = await getCodemodSource(codemod.indexPath); const transpiledSource = codemod.indexPath.endsWith('.ts') ? transpile(codemodSource.toString()) diff --git a/src/runner.ts b/src/runner.ts index fadfceb..c423dc7 100644 --- a/src/runner.ts +++ b/src/runner.ts @@ -39,6 +39,7 @@ export class Runner { protected readonly _argumentRecord: ArgumentRecord, protected readonly _name: string | null, protected readonly _currentWorkingDirectory: string, + protected readonly _getCodemodSource: (path: string) => Promise, homeDirectoryPath: string, ) { this.__caseHashDigest = randomBytes(20); @@ -125,6 +126,7 @@ export class Runner { (message) => this._printer.printMessage(message), safeArgumentRecord, this._currentWorkingDirectory, + this._getCodemodSource, ); await surfaceAgnosticCaseService.emitPostamble(); @@ -172,6 +174,7 @@ export class Runner { (message) => this._printer.printMessage(message), safeArgumentRecord, this._currentWorkingDirectory, + this._getCodemodSource, ); this._telemetry.sendEvent({ @@ -240,6 +243,7 @@ export class Runner { (message) => this._printer.printMessage(message), safeArgumentRecord, this._currentWorkingDirectory, + this._getCodemodSource, ); await surfaceAgnosticCaseService.emitPostamble(); diff --git a/test/runner.test.ts b/test/runner.test.ts index af87f5c..d92d9e7 100644 --- a/test/runner.test.ts +++ b/test/runner.test.ts @@ -22,7 +22,7 @@ export default function transform(file, api, options) { } `; -describe('Runner', function (this) { +describe('Runner', function () { it('should transform staged files using the pre-commit codemods', async () => { const volume = Volume.fromJSON({ '/code/a.ts': 'unchanged', @@ -105,6 +105,16 @@ describe('Runner', function (this) { const currentWorkingDirectory = '/'; const homedir = '/home/abc'; + const getCodemodSource = async (path: string) => { + const data = await ifs.promises.readFile(path); + + if (typeof data === 'string') { + return data; + } + + return data.toString('utf8'); + }; + const runner = new Runner( ifs, printer, @@ -119,6 +129,7 @@ describe('Runner', function (this) { {}, null, currentWorkingDirectory, + getCodemodSource, homedir, );