Skip to content

Commit

Permalink
Use prettier on generated code, and fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aaroncox committed Aug 10, 2023
1 parent 43ab23a commit db10adb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
1 change: 1 addition & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ semi: false
singleQuote: true
tabWidth: 4
trailingComma: 'es5'
parser: typescript
4 changes: 3 additions & 1 deletion src/codegen.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as ts from 'typescript'
import * as prettier from 'prettier'

import {
EOSIO_CORE_CLASSES,
Expand Down Expand Up @@ -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)
}
21 changes: 11 additions & 10 deletions test/tests/codegen.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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
Expand Down
11 changes: 7 additions & 4 deletions test/utils/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,24 @@ 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')) {
fs.mkdirSync('test/tmp')
}

// 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() {
Expand Down

0 comments on commit db10adb

Please sign in to comment.