Skip to content

Commit 85b1a52

Browse files
author
Fatme
authored
Merge pull request #3945 from NativeScript/fatme/fix-removed-files
fix: respect removed files from app folder
2 parents 6ea9746 + a125c27 commit 85b1a52

File tree

2 files changed

+110
-2
lines changed

2 files changed

+110
-2
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
});

lib/services/files-hash-service.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,13 @@ export class FilesHashService implements IFilesHashService {
5353
this.$fs.writeJson(hashesFilePath, hashes);
5454
}
5555

56-
private getChangesInShasums(oldHashes: IStringDictionary, newHashes: IStringDictionary): IStringDictionary {
57-
return _.omitBy(newHashes, (hash: string, pathToFile: string) => !!_.find(oldHashes, (oldHash: string, oldPath: string) => pathToFile === oldPath && hash === oldHash));
56+
public getChangesInShasums(oldHashes: IStringDictionary, newHashes: IStringDictionary): IStringDictionary {
57+
const addedFileHashes = _.omitBy(newHashes, (hash: string, pathToFile: string) => !!oldHashes[pathToFile] && oldHashes[pathToFile] === hash);
58+
const removedFileHashes = _.omitBy(oldHashes, (hash: string, pathToFile: string) => !!newHashes[pathToFile] && newHashes[pathToFile] === hash);
59+
const result = {};
60+
_.extend(result, addedFileHashes, removedFileHashes);
61+
62+
return result;
5863
}
5964
}
6065
$injector.register("filesHashService", FilesHashService);

0 commit comments

Comments
 (0)