Skip to content

Commit

Permalink
Missing helper file
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewosh committed Sep 10, 2024
1 parent 80869b6 commit 32780e2
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions test/helpers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const p = require('path')
const fs = require('fs')
const tmp = require('test-tmp')

const Hyperschema = require('hyperschema')
const Hyperswitch = require('../..')

class TestBuilder {
constructor (dir) {
this.dir = dir
this.schemaDir = p.join(dir, 'hyperschema')
this.switchDir = p.join(dir, 'hyperswitch')
this.module = null
this.version = 0
}

rebuild (builder) {
const schema = Hyperschema.from(this.schemaDir)
builder.schema(schema)
Hyperschema.toDisk(schema)
const hyperswitch = Hyperswitch.from(this.schemaDir, this.switchDir)
builder.switch(hyperswitch)
Hyperswitch.toDisk(hyperswitch)

if (this.module) {
delete require.cache[require.resolve(this.switchDir)]
delete require.cache[require.resolve(p.join(this.switchDir, 'switch.json'))]
}

this.module = require(this.switchDir)
this.json = require(p.join(this.switchDir, 'switch.json'))

return schema
}
}

async function createTestSchema (t) {
const dir = await tmp(t, { dir: p.join(__dirname, '../test-storage') })

// Copy the runtime into the tmp dir so that we don't need to override it in the codegen
const runtimePath = p.join(dir, 'node_modules', 'hyperswitch', 'runtime.js')
await fs.promises.mkdir(p.dirname(runtimePath), { recursive: true })
await fs.promises.copyFile(p.resolve(dir, '../../../runtime.js'), runtimePath)

return new TestBuilder(dir)
}

module.exports = {
createTestSchema
}

0 comments on commit 32780e2

Please sign in to comment.