Skip to content

Commit

Permalink
Use the comment "// 🐺 create code here" instead of `await qawolf.crea…
Browse files Browse the repository at this point in the history
…te();`
  • Loading branch information
jperl committed Jun 16, 2020
1 parent 2fe45cb commit b1c1173
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 16 deletions.
9 changes: 5 additions & 4 deletions src/create-code/CodeUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ export abstract class CodeUpdater extends EventEmitter {
protected async _prepare(): Promise<void> {
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<void> {
Expand Down
8 changes: 4 additions & 4 deletions src/create-code/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ 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');

export const getCreatePath = async (
callerFileNames: string[],
): Promise<string> => {
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) => {
Expand All @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/create-code/patchCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 16 additions & 4 deletions test/create-code/CodeFileUpdater.test.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -23,20 +23,20 @@ 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);
});

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();
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/create-code/__snapshots__/CodeFileUpdater.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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();"
`;

Expand All @@ -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();"
`;
2 changes: 1 addition & 1 deletion test/create-code/__snapshots__/CodeReconciler.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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`] = `
Expand Down

0 comments on commit b1c1173

Please sign in to comment.