|
7 | 7 | import { fail } from 'assert';
|
8 | 8 | import { join } from 'path';
|
9 | 9 | import { expect } from 'chai';
|
10 |
| -import { createSandbox, match } from 'sinon'; |
| 10 | +import * as unzipper from 'unzipper'; |
| 11 | +import { createSandbox, match, SinonStub } from 'sinon'; |
11 | 12 | import { getString } from '@salesforce/ts-types';
|
12 | 13 | import * as fs from 'graceful-fs';
|
13 | 14 | import {
|
14 | 15 | ComponentSet,
|
15 | 16 | ComponentStatus,
|
16 | 17 | FileResponse,
|
| 18 | + MetadataApiRetrieve, |
17 | 19 | MetadataApiRetrieveStatus,
|
| 20 | + RequestStatus, |
18 | 21 | RetrieveResult,
|
19 | 22 | SourceComponent,
|
20 | 23 | VirtualTreeContainer,
|
@@ -407,6 +410,67 @@ describe('MetadataApiRetrieve', () => {
|
407 | 410 | });
|
408 | 411 | });
|
409 | 412 |
|
| 413 | + describe('post', () => { |
| 414 | + const output = join('mdapi', 'retrieve', 'dir'); |
| 415 | + const format = 'metadata'; |
| 416 | + const zipFile = 'abcd1234'; |
| 417 | + const zipFileContents = Buffer.from(zipFile, 'base64'); |
| 418 | + const usernameOrConnection = '[email protected]'; |
| 419 | + const fakeResults = { status: RequestStatus.Succeeded, zipFile } as MetadataApiRetrieveStatus; |
| 420 | + let writeFileStub: SinonStub; |
| 421 | + let openBufferStub: SinonStub; |
| 422 | + let extractStub: SinonStub; |
| 423 | + const mdapiRetrieveExtractStub = env.stub().resolves({}); |
| 424 | + |
| 425 | + beforeEach(() => { |
| 426 | + writeFileStub = env.stub(fs, 'writeFileSync'); |
| 427 | + extractStub = env.stub().resolves(); |
| 428 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 429 | + openBufferStub = env.stub(unzipper.Open, 'buffer').resolves({ extract: extractStub } as any); |
| 430 | + }); |
| 431 | + |
| 432 | + it('should write the retrieved zip when format=metadata', async () => { |
| 433 | + const mdapiRetrieve = new MetadataApiRetrieve({ usernameOrConnection, output, format }); |
| 434 | + // @ts-ignore overriding private method |
| 435 | + mdapiRetrieve.extract = mdapiRetrieveExtractStub; |
| 436 | + await mdapiRetrieve.post(fakeResults); |
| 437 | + |
| 438 | + expect(writeFileStub.calledOnce).to.be.true; |
| 439 | + expect(writeFileStub.firstCall.args[0]).to.equal(join(output, 'unpackaged.zip')); |
| 440 | + expect(writeFileStub.firstCall.args[1]).to.deep.equal(zipFileContents); |
| 441 | + expect(openBufferStub.called).to.be.false; |
| 442 | + expect(mdapiRetrieveExtractStub.called).to.be.false; |
| 443 | + }); |
| 444 | + |
| 445 | + it('should unzip the retrieved zip when format=metadata and unzip=true', async () => { |
| 446 | + const mdapiRetrieve = new MetadataApiRetrieve({ usernameOrConnection, output, format, unzip: true }); |
| 447 | + // @ts-ignore overriding private method |
| 448 | + mdapiRetrieve.extract = mdapiRetrieveExtractStub; |
| 449 | + await mdapiRetrieve.post(fakeResults); |
| 450 | + |
| 451 | + expect(writeFileStub.calledOnce).to.be.true; |
| 452 | + expect(writeFileStub.firstCall.args[0]).to.equal(join(output, 'unpackaged.zip')); |
| 453 | + expect(writeFileStub.firstCall.args[1]).to.deep.equal(zipFileContents); |
| 454 | + expect(openBufferStub.called).to.be.true; |
| 455 | + expect(extractStub.called).to.be.true; |
| 456 | + expect(mdapiRetrieveExtractStub.called).to.be.false; |
| 457 | + }); |
| 458 | + |
| 459 | + it('should write the retrieved zip with specified name when format=metadata and zipFileName is set', async () => { |
| 460 | + const zipFileName = 'retrievedFiles.zip'; |
| 461 | + const mdapiRetrieve = new MetadataApiRetrieve({ usernameOrConnection, output, format, zipFileName }); |
| 462 | + // @ts-ignore overriding private method |
| 463 | + mdapiRetrieve.extract = mdapiRetrieveExtractStub; |
| 464 | + await mdapiRetrieve.post(fakeResults); |
| 465 | + |
| 466 | + expect(writeFileStub.calledOnce).to.be.true; |
| 467 | + expect(writeFileStub.firstCall.args[0]).to.equal(join(output, zipFileName)); |
| 468 | + expect(writeFileStub.firstCall.args[1]).to.deep.equal(zipFileContents); |
| 469 | + expect(openBufferStub.called).to.be.false; |
| 470 | + expect(mdapiRetrieveExtractStub.called).to.be.false; |
| 471 | + }); |
| 472 | + }); |
| 473 | + |
410 | 474 | describe('cancel', () => {
|
411 | 475 | it('should immediately stop polling', async () => {
|
412 | 476 | const component = COMPONENT;
|
|
0 commit comments