From eb52c1ac1d7e4f99d7882b01b0cde64e621bfdff Mon Sep 17 00:00:00 2001 From: Alex Andreev Date: Wed, 16 Feb 2022 15:09:39 +0300 Subject: [PATCH] Testing unpacking tar Signed-off-by: Alex Andreev --- .../__tests__/extension-updater.test.ts | 31 ++++++++++++++++-- .../__tests__/survey-mock.tgz | Bin 0 -> 2048 bytes .../bundled-extension-updater.ts | 11 +++++++ 3 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 src/main/extension-updater/__tests__/survey-mock.tgz diff --git a/src/main/extension-updater/__tests__/extension-updater.test.ts b/src/main/extension-updater/__tests__/extension-updater.test.ts index 73b8886fd3..794aebd803 100644 --- a/src/main/extension-updater/__tests__/extension-updater.test.ts +++ b/src/main/extension-updater/__tests__/extension-updater.test.ts @@ -7,10 +7,11 @@ import { BundledExtensionUpdater } from "../bundled-extension-updater"; import fs from "fs"; import mockFs from "mock-fs"; import nock from "nock"; +import path from "path"; const mockOpts = { "some-user-data-directory": { - "some-file.tgz": "file content here", + "some-file.tgz": mockFs.load(path.resolve(__dirname, "./survey-mock.tgz")), }, "extension-updates": { "file.txt": "text", @@ -47,7 +48,7 @@ describe("BundledExtensionUpdater", () => { expect(exist).toBeTruthy(); }); - it.only("Should skip download if no file found on server", () => { + it("Should skip download if no file found on server", () => { mockFs(mockOpts); nock("http://my-example-url.com") @@ -98,4 +99,30 @@ describe("BundledExtensionUpdater", () => { expect(exist).toBeTruthy(); }); + + it.only("Should unpack downloaded tar file", async () => { + mockFs(mockOpts); + + nock("http://my-example-url.com") + .get("/some-file.tgz") + .replyWithFile(200, `./some-user-data-directory/some-file.tgz`, { + "Content-Type": "application/tar", + }); + + await new BundledExtensionUpdater({ + name: "test-extension", + version: "0.0.1", + downloadUrl: "http://my-example-url.com/some-file.tgz", + }, "./extension-updates").update(); + + fs.readdirSync("./extension-updates/").forEach(file => { + console.log(file); + }); + + const existTar = fs.existsSync("./extension-updates/test-extension-0.0.1.tgz"); + const existFolder = fs.existsSync("./extension-updates/test-extension-0.0.1"); + + expect(existTar).toBeTruthy(); + expect(existFolder).toBeTruthy(); + }); }); diff --git a/src/main/extension-updater/__tests__/survey-mock.tgz b/src/main/extension-updater/__tests__/survey-mock.tgz new file mode 100644 index 0000000000000000000000000000000000000000..d3f05bbaaccf2fe6cf6165d2e8386a19c79cdbac GIT binary patch literal 2048 zcmXR(ttd&$%t_TN0Wuh90}Koe49v~V84Mr*oi;EqG-WU}F)}bVG%++VGG+j(H#Ih3 yP%xm4b5Kn!EiOqc0y+s*bj0vLaj%e^pI4HaS5iD;d_C&X(GVC7fzc2c4FLdrZx+x1 literal 0 HcmV?d00001 diff --git a/src/main/extension-updater/bundled-extension-updater.ts b/src/main/extension-updater/bundled-extension-updater.ts index 92bfec43b0..02f741805c 100644 --- a/src/main/extension-updater/bundled-extension-updater.ts +++ b/src/main/extension-updater/bundled-extension-updater.ts @@ -9,6 +9,7 @@ import request from "request"; import logger from "../logger"; import path from "path"; import { noop } from "../../common/utils"; +import tar from "tar-fs"; type Extension = { name: string @@ -28,6 +29,7 @@ export class BundledExtensionUpdater { public async update() { await this.download(); await this.removePreviousUpdateFolder(); + // await this.unpackTar(); } private get filePath() { @@ -85,4 +87,13 @@ export class BundledExtensionUpdater { }); } } + + private async unpackTar() { + logger.info(`[EXTENSION-UPDATER]: Unpacking '${this.filePath}' into '${this.folderPath}'`); + + const extract = tar.extract("./extension-updates/"); + + // console.log(this.filePath.replace("./", "")) + fs.createReadStream("extension-updates/file.txt").pipe(extract).end(); + } }