-
Notifications
You must be signed in to change notification settings - Fork 0
/
infinite_clone_test.mjs
65 lines (52 loc) · 1.81 KB
/
infinite_clone_test.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// @ts-check
import fs from 'fs/promises';
import crypto from 'crypto';
import path from 'path';
import { rimraf } from 'rimraf';
import chalk from 'chalk';
import { reflinkFile } from './index.js';
async function main() {
const originalSrcPath = path.resolve('./package.json');
const originalContent = await fs.readFile(originalSrcPath, 'utf-8');
let iteration = 0;
while (iteration < 50) {
// Remove and recreate the sandbox directory
rimraf.sync('./sandbox'); // Using synchronous rimraf for simplicity
await fs.mkdir('./sandbox', { recursive: true });
// Create a random name for the new base file and copy package.json to sandbox directory
const randomName = `base-${Math.random().toString(36).substring(2)}.json`;
const newSrcPath = path.join('./sandbox', randomName);
await fs.writeFile(newSrcPath, originalContent);
const srcFile = {
path: newSrcPath,
content: originalContent,
};
const srcHash = createHash(srcFile.content);
for (let i = 0; i < 1000; i++) {
const destPath = path.join('./sandbox', `file1-copy-${i}.txt`);
await reflinkFile(srcFile.path, destPath);
const destContent = await fs.readFile(destPath, 'utf-8');
const destHash = createHash(destContent);
if (destHash !== srcHash) {
console.log(`Hash mismatch detected on file: ${destPath}`);
console.log(`Src Hash: ${srcHash}, Dest Hash: ${destHash}`);
return;
}
}
iteration++;
console.log(
chalk.green(
`Iteration ${iteration} successful ${chalk.gray.dim(
`[${iteration * 1000} files cloned successfully]`
)}`
)
);
}
}
function createHash(content) {
return crypto.createHash('sha256').update(content).digest('hex');
}
main().catch((err) => {
console.error(err);
process.exit(1);
});