Skip to content

Commit

Permalink
adjust expected item count due to hash collision
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianrath committed Jun 22, 2021
1 parent 38ad18b commit 6865eb4
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions test/5.repo.commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ async function repoTest(t, commondirInside: boolean) {
await Repository.initExt(repoPath, { commondir: `${commondir}.external-snow` });
}

let howManyCharsAreEqualInHash = 0;

const firstCommitMessage = 'Add Foo';
const secondCommitMessage = 'Delete Foo';
await Repository.open(repoPath)
Expand All @@ -45,6 +47,14 @@ async function repoTest(t, commondirInside: boolean) {
const file2 = await createRandomFile(foopath2, 2048);
index.addFiles([file2.filepath]);

if (file1.filehash.substr(0, 4) === file2.filehash.substr(0, 4)) {
howManyCharsAreEqualInHash = 4;
} else if (file1.filehash.substr(0, 2) === file2.filehash.substr(0, 2)) {
howManyCharsAreEqualInHash = 2;
} else {
howManyCharsAreEqualInHash = 0;
}

return index.writeFiles();
}).then(() => repo.createCommit(repo.getFirstIndex(), firstCommitMessage))
.then((commit: Commit) => {
Expand All @@ -61,15 +71,22 @@ async function repoTest(t, commondirInside: boolean) {
})
.then(async (dirItems: DirItem[]) => {
if (commondirInside) {
t.is(dirItems.length, 24, 'expect 24 items');
console.log(dirItems.length);
console.log(dirItems);
await new Promise<void>((resolve) => {
setTimeout(() => {
resolve();
}, 1000);
});
console.log(dirItems.length);
// Two random files can generate hashes where the first 2 or 4 digits are equal.
// Depending on the situation, we expect either 22, 23 or 24 items since we might have fewer
// directories in the object database

// eslint-disable-next-line default-case
switch (howManyCharsAreEqualInHash) {
case 4: // if 4-digits of hash are equal, we have 2 directories less
t.is(dirItems.length, 22, 'expect 22 items');
break;
case 2: // if 2-digits of hash are equal, we have 1 directory less
t.is(dirItems.length, 23, 'expect 23 items');
break;
case 0: // by default we expect 24 items
t.is(dirItems.length, 24, 'expect 24 items');
break;
}
} else {
t.is(dirItems.length, 4, 'expect 3 items (foo + subdir + subdir/bar + .snow)');
}
Expand Down Expand Up @@ -176,10 +193,7 @@ test.only('repo open-commondir-inside', async (t) => {
1st commit: Add file 'foo'
2nd commit: Delete file 'foo'
*/
for (let i = 0; i < 50; ++i) {
// eslint-disable-next-line no-await-in-loop
await repoTest(t, true);
}
await repoTest(t, true);
});

test('custom-commit-data', async (t) => {
Expand Down

0 comments on commit 6865eb4

Please sign in to comment.