|
| 1 | +import { Yok } from "../../../yok"; |
| 2 | +import { FilesHashService } from "../../../../services/files-hash-service"; |
| 3 | +import { FileSystemStub, LoggerStub } from "../../../../../test/stubs"; |
| 4 | +import { assert } from "chai"; |
| 5 | + |
| 6 | +const fileHashes = { |
| 7 | + "file1": "hash1", |
| 8 | + "file2": "hash2", |
| 9 | + "file3": "hash3", |
| 10 | + "file4": "hash4", |
| 11 | + "file5": "hash5", |
| 12 | + "file6": "hash6", |
| 13 | + "file7": "hash7" |
| 14 | +}; |
| 15 | + |
| 16 | +function createTestInjector(): IInjector { |
| 17 | + const injector = new Yok(); |
| 18 | + injector.register("fs", FileSystemStub); |
| 19 | + injector.register("logger", LoggerStub); |
| 20 | + injector.register("filesHashService", FilesHashService); |
| 21 | + |
| 22 | + return injector; |
| 23 | +} |
| 24 | + |
| 25 | +function addFileHashes(hashes: IStringDictionary) { |
| 26 | + const result = {}; |
| 27 | + _.extend(result, fileHashes, hashes); |
| 28 | + return result; |
| 29 | +} |
| 30 | + |
| 31 | +function removeFileHashes(hashes: IStringDictionary) { |
| 32 | + const result = _.omitBy(fileHashes, (hash: string, filePath: string) => !!_.find(hashes, (newHash: string, newFilePath: string) => newHash === hash && newFilePath === filePath)); |
| 33 | + return result; |
| 34 | +} |
| 35 | + |
| 36 | +function mockFilesHashService(hashes: IStringDictionary): IFilesHashService { |
| 37 | + const injector = createTestInjector(); |
| 38 | + const filesHashService = injector.resolve("filesHashService"); |
| 39 | + filesHashService.generateHashes = async () => hashes; |
| 40 | + |
| 41 | + return filesHashService; |
| 42 | +} |
| 43 | + |
| 44 | +describe("filesHashService", () => { |
| 45 | + const testCases = [ |
| 46 | + { |
| 47 | + name: "should not return changes when no files are changed", |
| 48 | + newHashes: fileHashes, |
| 49 | + oldHashes: fileHashes, |
| 50 | + expectedChanges: {} |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "should return changes when a file is added", |
| 54 | + newHashes: addFileHashes({ "file8": "hash8" }), |
| 55 | + oldHashes: fileHashes, |
| 56 | + expectedChanges: { "file8": "hash8" } |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "should return changes when a file is removed", |
| 60 | + newHashes: removeFileHashes({ "file7": "hash7" }), |
| 61 | + oldHashes: fileHashes, |
| 62 | + expectedChanges: { "file7": "hash7" } |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "should return changes when a file is added and a file is removed from oldHashes", |
| 66 | + newHashes: addFileHashes({ "file9": "hash9" }), |
| 67 | + oldHashes: removeFileHashes({ "file1": "hash1" }), |
| 68 | + expectedChanges: { "file1": "hash1", "file9": "hash9" } |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "should return changes when no oldHashes are provided", |
| 72 | + newHashes: fileHashes, |
| 73 | + oldHashes: {}, |
| 74 | + expectedChanges: fileHashes |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "should return changes when no newHashes are provided", |
| 78 | + newHashes: {}, |
| 79 | + oldHashes: fileHashes, |
| 80 | + expectedChanges: fileHashes |
| 81 | + } |
| 82 | + ]; |
| 83 | + |
| 84 | + describe("getChanges", () => { |
| 85 | + _.each(testCases, (testCase: any) => { |
| 86 | + it(`${testCase.name}`, async () => { |
| 87 | + const filesHashService = mockFilesHashService(testCase.newHashes); |
| 88 | + const changes = await filesHashService.getChanges(_.keys(testCase.newHashes), testCase.oldHashes); |
| 89 | + assert.deepEqual(changes, testCase.expectedChanges); |
| 90 | + }); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + describe("hasChangesInShasums", () => { |
| 95 | + _.each(testCases, (testCase: any) => { |
| 96 | + it(`${testCase.name}`, () => { |
| 97 | + const filesHashService = mockFilesHashService(testCase.newHashes); |
| 98 | + const hasChanges = filesHashService.hasChangesInShasums(testCase.newHashes, testCase.oldHashes); |
| 99 | + assert.deepEqual(hasChanges, !!_.keys(testCase.expectedChanges).length); |
| 100 | + }); |
| 101 | + }); |
| 102 | + }); |
| 103 | +}); |
0 commit comments