Skip to content

fix: respect removed files from app folder #3945

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions lib/common/test/unit-tests/services/files-hash-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { Yok } from "../../../yok";
import { FilesHashService } from "../../../../services/files-hash-service";
import { FileSystemStub, LoggerStub } from "../../../../../test/stubs";
import { assert } from "chai";

const fileHashes = {
"file1": "hash1",
"file2": "hash2",
"file3": "hash3",
"file4": "hash4",
"file5": "hash5",
"file6": "hash6",
"file7": "hash7"
};

function createTestInjector(): IInjector {
const injector = new Yok();
injector.register("fs", FileSystemStub);
injector.register("logger", LoggerStub);
injector.register("filesHashService", FilesHashService);

return injector;
}

function addFileHashes(hashes: IStringDictionary) {
const result = {};
_.extend(result, fileHashes, hashes);
return result;
}

function removeFileHashes(hashes: IStringDictionary) {
const result = _.omitBy(fileHashes, (hash: string, filePath: string) => !!_.find(hashes, (newHash: string, newFilePath: string) => newHash === hash && newFilePath === filePath));
return result;
}

function mockFilesHashService(hashes: IStringDictionary): IFilesHashService {
const injector = createTestInjector();
const filesHashService = injector.resolve("filesHashService");
filesHashService.generateHashes = async () => hashes;

return filesHashService;
}

describe("filesHashService", () => {
const testCases = [
{
name: "should not return changes when no files are changed",
newHashes: fileHashes,
oldHashes: fileHashes,
expectedChanges: {}
},
{
name: "should return changes when a file is added",
newHashes: addFileHashes({ "file8": "hash8" }),
oldHashes: fileHashes,
expectedChanges: { "file8": "hash8" }
},
{
name: "should return changes when a file is removed",
newHashes: removeFileHashes({ "file7": "hash7" }),
oldHashes: fileHashes,
expectedChanges: { "file7": "hash7" }
},
{
name: "should return changes when a file is added and a file is removed from oldHashes",
newHashes: addFileHashes({ "file9": "hash9" }),
oldHashes: removeFileHashes({ "file1": "hash1" }),
expectedChanges: { "file1": "hash1", "file9": "hash9" }
},
{
name: "should return changes when no oldHashes are provided",
newHashes: fileHashes,
oldHashes: {},
expectedChanges: fileHashes
},
{
name: "should return changes when no newHashes are provided",
newHashes: {},
oldHashes: fileHashes,
expectedChanges: fileHashes
}
];

describe("getChanges", () => {
_.each(testCases, (testCase: any) => {
it(`${testCase.name}`, async () => {
const filesHashService = mockFilesHashService(testCase.newHashes);
const changes = await filesHashService.getChanges(_.keys(testCase.newHashes), testCase.oldHashes);
assert.deepEqual(changes, testCase.expectedChanges);
});
});
});

describe("hasChangesInShasums", () => {
_.each(testCases, (testCase: any) => {
it(`${testCase.name}`, () => {
const filesHashService = mockFilesHashService(testCase.newHashes);
const hasChanges = filesHashService.hasChangesInShasums(testCase.newHashes, testCase.oldHashes);
assert.deepEqual(hasChanges, !!_.keys(testCase.expectedChanges).length);
});
});
});
});
9 changes: 7 additions & 2 deletions lib/services/files-hash-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,13 @@ export class FilesHashService implements IFilesHashService {
this.$fs.writeJson(hashesFilePath, hashes);
}

private getChangesInShasums(oldHashes: IStringDictionary, newHashes: IStringDictionary): IStringDictionary {
return _.omitBy(newHashes, (hash: string, pathToFile: string) => !!_.find(oldHashes, (oldHash: string, oldPath: string) => pathToFile === oldPath && hash === oldHash));
public getChangesInShasums(oldHashes: IStringDictionary, newHashes: IStringDictionary): IStringDictionary {
const addedFileHashes = _.omitBy(newHashes, (hash: string, pathToFile: string) => !!oldHashes[pathToFile] && oldHashes[pathToFile] === hash);
const removedFileHashes = _.omitBy(oldHashes, (hash: string, pathToFile: string) => !!newHashes[pathToFile] && newHashes[pathToFile] === hash);
const result = {};
_.extend(result, addedFileHashes, removedFileHashes);

return result;
}
}
$injector.register("filesHashService", FilesHashService);