From db10adb8061fd861b69434120815e35f7a38c8fd Mon Sep 17 00:00:00 2001 From: Aaron Cox Date: Thu, 10 Aug 2023 14:17:56 -0700 Subject: [PATCH] Use prettier on generated code, and fixed tests --- .prettierrc | 1 + src/codegen.ts | 4 +++- test/tests/codegen.ts | 21 +++++++++++---------- test/utils/codegen.ts | 11 +++++++---- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/.prettierrc b/.prettierrc index 9810e62..57d64ba 100644 --- a/.prettierrc +++ b/.prettierrc @@ -6,3 +6,4 @@ semi: false singleQuote: true tabWidth: 4 trailingComma: 'es5' +parser: typescript diff --git a/src/codegen.ts b/src/codegen.ts index 3535271..97bfd16 100644 --- a/src/codegen.ts +++ b/src/codegen.ts @@ -1,4 +1,5 @@ import * as ts from 'typescript' +import * as prettier from 'prettier' import { EOSIO_CORE_CLASSES, @@ -115,5 +116,6 @@ export async function codegen(contractName, abi) { ts.NodeFlags.None ) - return printer.printFile(sourceFile) + const options = await prettier.resolveConfig(process.cwd()) + return await prettier.format(printer.printFile(sourceFile), options) } diff --git a/test/tests/codegen.ts b/test/tests/codegen.ts index eadb4b8..cd0f026 100644 --- a/test/tests/codegen.ts +++ b/test/tests/codegen.ts @@ -1,7 +1,7 @@ import {assert} from 'chai' +import fs from 'fs' import {ABI, APIClient, Name} from '@wharfkit/antelope' import {makeClient} from '@wharfkit/mock-data' - import {Contract} from 'src/contract' import * as MockRewardsGm from '$test/data/contracts/mock-rewards' @@ -11,19 +11,20 @@ import {runGenericContractTests} from './contract' const GeneratedRewardsGm = await generateCodegenContract('rewards.gm') const contracts = { MockRewardsGm, - // GeneratedRewardsGm, // TODO: DISABLED - readd to test codegen + GeneratedRewardsGm, + } + + const files = { + mock: fs.readFileSync('test/data/contracts/mock-rewards.ts').toString('utf-8'), + generated: GeneratedRewardsGm.text, } + const client = makeClient('https://eos.greymass.com') suite('codegen', function () { - // TODO: DISABLED - reimplement - // test('Contracts are identical', function () { - // // TODO: We need a better way to compare the files too, like w/ imports etc - // assert.equal( - // JSON.stringify(contracts.MockRewardsGm), - // JSON.stringify(contracts.GeneratedRewardsGm) - // ) - // }) + test('Contracts are identical', function () { + assert.equal(files.mock, files.generated) + }) Object.keys(contracts).forEach((contractKey) => { suite(`Testing namespace ${contractKey}`, function () { // The `RewardsGm` namespace diff --git a/test/utils/codegen.ts b/test/utils/codegen.ts index fab2bf1..41858e6 100644 --- a/test/utils/codegen.ts +++ b/test/utils/codegen.ts @@ -9,9 +9,9 @@ export async function generateCodegenContract(contractName: string) { const abi = new ABI(JSON.parse(abiJson)) // Generate the code - let generatedCode = await codegen(contractName, abi) + const generatedCode = await codegen(contractName, abi) - generatedCode = generatedCode.replace('@wharfkit/contract', '../../src/index') + const testGeneratedCode = generatedCode.replace('@wharfkit/contract', '../../src/index') // Create the tmp directory under the test directory if it does not exist if (!fs.existsSync('test/tmp')) { @@ -19,11 +19,14 @@ export async function generateCodegenContract(contractName: string) { } // Write the generated code to a file in the tmp directory - fs.writeFileSync(path.join('test/tmp', `${contractName}.ts`), generatedCode, { + fs.writeFileSync(path.join('test/tmp', `${contractName}.ts`), testGeneratedCode, { encoding: 'utf8', }) - return await import(`../tmp/${contractName}`) + return { + import: await import(`../tmp/${contractName}`), + text: generatedCode, + } } export function removeCodegenContracts() {