From b1c1173358c71cf6d944fc3d3f1f583228493552 Mon Sep 17 00:00:00 2001 From: Jon Perl Date: Tue, 16 Jun 2020 16:38:39 -0400 Subject: [PATCH] =?UTF-8?q?Use=20the=20comment=20"//=20=F0=9F=90=BA=20crea?= =?UTF-8?q?te=20code=20here"=20instead=20of=20`await=20qawolf.create();`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/create-code/CodeUpdater.ts | 9 +++++---- src/create-code/create.ts | 8 ++++---- src/create-code/patchCode.ts | 4 +++- test/create-code/CodeFileUpdater.test.ts | 20 +++++++++++++++---- .../CodeFileUpdater.test.ts.snap | 4 ++-- .../__snapshots__/CodeReconciler.test.ts.snap | 2 +- 6 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/create-code/CodeUpdater.ts b/src/create-code/CodeUpdater.ts index 0c9d6cba7..b10900644 100644 --- a/src/create-code/CodeUpdater.ts +++ b/src/create-code/CodeUpdater.ts @@ -37,10 +37,11 @@ export abstract class CodeUpdater extends EventEmitter { protected async _prepare(): Promise { const code = await this._loadCode(); - const patchLine = getLineIncludes(code, PATCH_HANDLE); - if (!patchLine) { - throw new Error(`Could not find "${PATCH_HANDLE}"`); - } + const createLine = getLineIncludes(code, `qawolf.create()`); + if (!createLine) return; + + const updatedCode = code.replace(createLine.trim(), PATCH_HANDLE); + await this._update(updatedCode); } public async finalize(): Promise { diff --git a/src/create-code/create.ts b/src/create-code/create.ts index 2fa153dc6..c181482d8 100644 --- a/src/create-code/create.ts +++ b/src/create-code/create.ts @@ -5,7 +5,7 @@ import { bold } from 'kleur'; import { findLast } from 'lodash'; import { CreateManager } from './CreateManager'; import { getLineIncludes } from './format'; -import { PATCH_HANDLE } from './patchCode'; +import { CREATE_HANDLE } from './patchCode'; import { Registry } from '../utils'; const debug = Debug('qawolf:create'); @@ -13,7 +13,7 @@ const debug = Debug('qawolf:create'); export const getCreatePath = async ( callerFileNames: string[], ): Promise => { - debug(`search caller files for ${PATCH_HANDLE} %j`, callerFileNames); + debug(`search caller files for ${CREATE_HANDLE} %j`, callerFileNames); const codes = await Promise.all( callerFileNames.map(async (filename) => { @@ -29,11 +29,11 @@ export const getCreatePath = async ( const item = findLast( codes, - ({ code }) => !!getLineIncludes(code, PATCH_HANDLE), + ({ code }) => !!getLineIncludes(code, CREATE_HANDLE), ); if (!item) { - throw new Error(`Could not find ${PATCH_HANDLE} in caller`); + throw new Error(`Could not find ${CREATE_HANDLE} in caller`); } return item.filename; diff --git a/src/create-code/patchCode.ts b/src/create-code/patchCode.ts index 56116ba96..60541fccd 100644 --- a/src/create-code/patchCode.ts +++ b/src/create-code/patchCode.ts @@ -5,7 +5,9 @@ type PatchOptions = { patch: string; }; -export const PATCH_HANDLE = 'await qawolf.create();'; +export const CREATE_HANDLE = 'qawolf.create()'; + +export const PATCH_HANDLE = '// 🐺 create code here'; export const canPatch = (code: string): boolean => { return code.includes(PATCH_HANDLE); diff --git a/test/create-code/CodeFileUpdater.test.ts b/test/create-code/CodeFileUpdater.test.ts index ffa11c9bd..02677f48b 100644 --- a/test/create-code/CodeFileUpdater.test.ts +++ b/test/create-code/CodeFileUpdater.test.ts @@ -1,7 +1,7 @@ import { outputFile, pathExists, readFile, remove } from 'fs-extra'; import { buildSteps } from '../../src/build-workflow/buildSteps'; import { CodeFileUpdater } from '../../src/create-code/CodeFileUpdater'; -import { PATCH_HANDLE } from '../../src/create-code/patchCode'; +import { CREATE_HANDLE, PATCH_HANDLE } from '../../src/create-code/patchCode'; // require manually since fs is mocked // eslint-disable-next-line @typescript-eslint/no-var-requires @@ -23,12 +23,12 @@ describe('CodeFileUpdater', () => { describe('discard', () => { it('reverts to initial code', async () => { - const initialCode = `initialCode()\n${PATCH_HANDLE}`; + const initialCode = `initialCode()\n${CREATE_HANDLE}`; mockedReadFile.mockResolvedValue(initialCode); const updater = await CodeFileUpdater.create('revertpath'); await updater.discard(); - const reverted = mockedOutputFile.mock.calls[0]; + const reverted = mockedOutputFile.mock.calls[1]; expect(reverted[0]).toEqual('revertpath'); expect(reverted[1]).toEqual(initialCode); expect(mockedRemove.mock.calls.length).toEqual(0); @@ -36,7 +36,7 @@ describe('CodeFileUpdater', () => { it('removes code if QAW_CREATE is set to true', async () => { process.env.QAW_CREATE = 'true'; - const initialCode = `initialCode()\n${PATCH_HANDLE}`; + const initialCode = `initialCode()\n${CREATE_HANDLE}`; mockedReadFile.mockResolvedValue(initialCode); const updater = await CodeFileUpdater.create('removepath'); await updater.discard(); @@ -46,6 +46,18 @@ describe('CodeFileUpdater', () => { }); describe('create', () => { + it('replaces qawolf.create call with the patch handle', async () => { + mockedReadFile.mockResolvedValue( + ` someCode();\n await qawolf.create();\n otherCode();`, + ); + await CodeFileUpdater.create('somepath'); + + const updatedFile = mockedOutputFile.mock.calls[0][1]; + expect(updatedFile).toBe( + ` someCode();\n ${PATCH_HANDLE}\n otherCode();`, + ); + }); + it('throws an error if qawolf.create is not found', async () => { mockedReadFile.mockResolvedValue('no create'); // eslint-disable-next-line jest/valid-expect diff --git a/test/create-code/__snapshots__/CodeFileUpdater.test.ts.snap b/test/create-code/__snapshots__/CodeFileUpdater.test.ts.snap index c0dd7cf09..de77a8f74 100644 --- a/test/create-code/__snapshots__/CodeFileUpdater.test.ts.snap +++ b/test/create-code/__snapshots__/CodeFileUpdater.test.ts.snap @@ -4,7 +4,7 @@ exports[`CodeFileUpdater update saves code with new steps 1`] = ` " someCode(); await page.click('text=\\"Log in\\"'); await page.click(\\"#username\\"); - await qawolf.create(); + // 🐺 create code here otherCode();" `; @@ -17,6 +17,6 @@ exports[`CodeFileUpdater update saves code with new steps 2`] = ` await page.type(\\"#password\\", \\"SuperSecretPassword!\\"); await page.press(\\"#password\\", \\"Enter\\"); await page.click('text=\\"Log out\\"'); - await qawolf.create(); + // 🐺 create code here otherCode();" `; diff --git a/test/create-code/__snapshots__/CodeReconciler.test.ts.snap b/test/create-code/__snapshots__/CodeReconciler.test.ts.snap index 7b3e8323e..0f7a7b7a3 100644 --- a/test/create-code/__snapshots__/CodeReconciler.test.ts.snap +++ b/test/create-code/__snapshots__/CodeReconciler.test.ts.snap @@ -2,7 +2,7 @@ exports[`CodeReconciler.reconcile inserts new expressions 1`] = ` "await page.click('text=\\"Log in\\"'); -await qawolf.create();" +// 🐺 create code here" `; exports[`CodeReconciler.reconcile updates the last expression 1`] = `