From d065bd5698ad89fa0fb8cbc5127cac6514e053d3 Mon Sep 17 00:00:00 2001 From: Milap Naik Date: Tue, 19 Mar 2024 15:39:00 -0400 Subject: [PATCH] Delete convertToSquashfs, move deployAndSignPackage tests to cli, move retrieveSignedPackage tests to under createSignedPackage --- src/RokuDeploy.spec.ts | 80 ++++++++++++++++++++++++++++++++++++++++ src/RokuDeployOptions.ts | 5 --- src/cli.spec.ts | 58 +++++++++++++++++++++-------- 3 files changed, 123 insertions(+), 20 deletions(-) diff --git a/src/RokuDeploy.spec.ts b/src/RokuDeploy.spec.ts index ea25416..ebc2cf6 100644 --- a/src/RokuDeploy.spec.ts +++ b/src/RokuDeploy.spec.ts @@ -1276,8 +1276,31 @@ describe('index', () => { }); describe('createSignedPackage', () => { + let onHandler: any; beforeEach(() => { fsExtra.outputFileSync(`${stagingDir}/manifest`, ``); + sinon.stub(fsExtra, 'ensureDir').callsFake(((pth: string, callback: (err: Error) => void) => { + //do nothing, assume the dir gets created + }) as any); + + //intercept the http request + sinon.stub(request, 'get').callsFake(() => { + let req: any = { + on: (event, callback) => { + process.nextTick(() => { + onHandler(event, callback); + }); + return req; + }, + pipe: async () => { + //if a write stream gets created, write some stuff and close it + const writeStream = await writeStreamPromise; + writeStream.write('test'); + writeStream.close(); + } + }; + return req; + }); }); it('should return our error if signingPassword is not supplied', async () => { @@ -1395,6 +1418,63 @@ describe('index', () => { `Package signing cancelled: provided devId '123' does not match on-device devId '789'` ); }); + + it('returns a pkg file path on success', async () => { + //the write stream should return null, which causes a specific branch to be executed + createWriteStreamStub.callsFake(() => { + return null; + }); + + // let onHandler: any; + onHandler = (event, callback) => { + if (event === 'response') { + callback({ + statusCode: 200 + }); + } + }; + + let body = `var pkgDiv = document.createElement('div'); + pkgDiv.innerHTML = '
P6953175d5df120c0069c53de12515b9a.pkg
package file (7360 bytes)
'; + node.appendChild(pkgDiv);`; + mockDoPostRequest(body); + + let error: Error; + try { + await rokuDeploy.createSignedPackage({ + host: '1.2.3.4', + password: 'password', + signingPassword: options.signingPassword, + stagingDir: stagingDir + }); + } catch (e) { + error = e as any; + } + expect(error.message.startsWith('Unable to create write stream for')).to.be.true; + }); + + it('throws when error in request is encountered', async () => { + onHandler = (event, callback) => { + if (event === 'error') { + callback(new Error('Some error')); + } + }; + + let body = `var pkgDiv = document.createElement('div'); + pkgDiv.innerHTML = '
P6953175d5df120c0069c53de12515b9a.pkg
package file (7360 bytes)
'; + node.appendChild(pkgDiv);`; + mockDoPostRequest(body); + + await expectThrowsAsync( + rokuDeploy.createSignedPackage({ + host: '1.2.3.4', + password: 'aaaa', + signingPassword: options.signingPassword, + stagingDir: stagingDir + }), + 'Some error' + ); + }); }); describe('prepublishToStaging', () => { diff --git a/src/RokuDeployOptions.ts b/src/RokuDeployOptions.ts index 9fcc638..058b22b 100644 --- a/src/RokuDeployOptions.ts +++ b/src/RokuDeployOptions.ts @@ -119,11 +119,6 @@ export interface RokuDeployOptions { */ devId?: string; - /** - * If true we convert to squashfs before creating the pkg file - */ - convertToSquashfs?: boolean; - /** * If true, the publish will fail on compile error */ diff --git a/src/cli.spec.ts b/src/cli.spec.ts index f836b7a..fd9468a 100644 --- a/src/cli.spec.ts +++ b/src/cli.spec.ts @@ -269,18 +269,38 @@ describe('cli', () => { expectPathExists(`${outDir}/roku-deploy.zip`); }); +}); + +describe('ExecCommand', () => { + beforeEach(() => { + fsExtra.emptyDirSync(tempDir); + //most tests depend on a manifest file existing, so write an empty one + fsExtra.outputFileSync(`${rootDir}/manifest`, ''); + sinon.restore(); + }); + afterEach(() => { + fsExtra.removeSync(tempDir); + sinon.restore(); + }); + function mockDoPostRequest(body = '', statusCode = 200) { + return sinon.stub(rokuDeploy as any, 'doPostRequest').callsFake((params) => { + let results = { response: { statusCode: statusCode }, body: body }; + rokuDeploy['checkRequest'](results); + return Promise.resolve(results); + }); + } it('does the whole migration', async () => { const mock = mockDoPostRequest(); - const args = { + const options = { host: '1.2.3.4', password: 'abcd', rootDir: rootDir, stagingDir: stagingDir, outDir: outDir }; - await new ExecCommand('stage|zip|close|sideload', args).run(); + await new ExecCommand('stage|zip|close|sideload', options).run(); expect(mock.getCall(2).args[0].url).to.equal('http://1.2.3.4:80/plugin_install'); expectPathExists(`${outDir}/roku-deploy.zip`); @@ -293,14 +313,14 @@ describe('cli', () => { ) ); const mock = mockDoPostRequest(); - const args = { + const options = { host: '1.2.3.4', password: 'abcd', rootDir: rootDir, stagingDir: stagingDir, outDir: outDir }; - await new ExecCommand('stage|zip|close|sideload', args).run(); + await new ExecCommand('stage|zip|close|sideload', options).run(); expect(mock.getCall(0).args[0].url).to.equal('http://1.2.3.4:8060/keypress/home'); expectPathExists(`${outDir}/roku-deploy.zip`); }); @@ -308,7 +328,7 @@ describe('cli', () => { it('should delete installed channel if requested', async () => { const spy = sinon.spy(rokuDeploy, 'deleteDevChannel'); mockDoPostRequest(); - const args = { + const options = { host: '1.2.3.4', password: 'abcd', rootDir: rootDir, @@ -317,7 +337,7 @@ describe('cli', () => { deleteDevChannel: true }; - await new ExecCommand('stage|zip|close|sideload', args).run(); + await new ExecCommand('stage|zip|close|sideload', options).run(); expect(spy.called).to.equal(true); }); @@ -325,7 +345,7 @@ describe('cli', () => { const spy = sinon.spy(rokuDeploy, 'deleteDevChannel'); mockDoPostRequest(); - const args = { + const options = { host: '1.2.3.4', password: 'abcd', rootDir: rootDir, @@ -334,15 +354,23 @@ describe('cli', () => { deleteDevChannel: false }; - await new ExecCommand('stage|zip|close|sideload', args).run(); + await new ExecCommand('stage|zip|close|sideload', options).run(); expect(spy.notCalled).to.equal(true); }); - function mockDoPostRequest(body = '', statusCode = 200) { - return sinon.stub(rokuDeploy as any, 'doPostRequest').callsFake((params) => { - let results = { response: { statusCode: statusCode }, body: body }; - rokuDeploy['checkRequest'](results); - return Promise.resolve(results); - }); - } + it('converts to squashfs if we request it to', async () => { + let stub = sinon.stub(rokuDeploy, 'convertToSquashfs').returns(Promise.resolve(null)); + mockDoPostRequest(); + const options = { + host: '1.2.3.4', + password: 'abcd', + rootDir: rootDir, + stagingDir: stagingDir, + outDir: outDir, + deleteDevChannel: false + }; + + await new ExecCommand('close|stage|zip|close|sideload|squash', options).run(); + expect(stub.getCalls()).to.be.lengthOf(1); + }); });