Skip to content

Commit 43df118

Browse files
author
SPRINX0\prochazka
committed
local tests passed
1 parent 23d71c6 commit 43df118

File tree

8 files changed

+248
-174
lines changed

8 files changed

+248
-174
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
2-
dist
2+
dist
3+
testrepos

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
"build": "tsc",
66
"test": "npm run build && jest --runInBand",
77
"test:ci": "jest --runInBand --json --outputFile=test-result.json --testLocationInResults --detectOpenHandles --forceExit",
8-
"start": "ts-node gitdiff.ts"
8+
"start": "ts-node gitdiff.ts",
9+
10+
"test:init": "yarn tsc && node dist/runtest init",
11+
"test:add": "yarn tsc && node dist/runtest add"
912
},
1013
"devDependencies": {
1114
"@types/fs-extra": "^11.0.4",

src/diflow.test.ts

Lines changed: 26 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -4,171 +4,68 @@ import { exec } from 'child_process';
44
import { rm } from 'fs/promises';
55
import { promisify } from 'util';
66
import { rimraf } from 'rimraf';
7-
8-
const execAsync = promisify(exec);
9-
10-
function getRepoPath(repo: string) {
11-
const repoPath = path.join(__dirname, 'testrepos', repo);
12-
return repoPath.replaceAll('\\', '/');
13-
}
14-
15-
async function initRepo(name: string) {
16-
const repoPath = getRepoPath(name);
17-
await fs.ensureDir(repoPath);
18-
await execAsync('git init', { cwd: repoPath });
19-
// Configure git user for the test
20-
await execAsync('git config user.email "[email protected]"', { cwd: repoPath });
21-
await execAsync('git config user.name "Test User"', { cwd: repoPath });
22-
}
23-
24-
async function createCommit(repoPath: string, fileName: string, content: string, repoid: string) {
25-
await fs.writeFile(path.join(repoPath, fileName), content);
26-
await execAsync('git add .', { cwd: repoPath });
27-
await execAsync(`git commit -m "Commit into ${repoid}"`, { cwd: repoPath });
28-
29-
const { stdout: commitHash } = await execAsync('git rev-parse HEAD', { cwd: repoPath });
30-
return commitHash.trim();
31-
}
7+
import { afterDiflow, beforeDiflow, createTestCommit, getTestRepoPath, initTestRepos } from './testrepo';
8+
import { Processor } from './processor';
9+
import { execAsync } from './tools';
3210

3311
describe('Git Repository Tests', () => {
3412
beforeEach(async () => {
35-
// Cleanup repositories
36-
try {
37-
await rimraf(path.join(__dirname, 'repos'));
38-
// await rm(path.join(__dirname, 'repos'), { recursive: true, force: true });
39-
} catch (e) {}
40-
try {
41-
await rimraf(path.join(__dirname, 'testrepos'));
42-
// await rm(path.join(__dirname, 'testrepos'), { recursive: true, force: true });
43-
} catch (e) {}
44-
45-
await initRepo('config');
46-
await initRepo('base');
47-
await initRepo('diff');
48-
await initRepo('merged');
49-
50-
// Setup initial files
51-
const baseHash = await createCommit(getRepoPath('base'), 'file1.txt', 'base content', 'base');
52-
const diffHash = await createCommit(getRepoPath('diff'), 'file1.txt', 'different content', 'diff');
53-
const mergedHash = await createCommit(getRepoPath('merged'), 'file1.txt', 'different content', 'merged');
54-
55-
// Create config.json in config repo
56-
const configContent = JSON.stringify(
57-
{
58-
branches: ['master'],
59-
repos: {
60-
base: getRepoPath('base'),
61-
diff: getRepoPath('diff'),
62-
merged: getRepoPath('merged'),
63-
},
64-
},
65-
null,
66-
2
67-
);
68-
await createCommit(getRepoPath('config'), 'config.json', configContent, 'config');
69-
70-
// Create state.json in config repo
71-
const stateContent = JSON.stringify(
72-
{
73-
base: {
74-
master: {
75-
lastProcessed: baseHash,
76-
},
77-
},
78-
diff: {
79-
master: {
80-
lastProcessed: diffHash,
81-
},
82-
},
83-
merged: {
84-
master: {
85-
lastProcessed: mergedHash,
86-
},
87-
},
88-
},
89-
null,
90-
2
91-
);
92-
await createCommit(getRepoPath('config'), 'state.json', stateContent, 'config');
13+
await initTestRepos();
9314
});
9415

95-
// afterEach(async () => {
96-
// // Cleanup repositories
97-
// try {
98-
// await rimraf(path.join(__dirname, 'repos'));
99-
// // await rm(path.join(__dirname, 'repos'), { recursive: true, force: true });
100-
// } catch (e) {}
101-
// try {
102-
// await rimraf(path.join(__dirname, 'testrepos'));
103-
// // await rm(path.join(__dirname, 'testrepos'), { recursive: true, force: true });
104-
// } catch (e) {}
105-
// });
106-
107-
async function beforeDiflow() {
108-
await execAsync('git checkout -b tmp', { cwd: getRepoPath('merged') });
109-
await execAsync('git checkout -b tmp', { cwd: getRepoPath('base') });
110-
await execAsync('git checkout -b tmp', { cwd: getRepoPath('diff') });
111-
}
112-
113-
async function afterDiflow() {
114-
await execAsync('git checkout master', { cwd: getRepoPath('merged') });
115-
await execAsync('git checkout master', { cwd: getRepoPath('base') });
116-
await execAsync('git checkout master', { cwd: getRepoPath('diff') });
117-
}
118-
11916
test('Adding new files', async () => {
12017
// Add new file in diff repo
121-
await createCommit(getRepoPath('diff'), 'newfile.txt', 'new content', 'diff');
18+
await createTestCommit(getTestRepoPath('diff'), 'newfile.txt', 'new content', 'diff');
12219

12320
await beforeDiflow();
12421

125-
// Run diflow tool
126-
await execAsync('node diflow.js ' + getRepoPath('config'), { cwd: __dirname });
22+
const processor = new Processor(getTestRepoPath('config'), path.join(__dirname, 'repos'));
23+
await processor.process();
12724

12825
await afterDiflow();
12926

13027
// Verify changes
131-
expect(await fs.exists(path.join(getRepoPath('merged'), 'newfile.txt'))).toBe(true);
132-
expect(await fs.readFile(path.join(getRepoPath('merged'), 'newfile.txt'), 'utf8')).toBe('new content');
133-
expect(await fs.exists(path.join(getRepoPath('base'), 'newfile.txt'))).toBe(false);
28+
expect(await fs.exists(path.join(getTestRepoPath('merged'), 'newfile.txt'))).toBe(true);
29+
expect(await fs.readFile(path.join(getTestRepoPath('merged'), 'newfile.txt'), 'utf8')).toBe('new content');
30+
expect(await fs.exists(path.join(getTestRepoPath('base'), 'newfile.txt'))).toBe(false);
13431
});
13532

13633
test('Removing files', async () => {
13734
// Remove file in diff repo
138-
await fs.unlink(path.join(getRepoPath('diff'), 'file1.txt'));
139-
await execAsync('git add .', { cwd: getRepoPath('diff') });
140-
await execAsync('git commit -m "Remove file1.txt"', { cwd: getRepoPath('diff') });
35+
await fs.unlink(path.join(getTestRepoPath('diff'), 'file1.txt'));
36+
await execAsync('git add .', { cwd: getTestRepoPath('diff') });
37+
await execAsync('git commit -m "Remove file1.txt"', { cwd: getTestRepoPath('diff') });
14138

14239
await beforeDiflow();
14340

144-
// Run diflow tool
145-
await execAsync('node diflow.js ' + getRepoPath('config'), { cwd: __dirname });
41+
const processor = new Processor(getTestRepoPath('config'), path.join(__dirname, 'repos'));
42+
await processor.process();
14643

14744
await afterDiflow();
14845

14946
// Verify changes
150-
expect(await fs.exists(path.join(getRepoPath('merged'), 'file1.txt'))).toBe(true);
151-
expect(await fs.exists(path.join(getRepoPath('base'), 'file1.txt'))).toBe(true);
152-
expect(await fs.readFile(path.join(getRepoPath('merged'), 'file1.txt'), 'utf8')).toBe('base content');
47+
expect(await fs.exists(path.join(getTestRepoPath('merged'), 'file1.txt'))).toBe(true);
48+
expect(await fs.exists(path.join(getTestRepoPath('base'), 'file1.txt'))).toBe(true);
49+
expect(await fs.readFile(path.join(getTestRepoPath('merged'), 'file1.txt'), 'utf8')).toBe('base content');
15350
});
15451

15552
test('Changing files', async () => {
15653
// Modify file in diff repo
157-
await fs.writeFile(path.join(getRepoPath('diff'), 'file1.txt'), 'modified content');
158-
await execAsync('git add .', { cwd: getRepoPath('diff') });
159-
await execAsync('git commit -m "Modify file1.txt"', { cwd: getRepoPath('diff') });
54+
await fs.writeFile(path.join(getTestRepoPath('diff'), 'file1.txt'), 'modified content');
55+
await execAsync('git add .', { cwd: getTestRepoPath('diff') });
56+
await execAsync('git commit -m "Modify file1.txt"', { cwd: getTestRepoPath('diff') });
16057

16158
await beforeDiflow();
16259

163-
// Run diflow tool
164-
await execAsync('node diflow.js ' + getRepoPath('config'), { cwd: __dirname });
60+
const processor = new Processor(getTestRepoPath('config'), path.join(__dirname, 'repos'));
61+
await processor.process();
16562

16663
await afterDiflow();
16764

16865
// Verify changes
169-
const baseContent = await fs.readFile(path.join(getRepoPath('base'), 'file1.txt'), 'utf8');
170-
const diffContent = await fs.readFile(path.join(getRepoPath('diff'), 'file1.txt'), 'utf8');
171-
const mergedContent = await fs.readFile(path.join(getRepoPath('merged'), 'file1.txt'), 'utf8');
66+
const baseContent = await fs.readFile(path.join(getTestRepoPath('base'), 'file1.txt'), 'utf8');
67+
const diffContent = await fs.readFile(path.join(getTestRepoPath('diff'), 'file1.txt'), 'utf8');
68+
const mergedContent = await fs.readFile(path.join(getTestRepoPath('merged'), 'file1.txt'), 'utf8');
17269

17370
expect(baseContent).toBe('base content');
17471
expect(diffContent).toBe('modified content');

src/diflow.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
import path from 'path';
12
import { Processor } from './processor';
23

3-
const processor = new Processor();
4+
if (process.argv.length < 3) {
5+
console.error('Usage: gitdiff <state-repo-url>');
6+
process.exit(1);
7+
}
8+
9+
const processor = new Processor(process.argv[2], path.join(__dirname, 'repos'));
410
processor.process();
511

612
console.log('Processing complete.');

0 commit comments

Comments
 (0)