Skip to content

Commit

Permalink
Resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Tatyana Kostromskaya authored Mar 23, 2022
1 parent f99e312 commit e5c65b6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 35 deletions.
23 changes: 0 additions & 23 deletions test/units/toolTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,29 +132,6 @@ describe('Tool Tests', function () {
});
});

it('different content-length and file size on disk', async function() {
const headers: nock.ReplyHeaders = { 'content-length': '200' };
nock('https://microsoft.com')
.get('/bytes/36')
.reply(200, {
username: 'bad',
password: 'size'
}, headers);

return new Promise<void>(async(resolve, reject)=> {
try {
let errorCodeUrl: string = "https://microsoft.com/bytes/36";
let downPath: string = await toolLib.downloadTool(errorCodeUrl);

reject('content-length of file and size of file on disk should not match, but they do');
}
catch (err){
assert.equal(err.message, `Content-Length (200 bytes) did not match downloaded file size (36 bytes).`);
resolve();
}
});
});

it('works with redirect code 302', async function () {
nock('https://microsoft.com')
.get('/redirect-to')
Expand Down
25 changes: 13 additions & 12 deletions tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,15 @@ export async function downloadTool(
let stream = response.message.pipe(file);
stream.on('close', () => {
tl.debug('download complete');
let fileSizeInBytes = _getFileSizeOnDisk(destPath);
let fileSizeInBytes: number;
try {
fileSizeInBytes = _getFileSizeOnDisk(destPath);
}
catch (err) {
fileSizeInBytes = NaN;
tl.warning(`Unable to check file size of ${destPath} due to error: ${err.Message}`);
}

if (!isNaN(fileSizeInBytes)) {
tl.debug(`Downloaded file size: ${fileSizeInBytes} bytes`);
} else {
Expand All @@ -265,8 +273,7 @@ export async function downloadTool(
if (!isNaN(downloadedContentLength) &&
!isNaN(fileSizeInBytes) &&
fileSizeInBytes !== downloadedContentLength) {
let err: Error = new Error(`Content-Length (${downloadedContentLength} bytes) did not match downloaded file size (${fileSizeInBytes} bytes).`);
reject(err);
tl.warning(`Content-Length (${downloadedContentLength} bytes) did not match downloaded file size (${fileSizeInBytes} bytes).`);
}

resolve(destPath);
Expand Down Expand Up @@ -308,15 +315,9 @@ function _getContentLengthOfDownloadedFile(response: httpm.HttpClientResponse):
* @param filePath the path to the file, saved to the disk
*/
function _getFileSizeOnDisk(filePath: string): number {
try {
let fileStats = fs.statSync(filePath);
let fileSizeInBytes = fileStats.size;
return fileSizeInBytes;
}
catch (err) {
tl.warning(`Unable to find file size for ${filePath} due to error: ${err.Message}`);
return NaN;
}
let fileStats = fs.statSync(filePath);
let fileSizeInBytes = fileStats.size;
return fileSizeInBytes;
}

//---------------------
Expand Down

0 comments on commit e5c65b6

Please sign in to comment.