From 64f02a21660cb158656277822fa1ab68e76410b6 Mon Sep 17 00:00:00 2001 From: Jan Keith Darunday Date: Thu, 8 Aug 2024 15:19:59 +0800 Subject: [PATCH] Add tests for pear dump --- test/05-commands.test.js | 456 ++++++++++++++++++++++++++++++++++++++- test/index.js | 2 +- 2 files changed, 452 insertions(+), 6 deletions(-) diff --git a/test/05-commands.test.js b/test/05-commands.test.js index 38157d954..21f55bcce 100644 --- a/test/05-commands.test.js +++ b/test/05-commands.test.js @@ -1,6 +1,7 @@ const test = require('brittle') const path = require('bare-path') const Helper = require('./helper') +const fs = require('bare-fs') const harness = path.join(Helper.root, 'test', 'fixtures', 'harness') const minimal = path.join(Helper.root, 'test', 'fixtures', 'minimal') @@ -16,12 +17,70 @@ class Rig { comment('local sidecar connected') } + getOrCreateDumpInstance = async () => { + if (this.pearDumpInstance) return this.pearDumpInstance + + const testId = Math.floor(Math.random() * 100000) + const argvStage = ['stage', '--json', 'test-' + testId, minimal] + const stager1 = await Helper.open(harness, { tags: ['exit'] }, { lineout: true }) + await stager1.inspector.evaluate(` + __PEAR_TEST__.command(${JSON.stringify(argvStage)}) + `, { returnByValue: false }) + + for await (const line of stager1.lineout) { + const result = JSON.parse(line) + if (result.tag === 'addendum') this.pearDumpInstance = result.data + if (result.tag === 'final') break + } + + await stager1.inspector.evaluate('__PEAR_TEST__.ipc.destroy()', { returnByValue: false }) + await stager1.inspector.close() + await stager1.until.exit + + const argvRelease = ['release', '--json', 'test-' + testId, minimal] + const releaser1 = await Helper.open(harness, { tags: ['exit'] }, { lineout: true }) + await releaser1.inspector.evaluate(` + __PEAR_TEST__.command(${JSON.stringify(argvRelease)}) + `, { returnByValue: false }) + + for await (const line of releaser1.lineout) { + const result = JSON.parse(line) + if (result.tag === 'final') break + } + + await releaser1.inspector.evaluate('__PEAR_TEST__.ipc.destroy()', { returnByValue: false }) + await releaser1.inspector.close() + await releaser1.until.exit + + fs.writeFileSync(path.join(minimal, 'testfile.txt'), 'this is a test file') + + const stager2 = await Helper.open(harness, { tags: ['exit'] }, { lineout: true }) + await stager2.inspector.evaluate(` + __PEAR_TEST__.command(${JSON.stringify(argvStage)}) + `, { returnByValue: false }) + + for await (const line of stager2.lineout) { + const result = JSON.parse(line) + if (result.tag === 'addendum') { + this.pearDumpInstance.versionOld = this.pearDumpInstance.version + this.pearDumpInstance.version = result.data.version + } + if (result.tag === 'final') break + } + + await stager2.inspector.evaluate('__PEAR_TEST__.ipc.destroy()', { returnByValue: false }) + await stager2.inspector.close() + await stager2.until.exit + + fs.unlinkSync(path.join(minimal, 'testfile.txt')) + + return this.pearDumpInstance + } + cleanup = async ({ comment }) => { comment('shutting down local sidecar') - setTimeout(async () => { - await this.helper.shutdown() - comment('local sidecar shut down') - }, 5000) + await this.helper.shutdown() + comment('local sidecar shut down') } } @@ -29,7 +88,7 @@ const rig = new Rig() test('commands setup', rig.setup) -test('pear stage --json ', async function ({ plan, alike, is }) { +test('pear stage --json ', async function ({ plan, alike, is }) { plan(2) const testId = Math.floor(Math.random() * 100000) @@ -58,4 +117,391 @@ test('pear stage --json ', async function ({ plan, alike, is }) { is(code, 0) }) +test.todo('pear stage ') +test.todo('pear stage ') +test.todo('pear stage (package.json pear.config.stage.entrypoints )') +test.todo('pear stage (package.json pear.config.stage.ignore )') +test.todo('pear stage --json ') +test.todo('pear stage --dry-run ') +test.todo('pear stage --dry-run --json ') +test.todo('pear stage --bare ') +test.todo('pear stage --bare --json ') +test.todo('pear stage --ignore ') +test.todo('pear stage --ignore --json ') +test.todo('pear stage --truncate ') +test.todo('pear stage --truncate --json ') +test.todo('pear stage --name ') +test.todo('pear stage --name --json ') +test.todo('pear stage --ignore --name ') +test.todo('pear stage --ignore --name --json ') +test.todo('pear stage --dry-run --bare --ignore --truncate --name ') +test.todo('pear stage --dry-run --bare --ignore --truncate --name --json ') +test.todo('pear stage pear://') +test.todo('pear stage --json pear://') +test.todo('pear stage --dry-run pear://') +test.todo('pear stage --dry-run --json pear://') +test.todo('pear stage --bare pear://') +test.todo('pear stage --bare --json pear://') +test.todo('pear stage --ignore pear://') +test.todo('pear stage --ignore --json pear://') +test.todo('pear stage --truncate pear://') +test.todo('pear stage --truncate --json pear://') +test.todo('pear stage --name pear://') +test.todo('pear stage --name --json pear://') +test.todo('pear stage --ignore --name pear://') +test.todo('pear stage --ignore --name --json pear://') +test.todo('pear stage --dry-run --bare --ignore --truncate --name pear://') +test.todo('pear stage --dry-run --bare --ignore --truncate --name --json pear://') + +test.todo('pear seed ') +test.todo('pear seed ') +test.todo('pear seed --json ') +test.todo('pear seed --seeders ') +test.todo('pear seed --seeders --json ') +test.todo('pear seed --name ') +test.todo('pear seed --name --json ') +test.todo('pear seed pear://') +test.todo('pear seed --json pear://') +test.todo('pear seed --seeders pear://') +test.todo('pear seed --seeders --json pear://') +test.todo('pear seed --name pear://') +test.todo('pear seed --name --json pear://') + +test.todo('pear run ') +test.todo('pear run ') +test.todo('pear run --tmp-store') +test.todo('pear run --store ') +test.todo('pear run --store ') +test.todo('pear run --unsafe-clear-app-storage') +test.todo('pear run --unsafe-clear-preferences') +test.todo('pear run file:///') +test.todo('pear run pear://') +test.todo('pear run pear:///') +test.todo('pear run pear:// --store ') +test.todo('pear run pear:// --tmp-store') +test.todo('pear run pear:// --unsafe-clear-app-storage') +test.todo('pear run pear:// --unsafe-clear-preferences') + +test.todo('pear run (package.json pear.config.previewFor )') +test.todo('pear run (package.json pear.config.links )') +test.todo('pear run (package.json pear.config.links )') +test.todo('pear run (package.json pear.config.type )') +test.todo('pear run --updates-diff') +test.todo('pear run --no-updates') +test.todo('pear run --link ') +test.todo('pear run --updates-diff --no-updates') // TODO: after task Paparam flag relationships +test.todo('pear run --tmp-store --store ') // TODO: after task Paparam flag relationships +test.todo('pear run pear:// --updates-diff') +test.todo('pear run pear:// --no-updates') +test.todo('pear run pear:// --link ') +test.todo('pear run pear:// --checkout ') +test.todo('pear run pear:// --checkout release') +test.todo('pear run pear:// --checkout staged') +test.todo('pear run pear:// --checkout --unsafe-clear-app-storage --unsafe-clear-preferences') + +test.todo('pear release ') +test.todo('pear release ') +test.todo('pear release --json ') +test.todo('pear release pear://') +test.todo('pear release --json pear://') +test.todo('pear release --checkout pear://') +test.todo('pear release --checkout --json pear://') +test.todo('pear release --checkout staged pear://') +test.todo('pear release --checkout staged --json pear://') +test.todo('pear release --checkout release pear://') +test.todo('pear release --checkout release --json pear://') + +test.todo('pear info') +test.todo('pear info --json') +test.todo('pear info ') +test.todo('pear info --json ') +test.todo('pear info --changelog ') +test.todo('pear info --changelog --json ') +test.todo('pear info --changelog --metadata ') +test.todo('pear info --changelog --metadata --json ') +test.todo('pear info --changelog --key ') +test.todo('pear info --changelog --key --json ') +test.todo('pear info --changelog --metadata --key ') +test.todo('pear info --changelog --metadata --key --json ') +test.todo('pear info --full-changelog ') +test.todo('pear info --full-changelog --metadata ') +test.todo('pear info --full-changelog --metadata --json ') +test.todo('pear info --full-changelog --metadata --key ') +test.todo('pear info --full-changelog --metadata --key --json ') +test.todo('pear info --full-changelog --changelog') // TODO: after task Paparam flag relationships +test.todo('pear info --metadata ') +test.todo('pear info --metadata --key ') +test.todo('pear info --metadata --key --json ') +test.todo('pear info --key ') +test.todo('pear info --key --json ') +test.todo('pear info pear://') +test.todo('pear info --json pear://') +test.todo('pear info --changelog pear://') +test.todo('pear info --changelog --json pear://') +test.todo('pear info --changelog --metadata pear://') +test.todo('pear info --changelog --metadata --json pear://') +test.todo('pear info --changelog --key pear://') +test.todo('pear info --changelog --key --json pear://') +test.todo('pear info --changelog --metadata --key pear://') +test.todo('pear info --changelog --metadata --key --json pear://') +test.todo('pear info --full-changelog pear://') +test.todo('pear info --full-changelog --metadata pear://') +test.todo('pear info --full-changelog --metadata --json pear://') +test.todo('pear info --full-changelog --metadata --key pear://') +test.todo('pear info --full-changelog --metadata --key --json pear://') +test.todo('pear info --metadata pear://') +test.todo('pear info --metadata --key pear://') +test.todo('pear info --metadata --key --json pear://') +test.todo('pear info --key pear://') +test.todo('pear info --key --json pear://') + +test.todo('pear info --no-changelog ') +test.todo('pear info --no-changelog --json ') +test.todo('pear info --no-metadata ') +test.todo('pear info --no-metadata --json ') +test.todo('pear info --no-key ') +test.todo('pear info --no-key --json ') +test.todo('pear info --no-changelog --no-metadata ') +test.todo('pear info --no-changelog --no-metadata --json ') +test.todo('pear info --no-changelog --no-key ') +test.todo('pear info --no-changelog --no-key --json ') +test.todo('pear info --no-key --no-metadata ') +test.todo('pear info --no-key --no-metadata --json ') +test.todo('pear info --no-changelog --no-metadata --no-key ') +test.todo('pear info --no-changelog --no-metadata --no-key --json ') +test.todo('pear info --no-changelog pear://') +test.todo('pear info --no-changelog --json pear://') +test.todo('pear info --no-metadata pear://') +test.todo('pear info --no-metadata --json pear://') +test.todo('pear info --no-key pear://') +test.todo('pear info --no-key --json pear://') +test.todo('pear info --no-changelog --no-metadata pear://') +test.todo('pear info --no-changelog --no-metadata --json pear://') +test.todo('pear info --no-changelog --no-key pear://') +test.todo('pear info --no-changelog --no-key --json pear://') +test.todo('pear info --no-key --no-metadata pear://') +test.todo('pear info --no-key --no-metadata --json pear://') +test.todo('pear info --no-changelog --no-metadata --no-key pear://') +test.todo('pear info --no-changelog --no-metadata --no-key --json pear://') + +test('pear dump pear:// ', async function ({ plan, is, teardown }) { + plan(5) + const { link } = await rig.getOrCreateDumpInstance() + + const testId = Math.floor(Math.random() * 100000) + const targetDir = path.join(harness, `pear-dump-${testId}`) + teardown(async () => fs.promises.rm(targetDir, { recursive: true })) + + const running = await Helper.open(harness, { tags: ['exit'] }, { lineout: true }) + const argv = ['dump', link, targetDir] + await running.inspector.evaluate(` + __PEAR_TEST__.command(${JSON.stringify(argv)}) + `, { returnByValue: false }) + + let dumpSuccess = false + for await (const line of running.lineout) { + if (line.endsWith('Success')) { + dumpSuccess = true + break + } + } + + await running.inspector.evaluate('__PEAR_TEST__.ipc.destroy()', { returnByValue: false }) + await running.inspector.close() + + is(dumpSuccess, true, 'should dump successfully') + is(fs.existsSync(targetDir), true, 'should create target directory') + is(fs.existsSync(path.join(targetDir, 'package.json')), true, 'should dump package.json') + is(fs.existsSync(path.join(targetDir, 'testfile.txt')), true, 'should dump testfile.txt') + const { code } = await running.until.exit + is(code, 0, 'should have exit code 0') +}) + +test('pear dump pear:// ', async function ({ plan, is, teardown }) { + plan(5) + const { link } = await rig.getOrCreateDumpInstance() + + const testId = Math.floor(Math.random() * 100000) + const targetDir = path.join(minimal, `pear-dump-${testId}`) + const targetDirRelative = path.relative(harness, targetDir) + teardown(async () => fs.promises.rm(targetDir, { recursive: true })) + + const running = await Helper.open(harness, { tags: ['exit'] }, { lineout: true }) + const argv = ['dump', link, targetDirRelative] + await running.inspector.evaluate(` + __PEAR_TEST__.command(${JSON.stringify(argv)}) + `, { returnByValue: false }) + + let dumpSuccess = false + for await (const line of running.lineout) { + if (line.endsWith('Success')) { + dumpSuccess = true + break + } + } + + await running.inspector.evaluate('__PEAR_TEST__.ipc.destroy()', { returnByValue: false }) + await running.inspector.close() + + is(dumpSuccess, true, 'should dump successfully') + is(fs.existsSync(targetDir), true, 'should create target directory') + is(fs.existsSync(path.join(targetDir, 'package.json')), true, 'should dump package.json') + is(fs.existsSync(path.join(targetDir, 'testfile.txt')), true, 'should dump testfile.txt') + const { code } = await running.until.exit + is(code, 0, 'should have exit code 0') +}) + +test('pear dump --checkout pear:// ', async function ({ plan, is, teardown }) { + plan(5) + const { link, versionOld } = await rig.getOrCreateDumpInstance() + + const testId = Math.floor(Math.random() * 100000) + const targetDir = path.join(minimal, `pear-dump-${testId}`) + const targetDirRelative = path.relative(harness, targetDir) + teardown(async () => fs.promises.rm(targetDir, { recursive: true })) + + const running = await Helper.open(harness, { tags: ['exit'] }, { lineout: true }) + const argv = ['dump', link, '--checkout', `${versionOld}`, targetDirRelative] + await running.inspector.evaluate(` + __PEAR_TEST__.command(${JSON.stringify(argv)}) + `, { returnByValue: false }) + + let dumpSuccess = false + for await (const line of running.lineout) { + if (line.endsWith('Success')) { + dumpSuccess = true + break + } + } + + await running.inspector.evaluate('__PEAR_TEST__.ipc.destroy()', { returnByValue: false }) + await running.inspector.close() + + is(dumpSuccess, true, 'should dump successfully') + is(fs.existsSync(targetDir), true, 'should create target directory') + is(fs.existsSync(path.join(targetDir, 'package.json')), true, 'should dump package.json') + is(fs.existsSync(path.join(targetDir, 'testfile.txt')), false, 'should not dump testfile.txt') + const { code } = await running.until.exit + is(code, 0, 'should have exit code 0') +}) + +test('pear dump --checkout staged pear:// ', async function ({ plan, is, teardown }) { + plan(5) + const { link } = await rig.getOrCreateDumpInstance() + + const testId = Math.floor(Math.random() * 100000) + const targetDir = path.join(minimal, `pear-dump-${testId}`) + const targetDirRelative = path.relative(harness, targetDir) + teardown(async () => fs.promises.rm(targetDir, { recursive: true })) + + const running = await Helper.open(harness, { tags: ['exit'] }, { lineout: true }) + const argv = ['dump', link, '--checkout', 'staged', targetDirRelative] + await running.inspector.evaluate(` + __PEAR_TEST__.command(${JSON.stringify(argv)}) + `, { returnByValue: false }) + + let dumpSuccess = false + for await (const line of running.lineout) { + if (line.endsWith('Success')) { + dumpSuccess = true + break + } + } + + await running.inspector.evaluate('__PEAR_TEST__.ipc.destroy()', { returnByValue: false }) + await running.inspector.close() + + is(dumpSuccess, true, 'should dump successfully') + is(fs.existsSync(targetDir), true, 'should create target directory') + is(fs.existsSync(path.join(targetDir, 'package.json')), true, 'should dump package.json') + is(fs.existsSync(path.join(targetDir, 'testfile.txt')), true, 'should dump testfile.txt') + const { code } = await running.until.exit + is(code, 0, 'should have exit code 0') +}) + +// TODO: Uncomment once --checkout release is supported +// test('pear dump --checkout release pear:// ', async function ({ plan, is, teardown }) { +// plan(5) +// const { link } = await rig.getOrCreateDumpInstance() +// +// const testId = Math.floor(Math.random() * 100000) +// const targetDir = path.join(minimal, `pear-dump-${testId}`) +// const targetDirRelative = path.relative(harness, targetDir) +// teardown(async () => fs.promises.rm(targetDir, { recursive: true })) +// +// const running = await Helper.open(harness, { tags: ['exit'] }, { lineout: true }) +// const argv = ['dump', link, '--checkout', 'release', targetDirRelative] +// await running.inspector.evaluate(` +// __PEAR_TEST__.command(${JSON.stringify(argv)}) +// `, { returnByValue: false }) +// +// let dumpSuccess = false +// for await (const line of running.lineout) { +// if (line.endsWith('Success')) { +// dumpSuccess = true +// break +// } +// } +// +// await running.inspector.evaluate('__PEAR_TEST__.ipc.destroy()', { returnByValue: false }) +// await running.inspector.close() +// +// is(dumpSuccess, true, 'should dump successfully') +// is(fs.existsSync(targetDir), true, 'should create target directory') +// is(fs.existsSync(path.join(targetDir, 'package.json')), true, 'should dump package.json') +// is(fs.existsSync(path.join(targetDir, 'testfile.txt')), false, 'should not dump testfile.txt') +// const { code } = await running.until.exit +// is(code, 0, 'should have exit code 0') +// }) + +test('pear dump --json pear:// ', async function ({ plan, is, alike, teardown }) { + plan(5) + const { link } = await rig.getOrCreateDumpInstance() + + const testId = Math.floor(Math.random() * 100000) + const targetDir = path.join(minimal, `pear-dump-${testId}`) + const targetDirRelative = path.relative(harness, targetDir) + teardown(async () => fs.promises.rm(targetDir, { recursive: true })) + + const running = await Helper.open(harness, { tags: ['exit'] }, { lineout: true }) + const argv = ['dump', '--json', link, targetDirRelative] + await running.inspector.evaluate(` + __PEAR_TEST__.command(${JSON.stringify(argv)}) + `, { returnByValue: false }) + + const seen = new Set() + const tags = [] + for await (const line of running.lineout) { + const result = JSON.parse(line) + if (seen.has(result.tag)) continue + seen.add(result.tag) + tags.push(result.tag) + if (result.tag === 'final') break + } + + await running.inspector.evaluate('__PEAR_TEST__.ipc.destroy()', { returnByValue: false }) + await running.inspector.close() + + alike(tags, ['dumping', 'byte-diff', 'final'], 'should output correct tags') + is(fs.existsSync(targetDir), true, 'should create target directory') + is(fs.existsSync(path.join(targetDir, 'package.json')), true, 'should dump package.json') + is(fs.existsSync(path.join(targetDir, 'testfile.txt')), true, 'should dump testfile.txt') + const { code } = await running.until.exit + is(code, 0, 'should have exit code 0') +}) + +test.todo('pear shift ') +test.todo('pear shift --json ') +test.todo('pear shift --force ') +test.todo('pear shift --force --json ') + +test.todo('pear gc releases') +test.todo('pear gc releases --json') +test.todo('pear gc sidecars') +test.todo('pear gc sidecars --json') + +test.todo('pear versions') +test.todo('pear versions --json') +test.todo('pear -v') + test('commands cleanup', rig.cleanup) diff --git a/test/index.js b/test/index.js index a00674c27..8aa5cc718 100644 --- a/test/index.js +++ b/test/index.js @@ -11,7 +11,7 @@ async function runTests () { await import('./02-shutdown.test.js') await import('./03-teardown.test.js') await import('./04-updates.test.js') - // await import('./05-commands.test.js') + await import('./05-commands.test.js') await import('./06-worker.test.js') test.resume()