@@ -4,171 +4,68 @@ import { exec } from 'child_process';
4
4
import { rm } from 'fs/promises' ;
5
5
import { promisify } from 'util' ;
6
6
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' ;
32
10
33
11
describe ( 'Git Repository Tests' , ( ) => {
34
12
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 ( ) ;
93
14
} ) ;
94
15
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
-
119
16
test ( 'Adding new files' , async ( ) => {
120
17
// 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' ) ;
122
19
123
20
await beforeDiflow ( ) ;
124
21
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 ( ) ;
127
24
128
25
await afterDiflow ( ) ;
129
26
130
27
// 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 ) ;
134
31
} ) ;
135
32
136
33
test ( 'Removing files' , async ( ) => {
137
34
// 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' ) } ) ;
141
38
142
39
await beforeDiflow ( ) ;
143
40
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 ( ) ;
146
43
147
44
await afterDiflow ( ) ;
148
45
149
46
// 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' ) ;
153
50
} ) ;
154
51
155
52
test ( 'Changing files' , async ( ) => {
156
53
// 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' ) } ) ;
160
57
161
58
await beforeDiflow ( ) ;
162
59
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 ( ) ;
165
62
166
63
await afterDiflow ( ) ;
167
64
168
65
// 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' ) ;
172
69
173
70
expect ( baseContent ) . toBe ( 'base content' ) ;
174
71
expect ( diffContent ) . toBe ( 'modified content' ) ;
0 commit comments