-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtest.js
131 lines (103 loc) · 4.63 KB
/
test.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const child_process = require('child_process');
const os = require('os');
const fs = require('fs');
const path = require('path');
const assert = require('assert');
const FindStale = require('./lib/find-stale.js');
const onlyPrepare = process.argv.find(one => one === '--prepare');
const bin = `${__dirname}${path.sep}index.js`;
let tempdir;
let bareDir;
let workingDir;
const setup = () => {
const tmp = os.tmpdir()
tempdir = fs.mkdtempSync(tmp + path.sep + 'git-removed-branches-');
bareDir = tempdir + path.sep + 'bare';
workingDir = tempdir + path.sep + 'working';
const file = `${workingDir}${path.sep}lolipop`;
fs.mkdirSync(bareDir);
console.log(`Using "${tempdir}" dir`);
// create bare repository
child_process.execSync('git init --bare --initial-branch=master', { cwd: bareDir, stderr: 'stdio'});
// clone repository
child_process.execSync('git clone bare working', { cwd: tempdir });
// create initial commit
fs.writeFileSync(file, 'lolipop content');
child_process.execSync('git add lolipop', { cwd: workingDir });
child_process.execSync('git commit -m "inital commit"', { cwd: workingDir });
// create new branch, which will be deleted by -d flag
child_process.execSync('git branch feature/fast-forwarded', { cwd: workingDir });
// create another branch with special character
child_process.execSync('git branch "#333-work"', { cwd: workingDir });
// create branch with renamed name, which is deleted on remote
child_process.execSync('git branch chore/local-name-deleted', { cwd: workingDir });
// create branch with renamed name, which is NOT deleted on remote
child_process.execSync('git branch chore/local-name-persistent', { cwd: workingDir });
// create new branch, which can be deleted only with -D flag
child_process.execSync('git branch no-ff', { cwd: workingDir });
// checkout working branch
child_process.execSync('git checkout no-ff', { cwd: workingDir });
// update file content
fs.writeFileSync(file, 'lolipop content changed');
child_process.execSync('git commit -a -m "second commit"', { cwd: workingDir });
// push all the branches to the remote and update config
child_process.execSync('git push origin -u master', { cwd: workingDir });
child_process.execSync('git push origin -u feature/fast-forwarded', { cwd: workingDir });
child_process.execSync('git push origin -u "#333-work"', { cwd: workingDir });
child_process.execSync('git push origin -u chore/local-name-deleted:chore/remote-name-deleted', { cwd: workingDir });
child_process.execSync('git push origin -u chore/local-name-persistent:chore/remote-name-persistent', { cwd: workingDir });
child_process.execSync('git push origin -u no-ff', { cwd: workingDir });
// remove all the branches from the remote, except for the local-name
child_process.execSync('git push origin :feature/fast-forwarded', { cwd: workingDir });
child_process.execSync('git push origin :no-ff', { cwd: workingDir });
child_process.execSync('git push origin :"#333-work"', { cwd: workingDir });
child_process.execSync('git push origin :chore/remote-name-deleted', { cwd: workingDir });
// checkout master branch
child_process.execSync('git checkout master', { cwd: workingDir });
};
const test_nothing = () => {
const output = child_process.execFileSync('node', [bin], {
cwd: workingDir,
encoding: 'utf8',
});
console.log(`------ test_nothing ------
${output}
-------------------`);
assert.equal(output.indexOf('- chore/local-name-persistent'), -1);
assert.notEqual(output.indexOf('- chore/local-name-deleted'), -1);
assert.notEqual(output.indexOf('- #333-work'), -1);
assert.notEqual(output.indexOf('- feature/fast-forwarded'), -1);
assert.notEqual(output.indexOf('- no-ff'), -1);
};
const testing_prune = () => {
const output = child_process.execFileSync('node', [bin, '--prune'], {
cwd: workingDir,
encoding: 'utf8',
});
console.log(`------ test_prune ------
${output}
-------------------`);
assert.notEqual(output.indexOf('Deleted branch #333-work'), -1)
assert.notEqual(output.indexOf('Deleted branch feature/fast-forwarded'), -1);
assert.notEqual(output.indexOf('Not all branches are removed:'), -1);
assert.notEqual(output.indexOf('- no-ff'), -1);
};
const testing_force = () => {
const output = child_process.execFileSync('node', [bin, '--force', '--prune'], {
cwd: workingDir,
encoding: 'utf8',
});
console.log(`------ test_force ------
${output}
-------------------`);
assert.notEqual(output.indexOf('Deleted branch no-ff'), -1);
};
setup();
if (onlyPrepare) {
console.log('All prepared');
} else {
test_nothing();
testing_prune();
testing_force();
console.log('We are good to go!');
}