From 0986ec525e941cb416c1f27355a0782a399a205f Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Tue, 13 Aug 2019 09:37:45 -0700 Subject: [PATCH] Throw error when unable to compute hash --- src/client/common/platform/fileSystem.ts | 6 +++--- src/client/common/platform/types.ts | 2 +- src/test/common/platform/filesystem.unit.test.ts | 11 +++++++++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/client/common/platform/fileSystem.ts b/src/client/common/platform/fileSystem.ts index 8e31446c4774..dede671d3fc6 100644 --- a/src/client/common/platform/fileSystem.ts +++ b/src/client/common/platform/fileSystem.ts @@ -146,11 +146,11 @@ export class FileSystem implements IFileSystem { return deferred.promise; } - public getFileHash(filePath: string): Promise { - return new Promise(resolve => { + public getFileHash(filePath: string): Promise { + return new Promise((resolve, reject) => { fs.lstat(filePath, (err, stats) => { if (err) { - resolve(); + reject(err); } else { const actual = createHash('sha512').update(`${stats.ctimeMs}-${stats.mtimeMs}`).digest('hex'); resolve(actual); diff --git a/src/client/common/platform/types.ts b/src/client/common/platform/types.ts index d14a5001cef1..aa0baa2e2aa5 100644 --- a/src/client/common/platform/types.ts +++ b/src/client/common/platform/types.ts @@ -56,7 +56,7 @@ export interface IFileSystem { getRealPath(path: string): Promise; copyFile(src: string, dest: string): Promise; deleteFile(filename: string): Promise; - getFileHash(filePath: string): Promise; + getFileHash(filePath: string): Promise; search(globPattern: string): Promise; createTemporaryFile(extension: string): Promise; createWriteStream(path: string): fs.WriteStream; diff --git a/src/test/common/platform/filesystem.unit.test.ts b/src/test/common/platform/filesystem.unit.test.ts index 2e65335b057f..2100e0810d9a 100644 --- a/src/test/common/platform/filesystem.unit.test.ts +++ b/src/test/common/platform/filesystem.unit.test.ts @@ -9,6 +9,7 @@ import { FileSystem } from '../../../client/common/platform/fileSystem'; import { IFileSystem, IPlatformService, TemporaryFile } from '../../../client/common/platform/types'; // tslint:disable-next-line:no-require-imports no-var-requires const assertArrays = require('chai-arrays'); +use(require('chai-as-promised')); use(assertArrays); // tslint:disable-next-line:max-func-body-length @@ -116,4 +117,14 @@ suite('FileSystem', () => { }); }); }); + test('Getting hash for non existent file should throw error', async () => { + const promise = fileSystem.getFileHash('some unknown file'); + + await expect(promise).to.eventually.be.rejected; + }); + test('Getting hash for a file should return non-empty string', async () => { + const hash = await fileSystem.getFileHash(__filename); + + expect(hash).to.be.length.greaterThan(0); + }); });