Skip to content

Commit

Permalink
Merge pull request #623 from browserstack/add_decompress_support
Browse files Browse the repository at this point in the history
Add decompress support
  • Loading branch information
agrawalsaurabhs committed Jul 11, 2023
2 parents e9aeb7d + 996138b commit a4b975a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 17 deletions.
14 changes: 8 additions & 6 deletions bin/helpers/buildArtifacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
const fs = require('fs'),
path = require('path');

const unzipper = require('unzipper');

const logger = require('./logger').winstonLogger,
utils = require("./utils"),
Constants = require("./constants"),
config = require("./config");

const request = require('request');
const decompress = require('decompress');


let BUILD_ARTIFACTS_TOTAL_COUNT = 0;
Expand Down Expand Up @@ -127,10 +126,13 @@ const downloadAndUnzip = async (filePath, fileName, url) => {

const unzipFile = async (filePath, fileName) => {
return new Promise( async (resolve, reject) => {
await unzipper.Open.file(path.join(filePath, fileName))
.then(d => d.extract({path: filePath, concurrency: 5}))
.catch((err) => reject(err));
resolve();
await decompress(path.join(filePath, fileName), filePath)
.then((files) => {
resolve();
})
.catch((error) => {
reject(error);
});
});
}

Expand Down
17 changes: 9 additions & 8 deletions bin/helpers/reporterHTML.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const fs = require('fs'),
path = require('path'),
request = require('request'),
unzipper = require('unzipper'),
logger = require('./logger').winstonLogger,
utils = require("./utils"),
Constants = require('./constants'),
config = require("./config");
config = require("./config"),
decompress = require('decompress');

let reportGenerator = (bsConfig, buildId, args, rawArgs, buildReportData, cb) => {
let options = {
Expand Down Expand Up @@ -150,14 +150,15 @@ function getReportResponse(filePath, fileName, reportJsonUrl) {

const unzipFile = async (filePath, fileName) => {
return new Promise( async (resolve, reject) => {
await unzipper.Open.file(path.join(filePath, fileName))
.then(d => d.extract({path: filePath, concurrency: 5}))
.catch((err) => {
reject(err);
process.exitCode = Constants.ERROR_EXIT_CODE;
});
await decompress(path.join(filePath, fileName), filePath)
.then((files) => {
let message = "Unzipped the json and html successfully."
resolve(message);
})
.catch((error) => {
reject(error);
process.exitCode = Constants.ERROR_EXIT_CODE;
});
});
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
"request": "2.88.2",
"requestretry": "7.1.0",
"table": "5.4.6",
"unzipper": "0.10.11",
"update-notifier": "5.1.0",
"uuid": "8.3.2",
"windows-release": "^5.1.0",
"winston": "2.4.4",
"yargs": "14.2.3"
"yargs": "14.2.3",
"decompress": "4.2.1"
},
"repository": {
"type": "git",
Expand Down
36 changes: 35 additions & 1 deletion test/unit/bin/helpers/reporterHTML.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const chai = require("chai"),
const fs = require('fs'),
path = require('path'),
request = require('request'),
unzipper = require('unzipper');
decompress = require('decompress');
Constants = require("../../../../bin/helpers/constants"),
logger = require("../../../../bin/helpers/logger").winstonLogger,
testObjects = require("../../support/fixtures/testObjects"),
Expand Down Expand Up @@ -241,6 +241,40 @@ describe("calls API to generate report", () => {
});
});

describe("unzipFile", () => {
var sandbox;
beforeEach(() => {
sandbox = sinon.createSandbox();
});

afterEach(() => {
sandbox.restore();
sinon.restore();
});

it("calls unzip and resolves with success message", () => {
let pathStub = sinon.stub(path, 'join');
pathStub.calledOnceWith('abc','efg.txt');
let decompressStub = sandbox.stub().returns(Promise.resolve("Unzipped the json and html successfully."));
let rewireReporterHTML = rewire('../../../../bin/helpers/reporterHTML');
rewireReporterHTML.__set__('decompress', decompressStub);
let unzipFile = rewireReporterHTML.__get__('unzipFile')
unzipFile('abc', 'efg');
});

it("calls unzip and rejects with error message on failure", () => {
let pathStub = sinon.stub(path, 'join');
pathStub.calledOnceWith('abc','efg.txt');
let processStub = sinon.stub(process, 'exit');
processStub.returns(Constants.ERROR_EXIT_CODE)
let decompressStub = sandbox.stub().returns(Promise.reject("Error"));
let rewireReporterHTML = rewire('../../../../bin/helpers/reporterHTML');
rewireReporterHTML.__set__('decompress', decompressStub);
let unzipFile = rewireReporterHTML.__get__('unzipFile')
unzipFile('abc', 'efg');
});
});

describe("generateCypressBuildReport", () => {
var sandbox;
beforeEach(() => {
Expand Down

0 comments on commit a4b975a

Please sign in to comment.