Skip to content

Commit

Permalink
added require.asset tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rafapaezbas committed Oct 22, 2024
1 parent b556374 commit d5aec2d
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 1 deletion.
3 changes: 2 additions & 1 deletion scripts/test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const force = Bare.argv.includes('--force-install')

const dirs = [
path.join(root, 'test', 'fixtures', 'harness', 'node_modules'),
path.join(root, 'test', 'fixtures', 'encrypted', 'node_modules')
path.join(root, 'test', 'fixtures', 'encrypted', 'node_modules'),
path.join(root, 'test', 'fixtures', 'app-with-assets', 'node_modules')
]
for (const dir of dirs) {
if (force === false && fs.existsSync(dir)) continue
Expand Down
43 changes: 43 additions & 0 deletions test/01-smoke.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const path = require('bare-path')
const hypercoreid = require('hypercore-id-encoding')
const Helper = require('./helper')
const harness = path.join(Helper.localDir, 'test', 'fixtures', 'harness')
const assets = path.join(Helper.localDir, 'test', 'fixtures', 'app-with-assets')

test('smoke', async function ({ ok, is, plan, comment, teardown, timeout, end }) {
timeout(180000)
Expand Down Expand Up @@ -49,3 +50,45 @@ test('smoke', async function ({ ok, is, plan, comment, teardown, timeout, end })
const { code } = await running.until.exit
is(code, 0, 'exit code is 0')
})

test('app with assets', async function ({ is, plan, comment, teardown, timeout }) {
timeout(180000)
plan(3)
const stager = new Helper()
teardown(() => stager.close(), { order: Infinity })
await stager.ready()
const dir = assets

const id = Math.floor(Math.random() * 10000)

comment('staging')
const staging = stager.stage({ channel: `test-${id}`, name: `test-${id}`, dir, dryRun: false, bare: true })
teardown(() => Helper.teardownStream(staging))
await Helper.pick(staging, { tag: 'final' })

comment('seeding')
const seeder = new Helper()
teardown(() => seeder.close(), { order: Infinity })
await seeder.ready()
const seeding = seeder.seed({ channel: `test-${id}`, name: `test-${id}`, dir, key: null, cmdArgs: [] })
teardown(() => Helper.teardownStream(seeding))
const until = await Helper.pick(seeding, [{ tag: 'key' }, { tag: 'announced' }])

const key = await until.key
await until.announced

comment('running')
const link = 'pear://' + key
const running = await Helper.open(link, { tags: ['exit'] })

const { value: fromIndex } = await running.inspector.evaluate('global.readAsset()', { awaitPromise: true })
is(fromIndex.trim(), 'This is the content of the asset', 'Read asset from entrypoint')

const { value: fromUtils } = await running.inspector.evaluate('global.readAssetFromUtils()', { awaitPromise: true })
is(fromUtils.trim(), 'This is the content of the asset', 'Read asset from lib')

await running.inspector.evaluate('disableInspector()')
await running.inspector.close()
const { code } = await running.until.exit
is(code, 0, 'exit code is 0')
})
28 changes: 28 additions & 0 deletions test/fixtures/app-with-assets/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const bareInspector = require('bare-inspector')
const { Inspector } = require('pear-inspect')
const fsp = require('bare-fs/promises')
const readAssetFromUtils = require('./lib/utils.js')

const inspector = new Inspector({ inspector: bareInspector })

async function readAsset () {
const text = await fsp.readFile(require.asset('./text-file.txt'))
return text.toString()
}

async function run () {
const key = await inspector.enable()
const inspectorKey = key.toString('hex')
console.log(`{ "tag": "inspector", "data": { "key": "${inspectorKey}" }}`)
}

run()


function disableInspector () {
inspector.disable()
}

global.disableInspector = disableInspector
global.readAsset = readAsset
global.readAssetFromUtils = readAssetFromUtils
6 changes: 6 additions & 0 deletions test/fixtures/app-with-assets/lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const fsp = require('bare-fs/promises')

module.exports = async () => {
const text = await fsp.readFile(require.asset('../text-file.txt'))
return text.toString()
}
15 changes: 15 additions & 0 deletions test/fixtures/app-with-assets/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "app-with-assets",
"main": "index.js",
"pear": {
"name": "app-with-assets",
"type": "terminal"
},
"devDependencies": {
"bare-inspector": "^3.0.1",
"pear-inspect": "^1.2.2"
},
"dependencies": {
"bare-fs": "^3.1.1"
}
}
1 change: 1 addition & 0 deletions test/fixtures/app-with-assets/text-file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is the content of the asset

0 comments on commit d5aec2d

Please sign in to comment.